【问题标题】:How can I get Z-depth from unlit shader?如何从无光照着色器中获得 Z 深度?
【发布时间】:2016-11-04 18:55:12
【问题描述】:

目前,我尝试将 z 深度效果作为图像效果,但结果图像未正确渲染。出事了……

如果我使用标准着色器(unity 5),结果图像被正确渲染(z 深度图像还可以),但不是无光照着色器。

发生了什么?如果您有任何想法,请告诉我原因。

着色器

Shader "Custom/RenderDepth"
{
    Properties
    {
        _DepthLevel ("Depth Level", Range(1, 3)) = 2
    }
    SubShader
    {
        Pass
        {

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            uniform sampler2D_float _CameraDepthTexture;
            uniform fixed _DepthLevel;
            uniform half4 _MainTex_TexelSize;

            struct uinput
            {
                float4 pos : POSITION;
                half2 uv : TEXCOORD0;
            };

            struct uoutput
            {
                float4 pos : SV_POSITION;
                half2 uv : TEXCOORD0;
            };

            uoutput vert(uinput i)
            {
                uoutput o;
                o.pos = mul(UNITY_MATRIX_MVP, i.pos);
                o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, i.uv);
                return o;
            }

            fixed4 frag(uoutput o) : COLOR
            {
                float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, o.uv));
                depth = pow(Linear01Depth(depth), _DepthLevel);
                return depth;
            }

            ENDCG
        }
    }
}

CS

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class Test : MonoBehaviour
{
    public Camera _cam;
    public Material mat;

    public float DepthLevel = 1.0F;

    void Start ()
    {
        _cam.depthTextureMode |= DepthTextureMode.Depth;
    }

    void Update ()
    {
    }

    void OnRenderImage (RenderTexture source, RenderTexture destination)
    {
        mat.SetFloat("_DepthLevel", DepthLevel);
        Graphics.Blit(source, destination, mat);
    }
}

【问题讨论】:

    标签: unity3d shader


    【解决方案1】:

    我找到了这个解决方案。在渲染我的未点亮对象之前,我使用 VertexLit 旧版着色器中的 SHADOWCASTER 通道。

    那么你的着色器应该是这样的:

    Shader "Custom/RenderDepth"
    {
        Properties
        {
            _DepthLevel ("Depth Level", Range(1, 3)) = 2
        }
        SubShader
        {
            UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    
            Pass
            {
                CGPROGRAM
    
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"
    
                uniform sampler2D_float _CameraDepthTexture;
                uniform fixed _DepthLevel;
                uniform half4 _MainTex_TexelSize;
    
                struct uinput
                {
                    float4 pos : POSITION;
                    half2 uv : TEXCOORD0;
                };
    
                struct uoutput
                {
                    float4 pos : SV_POSITION;
                    half2 uv : TEXCOORD0;
                };
    
                uoutput vert(uinput i)
                {
                    uoutput o;
                    o.pos = mul(UNITY_MATRIX_MVP, i.pos);
                    o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, i.uv);
                    return o;
                }
    
                fixed4 frag(uoutput o) : COLOR
                {
                    float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, o.uv));
                    depth = pow(Linear01Depth(depth), _DepthLevel);
                    return depth;
                }
    
                ENDCG
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要在无光照着色器中提供备用选项,以便它可以使用此备用着色器中的其他所需通道(深度/阴影/等)。

      将以下行添加到您的无光照着色器中应该会有所帮助。

      Fallback "Diffuse"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 2012-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多