Camera Distance Mask Shader

Camera Distance Mask Shader

If we want to make an object dull / darker when we move closer to it and brighter when we move far away, then what we need is a camera distance mask.

All we have to do is calculate the distance between the vertex position and the camera and add/scale it with some constant.

Distance Mask = ( Length( CameraPos – VertexPos ) + C1 ) * C2

First, Subtract the vertex position from the camera position. This will give the distance vector.

The ‘Length’ node will calculate the scalar magnitude of the distance vector. Add and multiply it with some constant values based on how fast the transition between dark to bright should happen.

Finally, assign this value to the ‘Base Color’ to visualize the effect.