shader内置函数

内置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:

float3 WorldSpaceLightDir (float4 v)

Computes world space direction (not normalized) to light, given object space vertex position.

参数是object space下的顶点坐标,取得world space下指向光源的方向

float3 ObjSpaceLightDir (float4 v)

Computes object space direction (not normalized) to light, given object space vertex position.

参数是object space下的顶点坐标,取得object space下指向光源的方向

float3 Shade4PointLights (...)

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:

float3 ShadeVertexLights (float4 vertex, float3 normal)

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值