UnityCG.cginc文件中的工具函数和宏(上)
4.2.1 数学常数
//源文件3~13行
#ifndef UNITY_CG_INCLUDED
#define UNITY_CG_INCLUDED
#define UNITY_PI 3.14159265359f
#define UNITY_TWO_PI 6.28318530718f
#define UNITY_FOUR_PI 12.56637061436f
#define UNITY_INV_PI 0.31830988618f
#define UNITY_INV_TWO_PI 0.15915494309f
#define UNITY_INV_FOUR_PI 0.07957747155f
#define UNITY_HALF_PI 1.57079632679f
#define UNITY_INV_HALF_PI 0.636619772367f
4.2.2 与颜色空间相关的常数和工具函数
//line78~127
//新版本不再使用,留下兼容旧版本及已经存在的着色器
//判断是否启用了伽马颜色空间
inline bool IsGammaSpace()
{
#ifdef UNITY_COLORSPACE_GAMMA
return true;
#else
return false;
#endif
}
//把一个颜色值精确的从伽马颜色空间(sRGB)变换到线性空间(CIE-XYZ)
inline float GammaToLinearSpaceExact (float value)
{
if (value <= 0.04045F)
return value / 12.92F;
else if (value < 1.0F)
return pow((value + 0.055F)/1.055F, 2.4F);
else
return pow(value, 2.2F);
}
//把一个颜色值近似的从伽马空间变换到线性空间
inline half3 GammaToLinearSpace (half3 sRGB)
{
// Approximate version from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
return sRGB * (sRGB * (sRGB * 0.305306011h + 0.682171111h) + 0.012522878h);
// Precise version, useful for debugging.
//return half3(GammaToLinearSpaceExact(sRGB.r), GammaToLinearSpaceExact(sRGB.g), GammaToLinearSpaceExact(sRGB.b));
}
//把一个颜色值精确的从线性空间变换到伽马颜色空间
inline float LinearToGammaSpaceExact (float value)
{
if (value <= 0.0F)
return 0.0F;
else if (value <= 0.0031308F)
return 12.92F * value;
else if (value < 1.0F)
return 1.055F * pow(value, 0.4166667F) - 0.055F;
else
return pow(value, 0.45454545F);
}
//把一个颜色值近似的从线性空间变换到伽马颜色空间
inline half3 LinearToGammaSpace (half3 linRGB)
{
linRGB = max(linRGB, half3(0.h, 0.h, 0.h));
// An almost-perfect approximation from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
return max(1.055h * pow(linRGB, 0.416666667h) - 0.055h, 0.h);
// Exact version, useful for debugging.
//return half3(LinearToGammaSpaceExact(linRGB.r), LinearToGammaSpaceExact(linRGB.g), LinearToGammaSpaceExact(linRGB.b));
}
4.2.3 描述顶点布局格式的结构体
Unity定义了几个在不同场合使用的顶点结构体布局,避免用一个大的顶点结构体造成数据冗余。
//Line 51~76
//顶点携带数据的载体
struct appdata_base {
float4 vertex : POSITION;//顶点坐标
float3 normal : NORMAL;//顶点法线
float4 texcoord : TEXCOORD0;//顶点使用的第一层纹理坐标
//UNITY_VERTEX_INPUT_INSTANCE_ID用于定义顶点多例化ID用的一个宏
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct appdata_tan {
float4 vertex : POSITION;
float4 tangent : TANGENT;//顶点切线
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct appdata_full {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;//顶点使用的第一层纹理坐标(如静态光照贴图uv)
float4 texcoord2 : TEXCOORD2;//顶点使用的第二层纹理坐标(如动态光照贴图uv)
float4 texcoord3 : TEXCOORD3;//顶点使用的第三层纹理坐标
fixed4 color : COLOR;//顶点颜色
UNITY_VERTEX_INPUT_INSTANCE_ID
};
4.2.4 用于空间变换的工具函数
在实际开发中通常遇到把某一位置或者方向向量从一个空间坐标系下变换到另一个空间坐标系的需求,Unity3D提供了一系列用来进行空间变换的工具函数。
float3 UnityObjectToWorldNormal( in float3 norm )
float3 UnityWorldSpaceLightDir( in float3 worldPos )
float3 UnityWorldSpaceViewDir( in float3 worldPos )
//Line 129~241
// Tranforms position from world to homogenous space
inline float4 UnityWorldToClipPos( in float3 pos )
{
return mul(UNITY_MATRIX_VP, float4(pos, 1.0));
}
// Tranforms position from view to homogenous space
inline float4 UnityViewToClipPos( in float3 pos )
{
return mul(UNITY_MATRIX_P, float4(pos, 1.0));
}
// Tranforms position from object to camera space
inline float3 UnityObjectToViewPos( in float3 pos )
{
return mul(UNITY_MATRIX_V, mul(unity_ObjectToWorld, float4(pos, 1.0))).xyz;
}
// overload for float4; avoids "implicit truncation" warning for existing shaders
inline float3 UnityObjectToViewPos(float4 pos)
{
return UnityObjectToViewPos(pos.xyz);
}
// Tranforms position from world to camera space
inline float3 UnityWorldToViewPos( in float3 pos )
{
return mul(UNITY_MATRIX_V, float4(pos, 1.0)).xyz;
}
// Transforms direction from object to world space
inline float3 UnityObjectToWorldDir( in float3 dir )
{
return normalize(mul((float3x3)unity_ObjectToWorld, dir));
}
// Transforms direction from world to object space
inline float3 UnityWorldToObjectDir( in float3 dir )
{
return normalize(mul((float3x3)unity_WorldToObject, dir));
}
// Transforms normal from object to world space
inline float3 UnityObjectToWorldNormal( in float3 norm )
{
#ifdef UNITY_ASSUME_UNIFORM_SCALING
return UnityObjectToWorldDir(norm);
#else
// mul(IT_M, norm) => mul(norm, I_M) => {dot(norm, I_M.col0), dot(norm, I_M.col1), dot(norm, I_M.col2)}
return normalize(mul(norm, (float3x3)unity_WorldToObject));
#endif
}
// 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
}
// 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);
}
// Computes object space light direction
inline float3 ObjSpaceLightDir( in float4 v )
{
float3 objSpaceLightPos = mul(unity_WorldToObject, _WorldSpaceLightPos0).xyz;
#ifndef USING_LIGHT_MULTI_COMPILE
return objSpaceLightPos.xyz - v.xyz * _WorldSpaceLightPos0.w;
#else
#ifndef USING_DIRECTIONAL_LIGHT
return objSpaceLightPos.xyz - v.xyz;
#else
return objSpaceLightPos.xyz;
#endif
#endif
}
// Computes world space view direction, from object space position
inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
{
return _WorldSpaceCameraPos.xyz - worldPos;
}
// 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);
}
// 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;
}
// Declares 3x3 matrix 'rotation', filled with tangent space basis
#define TANGENT_SPACE_ROTATION \
float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) ) * v.tangent.w; \
float3x3 rotation = float3x3( v.tangent.xyz, binormal, v.normal )
4.2.5 与光照计算相关的工具函数
//Line 246~318
//本函数将用在ForwardBase类型的渲染通道上,每个顶点被4个点光源照亮时,计算漫反射Lambert效果。
//参数lightPosX、lightPosY、lightPosZ的4个分量依次存储了4个点光源的x坐标、y坐标、z坐标。
//参数lightColor0~3存储了4个点光源颜色RGB。lightAttenSq存储4个点光源的二次项衰减系数。
float3 Shade4PointLights (
float4 lightPosX, float4 lightPosY, float4 lightPosZ,
float3 lightColor0, float3 lightColor1, float3 lightColor2, float3 lightColor3,
float4 lightAttenSq,
float3 pos, float3 normal)
{
// to light vectors
float4 toLightX = lightPosX - pos.x;
float4 toLightY = lightPosY - pos.y;
float4 toLightZ = lightPosZ - pos.z;
// squared lengths
float4 lengthSq = 0;
lengthSq += toLightX * toLightX;
lengthSq += toLightY * toLightY;
lengthSq += toLightZ * toLightZ;
// don't produce NaNs if some vertex position overlaps with the light
lengthSq = max(lengthSq, 0.000001);
// NdotL
float4 ndotl = 0;
ndotl += toLightX * normal.x;
ndotl += toLightY * normal.y;
ndotl += toLightZ * normal.z;
// correct NdotL
float4 corr = rsqrt(lengthSq);
ndotl = max (float4(0,0,0,0), ndotl * corr);
// attenuation
float4 atten = 1.0 / (1.0 + lengthSq * lightAttenSq);
float4 diff = ndotl * atten;
// final color
float3 col = 0;
col += lightColor0 * diff.x;
col += lightColor1 * diff.y;
col += lightColor2 * diff.z;
col += lightColor3 * diff.w;
return col;
}
// Used in Vertex pass: Calculates diffuse lighting from lightCount lights. Specifying true to spotLight is more expensive
// to calculate but lights are treated as spot lights otherwise they are treated as point lights.
//用在顶点着色器中,计算出光源产生的漫反射效果。
float3 ShadeVertexLightsFull (float4 vertex, float3 normal, int lightCount, bool spotLight)
{
//Unity提供的光源位置和方向都是在View空间的,因此先将vertex和normal从模型空间转到View空间。
float3 viewpos = UnityObjectToViewPos (vertex);
float3 viewN = normalize (mul ((float3x3)UNITY_MATRIX_IT_MV, normal));
float3 lightColor = UNITY_LIGHTMODEL_AMBIENT.xyz;
for (int i = 0; i < lightCount; i++) {
float3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w;
float lengthSq = dot(toLight, toLight);
// don't produce NaNs if some vertex position overlaps with the light
lengthSq = max(lengthSq, 0.000001);
toLight *= rsqrt(lengthSq);
float atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
if (spotLight)
{
float rho = max (0, dot(toLight, unity_SpotDirection[i].xyz));
//求聚光灯照射到的圆形2D平面区域内的衰减,小于1/4照射角度spotAtten为1,大于1/4小于1/2从1到0,大于1/2是0
float spotAtt = (rho - unity_LightAtten[i].x) * unity_LightAtten[i].y;
atten *= saturate(spotAtt);
}
float diff = max (0, dot (viewN, toLight));
lightColor += unity_LightColor[i].rgb * (diff * atten);
}
return lightColor;
}
//使用4个非聚光灯光源(用平行光或点光源),求得漫反射Lambert效果(点光源还有衰减效果)。
float3 ShadeVertexLights (float4 vertex, float3 normal)
{
return ShadeVertexLightsFull (vertex, normal, 4, false);
}
//Line 435~438
// Transforms 2D UV by scale/bias property
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
// Deprecated. Used to transform 4D UV by a fixed function texture matrix. Now just returns the passed UV.
#define TRANSFORM_UV(idx) v.texcoord.xy
VertexLight是一个简单的顶点光照计算函数,其颜色计算方式就是用顶点漫反射颜色乘以纹理颜色,然后加上纹素的Alpha值与顶点镜片反射颜色,两者之和就是最终的颜色。
//Line 442~455
struct v2f_vertex_lit {
float2 uv : TEXCOORD0;
fixed4 diff : COLOR0;
fixed4 spec : COLOR1;
};
inline fixed4 VertexLight( v2f_vertex_lit i, sampler2D mainTex )
{
fixed4 texcol = tex2D( mainTex, i.uv );
fixed4 c;
c.xyz = ( texcol.xyz * i.diff.xyz + i.spec.xyz * texcol.a );
c.w = texcol.w * i.diff.w;
return c;
}
ParallaxOffset函数用于视差贴图算法。根据当前片元对应的高度图中的高度值h,以及高度缩放系数height和切线空间中片元到摄像机连线向量,计算到带你过去片元实际上要使用外观纹理的哪一点的纹理。
// Calculates UV offset for parallax bump mapping
inline float2 ParallaxOffset( half h, half height, half3 viewDir )
{
h = h * height - height/2.0;
float3 v = normalize(viewDir);
v.z += 0.42;
return h * (v.xy / v.z);
}
Luminance函数把一个RGB颜色值转化成亮度值,当前的RGB颜色值基于伽马空间或线性空间,得到的亮度值有不同的结果。
LinearRgbToLuminace函数是把一个线性空间的颜色值转化成亮度值。它实际上是外部先将RGB颜色空间转换到CIE1931-Yxy颜色空间中,再通过Y=0.2126729R+0.7151522G+0.072175B计算得出。
// Converts color to luminance (grayscale)
inline half Luminance(half3 rgb)
{
return dot(rgb, unity_ColorSpaceLuminance.rgb);
}
// Convert rgb to luminance
// with rgb in linear space with sRGB primaries and D65 white point
half LinearRgbToLuminance(half3 linearRgb)
{
return dot(linearRgb, half3(0.2126729f, 0.7151522f, 0.0721750f));
}
4.2.6 与HDR及光照贴图颜色编解码相关的工具函数
高动态范围(high dynamic range,HDR)光照是一种用来实现超过了显示器所能表现的亮度范围的渲染技术。如果采用8位通道存储每一个颜色的RGB分量,则每个分量亮度级别只有256种。显然,只有256个亮度级别是不足以描述自然界中的亮度差别的情况的,如太阳的亮度可能是一个白炽灯亮度的数千倍,这将远远超出当前显示器的亮度表示能力。HDR技术就是将尽可能大的亮度能编码到尽可能小的存储空间里。如果线性的缩小,则可能导致颜色带状阶跃的问题,不够平滑渐变。所以实际的HDR一般遵循如下几步:1.在每个颜色通道是16或者32位的浮点纹理或者渲染目标上渲染当前的场景。2.使用RGBM、LogLuv等编码方式节省所需的内存和带宽。3.通过降采样计算场景亮度。4.根据场景亮度值做一个色调映射(tone mapping),将最终颜色值输出到每通道8位的RGB格式的渲染目标上。
渲染目标可以理解为一系列像素点的集合,在计算机中需要用一系列字节表示像素点的RGBA信息,最常见的是使用1字节表示像素点的一个颜色分量,这样表示一个像素点则需要4个字节,32位。但是1字节表示一个颜色分量,如Red最多只能表示256阶的信息。在很多情况下,尤其是在处理HDR信息时,256阶远远不够用。因此,应采用16位或者更高精度的浮点数表示每一位颜色分量。浮点渲染目标正式表示这一概念。
RGBM是一种颜色编码方式,M即shared multiplier。根据Unity3D文档介绍,如果是在伽马工作流中,M取值范围是[0,5]。如果是在线性工作流中,M取值范围是[0,pow(5,2.2)]。如上所述,为了解决精度不足以存储亮度范围信息的问题,可以创建一个精度更高的浮点渲染目标,但使用高精度的浮点渲染目标会带来性能问题,即需要更高的内存存储空间和更高的带宽。并且有些硬件无法操作16位浮点渲染目标像8位精度渲染目标那么快速。为了解决这个问题,需要采用一种编码方法把高范围数据编码进以8位颜色分量存储的数据。编码方式有很多种,如RGBM、LogLuv编码等。假如有一个给定的包含了RGB颜色分量的颜色值color,定义了一个编码后的取值“最大范围值”maxRGBM,将其编码成一个含有R、G、B、M这4个分量的颜色值的步骤如Unity3D引擎提供的UnityEncodeRGBM函数所示。
//Line 480~583
half4 UnityEncodeRGBM (half3 color, float maxRGBM)
{
float kOneOverRGBMMaxRange = 1.0 / maxRGBM;
const float kMinMultiplier = 2.0 * 1e-2;
float3 rgb = color * kOneOverRGBMMaxRange;
float alpha = max(max(rgb.r, rgb.g), max(rgb.b, kMinMultiplier));
alpha = ceil(alpha * 255.0) / 255.0;
// Division-by-zero warning from d3d9, so make compiler happy.
alpha = max(alpha, kMinMultiplier);
return half4(rgb / alpha, alpha);
}
// Decodes HDR textures
// handles dLDR, RGBM formats
inline half3 DecodeHDR (half4 data, half4 decodeInstructions)
{
// Take into account texture alpha if decodeInstructions.w is true(the alpha value affects the RGB channels)
half alpha = decodeInstructions.w * (data.a - 1.0) + 1.0;
// If Linear mode is not supported we can skip exponent part
#if defined(UNITY_COLORSPACE_GAMMA)
return (decodeInstructions.x * alpha) * data.rgb;
#else
# if defined(UNITY_USE_NATIVE_HDR)
return decodeInstructions.x * data.rgb; // Multiplier for future HDRI relative to absolute conversion.
# else
return (decodeInstructions.x * pow(alpha, decodeInstructions.y)) * data.rgb;
# endif
#endif
}
// Decodes HDR textures
// handles dLDR, RGBM formats
// Called by DecodeLightmap when UNITY_NO_RGBM is not defined.
inline half3 DecodeLightmapRGBM (half4 data, half4 decodeInstructions)
{
// If Linear mode is not supported we can skip exponent part
#if defined(UNITY_COLORSPACE_GAMMA)
# if defined(UNITY_FORCE_LINEAR_READ_FOR_RGBM)
return (decodeInstructions.x * data.a) * sqrt(data.rgb);
# else
return (decodeInstructions.x * data.a) * data.rgb;
# endif
#else
return (decodeInstructions.x * pow(data.a, decodeInstructions.y)) * data.rgb;
#endif
}
// Decodes doubleLDR encoded lightmaps.
inline half3 DecodeLightmapDoubleLDR( fixed4 color )
{
float multiplier = IsGammaSpace() ? 2.0f : GammaToLinearSpace(2.0f).x;
return multiplier * color.rgb;
}
inline half3 DecodeLightmap( fixed4 color, half4 decodeInstructions)
{
#if defined(UNITY_NO_RGBM)
return DecodeLightmapDoubleLDR( color );
#else
return DecodeLightmapRGBM( color, decodeInstructions );
#endif
}
half4 unity_Lightmap_HDR;
inline half3 DecodeLightmap( fixed4 color )
{
return DecodeLightmap( color, unity_Lightmap_HDR );
}
half4 unity_DynamicLightmap_HDR;
// Decodes Enlighten RGBM encoded lightmaps
// NOTE: Enlighten dynamic texture RGBM format is _different_ from standard Unity HDR textures
// (such as Baked Lightmaps, Reflection Probes and IBL images)
// Instead Enlighten provides RGBM texture in _Linear_ color space with _different_ exponent.
// WARNING: 3 pow operations, might be very expensive for mobiles!
inline half3 DecodeRealtimeLightmap( fixed4 color )
{
//@TODO: Temporary until Geomerics gives us an API to convert lightmaps to RGBM in gamma space on the enlighten thread before we upload the textures.
#if defined(UNITY_FORCE_LINEAR_READ_FOR_RGBM)
return pow ((unity_DynamicLightmap_HDR.x * color.a) * sqrt(color.rgb), unity_DynamicLightmap_HDR.y);
#else
return pow ((unity_DynamicLightmap_HDR.x * color.a) * color.rgb, unity_DynamicLightmap_HDR.y);
#endif
}
inline half3 DecodeDirectionalLightmap (half3 color, fixed4 dirTex, half3 normalWorld)
{
// In directional (non-specular) mode Enlighten bakes dominant light direction
// in a way, that using it for half Lambert and then dividing by a "rebalancing coefficient"
// gives a result close to plain diffuse response lightmaps, but normalmapped.
// Note that dir is not unit length on purpose. Its length is "directionality", like
// for the directional specular lightmaps.
half halfLambert = dot(normalWorld, dirTex.xyz - 0.5) + 0.5;
return color * halfLambert / max(1e-4h, dirTex.w);
}
4.2.7 把高精度数据编码到低精度缓冲区的函数
//Line 586~651
// Encoding/decoding [0..1) floats into 8 bit/channel RGBA. Note that 1.0 will not be encoded properly.
inline float4 EncodeFloatRGBA( float v )
{
float4 kEncodeMul = float4(1.0, 255.0, 65025.0, 16581375.0);
float kEncodeBit = 1.0/255.0;
float4 enc = kEncodeMul * v;
enc = frac (enc);
enc -= enc.yzww * kEncodeBit;
return enc;
}
inline float DecodeFloatRGBA( float4 enc )
{
float4 kDecodeDot = float4(1.0, 1/255.0, 1/65025.0, 1/16581375.0);
return dot( enc, kDecodeDot );
}
// Encoding/decoding [0..1) floats into 8 bit/channel RG. Note that 1.0 will not be encoded properly.
inline float2 EncodeFloatRG( float v )
{
float2 kEncodeMul = float2(1.0, 255.0);
float kEncodeBit = 1.0/255.0;
float2 enc = kEncodeMul * v;
enc = frac (enc);
enc.x -= enc.y * kEncodeBit;
return enc;
}
inline float DecodeFloatRG( float2 enc )
{
float2 kDecodeDot = float2(1.0, 1/255.0);
return dot( enc, kDecodeDot );
}
// Encoding/decoding view space normals into 2D 0..1 vector
inline float2 EncodeViewNormalStereo( float3 n )
{
float kScale = 1.7777;
float2 enc;
enc = n.xy / (n.z+1);
enc /= kScale;
enc = enc*0.5+0.5;
return enc;
}
inline float3 DecodeViewNormalStereo( float4 enc4 )
{
float kScale = 1.7777;
float3 nn = enc4.xyz*float3(2*kScale,2*kScale,0) + float3(-kScale,-kScale,1);
float g = 2.0 / dot(nn.xyz,nn.xyz);
float3 n;
n.xy = g*nn.xy;
n.z = g-1;
return n;
}
//将法线3个分量编码进xy两个分量,depth编码进zw
inline float4 EncodeDepthNormal( float depth, float3 normal )
{
float4 enc;
enc.xy = EncodeViewNormalStereo (normal);
enc.zw = EncodeFloatRG (depth);
return enc;
}
inline void DecodeDepthNormal( float4 enc, out float depth, out float3 normal )
{
depth = DecodeFloatRG (enc.zw);
normal = DecodeViewNormalStereo (enc);
}