Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

[FIXED - PEBKAC] HDRP image effect (raymarching) is different on game view and scene view

Discussion in 'Graphics Experimental Previews' started by hvent90, Feb 13, 2019.

  1. hvent90

    hvent90

    Joined:
    Jun 18, 2018
    Posts:
    19
    Hi all,

    I have an image effect on the Post Process V2 stack that adds full-screen raymarched Perlin noise with object occlusion.

    It looks and works perfectly in the scene camera, however, it is distorted in the game camera. The raymarch effects seem to move in the opposite Y-axis. The effects also seem to the visually stretched in the Y axis. If I look straight ahead and rotate left or right, everything looks fine.

    Does anyone have any ideas? I've had to shelf this issue for a couple weeks to not lose morale, and am trying to approach it again.


    Source Code:

    RaymarchPostProcess.cs (PostFX V2 image effect script running in HDRP)
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.Rendering.PostProcessing;
    5.  
    6. [Serializable]
    7. public sealed class ShaderParameter : ParameterOverride<Shader> { }
    8.  
    9. [Serializable]
    10. [PostProcess(typeof(RaymarchPostProcessRenderer), PostProcessEvent.BeforeStack, "Custom/RaymarchPostProcess")]
    11. public sealed class RaymarchPostProcess : PostProcessEffectSettings
    12. {
    13.     public IntParameter maxIterations = new IntParameter { value = 64 };
    14.     public FloatParameter maxDistance = new FloatParameter { value = 100f };
    15.     public FloatParameter minDistance = new FloatParameter { value = 0.01f };
    16.  
    17.     public DepthTextureMode GetCameraFlags()
    18.     {
    19.         return DepthTextureMode.Depth; // DepthTextureMode.DepthNormals;
    20.     }
    21. }
    22.  
    23. public sealed class RaymarchPostProcessRenderer : PostProcessEffectRenderer<RaymarchPostProcess>
    24. {
    25.     Transform directionalLight;
    26.  
    27.     public override void Init()
    28.     {
    29.         base.Init();
    30.  
    31.         GameObject light = GameObject.FindGameObjectWithTag("MainLight");
    32.  
    33.         if (light)
    34.             directionalLight = light.transform;
    35.     }
    36.  
    37.     public override void Render(PostProcessRenderContext context)
    38.     {
    39.         Camera _cam = context.camera;
    40.  
    41.         var sheet = context.propertySheets.Get(Shader.Find("Raymarch/RaymarchHDRP"));
    42.         sheet.properties.SetMatrix("_CamFrustum", FrustumCorners(_cam));
    43.         sheet.properties.SetMatrix("_CamToWorld", _cam.cameraToWorldMatrix);
    44.         sheet.properties.SetVector("_CamWorldSpace", _cam.transform.position);
    45.         sheet.properties.SetInt("_MaxIterations", settings.maxIterations);
    46.         sheet.properties.SetFloat("_MaxDistance", settings.maxDistance);
    47.         sheet.properties.SetFloat("_MinDistance", settings.minDistance);
    48.  
    49.         if (directionalLight)
    50.         {
    51.             Vector3 position = directionalLight.forward;
    52.             sheet.properties.SetVector("_LightDir", new Vector4(position.x, position.y, position.z, 1));
    53.         }
    54.  
    55.         context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    56.     }
    57.  
    58.     private Matrix4x4 FrustumCorners(Camera cam)
    59.     {
    60.         Transform camtr = cam.transform;
    61.  
    62.         Vector3[] frustumCorners = new Vector3[4];
    63.         cam.CalculateFrustumCorners(new Rect(0, 0, 1, 1),
    64.         cam.farClipPlane, cam.stereoActiveEye, frustumCorners);
    65.  
    66.         Vector3 bottomLeft = camtr.TransformVector(frustumCorners[1]);
    67.         Vector3 topLeft = camtr.TransformVector(frustumCorners[0]);
    68.         Vector3 bottomRight = camtr.TransformVector(frustumCorners[2]);
    69.  
    70.         Matrix4x4 frustumVectorsArray = Matrix4x4.identity;
    71.         frustumVectorsArray.SetRow(0, bottomLeft);
    72.         frustumVectorsArray.SetRow(1, bottomLeft + (bottomRight - bottomLeft) * 2);
    73.         frustumVectorsArray.SetRow(2, bottomLeft + (topLeft - bottomLeft) * 2);
    74.  
    75.         return frustumVectorsArray;
    76.     }
    77. }
    RaymarchHDRP.shader
    Code (CSharp):
    1. Shader "Raymarch/RaymarchHDRP"
    2. {
    3.  
    4.     SubShader
    5.     {
    6.         Cull Off ZWrite Off ZTest Always
    7.  
    8.         Pass
    9.         {
    10.             HLSLPROGRAM
    11.  
    12.             #pragma target 3.5
    13.  
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.  
    17.             //#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
    18.             //#include "HLSLSupport.cginc"
    19.             #include "UnityCG.cginc"
    20.  
    21.             //TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    22.             uniform sampler2D _MainTex;
    23.             uniform sampler2D_float _CameraDepthTexture, sampler_CameraDepthTexture;
    24.             half4 _MainTex_ST;
    25.             uniform float4 _CamWorldSpace;
    26.             uniform float4x4 _CamFrustum,  _CamToWorld;
    27.             uniform int _MaxIterations;
    28.             uniform float _MaxDistance;
    29.             uniform float _MinDistance;
    30.             uniform float3 _LightDir;
    31.             float4 _Tint;
    32.  
    33.             uniform float4 _MainTex_TexelSize;
    34.  
    35.             struct AttributesDefault
    36.             {
    37.                 float3 vertex : POSITION;
    38.                 half2 texcoord : TEXCOORD0;
    39.             };
    40.  
    41.             struct v2f
    42.             {
    43.              float4 vertex : SV_POSITION;
    44.              float2 texcoord : TEXCOORD0;
    45.              float2 texcoordStereo : TEXCOORD1;
    46.              float4 ray : TEXCOORD2;
    47.             };
    48.  
    49.             // Vertex manipulation
    50.             float2 TransformTriangleVertexToUV(float2 vertex)
    51.             {
    52.                 float2 uv = (vertex + 1.0) * 0.5;
    53.                 return uv;
    54.             }
    55.  
    56.             v2f vert(AttributesDefault v  )
    57.             {
    58.                 v2f o;
    59.                 v.vertex.z = 0.1;
    60.                 o.vertex = float4(v.vertex.xy, 0.0, 1.0);
    61.                 o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
    62.                 o.texcoordStereo = TransformStereoScreenSpaceTex(o.texcoord, 1.0);
    63.  
    64.                 int index = (o.texcoord.x / 2) + o.texcoord.y;
    65.                 o.ray = _CamFrustum[index];
    66.  
    67.                 return o;
    68.             }
    69.  
    70.             float sdSphere(float3 position, float3 origin, float radius)
    71.             {
    72.                 return distance(position, origin) - radius;
    73.             }
    74.  
    75.             float distanceField(float3 p) {
    76.                 return sdSphere(p, float3(1, 0, 0), 2);
    77.             }
    78.  
    79.             float3 getNormal(float3 p)
    80.             {
    81.                 const float2 offset = float2(0.001, 0.0);
    82.  
    83.                 float3 n = float3(
    84.                     distanceField(p + offset.xyy) - distanceField(p - offset.xyy),
    85.                     distanceField(p + offset.yxy) - distanceField(p - offset.yxy),
    86.                     distanceField(p + offset.yyx) - distanceField(p - offset.yyx));
    87.  
    88.                 return normalize(n);
    89.             }
    90.  
    91.  
    92.             fixed4 raymarching(float3 rayOrigin, float3 rayDirection, float depth) {
    93.                 fixed4 result = float4(1, 1, 1, 1);
    94.                 float t = 0.01; // Distance Traveled from ray origin (ro) along the ray direction (rd)
    95.  
    96.                 for (int i = 0; i < _MaxIterations; i++)
    97.                 {
    98.                     if (t > _MaxDistance || t >= depth)
    99.                     {
    100.                         result = float4(rayDirection, 0); // color backround from ray direction for debugging
    101.                         break;
    102.                     }
    103.  
    104.                     float3 p = rayOrigin + rayDirection * t;    // This is our current position
    105.                     float d = distanceField(p); // should be a sphere at (0, 0, 0) with a radius of 1
    106.                     if (d <= _MinDistance) // We have hit something
    107.                     {
    108.                         // shading
    109.                         float3 n = getNormal(p);
    110.                         float light = dot(-_LightDir, n);
    111.                         result = float4(fixed3(1, 1, 1) * light, 1); // yellow sphere should be drawn at (0, 0, 0)
    112.                         break;
    113.                     }
    114.  
    115.                     t += d;
    116.                 }
    117.  
    118.                 return result;
    119.             }
    120.  
    121.             float4 frag(v2f i) : SV_Target
    122.             {
    123.                 i.texcoord.y = 1 - i.texcoord.y;
    124.                 float4 col = tex2D(_MainTex, i.texcoord);
    125.                 float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(i.texcoord));
    126.                 depth = Linear01Depth(depth);
    127.                 depth *= length(i.ray);
    128.  
    129.                 float3 rayOrigin = _CamWorldSpace;
    130.                 float3 rayDirection = normalize(i.ray);
    131.                 float4 result = raymarching(rayOrigin, rayDirection, depth);
    132.  
    133.                 return fixed4(col * (1.0 - result.w) + result.xyz * result.w, 1.0);
    134.             }
    135.  
    136.             ENDHLSL
    137.         }
    138.     }
    139. }
    Here is an image of the scene and game camera in the same position and orientation.
     
    Last edited: Feb 13, 2019
  2. hvent90

    hvent90

    Joined:
    Jun 18, 2018
    Posts:
    19
    Update:
    The y-stretch fixes itself as soon as I select any object in the hierarchy except the main camera. The y-stretch comes back in the game view if I select the main camera. This doesn't fix the opposite movement in the Y axis though.

    Update2:
    The y-stretch is fixed when I unselect "camera" from the scene view's "gizmos". The opposite movement in the Y axis is still present.

     
    Last edited: Feb 13, 2019
  3. hvent90

    hvent90

    Joined:
    Jun 18, 2018
    Posts:
    19
    I found the issue that has been plaguing me for weeks.

    The scale on my main camera's Y was 0.33.

    Thank you and have a wonderful day.

    *plays lincoln park in the shower*
     
    Ehredt likes this.