Skip to content

Commit 4121fd2

Browse files
authored
Merge pull request #6130 from davepagurek/fix/ambient
Default ambientMaterial to the base material color if unspecified
2 parents ed20ca3 + f6972e8 commit 4121fd2

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

src/webgl/material.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ p5.prototype.ambientMaterial = function(v1, v2, v3) {
821821
p5._validateParameters('ambientMaterial', arguments);
822822

823823
const color = p5.prototype.color.apply(this, arguments);
824+
this._renderer._hasSetAmbient = true;
824825
this._renderer.curAmbientColor = color._array;
825826
this._renderer._useNormalMaterial = false;
826827
this._renderer._enableLighting = true;

src/webgl/p5.RendererGL.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) {
138138
}
139139
this._isBlending = false;
140140

141+
this._hasSetAmbient = false;
141142
this._useSpecularMaterial = false;
142143
this._useEmissiveMaterial = false;
143144
this._useNormalMaterial = false;
@@ -1235,6 +1236,7 @@ p5.RendererGL.prototype.push = function() {
12351236
properties.curSpecularColor = this.curSpecularColor;
12361237
properties.curEmissiveColor = this.curEmissiveColor;
12371238

1239+
properties._hasSetAmbient = this._hasSetAmbient;
12381240
properties._useSpecularMaterial = this._useSpecularMaterial;
12391241
properties._useEmissiveMaterial = this._useEmissiveMaterial;
12401242
properties._useShininess = this._useShininess;
@@ -1526,6 +1528,7 @@ p5.RendererGL.prototype._setFillUniforms = function(fillShader) {
15261528
}
15271529
fillShader.setUniform('uTint', this._tint);
15281530

1531+
fillShader.setUniform('uHasSetAmbient', this._hasSetAmbient);
15291532
fillShader.setUniform('uAmbientMatColor', this.curAmbientColor);
15301533
fillShader.setUniform('uSpecularMatColor', this.curSpecularColor);
15311534
fillShader.setUniform('uEmissiveMatColor', this.curEmissiveColor);

src/webgl/shaders/phong.frag

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
precision highp float;
33
precision highp int;
44

5+
uniform bool uHasSetAmbient;
56
uniform vec4 uSpecularMatColor;
67
uniform vec4 uAmbientMatColor;
78
uniform vec4 uEmissiveMatColor;
@@ -33,7 +34,9 @@ void main(void) {
3334
// channels by alpha to convert it to premultiplied alpha.
3435
: vec4(vColor.rgb * vColor.a, vColor.a);
3536
gl_FragColor = vec4(diffuse * baseColor.rgb +
36-
vAmbientColor * uAmbientMatColor.rgb +
37+
vAmbientColor * (
38+
uHasSetAmbient ? uAmbientMatColor.rgb : baseColor.rgb
39+
) +
3740
specular * uSpecularMatColor.rgb +
3841
uEmissiveMatColor.rgb, baseColor.a);
3942
}

test/unit/webgl/p5.RendererGL.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,33 @@ suite('p5.RendererGL', function() {
364364
});
365365
});
366366

367+
suite('materials', function() {
368+
test('ambient color defaults to the fill color', function() {
369+
myp5.createCanvas(100, 100, myp5.WEBGL);
370+
myp5.noStroke();
371+
myp5.lights();
372+
myp5.fill('red');
373+
myp5.sphere(25);
374+
const pixel = myp5.get(50, 50);
375+
expect(pixel[0]).to.equal(221);
376+
expect(pixel[1]).to.equal(0);
377+
expect(pixel[2]).to.equal(0);
378+
});
379+
380+
test('ambient color can be set manually', function() {
381+
myp5.createCanvas(100, 100, myp5.WEBGL);
382+
myp5.noStroke();
383+
myp5.lights();
384+
myp5.fill('red');
385+
myp5.ambientMaterial(255, 255, 255);
386+
myp5.sphere(25);
387+
const pixel = myp5.get(50, 50);
388+
expect(pixel[0]).to.equal(221);
389+
expect(pixel[1]).to.equal(128);
390+
expect(pixel[2]).to.equal(128);
391+
});
392+
});
393+
367394
suite('loadpixels()', function() {
368395
test('loadPixels color check', function(done) {
369396
myp5.createCanvas(100, 100, myp5.WEBGL);

0 commit comments

Comments
 (0)