Search Unity

Question Built in PP Stack V2 world position jittered in game view, working fine in scene view?!

Discussion in 'Shaders' started by John_Leorid, Sep 26, 2022.

  1. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    651
    Don't ask why, I want to draw topological height lines in post processing.
    And it's somewhat working but only in the scene view, in the game view the lines are bouncing every time I move the camera and it's driving me nuts.

    Of course I did read a couple of posts and it seems that this might have something to do with the depth texture jittering for TAA - but anti aliasing is disabled ..

    Here is the C# part:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Rendering.PostProcessing;
    4.  
    5. [Serializable]
    6. [PostProcess(typeof(HeightLinesPPFXRenderer), PostProcessEvent.BeforeTransparent, "Custom/HeightLinesPPFX")]
    7. public sealed class HeightLinesPPFX : PostProcessEffectSettings
    8. {
    9.     [Range(0f, 1f), Tooltip("HeightLinesPPFX effect intensity.")]
    10.     public FloatParameter blend = new FloatParameter { value = 0.5f };
    11.     [Range(0f, 10f), Tooltip("HeightLinesPPFX thickness.")]
    12.     public FloatParameter thickness = new FloatParameter { value = 0.5f };
    13.     public ColorParameter color = new ColorParameter { value = Color.white };
    14. }
    15. public sealed class HeightLinesPPFXRenderer : PostProcessEffectRenderer<HeightLinesPPFX>
    16. {
    17.     public override DepthTextureMode GetCameraFlags()
    18.     {
    19.         return DepthTextureMode.Depth;
    20.     }
    21.  
    22.     public override void Render(PostProcessRenderContext context)
    23.     {
    24.         var sheet = context.propertySheets.Get(Shader.Find("Hidden/Custom/HeightLinesPPFX"));
    25.  
    26.         sheet.properties.SetMatrix("_ViewProjectInverse", (context.camera.projectionMatrix * context.camera.worldToCameraMatrix).inverse);
    27.         sheet.properties.SetFloat("_LineThickness", settings.thickness);
    28.         sheet.properties.SetColor("_LineColor", settings.color);
    29.  
    30.         context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    31.     }
    32. }
    33.  
    And there goes the shader:
    Code (CSharp):
    1. Shader "Hidden/Custom/HeightLinesPPFX"
    2. {
    3.     HLSLINCLUDE
    4.         #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
    5.  
    6.         TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    7.         TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
    8.  
    9.         float4x4 UNITY_MATRIX_MVP;
    10.         float4x4 _ViewProjectInverse;
    11.  
    12.         float _LineThickness;
    13.         float3 _LineColor;
    14.      
    15.         struct FragInput
    16.         {
    17.             float4 vertex    : SV_Position;
    18.             float2 texcoord  : TEXCOORD0;
    19.             float3 cameraDir : TEXCOORD1;
    20.         };
    21.  
    22.         FragInput VertMain(AttributesDefault v)
    23.         {
    24.             FragInput o;
    25.          
    26.             o.vertex   = mul(UNITY_MATRIX_MVP, float4(v.vertex.xyz, 1.0));
    27.             o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
    28.  
    29. #if UNITY_UV_STARTS_AT_TOP
    30.             o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
    31. #endif
    32.  
    33.             float4 cameraLocalDir = mul(_ViewProjectInverse, float4(o.texcoord.x * 2.0 - 1.0, o.texcoord.y * 2.0 - 1.0, 0.5, 1.0));
    34.             cameraLocalDir.xyz /= cameraLocalDir.w;
    35.             cameraLocalDir.xyz -= _WorldSpaceCameraPos;
    36.  
    37.             float4 cameraForwardDir = mul(_ViewProjectInverse, float4(0.0, 0.0, 0.5, 1.0));
    38.             cameraForwardDir.xyz /= cameraForwardDir.w;
    39.             cameraForwardDir.xyz -= _WorldSpaceCameraPos;
    40.  
    41.             o.cameraDir = cameraLocalDir.xyz / length(cameraForwardDir.xyz);
    42.          
    43.             return o;
    44.         }
    45.  
    46.         float4 FragMain(FragInput i) : SV_Target
    47.         {
    48.             float3 sceneColor  = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord).rgb;
    49.             float  sceneDepth  = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord).r;
    50.  
    51.             float depth01 = Linear01Depth(sceneDepth);
    52.             float linearDepth = LinearEyeDepth(sceneDepth);
    53.             float3 worldPosition = (i.cameraDir * linearDepth) + _WorldSpaceCameraPos;
    54.          
    55.  
    56.             float isLine = float(abs(fmod(worldPosition.y, 10)) < _LineThickness && sceneDepth > 0.0);
    57.             sceneColor = lerp(sceneColor, _LineColor, isLine);
    58.  
    59.             //sceneColor = float3(1-depth01, 0, 0);
    60.  
    61.             return float4(sceneColor, 1.0);
    62.         }
    63.  
    64.  
    65.     ENDHLSL
    66.  
    67.     SubShader
    68.     {
    69.         Cull Off ZWrite Off ZTest Always
    70.  
    71.         Pass
    72.         {
    73.             HLSLPROGRAM
    74.                 #pragma vertex VertMain
    75.                 #pragma fragment FragMain
    76.             ENDHLSL
    77.         }
    78.     }
    79. }
    The effect (in the scene view) looks somewhat like the one in this post:
    https://forum.unity.com/threads/doe...-although-the-whole-scene-stays-still.552787/

    But only appears when I move the camera.

    Any help is highly appreciated.
     
  2. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    651
    Screen Recording of the issue: