From f6972e88b0ca738a306c419cdbaa74f7006a7241 Mon Sep 17 00:00:00 2001 From: Dave Pagurek Date: Thu, 4 May 2023 20:00:10 -0400 Subject: [PATCH] Default ambientMaterial to the base material color if unspecified --- src/webgl/material.js | 1 + src/webgl/p5.RendererGL.js | 3 +++ src/webgl/shaders/phong.frag | 5 ++++- test/unit/webgl/p5.RendererGL.js | 27 +++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/webgl/material.js b/src/webgl/material.js index 8c28924a09..f6d58811bf 100644 --- a/src/webgl/material.js +++ b/src/webgl/material.js @@ -821,6 +821,7 @@ p5.prototype.ambientMaterial = function(v1, v2, v3) { p5._validateParameters('ambientMaterial', arguments); const color = p5.prototype.color.apply(this, arguments); + this._renderer._hasSetAmbient = true; this._renderer.curAmbientColor = color._array; this._renderer._useNormalMaterial = false; this._renderer._enableLighting = true; diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 4c3ae7c5b1..c3a3d1183a 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -138,6 +138,7 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) { } this._isBlending = false; + this._hasSetAmbient = false; this._useSpecularMaterial = false; this._useEmissiveMaterial = false; this._useNormalMaterial = false; @@ -1235,6 +1236,7 @@ p5.RendererGL.prototype.push = function() { properties.curSpecularColor = this.curSpecularColor; properties.curEmissiveColor = this.curEmissiveColor; + properties._hasSetAmbient = this._hasSetAmbient; properties._useSpecularMaterial = this._useSpecularMaterial; properties._useEmissiveMaterial = this._useEmissiveMaterial; properties._useShininess = this._useShininess; @@ -1526,6 +1528,7 @@ p5.RendererGL.prototype._setFillUniforms = function(fillShader) { } fillShader.setUniform('uTint', this._tint); + fillShader.setUniform('uHasSetAmbient', this._hasSetAmbient); fillShader.setUniform('uAmbientMatColor', this.curAmbientColor); fillShader.setUniform('uSpecularMatColor', this.curSpecularColor); fillShader.setUniform('uEmissiveMatColor', this.curEmissiveColor); diff --git a/src/webgl/shaders/phong.frag b/src/webgl/shaders/phong.frag index 0626793f7b..fa80efa267 100644 --- a/src/webgl/shaders/phong.frag +++ b/src/webgl/shaders/phong.frag @@ -2,6 +2,7 @@ precision highp float; precision highp int; +uniform bool uHasSetAmbient; uniform vec4 uSpecularMatColor; uniform vec4 uAmbientMatColor; uniform vec4 uEmissiveMatColor; @@ -33,7 +34,9 @@ void main(void) { // channels by alpha to convert it to premultiplied alpha. : vec4(vColor.rgb * vColor.a, vColor.a); gl_FragColor = vec4(diffuse * baseColor.rgb + - vAmbientColor * uAmbientMatColor.rgb + + vAmbientColor * ( + uHasSetAmbient ? uAmbientMatColor.rgb : baseColor.rgb + ) + specular * uSpecularMatColor.rgb + uEmissiveMatColor.rgb, baseColor.a); } diff --git a/test/unit/webgl/p5.RendererGL.js b/test/unit/webgl/p5.RendererGL.js index 5a736625f4..786d42b160 100644 --- a/test/unit/webgl/p5.RendererGL.js +++ b/test/unit/webgl/p5.RendererGL.js @@ -364,6 +364,33 @@ suite('p5.RendererGL', function() { }); }); + suite('materials', function() { + test('ambient color defaults to the fill color', function() { + myp5.createCanvas(100, 100, myp5.WEBGL); + myp5.noStroke(); + myp5.lights(); + myp5.fill('red'); + myp5.sphere(25); + const pixel = myp5.get(50, 50); + expect(pixel[0]).to.equal(221); + expect(pixel[1]).to.equal(0); + expect(pixel[2]).to.equal(0); + }); + + test('ambient color can be set manually', function() { + myp5.createCanvas(100, 100, myp5.WEBGL); + myp5.noStroke(); + myp5.lights(); + myp5.fill('red'); + myp5.ambientMaterial(255, 255, 255); + myp5.sphere(25); + const pixel = myp5.get(50, 50); + expect(pixel[0]).to.equal(221); + expect(pixel[1]).to.equal(128); + expect(pixel[2]).to.equal(128); + }); + }); + suite('loadpixels()', function() { test('loadPixels color check', function(done) { myp5.createCanvas(100, 100, myp5.WEBGL);