Search Unity

Distance between camera and pixel

Discussion in 'Shaders' started by JMA_4, Jul 21, 2016.

  1. JMA_4

    JMA_4

    Joined:
    Jul 21, 2016
    Posts:
    3
    Hi,

    I'm making a shader that give me the distance between pixel and camera. To do it I've used a depth texture but and have a result that seem correct but the value aren't exactly what I excep. For example I've put a plane at the exact middle of near and far of the camera and the color is 0.533, I want a 0.500 instead.

    Here the code I use to get my depth texture :

    Code (CSharp):
    1. Shader "Custom/DepthGrayscale" {
    2. SubShader {
    3. Tags { "RenderType"="Opaque" }
    4.  
    5. Pass{
    6. CGPROGRAM
    7. #pragma vertex vert
    8. #pragma fragment frag
    9. #include "UnityCG.cginc"
    10.  
    11. sampler2D _CameraDepthTexture;
    12.  
    13. struct v2f {
    14.    float4 pos : SV_POSITION;
    15.    float4 scrPos:TEXCOORD1;
    16. };
    17.  
    18. //Vertex Shader
    19. v2f vert (appdata_base v){
    20.    v2f o;
    21.    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    22.    o.scrPos=ComputeScreenPos(o.pos);
    23.    return o;
    24. }
    25.  
    26. //Fragment Shader
    27. half4 frag (v2f i) : COLOR{
    28.    float depthValue = Linear01Depth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r);
    29.    half4 depth;
    30.  
    31.    depth.r = depthValue;
    32.    depth.g = depthValue;
    33.    depth.b = depthValue;
    34.  
    35.    depth.a = 1;
    36.    return depth;
    37. }
    38. ENDCG
    39. }
    40. }
    41. FallBack "Diffuse"
    42. }
    And after more reflexion about it, the depth can't answer my problem, because the depth is only the distance between pixel and near plane not the camera.

    I search how to do it but I can't find a solution, all I get is black screen (or white).
    I let you know I'm not a pro with shader.

    Can you help me to have a greyscale texture on camera that give me the pixel distance ?

    Here my last attempt :

    Code (CSharp):
    1. struct vertOut
    2.             {
    3.                 float4 position : SV_POSITION;
    4.                 float4 worldSpacePosition : TEXCOORD0;
    5.             };
    6.             vertOut vert(appdata_full vertIn)
    7.             {
    8.                 vertOut o;
    9.                 o.position = mul(UNITY_MATRIX_MVP, vertIn.vertex);
    10.                 o.worldSpacePosition = mul(_Object2World, vertIn.vertex);
    11.                 return o;
    12.             }
    13.             float4 frag(vertOut fragIn) : COLOR
    14.             {      
    15.                 float4 depth;
    16.                 // The interpolator calculated world space pixel coordinates for you here
    17.                 float4 pixelWorldSpacePosition = fragIn.worldSpacePosition;
    18.  
    19.                 float depthValue;
    20.  
    21.                 depthValue=length(_WorldSpaceCameraPos - pixelWorldSpacePosition.xyz)*_ProjectionParams.w;
    22.  
    23.                 depth.r = depthValue;
    24.                 depth.g = depthValue;
    25.                 depth.b = depthValue;
    26.                
    27.                 depth.a = 1;
    28.  
    29.  
    30.  
    31.                 return depth;
    32.             }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    The macro you're using when reading the depth texture, Linear01Depth, I believe is adjusting the depth to be going from the camera to the far plane and removing the near plane. Try looking at what you get from just the texture directly.

    If you want to draw depth add it should appear in a depth buffer you probably want to look at the shadow caster pass and related macros. If you haven't already you should download the built in shader source so you can see what the macros actually do.
     
    Last edited: Jul 21, 2016
  3. JMA_4

    JMA_4

    Joined:
    Jul 21, 2016
    Posts:
    3
    Thanks you for your answer

    The Linear01Depth adjust to the camera and not the near plane, ok, not exactly what I want but I can deal with it.

    I don't really want draw depth, in fact I need the position of pixel in world space. But even in my vertex shader the position seem wrong. When I print x or y it result on a linear shade of grey and the z is always 0.9
    I use my shader with Graphics.Blit if it matter.


    I need the angle between the vector Zcamera and camera-pixel, without the world position I don't how to do it.
    To continu my work I use the distance between the center of the texture and the pixel but I know it's not correct


    I try to multiply the depth texture with the far plan, but the depth texture is the distance between a plan and the pixel not between my camera and pixel so it's a wrong approximation again...


    And sorry, I don't understand how the shadow caster can help me
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    I'm not sure I entirely follow what you want. If you need the angle from the camera's center to the position being rendered then the depth is almost irrelevant. It's just the view space direction.

    In the vertex shader:
    o.viewDir = mul(UNITY_MATRIX_MV, v.vertex).xyz; // transform vertex position into camera relative space

    In the pixel shader:
    float viewOffAngleCosine = dot(normalize(i.viewDir), float3(0,0,1));

    If you only want the vertical or horizontal angle, normalize(i.viewDir).y is vertical, normalize(i.viewDir).x is horizontal.
     
  5. JMA_4

    JMA_4

    Joined:
    Jul 21, 2016
    Posts:
    3
    Thank for the angle calcul, it will be useful

    But o.viewDir = mul(UNITY_MATRIX_MV, v.vertex).xyz seem to not work correctly in my case.
    The Z result is always 0.098. In my scene I have a camera, and two plane at half distance of the far plan.
    I put the depth value in r and b, and the o.viewDir.z in g

    I add the image result and here the value of planes color : (0.498, 0.098, 0.498, 1.000)
    Except planes, every other pixel is (1.000, 0.098, 1.000, 1.000)
     

    Attached Files: