Search Unity

Raymarching Shader in VR

Discussion in 'VR' started by psomgeorg, Nov 12, 2019.

  1. psomgeorg

    psomgeorg

    Joined:
    Mar 16, 2019
    Posts:
    99
    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(https://github.com/hecomi/uRaymarching), 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.


    1. Code (CSharp):
      1. Shader "Hidden/TestVR"
      2. {
      3.     Properties
      4.     {
      5.         _MainTex ("Texture", 2D) = "white" {}
      6.      
      7.     }
      8.     SubShader
      9.     {
      10.         // No culling or depth
      11.         Cull Off ZWrite Off ZTest Always
      12.  
      13.         Pass
      14.         {
      15.             CGPROGRAM
      16.             #pragma vertex vert
      17.             #pragma fragment frag
      18.  
      19.  
      20.             #include "UnityCG.cginc"
      21.  
      22.        
      23.  
      24.             struct appdata
      25.             {
      26.                 float4 vertex : POSITION;
      27.                 float2 uv : TEXCOORD0;
      28.             };
      29.  
      30.             struct v2f {
      31.                 float4 pos : SV_POSITION;
      32.                 float2 uv : TEXCOORD0;
      33.                 float3 viewVector : TEXCOORD1;
      34.              
      35.  
      36.  
      37.             };
      38.  
      39.             v2f vert(appdata v) {
      40.                 v2f output;
      41.                 UNITY_INITIALIZE_OUTPUT(v2f, output);
      42.              
      43.                 output.pos = UnityObjectToClipPos(v.vertex);
      44.                 output.uv = v.uv;
      45.                 // Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
      46.                 // (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
      47.                 float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv * 2 - 1, 0, -1));
      48.                 output.viewVector = mul(unity_CameraToWorld, float4(viewVector, 0));
      49.              
      50.                 return output;
      51.             }
      52.             sampler2D _MainTex;
      53.             uniform sampler2D _CameraDepthTexture;
      54.      
      55.             float sdSphere(float3 p, float s)
      56.             {
      57.                 return length(p) - s;
      58.             }
      59.             float distanceField(float3 p)
      60.             {
      61.                 float Sphere1 = sdSphere(p - (float3(-60, -10, 0)), 2.0);
      62.                 return Sphere1;
      63.             }
      64.  
      65.             fixed4 raymarching(float3 ro, float3 rd, float depth)
      66.             {
      67.              
      68.                 fixed4 result = fixed4(1, 1, 1, 1);
      69.                 const int max_iteration = 128;
      70.                 float t = 0;
      71.  
      72.                 for (int i = 0; i < max_iteration; i++)
      73.                 {
      74.                     if (t > 100.0 || t >= depth)
      75.                     {
      76.                         return fixed4(1, 1, 1, 1);
      77.                      
      78.                     }
      79.                     float3 p = ro + rd * t;
      80.                     float d = distanceField(p);
      81.                     if (d < 0.01)
      82.                         return fixed4(1, 0, 0, 0);
      83.                     t += d;
      84.                 }
      85.                 return result;
      86.             }
      87.  
      88.          
      89.          
      90.             fixed4 frag(v2f i) : SV_Target
      91.             {
      92.              
      93.              
      94.                 float3 rayPos = _WorldSpaceCameraPos;
      95.                 float viewLength = length(i.viewVector);
      96.                 float3 rayDir = i.viewVector / viewLength;
      97.              
      98.  
      99.                 // Depth and cloud container intersection info:
      100.                 float nonlin_depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,i.uv);
      101.                 float depth = LinearEyeDepth(nonlin_depth) * viewLength;
      102.                 fixed4 col = tex2D(_MainTex, i.uv);
      103.  
      104.                 fixed4 result = raymarching(rayPos, rayDir,depth);
      105.              
      106.                 fixed4 color = col * (result.w) + fixed4(result.xyz, 1.0) * (1.0 - result.w);
      107.                 return  color;
      108.              
      109.              
      110.              
      111.              
      112.             }
      113.             ENDCG
      114.         }
      115.     }
      116. }
    Right now i see 2 spheres which also seem to move when i move the headset