摄像机方向

  1. WorldSpaceViewDir 输入一个模型空间中的顶点坐标 -> 输出(世界空间)从这个点到摄像机的观察方向;
WorldSpaceViewDir
// 内部实现也是用UnityWorldSpaceViewDir
// Computes world space view direction, from object space position
// *Legacy* Please use UnityWorldSpaceViewDir instead
inline float3 WorldSpaceViewDir( in float4 localPos )
{
    float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;
    return UnityWorldSpaceViewDir(worldPos);
}

  1. UnityWorldSpaceViewDir 输入一个模型空间中的顶点坐标 -> 输出(世界空间)从这个点到摄像机的观察方向;
UnityWorldSpaceViewDir
// Computes world space view direction, from object space position
inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
{
    return _WorldSpaceCameraPos.xyz - worldPos;
}

  1. ObjSpaceViewDir 模型空间中的顶点坐标 -> 模型空间从这个点到摄像机的观察方向;
ObjSpaceViewDir
// Computes object space view direction
inline float3 ObjSpaceViewDir( in float4 v )
{
    float3 objSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos.xyz, 1)).xyz;
    return objSpaceCameraPos - v.xyz;
}

光源方向

  1. WorldSpaceLightDir 模型空间中的顶点坐标 -> 世界空间中从这个点到光源的方向
WorldSpaceLightDir
// Computes world space light direction, from object space position
// *Legacy* Please use UnityWorldSpaceLightDir instead
inline float3 WorldSpaceLightDir( in float4 localPos )
{
    float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;
    return UnityWorldSpaceLightDir(worldPos);
}

  1. UnityWorldSpaceLightDir (ForwardBase Only,not normalized)世界空间中的顶点坐标 -> 世界空间中从这个点到光源的方向
WorldSpaceLightDir
// Computes world space light direction, from world space position
inline float3 UnityWorldSpaceLightDir( in float3 worldPos )
{
    #ifndef USING_LIGHT_MULTI_COMPILE
        return _WorldSpaceLightPos0.xyz - worldPos * _WorldSpaceLightPos0.w;
    #else
        #ifndef USING_DIRECTIONAL_LIGHT
        return _WorldSpaceLightPos0.xyz - worldPos;
        #else
        return _WorldSpaceLightPos0.xyz;
        #endif
    #endif
}

--完--