- Home /
ZBuffer and Object Depth
I can easily retrieve the depth of an object using camera's depth but this is dependent of the camera's z position. I would like to "normalize" the depth and not to be linked with the camera distance from the object.
Here is a simple example of applying depth of an object but this is view dependent. Maybe there's a matrix trick to normalize the view position.
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.screenPos = ComputeScreenPos(o.pos);
}
void surf(Input IN, inout MyOutput OUT)
{
half4 c = tex2D (_MainTex, IN.uv_MainTex);
OUT.Albedo = c.rgb;
OUT.Alpha = c.a;
float Depth = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r);
half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
edgeBlendFactors = saturate((Depth - IN.screenPos.w + _A) * _B); // _A = 7 and _B = 0.2
Depth = lerp(1.0, 0.0, edgeBlendFactors);
OUT.Albedo = Depth;
}
Thank you !
Answer by Bunny83 · Oct 07, 2014 at 12:47 PM
I'M not sure what you actually want to know. The "depth" as seen on the screen is relative to the camera as the camera represents the projection matrix. The camera is the view point. Your view starts at the camera's position. There is nothing like a "global" or "worldspace" view as this would mean your camera is at 0,0,0 and not rotated.
I understand what you say but is it maybe possible to constrain the camera position. Of course depth is dependent to camera orientation but I want to constraint the distance to the object and keep the same distance.
I don't get what you want. depth is the distance from the camera. A distance is a length between two points. While one point is your object's vertex the other is the camera's position. If you want a different distance, tell us what distance you mean, relative to what?
Is it possible that all you actually want is to move the camera with your object? That obviously can't be done inside a shader. If you can't describe what behaviour you actually want we can'T really help you.
When I am talking about depth I should probably use thickness. To calculate thickness I think that the best way is to use depth (in my opinion) that's why I want to constrain the depth. I've made another question here. $$anonymous$$aybe You'll better understand what I mean. Thickness Surface Shader
Your answer
Follow this Question
Related Questions
Thickness Surface Shader 0 Answers
Shader - Object Depth 0 Answers
How to add Bump/Normal Map to surface shader? (CG) 0 Answers
Depth issues with a shader 1 Answer
How to control Ambient Lighting and Spherical Harmonics 0 Answers