Is there an easy way to get the camera’s forward vector in a surface shader (the near clip plane’s normal)? Perhaps a built-in shader constant or input struct variable? I want something like the
viewDir variable from here:
but just the forward vector, not a ray per-pixel.
I’m trying to do some calculations off pixel distances projected onto the near clipping plane’s normal instead of the distance from the camera position.
I know it’s 5 years later on from the original question, but the solution mentioned in the comments above worked for me too.
I put this in my frag function and it works perfectly:
float3 viewDir = UNITY_MATRIX_IT_MV.xyz;
For those of you still attempting to use moosefetcher’s answer, I tried this in my vertex shader and it messed with my lighting for some reason.
In the end I used
float3 forward = mul((float3x3)unity_CameraToWorld, float3(0,0,1)); which worked fine.
If you want to get an analogue of
IsFacing in the vertex shader use:
half3 normal = UnityObjectToWorldNormal(v.normal);
half3 worldVert = mul(unity_ObjectToWorld, v.vertex);
half3 viewDir = _WorldSpaceCameraPos - worldVert;
normal *= lerp(-1, 1, dot(normal, viewDir) > 0);