Unity 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...屏幕坐标

投影坐标系->屏幕坐标系这是最简单的。2D坐标变换。也不多说。

使用例子:

o.position = mul(UNITY_MATRIX_MVP, v.vertex);

o.proj0 = ComputeScreenPos(o.position);

COMPUTE_EYEDEPTH(o.proj0.z);

有关深度的一些方法整理:

UNITY_TRANSFER_DEPTH(o):计算eye space的深度值,并写入变量o(float2)。

当需要渲染到一张深度贴图时,在vertex shader中使用该函数。

UNITY_OUTPUT_DEPTH(i): returns eye space depth from i (which must be a float2). Use it in a fragment programwhen rendering into a depth texture.

示例,制作深度贴图用:

Shader"Render Depth" {

SubShader{

    Tags { "RenderType"="Opaque"}

    Pass {

CGPROGRAM

#pragma vertex vert

#pragma fragment frag

#include"UnityCG.cginc"

structv2f {

    float4 pos : SV_POSITION;

    float2 depth : TEXCOORD0;

}

v2fvert (appdata_base v) {

    v2f o;

    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

    UNITY_TRANSFER_DEPTH(o.depth);

    return o;

}

half4frag(v2f i) : SV_Target {

    UNITY_OUTPUT_DEPTH(i.depth);

}

ENDCG

    }

}

}

COMPUTE_EYEDEPTH(o):computes eye spacedepth of the vertex and outputs it in o. Use it in a vertexprogram when not rendering into adepth texture

实现代码#define COMPUTE_EYEDEPTH(o) o =-mul( UNITY_MATRIX_MV, v.vertex ).z设置o为当前顶点视空间的z值,在vertex 函数中使用

                                       

DECODE_EYEDEPTH(i):given high precision value from depth texture i, returns corresponding eye space depth. This macro just returnsi*FarPlane on Direct3D. On platforms withnative depth textures it linearizes and expands the value to match camera’s range.

返回深度纹理值在眼空间的深度。i为从深度纹理中取样得到的值

z-buffer值不是线性的。

opengl 中是一个 0-1的值。0近1远,

directx中是一个-1到1 的值

Linear01Depth参数是0-1的深度值,可得最后计算结果为z/f;就是说,是将[0, f]映射到[0,1]空间,若要映射[n, f]到[0, 1]则是(z-n)/(f-n)。

LinearEyeDepth中参数是0-1的深度值,可得最后计算结果为z。很明显,LinearEyeDepth是将经过透视投影变换的深度值还原了。DECODE_EYEDEPTH跟该函数实现一样

猜你喜欢

转载自blog.csdn.net/escapist_zw/article/details/82968791