Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[can not get z depth from unlit shader]

Discussion in 'General Graphics' started by zincfinger, Mar 9, 2015.

  1. zincfinger

    zincfinger

    Joined:
    Jun 18, 2013
    Posts:
    1
    Hello everyone!

    Currently, I try to make z depth effect as Image Effect, but result image is not correctly rendered. something wrong...


    If I use standard shader (unity 5), result image was correctly rendered(z depth image is ok), but not unlit shader.


    what happen? if you have any idea, let me known!!



    ############Shader################

    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);
    }
    }







    standard.jpg
    unlit.jpg
     
    lucasjohansson likes this.
  2. OlliQueck

    OlliQueck

    Joined:
    Apr 11, 2013
    Posts:
    49
    hey same problem here, did you find a solution?
    afaik the problem is that the unlit shader, as well as transparent shader and simple fragment shader dont write to depth buffer.
     
  3. fprost

    fprost

    Joined:
    Mar 2, 2015
    Posts:
    1
    Yeah it seems that those unlit shader don't write into the depth buffer, I fell in that trap too. My guess is that these materials can be transparent (either a texture with alpha for "Unlit/Texture" or a color with alpha for "Unlit/Color") and in this case writing into the depth buffer doesn't make much sense ? I don't know...
    Anyway I was able to use another shader (Toon/Basic) which gave me pretty much the same result as I don't have any lights in my scene.
     
  4. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Your missing a fallback for the shader, unity relies on shader replacment for the depth generation unless you are using deferred.
    Add a fallback to your shader so it falls back to one of unity's unlit shaders and you should be fine.
     
  5. Serejka

    Serejka

    Joined:
    Oct 20, 2015
    Posts:
    6
    Wich fallback?
     
  6. opamped

    opamped

    Joined:
    Oct 3, 2015
    Posts:
    6
    VertexLit worked as a fallback for me.