Skip to content

Commit 296afbc

Browse files
author
Spongman
committed
tweak phong shader lighting balance, fix directionalLight sense
1 parent 332ebf4 commit 296afbc

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/webgl/shaders/phong.frag

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ varying vec3 vAmbientColor;
2525
vec3 V;
2626
vec3 N;
2727

28-
const float specularScale = 0.75;
29-
const float shininess = 20.0;
28+
const float shininess = 32.0;
29+
const float specularFactor = 2.0;
30+
const float diffuseFactor = 0.73;
3031

3132
struct LightResult {
3233
float specular;
@@ -39,7 +40,7 @@ float phongSpecular(
3940
vec3 surfaceNormal,
4041
float shininess) {
4142

42-
vec3 R = normalize(reflect(lightDirection, surfaceNormal));
43+
vec3 R = normalize(reflect(-lightDirection, surfaceNormal));
4344
return pow(max(0.0, dot(R, viewDirection)), shininess);
4445
}
4546

@@ -56,7 +57,7 @@ LightResult light(vec3 lightVector) {
5657
//compute our diffuse & specular terms
5758
LightResult lr;
5859
if (uSpecular)
59-
lr.specular = phongSpecular(L, V, N, shininess) * specularScale;
60+
lr.specular = phongSpecular(L, V, N, shininess);
6061
lr.diffuse = lambertDiffuse(L, N);
6162
return lr;
6263
}
@@ -81,17 +82,17 @@ void main(void) {
8182
if (uPointLightCount == k) break;
8283

8384
vec3 lightPosition = (uViewMatrix * vec4(uPointLightLocation[k], 1.0)).xyz;
84-
vec3 lightVector = lightPosition - vViewPosition;
85+
vec3 lightVector = vViewPosition - lightPosition;
8586

8687
//calculate attenuation
8788
float lightDistance = length(lightVector);
88-
float falloff = 300.0 / (lightDistance + 300.0);
89+
float falloff = 500.0 / (lightDistance + 500.0);
8990

9091
LightResult result = light(lightVector);
9192
diffuse += result.diffuse * falloff * uPointLightColor[k];
9293
specular += result.specular * falloff;
9394
}
9495

9596
gl_FragColor = isTexture ? texture2D(uSampler, vTexCoord) : uMaterialColor;
96-
gl_FragColor.rgb = gl_FragColor.rgb * (diffuse + vAmbientColor) + specular;
97+
gl_FragColor.rgb = gl_FragColor.rgb * (diffuse * diffuseFactor + vAmbientColor) + specular * specularFactor;
9798
}

0 commit comments

Comments
 (0)