Search Unity

Question Problem with shader in multipass vr, How can I fix the shader to position correctly for both eyes?

Discussion in 'VR' started by Zagorsk, May 19, 2022.

  1. Zagorsk

    Zagorsk

    Joined:
    May 18, 2022
    Posts:
    8
    Hi, I have a problem. I connected vr for my project, which contains volumetric clouds that are created inside the container boundaries using raymarching. When render is enabled, the clouds are heavily offset from the edges of the container for different eyes. For clarity, I placed the cube inside the container to show the offset.
     

    Attached Files:

    • qwe1.PNG
      qwe1.PNG
      File size:
      316.3 KB
      Views:
      276
    • qwe2.PNG
      qwe2.PNG
      File size:
      411.4 KB
      Views:
      275
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Hello, Doesn't it look like the left, and right views just are mixed up for the clouds?
     
  3. Zagorsk

    Zagorsk

    Joined:
    May 18, 2022
    Posts:
    8
    No, they are offset from the container, although they should fit into it. In normal rendering (qwe1.PNG), they are clearly inscribed in the container.
     
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    I still think it looks like the left, and right views are flipped for the clouds compared to the box.

    stereo.png
     
    Zagorsk likes this.
  5. Zagorsk

    Zagorsk

    Joined:
    May 18, 2022
    Posts:
    8
    Oh, really, thanks! Do you have any ideas why this might be happening?
     
  6. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Guess it may has something to do with how the shader makes the clouds, or how they are rendered in the scene. Is it for example a skybox, or a dome mesh?
     
  7. Zagorsk

    Zagorsk

    Joined:
    May 18, 2022
    Posts:
    8
    Skybox, container coordinates are passed to the shader.

    Code (CSharp):
    1.         material.SetVector ("boundsMin", cloudsContainer.position - cloudsContainer.localScale / 2);
    2.         material.SetVector ("boundsMax", cloudsContainer.position + cloudsContainer.localScale / 2);
    After that, the shader calculates the distance to the container and inside the container.


    Code (CSharp):
    1.             float2 distanceToContainer(float3 boundsMin, float3 boundsMax, float3 rayOrigin, float3 invRaydir) {
    2.  
    3.                 float3 t0 = (boundsMin - rayOrigin) * invRaydir;
    4.                 float3 t1 = (boundsMax - rayOrigin) * invRaydir;
    5.                 float3 tmin = min(t0, t1);
    6.                 float3 tmax = max(t0, t1);
    7.                
    8.                 float dstA = max(max(tmin.x, tmin.y), tmin.z);
    9.                 float dstB = min(tmax.x, min(tmax.y, tmax.z));
    10.  
    11.                 float dstToContainer = max(0, dstA);
    12.                 float dstInsideContainer = max(0, dstB - dstToContainer);
    13.                 return float2(dstToContainer, dstInsideContainer);
    14.             }
     
  8. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Since its correct in Mono, perhaps it's just to focus on how the stereo separation is done?
     
  9. Zagorsk

    Zagorsk

    Joined:
    May 18, 2022
    Posts:
    8
    Can you suggest where I should start in this case?
     
  10. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    How about in the shader?
     
  11. Zagorsk

    Zagorsk

    Joined:
    May 18, 2022
    Posts:
    8
    I'm trying, but I can't find where the mixing is happening. Maybe I need more specific built-in functions for stereo vr?

    Code (CSharp):
    1.             struct appdata {
    2.                 float4 vertex : POSITION;
    3.                 float2 uv : TEXCOORD0;
    4.             };
    5.  
    6.             struct v2f {
    7.                 float4 pos : SV_POSITION;
    8.                 float2 uv : TEXCOORD0;
    9.                 float3 viewVector : TEXCOORD1;
    10.             };
    11.            
    12.             v2f vert (appdata v) {
    13.                 v2f output;
    14.                 output.pos = UnityWorldToClipPos(v.vertex);
    15.                 output.uv = v.uv;
    16.  
    17.                 float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv * 2 - 1, 0, -1));
    18.                 output.viewVector = mul(unity_CameraToWorld, float4(viewVector,0));
    19.                 output.viewVector.x -= unity_StereoEyeIndex * 0.5;
    20.                 return output;
     
  12. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    You are probably on to something. If the shader is rendering the skybox in mono, instead of stereo for vr, the image will also be off for left, and right eyes for nearby objects.