Transparent Shader problem

Started by
5 comments, last by AhmedSaleh 6 years, 5 months ago
hi all
I'm having a problem with fragment shader
I don't want to render the parts that are in red in the image
I have to passes which culls clockwise and anticlockwise
and I'm getting the dot product of the normal with the camera position
if its less than 0 I set a transparent fragment
otherwise discard the fargment
here is the shader
 
Game Programming is the process of converting dead pictures to live ones .
Advertisement

That trick to cull backfaces won't work in all cases with vertex normals because the interpolated normal isn't consistent with the planar geometry. I notice similar artifacts when using that trick for 2-sided lighting. You will need to use face normals, since these are consistent with the underlying geometry, or just tolerate the artifacts.

How would I use the face normals ?

Game Programming is the process of converting dead pictures to live ones .

here is another screenshot of that problem

 

 

2017-11-19.png

Game Programming is the process of converting dead pictures to live ones .

You can get face normals using the derivatives of the interpolated surface position:


varying vec3 lerpPosition; // surface position, interpolated from vertex positions
void main(void)
{
	vec3 faceNormal = normalize( cross( dFdx( lerpPosition ), dFdy( lerpPosition ) ))
}

 

I have tried to use the face normal but the result is very bad all the faces are culled.

 

here is how i use it


#version 100
precision highp int;
precision highp float;


attribute vec4 vertex;
attribute vec3 normal;


uniform mat4 normalMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelView;
uniform vec3 camera_world_position;
varying vec3 ec_pos;

varying vec3 camPos;
void main()
{        
    gl_Position = modelViewProjectionMatrix * vertex;
      vec3 norm = normal;
      //norm *=-1.0;
      ec_pos = vec3(gl_Position.x, gl_Position.y, gl_Position.z);
    camPos = camera_world_position;

    //lightDiffuse = dot(normalize(vec3(norm.x, norm.y, norm.z)), normalize(camera_world_position - vec3(gl_Position.x, gl_Position.y, gl_Position.z)));


     
 


#version 100
precision highp int;
precision highp float;

uniform float time;
uniform float touchX;
uniform float touchY;
uniform float touchZ;
uniform float line;

varying vec3 ec_pos;

varying vec3 camPos;


void main()
{
vec3 ec_normal = normalize(cross(dFdx(ec_pos), dFdy(ec_pos)));



float  lightDiffuse = dot( ec_normal, normalize(camPos));


    float rampLight =lightDiffuse;
    
    float light = (1.0 - rampLight) * 1.0;
    vec4 lightColor = vec4(1.0,1.0,1.0, 1.0);
    vec4 diffuseColor = lightColor * light;
       
    if(rampLight <0.0)
      {
        discard;
      }
    diffuseColor = smoothstep(vec4(0.0, 0.0, 0.0, 0.0), vec4(0.7, 0.7, 0.7, 0.7), vec4(diffuseColor));
    gl_FragColor = diffuseColor;
     
}

    

Game Programming is the process of converting dead pictures to live ones .

This topic is closed to new replies.

Advertisement