Search Unity

URP alternative for SAMPLE_DEPTH_TEXTURE_PROJ and UNITY_PROJ_COORD

Discussion in 'Shaders' started by MadeFromPolygons, Oct 8, 2019.

  1. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    I'm trying to have a go at learning to write shaders for URP and having issue with sampling depth.

    Ive done this in the past for built in:

    Code (CSharp):
    1.    half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos )));
    but with URP 7.1.2 it seems that SAMPLE_DEPTH_TEXTURE_PROJ and UNITY_PROJ_COORD either dont exist, or are located elsewhere.

    Does anyone know what the URP alternatives are for SAMPLE_DEPTH_TEXTURE_PROJ & UNITY_PROJ_COORD ?

    Thanks in advance :)
     
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    I don't believe HLSL has that kind of texture sampling function, as such there aren't any macros for that functionality, but its pretty easy to do the equivalent without macros;

    Code (CSharp):
    1. half depth = LinearEyeDepth (_CameraDepthTexture.Sample (sampler_CameraDepthTexture, i.scrPos.xy / i.scrPos.w).r);
     
    Dance_M likes this.
  3. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    Your a legend, gonna try this out later :) Thanks!

    Edit: @Namey5 I also have a related question here and if you have any freetime I would greatly appreciate if you take a look at it! :)
     
    Last edited: Oct 9, 2019
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    UNITY_PROJ_COORD()
    doesn't do anything anymore anyways, it only existed to fix a platform specific bug that is no longer around. They just didn't remove the macro from the old pipeline since that would break a lot of shaders doing so. Right now on the old pipeline, all it does is compile down to the input variable, nothing changed.

    As for
    SAMPLE_DEPTH_TEXTURE_PROJ
    , it just compiles to a
    tex2Dproj()
    (or a texture array sampler if in VR), you still need to feed it the adjusted UV of
    uv.xyz / uv.w
    , similar to @Namey5 's example.
    The new RPs do have
    SAMPLE_DEPTH_TEXTURE(tex, sampler, coord)
    , but it doesn't do anything special, it's a regular texture sample but only returns the .r component like you'd want for depth. You still have to feed the
    uv.xy / uv.w
    coordinate into it. But I'd still recommend using it just to help keep your code safer from future breaking changes.

    There's also the function URP has defined for the ShaderGraph to use that illustrates how they're doing it (still requires you to do the UV division beforehand):
    Code (CSharp):
    1. float shadergraph_LWSampleSceneDepth(float2 uv)
    2. {
    3. #if defined(REQUIRE_DEPTH_TEXTURE)
    4. #if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
    5.     float rawDepth = SAMPLE_TEXTURE2D_ARRAY(_CameraDepthTexture, sampler_CameraDepthTexture, uv, unity_StereoEyeIndex).r;
    6. #else
    7.     float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(uv));
    8. #endif
    9.     return rawDepth;
    10. #endif // REQUIRE_DEPTH_TEXTURE
    11.     return 0;
    12. }
    Or from their URP SoftParticles example:
    Code (CSharp):
    1. float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, projection.xy / projection.w), _ZBufferParams);
     
    Last edited: Oct 10, 2019