着色器中的透明度2

时间:2017-11-26 11:19:46

标签: opengl-es

我有以下着色器,有两个传递和场景混合,但我看到那些面部标记为黑色..

如何保持透明度,而不是在图片中看到这些面孔?

材料逆时针方向在剔除面上进行两次通过,另一种方向顺时针方向通过剔除面。

着色器在法线和相机位置之间产生一个点积,然后根据该结果调整透明度。

enter image description here

#version 100
precision highp int;
precision highp float;

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



varying float  lightDiffuse;

void main()
{

    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;

    vec4 c;
    if(rampLight < 0.0 )
    {
        discard;
    }
    diffuseColor = smoothstep(vec4(0.0, 0.0, 0.0, 0.0), vec4(0.8, 0.8, 0.8, 0.8), vec4(diffuseColor));
    gl_FragColor = diffuseColor;

}




material Router
{
    technique
    {
        pass
        {

            scene_blend alpha_blend
            depth_write on
            depth_check on
            cull_hardware anticlockwise

            vertex_program_ref movingline_101_vs 
            {

            }

            fragment_program_ref movingline_101_fs
            {

            }
        }


        pass
        {


            scene_blend alpha_blend
            cull_hardware clockwise
            depth_write on
            depth_check on

            vertex_program_ref movingline_101_vs 
            {

            }

            fragment_program_ref movingline_101_fs
            {

            }
        }

    }
}

更新

material Router
{
    technique
    {       
        pass
        {

            depth_write on

             vertex_program_ref pass_101_vs 
            {

            }

            fragment_program_ref pass_101_fs
            {

            }


        }

          pass
        {
            depth_write off

            depth_fun equal
            scene_blend add

            vertex_program_ref movingline_101_vs 
            {

            }

            fragment_program_ref movingline_101_fs
            {

            }
        }

    }
}

传递着色器

void main()
{

    gl_FragColor = vec4(0.0,0.0,0.0,0.0);

}

主着色器:

#version 120
precision highp int;
precision highp float;

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



    varying float  lightDiffuse;

    void main()
    {

        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;


        diffuseColor = smoothstep(vec4(0.0, 0.0, 0.0, 0.0), vec4(0.9, 0.9, 0.9, 0.9), vec4(diffuseColor));
         gl_FragColor = diffuseColor;

    }

1 个答案:

答案 0 :(得分:0)

如果您只是尝试渲染具有透明度的东西,但是如果它不透明则无法看到将被遮挡的面,那么一种常见的简单技术是首先填充深度缓冲区。

传递1:只写入深度缓冲区。实现这一目标的最简单方法是使用混合渲染和仅为每个片段输出vec4(0.0,0.0,0.0,0.0)的着色器。可能有更有效的方法来填充深度缓冲区(例如使用glColorMask而不是混合),但这种方法很简单,可能还不错。

传递2:使用GL_EQUAL的深度测试进行渲染。使用你喜欢的任何着色器。

性能说明:丢弃通常比仅为想要在移动硬件上透明的像素输出vec4(0.0,0.0,0.0,0.0)慢,所以除非你真的需要它,否则请避免丢弃 - 在这种情况下你不要需要它。