fix: correct use of specular strength and shininess + opacity

This commit is contained in:
2025-10-01 11:25:43 +02:00
parent fdb891d860
commit 4a9f45b6ef

View File

@ -17,7 +17,12 @@ uniform vec3 diffuseColor;
uniform vec3 specularColor;
uniform float ambientStrength;
uniform float specularStrength;
uniform float shininess;
uniform bool useSpecular;
uniform float opacity;
uniform sampler2D diffuseTex;
uniform bool useTexture;
@ -33,18 +38,31 @@ void main()
vec3 reflectDir = reflect(-lightDir, norm);
// Phong components
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * specularColor;
// float spec = pow(max(dot(viewDir, reflectDir), 0.0), clamp(shininess, 2, 256));
// vec3 specular = (useSpecular) ? specularStrength * spec * specularColor : vec3(0.0);
vec3 halfDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(norm, halfDir), 0.0), clamp(shininess, 2.0, 256.0));
vec3 specular = (useSpecular) ? specularStrength * spec * specularColor : vec3(0.0);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * diffuseColor;
vec3 ambient = ambientStrength * ambientColor;
float distance = length(lightPos - vertexPos);
float attenuation = 1.0 / (1.0 + 0.09 * distance + 0.032 * (distance * distance));
// tweak 0.09 and 0.032 for range/falloff
// diffuse *= attenuation;
// specular *= attenuation;
// ambient *= attenuation; // optional, sometimes left constant
vec3 texColor = (useTexture)
? texture(diffuseTex, TexCoords).rgb
: diffuseColor;
vec3 result = (ambient + diffuse + specular) * texColor;
FragColor = vec4(result, 1.0);
FragColor = vec4(result, opacity);
}