Search Unity

Bug Depth Texture entirely white in WebGL

Discussion in 'Shaders' started by OMGTOASTER, May 19, 2023.

  1. OMGTOASTER

    OMGTOASTER

    Joined:
    Nov 3, 2014
    Posts:
    25
    Hello!

    I've been trying to set up depth texture based fog in my project that I build to WebGL. As it is URP, the fog is using a render feature. While running in the editor everything looks fine and the fog fades into the skybox, in the WebGL build the fog appears to immediately and uniformly cover the view. Upon inspection, it appears that the depth texture is completely white. The depth texture is sampled as shown in the code below:


    Code (CSharp):
    1. float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(i.uv_depth));
    2.  
    3. float depth = Linear01Depth(rawDepth);
    I apologize if that is not enough code context. The shader is from a paid asset (UniStorm) so I am unsure how much of it I am able to show. Of course I will be glad to provide additional information as needed. Depth texture is enabled on the URP asset as well as the only camera in the scene. Again it does render fine in the editor. Is there something for WebGL that I am missing?
     
  2. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Hi, I'm not familiar with OpenGL depth, but its range should be different from DirectX. You can change the editor's graphics API to OpenGL and check the depth texture in Frame Debugger.

    Platform-specific rendering differences:
    Code (CSharp):
    1. Direct3D-like: The clip space depth goes from 0.0 at the near plane to +1.0 at the far plane. This applies to Direct3D, Metal and consoles.
    2.  
    3. OpenGL-like: The clip space depth goes from -1.0 at the near plane to +1.0 at the far plane. This applies to OpenGL and OpenGL ES.
    The range of _CameraDepthTexture should be between 0 and 1, so you can add a pragma to invert the rawDepth.

    Code (CSharp):
    1. #if (UNITY_REVERSED_Z == 0) // OpenGL platforms
    2.     rawDepth = 1.0 - rawDepth; // [0, 1] to [1, 0]
    3. #endif
    Or I'm wrong, the depth range is between -1 and 1:
    Code (CSharp):
    1. #if (UNITY_REVERSED_Z == 0) // OpenGL platforms
    2.     rawDepth = rawDepth * -0.5 + 0.5; // [-1, 1] to [1, 0]
    3. #endif
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    While true, the
    Linear01Depth
    function corrects for that already and outputs the same linear depth as DirectX or other APIs.

    The problem is likely the project’s quality setting for WebGL are disabling the camera depth texture. I forget which setting it is, but if you make sure the quality level isn’t defaulting to “low” it should work.
     
    wwWwwwW1 likes this.
  4. OMGTOASTER

    OMGTOASTER

    Joined:
    Nov 3, 2014
    Posts:
    25
    Hi, thanks for the replies! I had thought that Linear01Depth was supposed to correct the outputs as bgolus mentioned. Nevertheless, I tried making those conversions to both the rawDepth, as well as to the depth after Linear01Depth, just to see if it would work. Unfortunately, it didn't seem to correct the problem :( .

    For the quality settings, I'm not too sure which setting it could be either. So for the moment, I removed all other quality settings so that only the highest quality remained. Sadly this too did not seem to fix the issue. I should mention though that the project is hosted on Azure DevOps with builds and deployment handled by a pipeline using a task from Unity Tools for Azure DevOps by Dinomite Studios. Could this be affecting the build output to use a different quality level? As I mentioned though I've removed all other quality levels save for the highest.
     
  5. OMGTOASTER

    OMGTOASTER

    Joined:
    Nov 3, 2014
    Posts:
    25
    @bgolus do you happen to remember which setting you were referring to? I feel like I've tried every setting at this point and still can't seem to get it to work.

    UPDATE: I seem to have gotten it to work by using _CameraDepthAttachment instead. If my understanding is correct, this texture is copied to _CameraDepthTexture. Perhaps it's not clearing properly or something else is writing to it after? Is there any reason not to use _CameraDepthAttachment instead?
     
    Last edited: May 22, 2023