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

Question [HDRP] Custom post-processing Raymarch with flip coordinate

Discussion in 'Image Effects' started by anhungxadieu, Oct 19, 2021.

  1. anhungxadieu

    anhungxadieu

    Joined:
    Oct 20, 2019
    Posts:
    2
    Hi,
    I do a simple ray marching using post processing but I get a result with a complete flip coordinate, you can see in the image, a Sphere 1 is a ray marching object, it's should be at a same place as a Sphere 2
    upload_2021-10-19_15-6-5.png

    script
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.HighDefinition;
    4. using System;
    5.  
    6. [Serializable, VolumeComponentMenu("Post-processing/Custom/rmpp")]
    7. public sealed class rmpp : CustomPostProcessVolumeComponent, IPostProcessComponent
    8. {
    9.     [Tooltip("Controls the intensity of the effect.")]
    10.     public ClampedFloatParameter intensity = new ClampedFloatParameter(0f, 0f, 1f);
    11.     public Vector3Parameter planetCentre = new Vector3Parameter(new Vector3(0,0,0));
    12.     public FloatParameter atmosphereRadius = new FloatParameter(1f);
    13.     public FloatParameter planetRadius = new FloatParameter(1f);
    14.  
    15.     Material m_Material;
    16.  
    17.     public bool IsActive() => m_Material != null && intensity.value > 0f;
    18.  
    19.     // Do not forget to add this post process in the Custom Post Process Orders list (Project Settings > HDRP Default Settings).
    20.     public override CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.AfterOpaqueAndSky;
    21.  
    22.     const string kShaderName = "Hidden/Shader/rmpp";
    23.  
    24.     public override void Setup()
    25.     {
    26.         if (Shader.Find(kShaderName) != null)
    27.             m_Material = new Material(Shader.Find(kShaderName));
    28.         else
    29.             Debug.LogError($"Unable to find shader '{kShaderName}'. Post Process Volume rmpp is unable to load.");
    30.     }
    31.  
    32.     public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination)
    33.     {
    34.         if (m_Material == null)
    35.             return;
    36.  
    37.         m_Material.SetFloat("_Intensity", intensity.value);
    38.         m_Material.SetTexture("_InputTexture", source);
    39.         m_Material.SetVector("_cam", Camera.main.transform.localToWorldMatrix.GetColumn(2));
    40.         m_Material.SetVector("planetCentre",planetCentre.value);
    41.         m_Material.SetFloat("planetRadius",planetRadius.value);
    42.  
    43.         HDUtils.DrawFullScreen(cmd, m_Material, destination);
    44.     }
    45.     public override void Cleanup()
    46.     {
    47.         CoreUtils.Destroy(m_Material);
    48.     }
    49. }
    50.  
    shader
    Code (CSharp):
    1. Shader "Hidden/Shader/rmpp"
    2. {
    3.     HLSLINCLUDE
    4.  
    5.     #pragma target 4.5
    6.     #pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
    7.  
    8.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    9.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    10.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
    11.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
    12.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
    13.  
    14.     #define MARCHING_ITERATION 128
    15.     #define MARCHING_ADAPTIVE_EPS_BASE 1e-9
    16.     #define MARCHING_MAXDIST 128
    17.  
    18.     sampler2D _MainTex;
    19.     half4 _MainTex_ST;
    20.  
    21.     struct Attributes
    22.     {
    23.         uint vertexID   : SV_VertexID;
    24.         float2 uv       : TEXCOORD0;
    25.         UNITY_VERTEX_INPUT_INSTANCE_ID
    26.     };
    27.  
    28.     struct Varyings
    29.     {
    30.         float4 positionCS   : SV_POSITION;
    31.         float2 uv           : TEXCOORD0;
    32.         float3 viewVector   : TEXCOORD1;
    33.         UNITY_VERTEX_OUTPUT_STEREO
    34.     };
    35.  
    36.     Varyings Vert(Attributes input)
    37.     {
    38.         Varyings output;
    39.         UNITY_SETUP_INSTANCE_ID(input);
    40.         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    41.         output.positionCS   = GetFullScreenTriangleVertexPosition(input.vertexID);
    42.         output.uv           = GetFullScreenTriangleTexCoord(input.vertexID);
    43.  
    44.         output.viewVector   = mul( _InvProjMatrix, float4(input.uv * 2 - 1, 0, -1));
    45.         output.viewVector   = mul(UNITY_MATRIX_M, float4(output.viewVector,0));
    46.  
    47.         return output;
    48.     }
    49.  
    50.     // List of properties to control your post process effect
    51.     float3  _cam;
    52.     float3  planetCentre;
    53.  
    54.     float   _Intensity;
    55.     float   planetRadius;
    56.  
    57.     TEXTURE2D_X(_InputTexture);
    58.    
    59.     float LinearEyeDepth(float z)
    60.     {
    61.         return 1.0 / (_ZBufferParams.z * z + _ZBufferParams.w);
    62.     }
    63.  
    64.     float map(float3 p)
    65.     {
    66.         float d = distance(p, planetCentre) - planetRadius;
    67.         return d;
    68.     }
    69.  
    70.     float3 calcNormal(in float3 p)
    71.     {
    72.         float2 e = float2(1.0, -1.0) * 0.005;
    73.         return normalize(
    74.             e.xyy * map(p + e.xyy) +
    75.             e.yyx * map(p + e.yyx) +
    76.             e.yxy * map(p + e.yxy) +
    77.             e.xxx * map(p + e.xxx));
    78.     }
    79.  
    80.     float rm(float3 ro, float3 rd)
    81.     {
    82.         float h, t = 0.;
    83.         for (int i = 0; i < MARCHING_ITERATION; i++) {
    84.             h = map(ro+rd*t);
    85.             t += h;
    86.             if (h < MARCHING_ADAPTIVE_EPS_BASE || h > MARCHING_MAXDIST) break;
    87.         }
    88.         return t;
    89.     }
    90.  
    91.     float remap(float value, float min1, float max1, float min2, float max2){
    92.         float perc  = (value - min1) / (max1 - min1);
    93.         value       = perc * (max2 - min2) + min2;
    94.         return value;
    95.     }
    96.  
    97.     float4 CustomPostProcess(Varyings input) : SV_Target
    98.     {
    99.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    100.         float4 outColor = 0;
    101.  
    102.         // rm ---------------------------
    103.         // scenedepth
    104.         float sceneDepthNonLinear   = SampleCameraDepth(input.uv);
    105.         float sceneDepth            = LinearEyeDepth(sceneDepthNonLinear) * length(input.viewVector);
    106.         // viewdirection
    107.         PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw, sceneDepth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
    108.         float3 viewDirection    = GetWorldSpaceNormalizeViewDir(posInput.positionWS);//posInput.positionWS);
    109.      
    110.         // rayorigin
    111.         uint2 positionSS    = input.uv * _ScreenSize.xy;
    112.         float  deviceDepth  = LoadCameraDepth(positionSS);
    113.         float2 positionNDC  = positionSS * _ScreenSize.zw + (0.5 * _ScreenSize.zw);
    114.         float3 positionWS   = ComputeWorldSpacePosition(positionNDC, deviceDepth, UNITY_MATRIX_I_VP);
    115.  
    116.         // float3 ro           = GetAbsolutePositionWS(positionWS);
    117.         // float3 rd           = normalize(viewDirection);
    118.         float3 ro           = GetCameraRelativePositionWS(posInput.positionWS);
    119.         float3 rd           = normalize(viewDirection);
    120.            
    121.         outColor        = LOAD_TEXTURE2D_X(_InputTexture, positionSS);                  
    122.  
    123.         float d = rm(ro,rd);
    124.  
    125.         if (d < MARCHING_MAXDIST)
    126.         //     discard;
    127.         // else
    128.         {
    129.             float3 p        = ro + rd * d;
    130.             float3 normal   = calcNormal(p);
    131.             float3 light    = float3(2, 2, 2);
    132.             float dif       = clamp(dot(normal, normalize(light - p)), 0., 1.);
    133.             dif             += float3(.03,.03,.03);
    134.             dif             *= remap(dot(rd,normal),-0.4,1,1,0);
    135.  
    136.             outColor.xyz    = dif;     // Gamma correction
    137.         }
    138.  
    139.         // ------------------------------
    140.  
    141.         return outColor;
    142.     }
    143.  
    144.     ENDHLSL
    145.  
    146.     SubShader
    147.     {
    148.         Pass
    149.         {
    150.             Name "rmpp"
    151.  
    152.             ZWrite Off
    153.             ZTest Always
    154.             Blend Off
    155.             Cull Off
    156.  
    157.             HLSLPROGRAM
    158.                 #pragma fragment CustomPostProcess
    159.                 #pragma vertex Vert
    160.             ENDHLSL
    161.         }
    162.     }
    163.     Fallback Off
    164. }
    165.  
    How can I fix it ..?
    Thanks for help.
     
  2. anhungxadieu

    anhungxadieu

    Joined:
    Oct 20, 2019
    Posts:
    2
    I end up by do a work around
    Code (CSharp):
    1. float3 viewDirection    = GetWorldSpaceNormalizeViewDir(posInput.positionWS*float3(-1,-1,-1));
    2. float3 ro               = GetAbsolutePositionWS(posInput.positionWS*float3(-1,-1,-1));