How to modify the Deferred Shading when using Transparent texture model?

//
#version 120
varying vec3 vertNormal;
varying vec4 vertPosition;
uniform sampler2D colorTexture;

vec3 phong(vec3 normal, vec3 lightPosition, vec3 position, vec3 ka, vec3 kd, vec3 ks, float shine)
{
vec3 s = normalize(vec3(lightPosition) - position);
vec3 v = normalize(vec3(-position));
vec3 r = reflect(-s, normal);
return ka + kd * max( dot(s, normal), 0.0 ) + ks * pow(max(dot(r,v), 0.0), shine);
}

void main(void)
{
vec3 n = normalize(vertNormal);
//
vec3 lightPosition = vec3(0); // headlight
//
vec4 texColor = texture2D(colorTexture, gl_TexCoord[0].st);
vec3 color = phong(n, lightPosition, vertPosition.xyz, vec3(0),texColor.rgb, vec3(0), 16.0f);
// Color buffer
gl_FragData[0] = vec4(color, 1.0);
// Normal buffer
gl_FragData[1] = vec4(n * 0.5 + 0.5, 1.0);
}

Alpha testing, i.e. discard the fragment (so nothing is written to the G-buffer) if the texture alpha is below a given threshold.

For performance reasons, you only want to do this for textures which actually have an alpha channel. The presence of any discard statements within the fragment shader will typically disable early fragment tests regardless of whether they’re executed.

By so judging texColor.w < 1.0 ?

void main(void)
{
vec3 n = normalize(vertNormal);
//
vec3 lightPosition = vec3(0); // headlight
//
vec4 texColor = texture2D(colorTexture, gl_TexCoord[0].st);
if(texColor.w < 1.0)
{
// Color buffer
gl_FragData[0] = texColor;
}
else
{
vec3 color = phong(n, lightPosition, vertPosition.xyz, vec3(0),texColor.rgb, vec3(0), 16.0f);
// Color buffer
gl_FragData[0] = vec4(color, 1.0);
}
// Normal buffer
gl_FragData[1] = vec4(n * 0.5 + 0.5, 1.0);
}

Well, you’d probably set the threshold somewhat lower.

If the alpha test fails, you need to discard the fragment, i.e.

 if (texColor.a < 0.5)
    discard;

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.