Shader View Vector

I am searching how to get the view vector in a shader (Surface shader or Vertex&Fragment shader).
There is the viewDir but this vector doesn’t varies according to the orientation or rotation of the camera.

There’s UNITY_MATRIX_IT_MV[2].xyz but this doesn’t change according to the translation of the camera.

How can I combine both ?

I’m not really sure about this but have you tried these?

// Computes world space view direction
inline float3 WorldSpaceViewDir( in float4 v )
{
    return _WorldSpaceCameraPos.xyz - mul(_Object2World, v).xyz;
}

// Computes object space view direction
inline float3 ObjSpaceViewDir( in float4 v )
{
    float3 objSpaceCameraPos = mul(_World2Object, float4(_WorldSpaceCameraPos.xyz, 1)).xyz * unity_Scale.w;
    return objSpaceCameraPos - v.xyz;
}

or

float3 viewDir = IN.worldPos - _WorldSpaceCameraPos;

in Surface shader

also don’t forget to put tangent rotation, i think it had some part on it

1 Like