简体   繁体   中英

OpenGL ES 2.0 Android Clipping Color

i'm using my fragment shader to clip objects in OpenGL ES 2.0. Everything is working well, however the colour of the clipped surface is all black... I can not figure out how to change the colour (well ideally I'd want to make a similar texture to the rest of the object. I have included the code for my fragment shader below.

       precision mediump float; 

       varying vec2 texCoord;
       varying vec3 v_Normal;
       varying vec3 v_Position;
       varying vec4 originalPosition;

       uniform sampler2D texSampler2D;
       uniform vec3 lightPosition;
       uniform vec4 lightColor;

       void main()
       {
          vec3 L = normalize(lightPosition - v_Position);
          vec3 N = normalize(v_Normal);
          float NdotL = max(dot(N,L),0.0);

         if(originalPosition.y >= 2.0){
              discard;
         }else{
              gl_FragColor = NdotL * lightColor * texture2D(texSampler2D, texCoord);
         }
       }

Using discard in a fragment shader doesn't render anything to the pixel, it just leaves it exactly as it was before, so the black color is probably your clear color or whatever you had rendered previously. If you want to render a particular color to the pixels you are currently discarding, add another uniform to your shader for the clip color, like this:

 uniform vec4 clipColor;

Set it in the same way you set the lightColor , then instead of discarding the pixel when clipping, you can set the pixel to the clip color:

if(originalPosition.y >= 2.0) {
     gl_FragColor = clipColor;
} else {
     gl_FragColor = NdotL * lightColor * texture2D(texSampler2D, texCoord);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM