Hi , i need someone to point out what i need to change to this simple raymarch shader in order to work for VR(multipass). I know it has to do with the positions and view matrices of each eye. I really tried to solve it but it just doesnt work. I found a sample of raymarching that works at VR as well(GitHub - hecomi/uRaymarching: Raymarching Shader Generator in Unity), but i just cant understand which paths the shader takes since there are so many include files with different paths. I tried even with renderdoc to watch step by step.
So please anyone who has some idea about it. Here is a simple shader that draws a sphere.
Shader "Hidden/TestVR"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 viewVector : TEXCOORD1;
};
v2f vert(appdata v) {
v2f output;
UNITY_INITIALIZE_OUTPUT(v2f, output);
output.pos = UnityObjectToClipPos(v.vertex);
output.uv = v.uv;
// Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
// (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv * 2 - 1, 0, -1));
output.viewVector = mul(unity_CameraToWorld, float4(viewVector, 0));
return output;
}
sampler2D _MainTex;
uniform sampler2D _CameraDepthTexture;
float sdSphere(float3 p, float s)
{
return length(p) - s;
}
float distanceField(float3 p)
{
float Sphere1 = sdSphere(p - (float3(-60, -10, 0)), 2.0);
return Sphere1;
}
fixed4 raymarching(float3 ro, float3 rd, float depth)
{
fixed4 result = fixed4(1, 1, 1, 1);
const int max_iteration = 128;
float t = 0;
for (int i = 0; i < max_iteration; i++)
{
if (t > 100.0 || t >= depth)
{
return fixed4(1, 1, 1, 1);
}
float3 p = ro + rd * t;
float d = distanceField(p);
if (d < 0.01)
return fixed4(1, 0, 0, 0);
t += d;
}
return result;
}
fixed4 frag(v2f i) : SV_Target
{
float3 rayPos = _WorldSpaceCameraPos;
float viewLength = length(i.viewVector);
float3 rayDir = i.viewVector / viewLength;
// Depth and cloud container intersection info:
float nonlin_depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,i.uv);
float depth = LinearEyeDepth(nonlin_depth) * viewLength;
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 result = raymarching(rayPos, rayDir,depth);
fixed4 color = col * (result.w) + fixed4(result.xyz, 1.0) * (1.0 - result.w);
return color;
}
ENDCG
}
}
}
Right now i see 2 spheres which also seem to move when i move the headset