Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Fragment Shader draw into _CamerDepthNormalTexture

Discussion in 'Shaders' started by Wayward_Studios, Jul 10, 2019.

  1. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Hello, i'm using a post processing effect, which relies on cameraDepth and cameraDepthNormal textures to create an outline effect.
    I'm currently in the process of adding a cel-shader to render individual objects. For this i'm using a fragment shader, in which i calculate the cell shading through the classic (dot(normalize(_WorldSpaceLightPos0), normal)).

    The probem i'm encountering now is that this fragment shader is not writing model-normal information into the _CamerDepthNormalTexture, meaning it's not working with th epost processing effect correctly. Is there a pass i can use which automates this or any is it only possible by actually altering the _CamerDepthNormalTexture.

    Here is a post processing effect only rendering the depth normal with the standard shader vs with my cel shading shader.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You need to set a RenderType in the SubShader Tags. Most likely "Opaque".
     
  3. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Already have a render type opaque, here i'll share part of my code, maybe you can spot something.
    Code (CSharp):
    1. Shader "Wayward/CellTest1"
    2. {
    3.     Properties
    4.     {
    5.         ...
    6.     }
    7.     SubShader
    8.     {
    9.         Pass
    10.         {
    11.             Tags {"RenderType" = "Opaque" "PassFlags" = "OnlyDirectional"}
    12.  
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.          
    17.             #include "UnityCG.cginc"
    18.             #include "Lighting.cginc"
    19.             #include "AutoLight.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;              
    24.                 float4 uv : TEXCOORD0;
    25.                 float3 normal : NORMAL;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float4 pos : SV_POSITION;
    31.                 float3 worldNormal : NORMAL;
    32.                 float2 uv : TEXCOORD0;
    33.                 float3 viewDir : TEXCOORD1;
    34.                 SHADOW_COORDS(2)
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             sampler2D _GlossMap;
    39.             float4 _MainTex_ST;
    40.          
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.pos = UnityObjectToClipPos(v.vertex);
    45.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);      
    46.                 o.viewDir = WorldSpaceViewDir(v.vertex);
    47.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    48.                 TRANSFER_SHADOW(o)
    49.                 return o;
    50.             }
    51.          
    52.             float4 _Color;
    53.  
    54.             float4 _AmbientColor;
    55.          
    56.             float4 _GlossColor;
    57.             float _GlossFade;
    58.             float _GlossStrength;
    59.          
    60.             float4 _LightColor;
    61.             float _LightFade;  
    62.             float _LightStrength;  
    63.          
    64.             float4 _ShadowColor1;
    65.             float _Shadow1Fade;  
    66.             float _Shadow1Strength;  
    67.             float _Shadow1Lerp;  
    68.          
    69.             float4 _ShadowColor2;
    70.             float _Shadow2Strength;  
    71.             float _Shadow2Lerp;  
    72.  
    73.             float _Waveyness;
    74.          
    75.             float4 frag (v2f i) : SV_Target
    76.             {
    77.                 ...
    78.             }
    79.             ENDCG
    80.         }
    81.         UsePass "VertexLit/ShadowCaster"
    82.     }
    83.     Fallback "Diffuse"
    84. }
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You have the render type defined in the Pass's Tags, which isn't valid. It must be in the SubShader's Tags.
     
  5. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Oh yeah, my bad, sadly still doesn't make a difference if i move it outside.
     
  6. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Just tried a clean fragment shader, which is also not drawing anything into the _CameraDepthNormalTexture
    Code (CSharp):
    1. Shader "Wayward/Normals Test"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             // make fog work
    18.             #pragma multi_compile_fog
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;              
    25.                 float4 uv : TEXCOORD0;
    26.                 float3 normal : NORMAL;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float4 pos : SV_POSITION;
    32.                 float3 worldNormal : NORMAL;
    33.                 float2 uv : TEXCOORD0;
    34.                 float3 viewDir : TEXCOORD1;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.  
    40.             v2f vert (appdata v)
    41.             {
    42.                 v2f o;
    43.                 o.pos = UnityObjectToClipPos(v.vertex);
    44.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);      
    45.                 o.viewDir = WorldSpaceViewDir(v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 return o;
    48.             }
    49.  
    50.             fixed4 frag (v2f i) : SV_Target
    51.             {
    52.                 // sample the texture
    53.                 fixed4 col = tex2D(_MainTex, i.uv);
    54.                 // apply fog
    55.                 UNITY_APPLY_FOG(i.fogCoord, col);
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.         UsePass "Standard/ShadowCaster"
    61.     }
    62.     Fallback "Standard"
    63. }
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    I'm not sure what else could be the problem. Nothing in the shader itself should matter for the _CameraDepthNormalTexture. When using the forward rendering path, the _CameraDepthNormalTexture is generated using a replacement shader render of the scene using the Internal-DepthNormalsTexture.shader, which ignores your shader's code entirely apart from the RenderType.


    Unless ...


    Are you rendering using the deferred rendering path? The normals in the depth normal texture for any forward rendered object will be broken when using the deferred rendering path. Any shader that's not setup to work with the deferred pipeline will be rendered using the forward rendering path, and inject only depth information into the gbuffers using the shader's shadow caster pass which only renders to the diffuse gbuffer and depth. The _CameraDepthNormalTexture is then generated by combining the gbuffer normal and depth textures rather than using a replacement shader pass. This means any forward rendered opaque objects do not show up in the normals of that texture.

    If you're trying to do a toon shaded look for your game, you want to modify the deferred shading shader itself and not necessarily the on object shaders. Or if you're looking to have more control over each objects' appearance you'll likely want to be using the forward rendering path.
     
  8. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Yeah that's it, i used deferred rendering. But now my _CameraDepthNormalTexture shows only grey (probably default unity grey), with and without Cel-Shader. I did change the depthtexturemode of the camera to depthNormals. Am i missing something?
     
  9. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    I tried a bunch of stuff, but the Internal-DepthNormalsTexture.shader does not seem to be writing to
    _CameraDepthNormalsTexture nor _CameraNormalsTexture, as they both appear plain grey. I tried calculating the worldspace normals myself, but ran into problems on that front. Is it possible to let the shader draw world normal information into these textures in forward rendering or is this limited to deferred rendering? The depth texture in both rendering paths seems fine.
     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    That should be all you need in forward to tell Unity the texture should be rendered. The only other thing I can think of is if you modified the Graphics settings to not include that shader.

    Like this:
    upload_2019-7-11_9-37-47.png

    That should be set to "Built-in shader".

    It could also be a bug with how you're setting the depthTextureMode, mainly if you have multiple scripts setting the value. When running the camera component should list what modes are enabled, so make sure it's listed. The normal depth texture gets automatically enabled by several internal things, so it may be active even if it's not explicitly enabled on the camera.

    If it's not listed as being enabled on the camera, make sure you don't have any code doing:
    cam.depthTextureMode = *something*

    You should always use:
    cam.depthTextureMode |= *something*

    Unless you're explicitly setting the mode to none.
     
  11. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    It is set to build-in Shader, maybe i'm doing something wrong in the shader that draws the normal.
    This is the code i'm using to render the depth normal as post process effect:
    Code (CSharp):
    1. Shader "Wayward/Draw Depth Normal"
    2. {
    3.     SubShader
    4.     {
    5.         Cull Off ZWrite Off ZTest Always
    6.  
    7.         Pass
    8.         {
    9.             HLSLPROGRAM
    10.             #pragma vertex Vert
    11.             #pragma fragment Frag
    12.             #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
    13.  
    14.             TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    15.             TEXTURE2D_SAMPLER2D(_CameraNormalsTexture, sampler_CameraNormalsTexture);
    16.        
    17.             float4 _MainTex_TexelSize;
    18.  
    19.             struct v2f
    20.             {
    21.                 float4 vertex : SV_POSITION;
    22.                 float2 texcoord : TEXCOORD0;
    23.             };
    24.  
    25.             v2f Vert(AttributesDefault v)
    26.             {
    27.                 v2f o;
    28.                 o.vertex = float4(v.vertex.xy, 0.0, 1.0);
    29.                 o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
    30.                 o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
    31.  
    32.  
    33.                 return o;
    34.             }
    35.  
    36.             float4 Frag(v2f i) : SV_Target
    37.             {
    38.                 float4 color = SAMPLE_TEXTURE2D(_CameraNormalsTexture, sampler_CameraNormalsTexture, i.texcoord);
    39.  
    40.                 return color;
    41.             }
    42.             ENDHLSL
    43.         }
    44.     }
    45. }
    Which renders correctly with deferred but not with forward.
    Also created a new project and just included the shader above, same thing, no normal texture in forward.
    Also thanks for the many replies! Have been seeing you around these forums for over a year now under almost every shader relatet thread and just wanted to thank you for that!
     
    gattusoarthur likes this.
  12. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The _CameraNormalsTexture only exists when using the deferred rendering path, and I believe is the same as _CameraGBufferTexture2. _CameraDepthNormalsTexture is the one that exists in both the deferred and forward path.
     
  13. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Also tried that, had no effect on the results, still just plain grey. But using _CameraDepthNormalsTexture gives me grey results even in deferred rendering.
     
  14. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Did you check the camera actually has DepthNormals enabled?
    upload_2019-7-11_10-58-31.png
     
  15. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Yes it does, i found out, that the _CameraDepthNormalsTexture, can't be handled like the other textures. i didn't know you had to decode it using "DecodeDepthNormal", but i am now stuck with a single colored screen, which changes on camera position and rotation, so i'm assuming the uv is not the same as the display, which confuses me a little bit.
    I'll be trying some things and try to get it to work.
    Here's the code for the whole screen normal if you want to call it that :D
    Code (CSharp):
    1. Shader "Wayward/Draw Depth Normal"
    2. {
    3.     SubShader
    4.     {
    5.         Cull Off ZWrite Off ZTest Always
    6.  
    7.         Pass
    8.         {
    9.             HLSLPROGRAM
    10.             #pragma vertex vert
    11.             #pragma fragment frag
    12.             #include "UnityCG.cginc"
    13.            
    14.             sampler2D _MainTex, sampler_MainTex;
    15.             sampler2D _CameraDepthNormalsTexture , sampler_CameraDepthNormalsTexture;
    16.        
    17.             float4 _MainTex_TexelSize;
    18.            
    19.             struct appdata{
    20.                 float4 vertex : POSITION;
    21.                 float2 uv : TEXCOORD0;
    22.             };
    23.  
    24.             struct v2f
    25.             {
    26.                 float4 vertex : SV_POSITION;
    27.                 float2 texcoord : TEXCOORD0;
    28.             };
    29.  
    30.             v2f vert(appdata v)
    31.             {
    32.                 v2f o;
    33.                 o.vertex = float4(v.vertex.xy, 0.0, 1.0);
    34.                 o.texcoord = v.uv;
    35.  
    36.  
    37.                 return o;
    38.             }
    39.  
    40.             float4 frag(v2f i) : SV_Target
    41.             {
    42.                 float4 depthnormal = tex2D(_CameraDepthNormalsTexture, i.texcoord);
    43.                
    44.                 float3 normal;
    45.                 float depth;
    46.                
    47.                 DecodeDepthNormal(depthnormal, depth, normal);
    48.                
    49.                 float4 color = tex2D(_CameraDepthNormalsTexture, i.texcoord);
    50.  
    51.                 return float4(normal, 0);
    52.             }
    53.             ENDHLSL
    54.         }
    55.     }
    56. }
     
  16. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The DepthNormals texture requires special handling to decode the normals, and those normals are in view space.
    https://docs.unity3d.com/Manual/SL-CameraDepthTexture.html

    You can transform them back into world space using:
    Code (csharp):
    1. float3 viewNormal;
    2. float depth;
    3. DecodeDepthNormal(depthnormal, depth, viewNormal);
    4. float3 worldNormal = mul(float3x3)unity_CameraToWorld, viewNormal);
    Still no idea why you're getting a solid color if there are objects in view of the camera. I used the above shader you posted and I get correct normals in the depth normals texture as I would expect.

    Also, try using the frame debugger (under the Window menu, or Window > Analysis in more recent versions of the editor). Then you can step through rendering and preview the depth normals texture with out needing to write your own code to do so, though it does show the texture directly and not the decoded form. For the normals in the texture those are still easy to see though without decoding.

    The top sphere is using the "Wayward/Normals Test" shader you posted. The capsule is the standard shader.
    upload_2019-7-11_11-10-14.png


    Here's the script I used to enable the depth normals texture:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Camera))]
    6. public class SetDepthNormalsMode : MonoBehaviour
    7. {
    8.     public bool EnableDepth;
    9.     public bool EnableDepthNormals;
    10.     public bool EnableMotionVectors;
    11.  
    12.     void Start()
    13.     {
    14.         var cam = GetComponent<Camera>();
    15.         if (EnableDepth)
    16.             cam.depthTextureMode |= DepthTextureMode.Depth;
    17.         if (EnableDepthNormals)
    18.             cam.depthTextureMode |= DepthTextureMode.DepthNormals;
    19.         if (EnableMotionVectors)
    20.             cam.depthTextureMode |= DepthTextureMode.MotionVectors;
    21.     }
    22. }
     
    Last edited: Jul 11, 2019
  17. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Now i'm totally confused, it's not working for me, for some reasen. The depthnormals texture is rendered correctly in the frame debugger, everything looks fine, but i still get a solid color. I'll start another empty project and try it there. Maybe i jsut overlooked some setting i tinkered with and forgot to reset. Will give an update soon.
     
  18. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Sounds like you have a typo someplace in your shader for the name of _CameraDepthNormalsTexture?

    Also, I was wrong about this:
    Unfortunately, while Unity does have that as an available matrix, it doesn't appear to actually get set? Not sure, but it explains why everyone passes in their own matrix from script.
    https://www.ronja-tutorials.com/2018/07/08/postprocessing-normal.html
     
  19. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    I'm even more confused, did you use the exact same sahder i posted?
    Always get the same one colored result. I can even confirm that the texture inputtet into the shader is correct. upload_2019-7-11_20-29-4.png
     
  20. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    ... oh. I just noticed this line:
    Yeah, no. That's now how that works. You're confusing two different ways of defining textures in shaders. the D3D9 style and the D3D11 style.

    D3D9 looks like this:
    Code (csharp):
    1. // define texture
    2. sampler2D _TextureName;
    3.  
    4. // sample texture
    5. fixed4 col = tex2D(_TextureName, uv);
    D3D11 looks like this:
    Code (csharp):
    1. // define texture & sampler
    2. Texture2D _TextureName;
    3. SamplerState sampler_TextureName;
    4.  
    5. // sample texture
    6. fixed4 col = _TextureName.Sample(sampler_TextureName, uv);
    The post processing stack and the SRPs use macros to wrap the D3D11 style code into something that looks like this:
    Code (csharp):
    1. // define texture & sampler
    2. TEXTURE2D_SAMPLER2D(_TextureName, sampler_TextureName);
    3.  
    4. // sample texture
    5. SAMPLE_TEXTURE2D(_TextureName, sampler_TextureName, uv);
    You can't mix the D3D9 and D3D11 styles for defining and sampling a single texture, you have to use one or the other. Technically you are still defining _CameraDepthNormalsTexture properly, but it's possible having the additional sampler_CameraDepthNormalsTexture is causing some problems.
     
  21. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    I used "Wayward/Normal Test" exactly as you posted it with zero modifications. Purely copy and paste.

    Note, I did not use "Wayward/Draw Depth Normal" to preview, I used the frame debugger and additionally my own in-scene preview shader since I was being lazy about wanting to write an image effect script.
     
  22. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    Oh yeah, taht was my mistake, was using DX11 at the beginning and switched to dx9 but just let that there. Still sadly didn't make a difference deleting it. I also tried directly copying the post processing from the link you postet, but using the post processing stack (hlslprogram) and now i get this result: upload_2019-7-11_20-47-54.png
    Still not what i want, but at least the screen is just partially blue now :D
    Here the shader:
    Code (CSharp):
    1. Shader "Wayward/Draw Depth Normal"
    2. {
    3.     SubShader
    4.     {
    5.         Cull Off ZWrite Off ZTest Always
    6.         Pass
    7.         {
    8.             HLSLPROGRAM
    9.             #pragma vertex vert
    10.             #pragma fragment frag
    11.             #include "UnityCG.cginc"
    12.          
    13.             sampler2D _CameraDepthNormalsTexture;
    14.      
    15.             struct appdata{
    16.                 float4 vertex : POSITION;
    17.                 float2 uv : TEXCOORD0;
    18.             };
    19.             struct v2f{
    20.                 float4 position : SV_POSITION;
    21.                 float2 uv : TEXCOORD0;
    22.             };
    23.             v2f vert(appdata v){
    24.                 v2f o;
    25.                 o.position = UnityObjectToClipPos(v.vertex);
    26.                 o.uv = v.uv;
    27.                 return o;
    28.             }
    29.             float4 frag(v2f i) : SV_Target
    30.             {
    31.                 float4 depthnormal = tex2D(_CameraDepthNormalsTexture, i.uv);
    32.              
    33.                 float3 viewNormal;
    34.                
    35.                 float depth;
    36.                 DecodeDepthNormal(depthnormal, depth, viewNormal);
    37.              
    38.                 return float4(viewNormal, 0);
    39.             }
    40.             ENDHLSL
    41.         }
    42.     }
    43. }
     
  23. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Here's the test shader I use:
    Code (csharp):
    1. Shader "Unlit/DepthNormalsPreview"
    2. {
    3.     Properties
    4.     {
    5.         [Toggle] _ShowDepth ("Show Depth", Float) = 0
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "Queue"="Transparent" }
    10.         LOD 100
    11.         Cull Off
    12.         ZWrite Off
    13.         ZTest Always
    14.  
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.  
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float4 pos : SV_POSITION;
    32.                 float2 uv : TEXCOORD0;
    33.             };
    34.  
    35.             sampler2D _CameraDepthNormalsTexture;
    36.             bool _ShowDepth;
    37.  
    38.             v2f vert (appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.pos = float4(v.vertex.xy * float2(2,-2), 0.0, 1);
    42.                 o.uv = v.uv;
    43.                 return o;
    44.             }
    45.  
    46.             fixed4 frag (v2f i) : SV_Target
    47.             {
    48.                 fixed4 depthNormals = tex2D(_CameraDepthNormalsTexture, i.uv);
    49.                 half3 viewNormal;
    50.                 float depth;
    51.                 DecodeDepthNormal(depthNormals, depth, viewNormal);
    52.  
    53.                 // UNITY_MATRIX_I_V only works when not an image effect
    54.                 half3 worldNormal = mul((float3x3)UNITY_MATRIX_I_V, viewNormal);
    55.  
    56.                 if (_ShowDepth)
    57.                     return fixed4(depth, depth, depth, 1);
    58.                 if (depth >= 0.99999)
    59.                     return fixed4(0,0,0,1);
    60.                 return fixed4(worldNormal, 1);
    61.             }
    62.             ENDCG
    63.         }
    64.     }
    65. }
    This is just thrown on a quad thrown in the scene. If the quad's game object is in view, the camera will show the depth normal texture's world normal, with the "background" (anywhere at the max depth) blacked out.
     
  24. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    that actually works, thanks a lot! Now i just need to try to get it as an post rpocessing effect, so i can use it in my outline post processing!
     
  25. Wayward_Studios

    Wayward_Studios

    Joined:
    Dec 15, 2018
    Posts:
    23
    I actually managed to do it, after some dumb stuff i did :D
    upload_2019-7-11_21-41-19.png
    For anyone who want to have this as a post processing effect, here's a shader, it does a little bit more and is completely trashed up, but you can get the gist from it!
    Code (CSharp):
    1. Shader "Wayward/Draw Depth Normal"
    2. {
    3.     SubShader
    4.     {
    5.         Cull Off ZWrite Off ZTest Always
    6.  
    7.         Pass
    8.         {
    9.             HLSLPROGRAM
    10.             #pragma vertex Vert
    11.             #pragma fragment Frag
    12.             #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
    13.  
    14.             TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    15.             TEXTURE2D_SAMPLER2D(_CameraDepthNormalsTexture, sampler_CameraDepthNormalsTexture);
    16.             TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
    17.      
    18.             float4 _MainTex_TexelSize;
    19.  
    20.             float4x4 _ClipToView;
    21.  
    22.             struct v2f
    23.             {
    24.                 float4 vertex : SV_POSITION;
    25.                 float2 texcoord : TEXCOORD0;
    26.                 float2 texcoordStereo : TEXCOORD1;
    27.                 float3 viewSpaceDir : TEXCOORD2;
    28.             };
    29.          
    30.             inline float DecodeFloatRG( float2 enc )
    31.             {
    32.                 float2 kDecodeDot = float2(1.0, 1/255.0);
    33.                 return dot( enc, kDecodeDot );
    34.             }
    35.          
    36.             inline void DecodeDepthNormal( float4 enc, out float depth, out float3 normal )
    37.             {
    38.                 depth = DecodeFloatRG (enc.zw);
    39.                 normal = DecodeViewNormalStereo (enc);
    40.             }
    41.  
    42.             v2f Vert(AttributesDefault v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = float4(v.vertex.xy, 0.0, 1.0);
    46.                 o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
    47.                 o.viewSpaceDir = mul(_ClipToView, o.vertex).xyz;
    48.  
    49.             #if UNITY_UV_STARTS_AT_TOP
    50.                 o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
    51.             #endif
    52.                 return o;
    53.             }
    54.  
    55.             float4 Frag(v2f i) : SV_Target
    56.             {
    57.                 float4 depthNormals = SAMPLE_TEXTURE2D(_CameraDepthNormalsTexture, sampler_CameraDepthNormalsTexture , i.texcoord);
    58.                 half3 viewNormal;
    59.                 float depth;
    60.                 DecodeDepthNormal(depthNormals, depth, viewNormal);
    61.  
    62.                 return float4(viewNormal,1);
    63.             }
    64.             ENDHLSL
    65.         }
    66.     }
    67. }
    Also many thanks to ben golus for helping that much!
    Maybe someday someone other than me get's a use out of this thread in some way. :D