UnityCG.cginc中常用函数
摄像机方向
- 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);
}
- UnityWorldSpaceViewDir 输入一个模型空间中的顶点坐标 -> 输出(世界空间)从这个点到摄像机的观察方向;
UnityWorldSpaceViewDir
// Computes world space view direction, from object space position
inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
{
return _WorldSpaceCameraPos.xyz - worldPos;
}
- 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;
}
光源方向
- 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);
}
- 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
}
--完--
- 原文作者: 留白
- 原文链接: https://zfunnily.github.io/2022/04/cginc/
- 更新时间:2024-04-16 01:01:05
- 本文声明:转载请标记原文作者及链接