Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

[some progress] Sampling depth texture from custom camera

Discussion in 'Shaders' started by highFlylowFlow, Apr 12, 2021.

  1. highFlylowFlow


    Nov 27, 2019
    I managed to create a function that takes in _CameraDepthTexture from main camera and converts it into point coordinate in object space.

    I also managed to sample the depth texture from a custom camera, but i have no idea how to determine the second parameter from tex2Dproj(), since it is a float4 type and i haven't found any useful description other than some vague info in Nvidia docs (they say that x and y are divied by w, but what ARE they ?).
    I need to do the same thing but for custom camera.

    My goal is to provide depth information to my shader from a custom camera that writes into a depth texture.

    Scene is setup with a unity stock cube using my custom shader and custom camera is set with :
    - culling mask that ignores only the cube, so anything passing through the volume of the cube has its depth sampled
    - set to orthographic projection mode
    - renders depth texture into a 512x512 depth texture (used by shader)
    - it and cube are always aligned in relation to each other
    - it is always in center on cube's side
    - orthographicSize, nearClipPlane and farClipPlane are set to exactly match the cube size, essentially the whole "scene" frustum is covering the volume of cube

    Main camera freely moves/rotates, the cube/camera combo is inside empty game object and is arbitrarily positioned/rotated like any other object in scene.

    There is more going on, but this is the gist of it.

    Any ideas ?

    @bgolus (you must be receiving a ton of pings ...)

    Setup of fbx model and a cube drawn with with custom shader

    Setup of camera frustum enclosing the cube and model

    Last edited: Apr 12, 2021
  2. highFlylowFlow


    Nov 27, 2019
    Update : i got it to work at basic level. through a lucky accident i got correct x and y coordinates and the final z coord i got by using SAMPLE_DEPTH_TEXTURE that needs only a tex and a float2 uv inputs. The linearizing needed more work. The builtin Linear01Depth() uses near and far clipping plane from main camera so i looked how is function implemented and wrote a version that uses custom camera clipping planes floats. The depth tex and near/far floats are set through script. Once i'm done (i hope not too far in future) i'll post a link to finished code so others may figure out their own ideas.
    Source used for custom depth linearize at
    Last edited: Apr 20, 2021