内置shader辅助函数定义在UnityCG.cginc文件中
顶点转换函数:
Function: | Description: |
float4 UnityObjectToClipPos(float3 pos) | Transforms a point from object space to the camera’s clip space in homogeneous coordinates. This is the equivalent of mul(UNITY_MATRIX_MVP, float4(pos, 1.0)), and should be used in its place. homogeneous coordinates:齐次坐标 等价于:mul(UNITY_MATRIX_MVP, float4(pos, 1.0)), |
float3 UnityObjectToViewPos(float3 pos) | Transforms a point from object space to view space. This is the equivalent of mul(UNITY_MATRIX_MV, float4(pos, 1.0)).xyz, and should be used in its place. 等价于:mul(UNITY_MATRIX_MV, float4(pos, 1.0)). |
Generic helper functions(定义在UnityCG.cginc文件中)
Function: | Description: |
float3 WorldSpaceViewDir (float4 v) | Returns world space direction (not normalized) from given object space vertex position towards the camera .(参数是object space下的顶点坐标,取得world space下指向摄像机的方向,即视角方向) |
float3 ObjSpaceViewDir (float4 v) | Returns object space direction (not normalized) from given object space vertex position towards the camera. (同上,不过取到的视角方向是在object space上的) |
float2 ParallaxOffset (half h, half height, half3 viewDir) | calculates UV offset for parallax normal mapping. 为视差法线贴图计算UV偏移 |
fixed Luminance (fixed3 c) | Converts color to luminance (grayscale). 将颜色转换为亮度(灰度) |
fixed3 DecodeLightmap (fixed4 color) | Decodes color from Unity lightmap (RGBM or dLDR depending on platform). 从烘焙贴图解码,烘焙贴图生成的是EXR格式的HDR贴图,根据不同平台返回RGBM或dLDR |
float4 EncodeFloatRGBA (float v) | Encodes [0..1) range float into RGBA color, for storage in low precision render target. 把float编码到RGBA8 |
float DecodeFloatRGBA (float4 enc) | Decodes RGBA color into a float.上面方法的反编 |
float2 EncodeFloatRG (float v) | Encodes [0..1) range float into a float2. 编码 [0.0,1.0)float--> float2 |
float DecodeFloatRG (float2 enc) | Decodes a previously-encoded RG float. float2解码--> [0.0,1.0) |
float2 EncodeViewNormalStereo (float3 n) | Encodes view space normal into two numbers in 0..1 range. 视空间法线(float3)编码到float2 |
float3 DecodeViewNormalStereo (float4 enc4) | Decodes view space normal from enc4.xy. 上面方法的解码,只使用参数的xy值 |
Forwardrendering helper functions in UnityCG.cginc
These functions are only useful when using forward rendering(ForwardBase or ForwardAdd pass types).
仅用于前向渲染
Function: | Description: |
| Computes world space direction (not normalized) to light, given object space vertex position. 参数是object space下的顶点坐标,取得world space下指向光源的方向 |
| Computes object space direction (not normalized) to light, given object space vertex position. 参数是object space下的顶点坐标,取得object space下指向光源的方向 |
| Computes illumination from four point lights, with light data tightly packed into vectors. Forward rendering uses this to compute per-vertex lighting. 正向渲染中,最多有4个点光源会以逐顶点渲染的方式被计算。 |
Vertex-lithelper functions in UnityCG.cginc
These functionsare only useful when using per-vertex lit shaders (“Vertex” pass type).
仅用于per-vertex lit shaders
Function: | Description: |
| Computes illumination from four per-vertex lights and ambient, given object space position & normal. 参数为顶点跟法线,根据四个逐顶点光源跟环境光计算光照 |
mul(UNITY_MATRIX_MVP,v)跟ComputeScreenPos的区别
一个是model position->projection position 投影坐标
一个是projection position->screen position...屏幕坐标
转载自:http://blog.csdn.net/qingshui37/article/details/51476404