【问题标题】:Write to the depth buffer in a Unity 3D unlit shader?在 Unity 3D 无光照着色器中写入深度缓冲区?
【发布时间】:2019-04-30 15:29:53
【问题描述】:

我承认我对着色器有点迷茫。下面是我的未点亮着色器。我可以添加什么来写入深度缓冲区和深度纹理?另外,我在这个着色器中做错了什么或不必要的事情吗?

Shader "Unlit/Environment Texture" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _MainTexBias ("Mip Bias (-1 to 1)", float) = -1
}

SubShader {
    Tags { "RenderType"="Opaque" }

    Lighting Off
    Fog { Mode off }

        Pass {  
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"

                struct appdata_t {
                    float4 vertex : POSITION;
                    float2 texcoord : TEXCOORD0;
                };

                struct v2f {
                    float4 vertex : SV_POSITION;
                    half2 texcoord : TEXCOORD0;
                };

                sampler2D _MainTex;
                float4 _MainTex_ST;
                half _MainTexBias;

                v2f vert (appdata_t v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                    return o;
                }

                fixed4 frag (v2f i) : SV_Target
                {
                    fixed4 col = tex2Dbias(_MainTex, half4(i.texcoord.x,i.texcoord.y,0.0,_MainTexBias));
                    return col;
                }
            ENDCG
        }
    }
}

【问题讨论】:

    标签: unity3d shader


    【解决方案1】:

    片段应该具有 SV_Depth 语义

    struct fragOutput {
        fixed4 color : SV_Target;
        float depth:SV_Depth;
    };
    
    fragOutput frag (v2f i)
    {
        fragOutput o;
        o.color = tex2D(_MainTex, i.uv);
        o.depth = i.depth;
        return o;
    }
    

    【讨论】:

      【解决方案2】:

      嗯...这应该已经写入深度缓冲区了?您可以将“Queue”="Opaque" 添加到标签以启用从前到后的排序,但我认为无论如何它都应该默认为这个。 Ztest On 也是隐含的。它在 Unity 中的表现如何?

      【讨论】:

        猜你喜欢
        • 2016-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-18
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        相关资源
        最近更新 更多