@@ -30,7 +30,7 @@ uniform vec3 uSpotLightDirection[5];
30
30
31
31
uniform bool uSpecular;
32
32
uniform float uShininess;
33
- uniform float metallic ;
33
+ uniform float uMetallic ;
34
34
35
35
uniform float uConstantAttenuation;
36
36
uniform float uLinearAttenuation;
@@ -43,8 +43,6 @@ uniform bool uUseImageLight;
43
43
uniform sampler2D environmentMapDiffused;
44
44
// texture for use in calculateImageSpecular
45
45
uniform sampler2D environmentMapSpecular;
46
- // roughness for use in calculateImageSpecular
47
- uniform float levelOfDetail;
48
46
49
47
const float specularFactor = 2.0 ;
50
48
const float diffuseFactor = 0.73 ;
@@ -68,7 +66,7 @@ float _lambertDiffuse(vec3 lightDirection, vec3 surfaceNormal) {
68
66
return max (0.0 , dot (- lightDirection, surfaceNormal));
69
67
}
70
68
71
- LightResult _light(vec3 viewDirection, vec3 normal, vec3 lightVector, float shininess) {
69
+ LightResult _light(vec3 viewDirection, vec3 normal, vec3 lightVector, float shininess, float metallic ) {
72
70
73
71
vec3 lightDir = normalize (lightVector);
74
72
@@ -109,7 +107,7 @@ vec2 mapTextureToNormal( vec3 v ){
109
107
}
110
108
111
109
112
- vec3 calculateImageDiffuse( vec3 vNormal, vec3 vViewPosition ){
110
+ vec3 calculateImageDiffuse(vec3 vNormal, vec3 vViewPosition, float metallic ){
113
111
// make 2 seperate builds
114
112
vec3 worldCameraPosition = vec3 (0.0 , 0.0 , 0.0 ); // hardcoded world camera position
115
113
vec3 worldNormal = normalize (vNormal * uCameraRotation);
@@ -120,14 +118,21 @@ vec3 calculateImageDiffuse( vec3 vNormal, vec3 vViewPosition ){
120
118
return mix (smoothstep (vec3 (0.0 ), vec3 (1.0 ), texture.xyz), vec3 (0.0 ), metallic);
121
119
}
122
120
123
- vec3 calculateImageSpecular( vec3 vNormal, vec3 vViewPosition ){
121
+ vec3 calculateImageSpecular(vec3 vNormal, vec3 vViewPosition, float shininess, float metallic ){
124
122
vec3 worldCameraPosition = vec3 (0.0 , 0.0 , 0.0 );
125
123
vec3 worldNormal = normalize (vNormal);
126
124
vec3 lightDirection = normalize ( vViewPosition - worldCameraPosition );
127
125
vec3 R = reflect (lightDirection, worldNormal) * uCameraRotation;
128
126
vec2 newTexCoor = mapTextureToNormal( R );
129
127
#ifdef WEBGL2
130
- vec4 outColor = textureLod(environmentMapSpecular, newTexCoor, levelOfDetail);
128
+ // In p5js the range of shininess is >= 1,
129
+ // Therefore roughness range will be ([0,1]*8)*20 or [0, 160]
130
+ // The factor of 8 is because currently the getSpecularTexture
131
+ // only calculated 8 different levels of roughness
132
+ // The factor of 20 is just to spread up this range so that,
133
+ // [1, max] of shininess is converted to [0,160] of roughness
134
+ float roughness = 20 . / shininess;
135
+ vec4 outColor = textureLod(environmentMapSpecular, newTexCoor, roughness * 8 .);
131
136
#else
132
137
vec4 outColor = TEXTURE(environmentMapSpecular, newTexCoor);
133
138
#endif
@@ -144,6 +149,7 @@ void totalLight(
144
149
vec3 modelPosition,
145
150
vec3 normal,
146
151
float shininess,
152
+ float metallic,
147
153
out vec3 totalDiffuse,
148
154
out vec3 totalSpecular
149
155
) {
@@ -164,7 +170,7 @@ void totalLight(
164
170
vec3 lightVector = (uViewMatrix * vec4 (uLightingDirection[j], 0.0 )).xyz;
165
171
vec3 lightColor = uDirectionalDiffuseColors[j];
166
172
vec3 specularColor = uDirectionalSpecularColors[j];
167
- LightResult result = _light(viewDirection, normal, lightVector, shininess);
173
+ LightResult result = _light(viewDirection, normal, lightVector, shininess, metallic );
168
174
totalDiffuse += result.diffuse * lightColor;
169
175
totalSpecular += result.specular * lightColor * specularColor;
170
176
}
@@ -178,7 +184,7 @@ void totalLight(
178
184
vec3 lightColor = lightFalloff * uPointLightDiffuseColors[j];
179
185
vec3 specularColor = lightFalloff * uPointLightSpecularColors[j];
180
186
181
- LightResult result = _light(viewDirection, normal, lightVector, shininess);
187
+ LightResult result = _light(viewDirection, normal, lightVector, shininess, metallic );
182
188
totalDiffuse += result.diffuse * lightColor;
183
189
totalSpecular += result.specular * lightColor * specularColor;
184
190
}
@@ -204,16 +210,16 @@ void totalLight(
204
210
vec3 lightColor = uSpotLightDiffuseColors[j];
205
211
vec3 specularColor = uSpotLightSpecularColors[j];
206
212
207
- LightResult result = _light(viewDirection, normal, lightVector, shininess);
213
+ LightResult result = _light(viewDirection, normal, lightVector, shininess, metallic );
208
214
209
215
totalDiffuse += result.diffuse * lightColor * lightFalloff;
210
216
totalSpecular += result.specular * lightColor * specularColor * lightFalloff;
211
217
}
212
218
}
213
219
214
220
if ( uUseImageLight ){
215
- totalDiffuse += calculateImageDiffuse(normal, modelPosition);
216
- totalSpecular += calculateImageSpecular(normal, modelPosition);
221
+ totalDiffuse += calculateImageDiffuse(normal, modelPosition, metallic );
222
+ totalSpecular += calculateImageSpecular(normal, modelPosition, shininess, metallic );
217
223
}
218
224
219
225
totalDiffuse *= diffuseFactor;
0 commit comments