Search Unity

Question Is this Depth Write Shader Possible in Shader Graph?

Discussion in 'Shader Graph' started by ChristmasGT, Sep 20, 2019.

  1. ChristmasGT

    ChristmasGT

    Joined:
    Sep 21, 2011
    Posts:
    11
    Hello! I've been using Unity for quite some time, but unfortunately I'm not quite the best with Shaders at this level.

    The main question I have is: Does Shader Graph have the capability to utilize the below shader? Effectively, the shader includes a color and depth image, allowing the player to move about the active camera with a still image (similar to a pre-rendered background)

    The documentation I have on the shader is from: https://mehm.net/blog/?p=1218 if that's of any help as well. Also added is an image of some early work I was doing on converting it, does it seem I'm on the right path?

    Appreciate any comments or suggestions that could help me along greatly, thank you!

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Background/DepthWrite" {
    4. Properties {
    5.         _DepthTex ("Depth Texture", 2D) = "white" {}
    6.         _RenderedTex ("Rendered Image", 2D) = "white" {}
    7.         _Near("Near clipping plane", Float) = 1
    8.         _Far("Far clipping plane", Float) = 40
    9.     }  
    10.     SubShader {
    11.         Pass {
    12.             CGPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.  
    16.             #include "UnityCG.cginc"
    17.             struct vertOut {
    18.                 float4 pos:SV_POSITION;
    19.                 float2 uv:TEXCOORD0;
    20.             };
    21.  
    22.             vertOut vert(appdata_base v) {
    23.                 vertOut o;
    24.                 o.pos = UnityObjectToClipPos (v.vertex);
    25.                 o.uv = v.texcoord.xy;
    26.                 return o;
    27.             }
    28.  
    29.             uniform sampler2D _DepthTex;
    30.             uniform sampler2D _RenderedTex;
    31.             uniform float4x4 _Perspective;    // The perspective projection of the Unity camera
    32.             uniform float _Near;
    33.             uniform float _Far;
    34.  
    35.             struct fout {
    36.                     half4 color : COLOR;
    37.                     float depth : DEPTH;
    38.                 };  
    39.            
    40.            
    41.             fout frag( vertOut i ) {            
    42.                     fout fo;
    43.  
    44.                     // Read the depth from the depth texture
    45.                     float4 imageDepth4 = tex2D(_DepthTex, i.uv);
    46.                     float imageDepth = -imageDepth4.x;
    47.  
    48.                     // Go back to clip space by computing the depth as the depth of the pixel from the camera
    49.                     float4 temp = float4(0, 0, (imageDepth * (_Far - _Near) - _Near) , 1);
    50.                     float4 clipSpace = mul(_Perspective, temp);
    51.                
    52.                     // Carry out the perspective division and map into screen space (DirectX)                  
    53.                     // We only care about z
    54.                     clipSpace.z /= clipSpace.w;
    55.                     clipSpace.z = 0.5*(1.0-clipSpace.z);
    56.                     float z = clipSpace.z;
    57.  
    58.                     // Write out the pre-computed color and the correct depth.
    59.                     fo.color = tex2D(_RenderedTex, i.uv);
    60.                     fo.depth = z;
    61.                     return fo;
    62.                 }
    63.  
    64.             ENDCG
    65.         }
    66.     }
    67. }
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Nope.
     
  3. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    862
    Is this still the case?

    EDIT: I am asking because I would like to do raymarching using ShaderGraph in HDRP. If one could write explicitly to the depth buffer, then hopefully light, shadows, reflections ect. would work without HDRP shader modification.
     
    Last edited: Jan 11, 2023
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    HDRP specifically does indeed support this now for some stacks. You need to add the Depth Offset input to the master stack. The depth offset is, as you might guess, an offset of the depth value. So you have to calculate the difference from the current depth rather than just overriding it like in your original shader.
     
    KingOfSnake, Matjio and cecarlsen like this.