Skip to content

Commit

Permalink
[Shaders] Discarding fragments with an alpha value too low
Browse files Browse the repository at this point in the history
- This (finally) effectively handles opacity masking
  • Loading branch information
Razakhel committed May 23, 2024
1 parent 2212d16 commit bc9db43
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
11 changes: 9 additions & 2 deletions shaders/blinn-phong.frag
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ layout(location = 1) out vec3 fragNormal;
layout(location = 2) out vec4 fragSpecular;

void main() {
vec3 color = texture(uniMaterial.baseColorMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.baseColor;
vec4 baseColor = texture(uniMaterial.baseColorMap, vertMeshInfo.vertTexcoords).rgba;
float opacity = texture(uniMaterial.opacityMap, vertMeshInfo.vertTexcoords).r;
float alpha = min(baseColor.a, opacity) * uniMaterial.opacity;

if (alpha < 0.1)
discard;

vec3 color = baseColor.rgb * uniMaterial.baseColor;
vec3 specFactor = texture(uniMaterial.specularMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.specular;
vec3 normal = vertMeshInfo.vertTBNMatrix[2];
vec3 viewDir = normalize(uniCameraPos - vertMeshInfo.vertPosition);
Expand Down Expand Up @@ -87,7 +94,7 @@ void main() {
vec3 ambient = color * 0.05;
vec3 emissive = texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.emissive;

fragColor = vec4(ambient + diffuse + specular + emissive, 1.0);
fragColor = vec4(ambient + diffuse + specular + emissive, alpha);
fragNormal = normal * 0.5 + 0.5;
fragSpecular = vec4(specFactor, 1.0 - max(specFactor.x, max(specFactor.y, specFactor.z)));
}
10 changes: 7 additions & 3 deletions shaders/cook-torrance.frag
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ float computeGeometry(vec3 normal, vec3 viewDir, vec3 lightDir, float roughness)
}

void main() {
// Gamma correction for albedo (sRGB presumed)
vec3 albedo = pow(texture(uniMaterial.baseColorMap, vertMeshInfo.vertTexcoords).rgb, vec3(2.2)) * uniMaterial.baseColor;
vec4 baseColor = texture(uniMaterial.baseColorMap, vertMeshInfo.vertTexcoords).rgba;

if (baseColor.a < 0.1)
discard;

vec3 albedo = pow(baseColor.rgb, vec3(2.2)) * uniMaterial.baseColor;
float metallic = texture(uniMaterial.metallicMap, vertMeshInfo.vertTexcoords).r * uniMaterial.metallicFactor;
float roughness = texture(uniMaterial.roughnessMap, vertMeshInfo.vertTexcoords).r * uniMaterial.roughnessFactor;
float ambOcc = texture(uniMaterial.ambientMap, vertMeshInfo.vertTexcoords).r;
Expand Down Expand Up @@ -156,7 +160,7 @@ void main() {
// Gamma correction
color = pow(color, vec3(1.0 / 2.2));

fragColor = vec4(color, 1.0);
fragColor = vec4(color, baseColor.a);
fragNormal = normal * 0.5 + 0.5;
fragSpecular = vec4(baseReflectivity, roughness);
}
11 changes: 9 additions & 2 deletions shaders/lambert.frag
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec3 fragNormal;

void main() {
vec4 baseColor = texture(uniMaterial.baseColorMap, vertMeshInfo.vertTexcoords).rgba;
float opacity = texture(uniMaterial.opacityMap, vertMeshInfo.vertTexcoords).r;
float alpha = min(baseColor.a, opacity) * uniMaterial.opacity;

if (alpha < 0.1)
discard;

vec3 normal = vertMeshInfo.vertTBNMatrix[2];

float lightHitAngle = 0.0;
Expand All @@ -65,9 +72,9 @@ void main() {
lightHitAngle = max(lightHitAngle, clamp(dot(lightDir, normal), 0.0, 1.0));
}

vec3 diffuse = lightHitAngle * texture(uniMaterial.baseColorMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.baseColor;
vec3 diffuse = lightHitAngle * baseColor.rgb * uniMaterial.baseColor;
vec3 emissive = texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.emissive;

fragColor = vec4(diffuse + emissive, 1.0);
fragColor = vec4(diffuse + emissive, alpha);
fragNormal = normal;
}

0 comments on commit bc9db43

Please sign in to comment.