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): Shader "Custom/DepthGrayscale" { SubShader { Tags { "RenderType"="Opaque" } Pass{ CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _CameraDepthTexture; struct v2f { float4 pos : SV_POSITION; float4 scrPos:TEXCOORD1; }; //Vertex Shader v2f vert (appdata_base v){ v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.scrPos=ComputeScreenPos(o.pos); return o; } //Fragment Shader half4 frag (v2f i) : COLOR{ float depthValue = Linear01Depth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r); half4 depth; depth.r = depthValue; depth.g = depthValue; depth.b = depthValue; depth.a = 1; return depth; } ENDCG } } FallBack "Diffuse" } 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): struct vertOut { float4 position : SV_POSITION; float4 worldSpacePosition : TEXCOORD0; }; vertOut vert(appdata_full vertIn) { vertOut o; o.position = mul(UNITY_MATRIX_MVP, vertIn.vertex); o.worldSpacePosition = mul(_Object2World, vertIn.vertex); return o; } float4 frag(vertOut fragIn) : COLOR { float4 depth; // The interpolator calculated world space pixel coordinates for you here float4 pixelWorldSpacePosition = fragIn.worldSpacePosition; float depthValue; depthValue=length(_WorldSpaceCameraPos - pixelWorldSpacePosition.xyz)*_ProjectionParams.w; depth.r = depthValue; depth.g = depthValue; depth.b = depthValue; depth.a = 1; return depth; }
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.
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
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.
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)