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

script
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using System;
[Serializable, VolumeComponentMenu("Post-processing/Custom/rmpp")]
public sealed class rmpp : CustomPostProcessVolumeComponent, IPostProcessComponent
{
[Tooltip("Controls the intensity of the effect.")]
public ClampedFloatParameter intensity = new ClampedFloatParameter(0f, 0f, 1f);
public Vector3Parameter planetCentre = new Vector3Parameter(new Vector3(0,0,0));
public FloatParameter atmosphereRadius = new FloatParameter(1f);
public FloatParameter planetRadius = new FloatParameter(1f);
Material m_Material;
public bool IsActive() => m_Material != null && intensity.value > 0f;
// Do not forget to add this post process in the Custom Post Process Orders list (Project Settings > HDRP Default Settings).
public override CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.AfterOpaqueAndSky;
const string kShaderName = "Hidden/Shader/rmpp";
public override void Setup()
{
if (Shader.Find(kShaderName) != null)
m_Material = new Material(Shader.Find(kShaderName));
else
Debug.LogError($"Unable to find shader '{kShaderName}'. Post Process Volume rmpp is unable to load.");
}
public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination)
{
if (m_Material == null)
return;
m_Material.SetFloat("_Intensity", intensity.value);
m_Material.SetTexture("_InputTexture", source);
m_Material.SetVector("_cam", Camera.main.transform.localToWorldMatrix.GetColumn(2));
m_Material.SetVector("planetCentre",planetCentre.value);
m_Material.SetFloat("planetRadius",planetRadius.value);
HDUtils.DrawFullScreen(cmd, m_Material, destination);
}
public override void Cleanup()
{
CoreUtils.Destroy(m_Material);
}
}
shader
Shader "Hidden/Shader/rmpp"
{
HLSLINCLUDE
#pragma target 4.5
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
#define MARCHING_ITERATION 128
#define MARCHING_ADAPTIVE_EPS_BASE 1e-9
#define MARCHING_MAXDIST 128
sampler2D _MainTex;
half4 _MainTex_ST;
struct Attributes
{
uint vertexID : SV_VertexID;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float3 viewVector : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};
Varyings Vert(Attributes input)
{
Varyings output;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
output.uv = GetFullScreenTriangleTexCoord(input.vertexID);
output.viewVector = mul( _InvProjMatrix, float4(input.uv * 2 - 1, 0, -1));
output.viewVector = mul(UNITY_MATRIX_M, float4(output.viewVector,0));
return output;
}
// List of properties to control your post process effect
float3 _cam;
float3 planetCentre;
float _Intensity;
float planetRadius;
TEXTURE2D_X(_InputTexture);
float LinearEyeDepth(float z)
{
return 1.0 / (_ZBufferParams.z * z + _ZBufferParams.w);
}
float map(float3 p)
{
float d = distance(p, planetCentre) - planetRadius;
return d;
}
float3 calcNormal(in float3 p)
{
float2 e = float2(1.0, -1.0) * 0.005;
return normalize(
e.xyy * map(p + e.xyy) +
e.yyx * map(p + e.yyx) +
e.yxy * map(p + e.yxy) +
e.xxx * map(p + e.xxx));
}
float rm(float3 ro, float3 rd)
{
float h, t = 0.;
for (int i = 0; i < MARCHING_ITERATION; i++) {
h = map(ro+rd*t);
t += h;
if (h < MARCHING_ADAPTIVE_EPS_BASE || h > MARCHING_MAXDIST) break;
}
return t;
}
float remap(float value, float min1, float max1, float min2, float max2){
float perc = (value - min1) / (max1 - min1);
value = perc * (max2 - min2) + min2;
return value;
}
float4 CustomPostProcess(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float4 outColor = 0;
// rm ---------------------------
// scenedepth
float sceneDepthNonLinear = SampleCameraDepth(input.uv);
float sceneDepth = LinearEyeDepth(sceneDepthNonLinear) * length(input.viewVector);
// viewdirection
PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw, sceneDepth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
float3 viewDirection = GetWorldSpaceNormalizeViewDir(posInput.positionWS);//posInput.positionWS);
// rayorigin
uint2 positionSS = input.uv * _ScreenSize.xy;
float deviceDepth = LoadCameraDepth(positionSS);
float2 positionNDC = positionSS * _ScreenSize.zw + (0.5 * _ScreenSize.zw);
float3 positionWS = ComputeWorldSpacePosition(positionNDC, deviceDepth, UNITY_MATRIX_I_VP);
// float3 ro = GetAbsolutePositionWS(positionWS);
// float3 rd = normalize(viewDirection);
float3 ro = GetCameraRelativePositionWS(posInput.positionWS);
float3 rd = normalize(viewDirection);
outColor = LOAD_TEXTURE2D_X(_InputTexture, positionSS);
float d = rm(ro,rd);
if (d < MARCHING_MAXDIST)
// discard;
// else
{
float3 p = ro + rd * d;
float3 normal = calcNormal(p);
float3 light = float3(2, 2, 2);
float dif = clamp(dot(normal, normalize(light - p)), 0., 1.);
dif += float3(.03,.03,.03);
dif *= remap(dot(rd,normal),-0.4,1,1,0);
outColor.xyz = dif; // Gamma correction
}
// ------------------------------
return outColor;
}
ENDHLSL
SubShader
{
Pass
{
Name "rmpp"
ZWrite Off
ZTest Always
Blend Off
Cull Off
HLSLPROGRAM
#pragma fragment CustomPostProcess
#pragma vertex Vert
ENDHLSL
}
}
Fallback Off
}
How can I fix it …?
Thanks for help.