Jump to content
  • Advertisement
Sign in to follow this  
AhmedSaleh

3D Transparent Shader problem

Recommended Posts

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
 
Edited by AhmedSaleh

Share this post


Link to post
Share on other sites
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.

Edited by Aressera

Share this post


Link to post
Share on other sites

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 ) ))
}

 

Share this post


Link to post
Share on other sites

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;
     
}

    

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!