Skip to content

Commit 186f423

Browse files
committed
Merge remote-tracking branch 'upstream/webgpu' into webgpu
2 parents ed88f91 + ae2c566 commit 186f423

File tree

10 files changed

+523
-61
lines changed

10 files changed

+523
-61
lines changed

src/core/p5.Renderer3D.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,17 +1499,20 @@ export class Renderer3D extends Renderer {
14991499
);
15001500

15011501
// TODO: sum these here...
1502-
const ambientLightCount = this.states.ambientLightColors.length / 3;
1503-
this.mixedAmbientLight = [...this.states.ambientLightColors];
1504-
1505-
if (this.states._useMetalness > 0) {
1506-
this.mixedAmbientLight = this.mixedAmbientLight.map((ambientColors) => {
1507-
let mixing = ambientColors - this.states._useMetalness;
1508-
return Math.max(0, mixing);
1509-
});
1502+
let mixedAmbientLight = [0, 0, 0];
1503+
for (let i = 0; i < this.states.ambientLightColors.length; i += 3) {
1504+
for (let off = 0; off < 3; off++) {
1505+
if (this.states._useMetalness > 0) {
1506+
mixedAmbientLight[off] += Math.max(
1507+
0,
1508+
this.states.ambientLightColors[i + off] - this.states._useMetalness
1509+
);
1510+
} else {
1511+
mixedAmbientLight[off] += this.states.ambientLightColors[i + off];
1512+
}
1513+
}
15101514
}
1511-
fillShader.setUniform("uAmbientLightCount", ambientLightCount);
1512-
fillShader.setUniform("uAmbientColor", this.mixedAmbientLight);
1515+
fillShader.setUniform("uAmbientColor", mixedAmbientLight);
15131516

15141517
const spotLightCount = this.states.spotLightDiffuseColors.length / 3;
15151518
fillShader.setUniform("uSpotLightCount", spotLightCount);

src/webgl/light.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,8 @@ function light(p5, fn){
16201620
angle,
16211621
concentration
16221622
) {
1623+
if (this.states.spotLightDiffuseColors.length / 3 >= 4) return;
1624+
16231625
let color, position, direction;
16241626
const length = arguments.length;
16251627

@@ -1777,18 +1779,26 @@ function light(p5, fn){
17771779
return;
17781780
}
17791781
this.states.setValue('spotLightDiffuseColors', [
1782+
...this.states.spotLightDiffuseColors,
17801783
color._array[0],
17811784
color._array[1],
17821785
color._array[2]
17831786
]);
17841787

17851788
this.states.setValue('spotLightSpecularColors', [
1789+
...this.states.spotLightSpecularColors,
17861790
...this.states.specularColors
17871791
]);
17881792

1789-
this.states.setValue('spotLightPositions', [position.x, position.y, position.z]);
1793+
this.states.setValue('spotLightPositions', [
1794+
...this.states.spotLightPositions,
1795+
position.x,
1796+
position.y,
1797+
position.z
1798+
]);
17901799
direction.normalize();
17911800
this.states.setValue('spotLightDirections', [
1801+
...this.states.spotLightDirections,
17921802
direction.x,
17931803
direction.y,
17941804
direction.z
@@ -1808,8 +1818,8 @@ function light(p5, fn){
18081818
}
18091819

18101820
angle = this._pInst._toRadians(angle);
1811-
this.states.setValue('spotLightAngle', [Math.cos(angle)]);
1812-
this.states.setValue('spotLightConc', [concentration]);
1821+
this.states.setValue('spotLightAngle', [...this.states.spotLightAngle, Math.cos(angle)]);
1822+
this.states.setValue('spotLightConc', [...this.states.spotLightConc, concentration]);
18131823

18141824
this.states.setValue('enableLighting', true);
18151825
}

src/webgl/p5.Shader.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,14 +976,18 @@ class Shader {
976976
* </code>
977977
* </div>
978978
*/
979-
setUniform(uniformName, data) {
979+
setUniform(uniformName, rawData) {
980980
this.init();
981981

982982
const uniform = this.uniforms[uniformName];
983983
if (!uniform) {
984984
return;
985985
}
986986

987+
const data = this._renderer._mapUniformData
988+
? this._renderer._mapUniformData(uniform, rawData)
989+
: rawData;
990+
987991
if (uniform.isArray) {
988992
if (
989993
uniform._cachedData &&

src/webgl/shaders/basic.frag

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
IN vec4 vColor;
22
void main(void) {
33
HOOK_beforeFragment();
4-
OUT_COLOR = HOOK_getFinalColor(vec4(vColor.rgb, 1.) * vColor.a);
4+
OUT_COLOR = HOOK_getFinalColor(vColor);
5+
OUT_COLOR.rgb *= OUT_COLOR.a; // Premultiply alpha before rendering
56
HOOK_afterFragment();
67
}

src/webgl/shaders/lighting.glsl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ uniform mat4 uViewMatrix;
77

88
uniform bool uUseLighting;
99

10-
uniform int uAmbientLightCount;
11-
uniform vec3 uAmbientColor[5];
1210
uniform mat3 uCameraRotation;
1311
uniform int uDirectionalLightCount;
1412
uniform vec3 uLightingDirection[5];

src/webgl/shaders/phong.frag

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
precision highp int;
33

44
uniform bool uHasSetAmbient;
5+
uniform vec3 uAmbientColor;
56
uniform vec4 uSpecularMatColor;
67
uniform vec4 uAmbientMatColor;
78
uniform vec4 uEmissiveMatColor;
@@ -13,7 +14,6 @@ uniform bool isTexture;
1314
IN vec3 vNormal;
1415
IN vec2 vTexCoord;
1516
IN vec3 vViewPosition;
16-
IN vec3 vAmbientColor;
1717
IN vec4 vColor;
1818

1919
struct ColorComponents {
@@ -45,7 +45,7 @@ void main(void) {
4545
Inputs inputs;
4646
inputs.normal = normalize(vNormal);
4747
inputs.texCoord = vTexCoord;
48-
inputs.ambientLight = vAmbientColor;
48+
inputs.ambientLight = uAmbientColor;
4949
inputs.color = isTexture
5050
? TEXTURE(uSampler, vTexCoord) * (vec4(uTint.rgb/255., 1.) * uTint.a/255.)
5151
: vColor;
@@ -67,7 +67,6 @@ void main(void) {
6767

6868
// Calculating final color as result of all lights (plus emissive term).
6969

70-
vec2 texCoord = inputs.texCoord;
7170
vec4 baseColor = inputs.color;
7271
ColorComponents c;
7372
c.opacity = baseColor.a;

src/webgl/shaders/phong.vert

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ IN vec3 aNormal;
77
IN vec2 aTexCoord;
88
IN vec4 aVertexColor;
99

10-
uniform vec3 uAmbientColor[5];
11-
1210
#ifdef AUGMENTED_HOOK_getWorldInputs
1311
uniform mat4 uModelMatrix;
1412
uniform mat4 uViewMatrix;
@@ -19,7 +17,6 @@ uniform mat4 uModelViewMatrix;
1917
uniform mat3 uNormalMatrix;
2018
#endif
2119
uniform mat4 uProjectionMatrix;
22-
uniform int uAmbientLightCount;
2320

2421
uniform bool uUseVertexColor;
2522
uniform vec4 uMaterialColor;
@@ -74,14 +71,6 @@ void main(void) {
7471
vNormal = inputs.normal;
7572
vColor = inputs.color;
7673

77-
// TODO: this should be a uniform
78-
vAmbientColor = vec3(0.0);
79-
for (int i = 0; i < 5; i++) {
80-
if (i < uAmbientLightCount) {
81-
vAmbientColor += uAmbientColor[i];
82-
}
83-
}
84-
8574
gl_Position = uProjectionMatrix * vec4(inputs.position, 1.);
8675
HOOK_afterVertex();
8776
}

0 commit comments

Comments
 (0)