Skip to content

Commit fb6ce7e

Browse files
authored
Merge pull request #5774 from ShenpaiSharma/Multiple_Mat
Feature Implemeted -- Multiple Material support for geometry
2 parents d03d6f5 + c95d5d8 commit fb6ce7e

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

src/webgl/material.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ p5.prototype.normalMaterial = function(...args) {
719719
};
720720

721721
/**
722-
* Sets the current material as an ambient material of the given color.
722+
* Sets the ambient color of the material.
723723
*
724724
* The ambientMaterial() color is the color the object will reflect
725725
* under **any** lighting.
@@ -823,9 +823,7 @@ p5.prototype.ambientMaterial = function(v1, v2, v3) {
823823
p5._validateParameters('ambientMaterial', arguments);
824824

825825
const color = p5.prototype.color.apply(this, arguments);
826-
this._renderer.curFillColor = color._array;
827-
this._renderer._useSpecularMaterial = false;
828-
this._renderer._useEmissiveMaterial = false;
826+
this._renderer.curAmbientColor = color._array;
829827
this._renderer._useNormalMaterial = false;
830828
this._renderer._enableLighting = true;
831829
this._renderer._tex = null;
@@ -834,8 +832,7 @@ p5.prototype.ambientMaterial = function(v1, v2, v3) {
834832
};
835833

836834
/**
837-
* Sets the current material as an emissive material of
838-
* the given color.
835+
* Sets the emissive color of the material.
839836
*
840837
* An emissive material will display the emissive color at
841838
* full strength regardless of lighting. This can give the
@@ -897,8 +894,7 @@ p5.prototype.emissiveMaterial = function(v1, v2, v3, a) {
897894
p5._validateParameters('emissiveMaterial', arguments);
898895

899896
const color = p5.prototype.color.apply(this, arguments);
900-
this._renderer.curFillColor = color._array;
901-
this._renderer._useSpecularMaterial = false;
897+
this._renderer.curEmissiveColor = color._array;
902898
this._renderer._useEmissiveMaterial = true;
903899
this._renderer._useNormalMaterial = false;
904900
this._renderer._enableLighting = true;
@@ -908,7 +904,7 @@ p5.prototype.emissiveMaterial = function(v1, v2, v3, a) {
908904
};
909905

910906
/**
911-
* Sets the current material as a specular material of the given color.
907+
* Sets the specular color of the material.
912908
*
913909
* A specular material is reflective (shiny). The shininess can be
914910
* controlled by the <a href="#/p5/shininess">shininess()</a> function.
@@ -985,9 +981,8 @@ p5.prototype.specularMaterial = function(v1, v2, v3, alpha) {
985981
p5._validateParameters('specularMaterial', arguments);
986982

987983
const color = p5.prototype.color.apply(this, arguments);
988-
this._renderer.curFillColor = color._array;
984+
this._renderer.curSpecularColor = color._array;
989985
this._renderer._useSpecularMaterial = true;
990-
this._renderer._useEmissiveMaterial = false;
991986
this._renderer._useNormalMaterial = false;
992987
this._renderer._enableLighting = true;
993988
this._renderer._tex = null;

src/webgl/p5.RendererGL.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) {
9595
this.drawMode = constants.FILL;
9696

9797
this.curFillColor = this._cachedFillStyle = [1, 1, 1, 1];
98+
this.curAmbientColor = this._cachedFillStyle = [0, 0, 0, 0];
99+
this.curSpecularColor = this._cachedFillStyle = [0, 0, 0, 0];
100+
this.curEmissiveColor = this._cachedFillStyle = [0, 0, 0, 0];
98101
this.curStrokeColor = this._cachedStrokeStyle = [0, 0, 0, 1];
99102

100103
this.curBlendMode = constants.BLEND;
@@ -1017,6 +1020,9 @@ p5.RendererGL.prototype.push = function() {
10171020
properties.curStrokeWeight = this.curStrokeWeight;
10181021
properties.curStrokeColor = this.curStrokeColor;
10191022
properties.curFillColor = this.curFillColor;
1023+
properties.curAmbientColor = this.curAmbientColor;
1024+
properties.curSpecularColor = this.curSpecularColor;
1025+
properties.curEmissiveColor = this.curEmissiveColor;
10201026

10211027
properties._useSpecularMaterial = this._useSpecularMaterial;
10221028
properties._useEmissiveMaterial = this._useEmissiveMaterial;
@@ -1257,6 +1263,9 @@ p5.RendererGL.prototype._setFillUniforms = function(fillShader) {
12571263
}
12581264
fillShader.setUniform('uTint', this._tint);
12591265

1266+
fillShader.setUniform('uAmbientMatColor', this.curAmbientColor);
1267+
fillShader.setUniform('uSpecularMatColor', this.curSpecularColor);
1268+
fillShader.setUniform('uEmissiveMatColor', this.curEmissiveColor);
12601269
fillShader.setUniform('uSpecular', this._useSpecularMaterial);
12611270
fillShader.setUniform('uEmissive', this._useEmissiveMaterial);
12621271
fillShader.setUniform('uShininess', this._useShininess);

src/webgl/shaders/phong.frag

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
precision highp float;
33
precision highp int;
44

5+
uniform vec4 uSpecularMatColor;
6+
uniform vec4 uAmbientMatColor;
7+
uniform vec4 uEmissiveMatColor;
8+
59
uniform vec4 uMaterialColor;
610
uniform vec4 uTint;
711
uniform sampler2D uSampler;
812
uniform bool isTexture;
9-
uniform bool uEmissive;
1013

1114
varying vec3 vNormal;
1215
varying vec2 vTexCoord;
@@ -19,11 +22,11 @@ void main(void) {
1922
vec3 specular;
2023
totalLight(vViewPosition, normalize(vNormal), diffuse, specular);
2124

22-
if(uEmissive && !isTexture) {
23-
gl_FragColor = uMaterialColor;
24-
}
25-
else {
26-
gl_FragColor = isTexture ? texture2D(uSampler, vTexCoord) * (uTint / vec4(255, 255, 255, 255)) : uMaterialColor;
27-
gl_FragColor.rgb = gl_FragColor.rgb * (diffuse + vAmbientColor) + specular;
28-
}
25+
// Calculating final color as result of all lights (plus emissive term).
26+
27+
gl_FragColor = isTexture ? texture2D(uSampler, vTexCoord) * (uTint / vec4(255, 255, 255, 255)) : uMaterialColor;
28+
gl_FragColor.rgb = diffuse * gl_FragColor.rgb +
29+
vAmbientColor * uAmbientMatColor.rgb +
30+
specular * uSpecularMatColor.rgb +
31+
uEmissiveMatColor.rgb;
2932
}

test/unit/webgl/p5.Shader.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ suite('p5.Shader', function() {
9292
'uMaterialColor',
9393
'uSampler',
9494
'isTexture',
95-
'uEmissive',
9695
'uConstantAttenuation',
9796
'uLinearAttenuation',
9897
'uQuadraticAttenuation'

0 commit comments

Comments
 (0)