Skip to content

Commit f9a1859

Browse files
Feat: disabling normalOffset calculation when not necessary
1 parent 3c8ad44 commit f9a1859

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

webgl-shaders/FragmentShader.glsl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ void main() {
4444
bool lightsEnabled = (u_flatShading == 0.0) && (u_numLights > 0.0 || length(u_ambientLight) > 0.0);
4545

4646
vec3 normal = normalize(v_normal);
47-
vec3 tangent = normalize(v_tangent);
48-
49-
tangent = normalize(tangent - dot(tangent, normal) * normal);
50-
vec3 bitangent = cross(tangent, normal);
51-
vec3 normalOffset = u_normals.x < 0.0 ? applyMaterial(u_normals) * 2.0 - 1.0 : vec3(0.0);
52-
normalOffset.y *= 1.0;
53-
54-
mat3 TBNMatrix = mat3(tangent, bitangent, normal);
55-
vec3 newNormal = normalize(TBNMatrix * normalOffset);
5647

48+
if (u_normals.x < 0.0) {
49+
vec3 tangent = normalize(v_tangent);
50+
tangent = normalize(tangent - dot(tangent, normal) * normal);
51+
vec3 bitangent = cross(tangent, normal);
52+
vec3 normalOffset = applyMaterial(u_normals) * 2.0 - 1.0;
53+
normalOffset.y *= 1.0;
54+
mat3 TBNMatrix = mat3(tangent, bitangent, normal);
55+
normal = normalize(TBNMatrix * normalOffset);
56+
}
57+
5758
// normal = normalize(u_normalMatrix * normal);
5859

5960
vec4 glossiness = u_glossiness.x < 0.0 ? applyMaterial(u_glossiness) : u_glossiness;
@@ -62,7 +63,7 @@ void main() {
6263
vec3 ambience = u_ambientLight * u_baseColor.rgb;
6364

6465
vec4 color = !lightsEnabled ? material :
65-
applyLight(material, newNormal, glossiness, numLights, ambience,
66+
applyLight(material, normal, glossiness, numLights, ambience,
6667
eyeVector, u_lightPosition, u_lightColor, v_position);
6768

6869
gl_FragColor = color;

0 commit comments

Comments
 (0)