How do I get the exact values of the near and far planes in clip space, both in a shader and in C#?
I'm playing around with a plugin idea that manually does something similar to shadow-maps, and I need to know the values for the near and far Z bounds in clip space -- depending on the graphics API, they could be 0 to 1, -1 to 1, or possibly 1 to 0 (and maybe even 1 to -1?). Unity docs mention the macros UNITY_NEAR_CLIP_VALUE
and UNITY_REVERSED_Z
, but there are two problems:
It doesn't give a concrete example of actually defining the near/far values in a fully platform-agnostic way.
It only explains how to find these values in a shader, not in a C# script.
If you can answer either of these two issues, I'd appreciate it!
I have an idea for how to get the near/far values in a shader, but there's no way to know if it's platform-agnostic without testing every concievable platform, which is unfeasible:
#if defined(UNITY_REVERSED_Z)
const float nearPlane = 1.0,
farPlane = UNITY_NEAR_CLIP_VALUE;
#else
const float nearPlane = UNITY_NEAR_CLIP_VALUE,
farPlane = 1.0;
#endif
I believe he wants the values in clip space which can vary depending on the graphics API.
There's probably a better way than this but you can probably multiply the projection matrix in C# by the forward vector with z at both near and far and see what you get (don't forget perspective divide by w). Then again, that probably doesn't account for UNITY_REVERSED_Z but I think if you can deter$$anonymous$$e if that's being used, just swap near and far.
Your answer
Follow this Question
Related Questions
Something wrong with the cut out feature of the standard shader? 0 Answers
Change Color Of Texture Through Shader At Level Start and Save To Memory To Stop Shader Use 0 Answers
Just Cause 3 Volumetric Explosions 1 Answer
Is there any way to make a custom written shader with hdrp? 0 Answers
How to enhance 3D graphics in Unity for mobile devices? 0 Answers