Unity3d Cg:省略深度写入

Unity3d Cg:省略深度写入,unity3d,cg,Unity3d,Cg,我在一个分支中输出Cg深度,如下所示: ZWrite On .. void frag(v2f IN, out color : COLOR, out depth : DEPTH) { if (statement) { color = float4(1); } else { color = float4(0); depth = 0; } } 然而,正如你所看到的,我在第一个条件中省略了写深度。这会导致未定义的行为,但我相信这是GLSL中的常见做法(省略对glFragDepth的写

我在一个分支中输出Cg深度,如下所示:

ZWrite On
..
void frag(v2f IN, out color : COLOR, out depth : DEPTH) {
 if (statement) {
  color = float4(1);
 } else {
  color = float4(0);
  depth = 0;
 }
}
然而,正如你所看到的,我在第一个条件中省略了写深度。这会导致未定义的行为,但我相信这是GLSL中的常见做法(省略对glFragDepth的写入将导致原始深度)


当有一个深度值用于输出时,在Cg中的第一个条件下,我应该如何获得原始深度?

YMMV w/此脚本。我记得,代码需要针对OpenGL的旧实现,否则会出现类似
无法屏蔽着色器寄存器的错误

但我相信你可以从相机的深度纹理中提取深度并重写出来。确实需要首先使用顶点着色器中的
ComputeScreenPos
计算投影位置。AFAIK,函数
Linear01Depth
LinearEyeDepth
的文档不存在,因此我无法告诉您性能影响可能是什么

Shader "Depth Shader" { // defines the name of the shader 
SubShader { // Unity chooses the subshader that fits the GPU best
  Pass { // some shaders require multiple passes
     ZWrite On
     CGPROGRAM // here begins the part in Unity's Cg

     #pragma vertex vert 
     #pragma fragment frag
     #include "UnityCG.cginc"
     struct v2f
     {
        float4 position : POSITION;
        float4 projPos : TEXCOORD1;
     };

     v2f vert(float4 vertexPos : POSITION)
     {
        v2f OUT;
        OUT.position = mul(UNITY_MATRIX_MVP, vertexPos);
        OUT.projPos = ComputeScreenPos(OUT.position);
        return OUT;
     }

     //camera depth texture here
     uniform sampler2D _CameraDepthTexture; //Depth Texture
     void frag(v2f IN, out float4 color:COLOR, out float depth:DEPTH) // fragment shader
     {
        color = float4(0);
         // use eye depth for actual z...
        depth = LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos)).r); 

        //or this for depth in between [0,1]
        //depth = Linear01Depth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos)).r);
     }

     ENDCG // here ends the part in Cg 
  }
}
}

谢谢你,杰达克。我第二次传球,但很高兴知道这是另一种选择。