I am writing a vertex shader which fades the opacity (alpha) when each vertex is getting closer to the camera. Problem is Unity’s camera has a default value of 0.3 for nearClipPlane (I’m writing this while on the bus, I just check the Unity’s manual.) I can simply minus 0.3 to the distance between the world positions of camera and vertex so the vertex fades out completely before nearClipPlane kicks in. But what if the user, or even Unity in the future, change the nearClipPlane value? Is there anyway to obtain nearClipPlane value without writing an extra C# script to pass that value to shader?
That’s getting the distance from the camera, the clipping planes are depth planes. If you replace the 0.3 with _ProjectionParams.y you’ll still see objects clip near the sides of the camera.
You’ll want to calculate the view space z depth to do the fade, or you’ll need to add some “fudge” to deal with the corners being further than the center. Technically if you still wanted the positions to be distance based you could calculate the camera corner distance and use that.
I think this will get you the distance to the corner… there’s probably a faster way though. length(mul(unity_CameraInvProjection, float4(1,1,0,1)).xyz)
Thanks. Actually I want to fade by depth instead of making the distance act like a radius. So this is what I manage to get my desired fading effect, if not too costy for the UNITY_MATRIX_MVP transformation…
That gets you the clip space position, in which case the z == 0.0 (or -1, or 1, depending on the platform) is already at the camera clip plane so the - _ProjectionParams.y might be doing nothing but moving the fade even further out. You want to use UNITY_MATRIX_MV to get it into view space (what the projection params’ .y and .z are in) not _MVP.