Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Adding detph to channel alpha

Discussion in 'Shaders' started by IngElecPuc, Aug 21, 2020.

  1. IngElecPuc

    IngElecPuc

    Joined:
    Sep 27, 2019
    Posts:
    4
    I'm trying to simulate a RGBD camera with unity. So far I added a script from this thread https://answers.unity.com/questions/877170/render-scene-depth-to-a-texture.html and is working well.
    I made a small modification to the
    frag
    method to linearly control the depth sample, here is the code:
    Code (CSharp):
    1.     fixed4 frag(output o) : COLOR
    2.     {
    3.         float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, o.uv));
    4.         return fixed4(depth, depth, depth, 1);
    5.     }
    I want to save a png image with the RGB channels unchanged, but with its alpha channel using the depth information that I rescued from the previous method. I need these images to train a neural network, so it is important to use that format.
    I am really new to shaders. I suppose that sampling directly from the camera should do the work, but I don't know how to do that in the fragment state, or any. I'm looking for something like these or similar:
    Code (CSharp):
    1.     fixed4 frag(output o, float red, float green, float blue) : COLOR
    2.     {
    3.         float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, o.uv));
    4.         return fixed4(red, green, blue, depth);
    5.     }
    Thanks
     
    Last edited: Aug 22, 2020
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    The more important question is how / when are you rendering this shader? If you're doing this as a
    Blit()
    , presumably as part of an
    OnRenderImage()
    c# function, then you'll need to pass the render texture source to the
    Blit()
    , and sample it in the shader. See the default Image Effect shader when you create one in the Project view for a basic example.

    Then add the code to sample the depth texture and output that as the alpha, with the RGB from the texture.
    Code (csharp):
    1. half4 frag(v2f i) : SV_Target
    2. {
    3.     half4 color = tex2D(_MainTex, i.uv);
    4.     float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv));;
    5.     return half4(color.rgb, depth);
    6. }
    If instead you're rendering this as a mesh in front of the camera with a high material queue, you just need to use
    ColorMask A
    and output the depth as the color value (
    return float4(depth, depth, depth, depth);
    ).
    https://docs.unity3d.com/Manual/SL-Pass.html
     
    IngElecPuc likes this.
  3. IngElecPuc

    IngElecPuc

    Joined:
    Sep 27, 2019
    Posts:
    4
    I actually was rendering with
    Blit()
    in the
    OnRenderImage()
    method as you said. I tried the modifications that you proposed and it worked excellently. Thank you.
     
  4. telos5inc

    telos5inc

    Joined:
    Sep 15, 2020
    Posts:
    1
    Hello
    I am a Korean developer
    Can you tell me how you solved it?