unity ShaderLab basis of the [library] command Detailed UnityCG.cginc

UnityCG.cginc library

UnityCG.cginc The file contains a number of parameters Serve method. Easy to use

Import documents
CGPROGRAM
#include "UnityCG.cginc"
ENDCG
unitycg.cginc common structure
command parameter Examples Explanation
appdata_base Vertex position, vertex normals, texture coordinates of the first group float4 vertex : POSITION; float3 normal : NORMAL; float4 texcoord: TEXCOORD0; It can be used to input a vertex shader
appdata_tan Vertex position, vertex tangent, vertex normals, texture coordinates of the first group float4 vertex : POSITION; float4 tangent : TANGENT; float3 normal : NORMAL; float4 texcoord : TEXCOORD0; It can be used to input a vertex shader
appdata_full Vertex position, vertex tangent, vertex normals, four (or more) texture coordinate i cfloat4 vertex : POSITION; float4 tangent : TANGENT; float3 normal : NORMAL; float4 texcoord : TEXCOORD0; float4 texcoord1 : TEXCOORD1; float4 texcoord2 : TEXCOORD2; float4 texcoord3 : TEXCOORD3; #if defined(SHADER_API_XBOX360) half4 texcoord4 : TEXCOORD4; half4 texcoord5 : TEXCOORD5; #endif fixed4 color : COLOR; It can be used to input a vertex shader
appdata_img It can be used to input a vertex shader float4 vertex : POSITION; half2 texcoord : TEXCOORD0; It can be used to input a vertex shader
v2f_img Clip space position, texture coordinates It can be used to output a vertex shader
unitycg.cginc commonly used functions
command parameter Examples Explanation
float4 WorldSpaceViewDir(float4 v) Input vertex position of a model space, world space returned from that point to the viewing direction of the camera
float4 UnityWorldSpaceViewDir (float4 v) Enter the position of a vertex in world space, world space returned from that point to the viewing direction of the camera
float4 ObjSpaceViewDir (float4 in) Input vertex position of a model space and returns the observation direction from model space to store the camera
float4 WorldSpace LightDir(flaot4 v) Only for rendering forward. Input vertex position of a model space, the direction of illumination returned from world space to the point light source. Not normalized
float4 ObjectSpaceLightDir(float4 v) 仅用于向前渲染中,输入一个模型空间中的顶点位置, 返回模型空间中从该点到光源的光照方向。没有被归一化
float4 UnityWorldSpaceLightDir(float4 v) 仅用于向前渲染中,输入一个世界空间中的顶点位置, 返回世界空间中从该点到光源的光照方向。没有被归一化
float3 UnityObjectToWorldNormal(float3 norm) 把法线方向从模型空间中转换到世界空间中
float3 UnityObjectToWorldDir(float3 dir) 把方向矢量从模型空间中变换到世界空间中
float3 Unity WorldToObjectDir(float3 dir) 把方向矢量从世界空间变换到模型空间中

shader数学函数:

函数 说明 实例
radians(degree) 角度变弧度(一般默认都用弧度)
degrees(radian) 弧度变角度
sin(angle), cos(angle), tan(angle) 三角函数
asin(x) arc sine, 返回弧度 [-PI/2, PI/2];
acos(x) arc cosine,返回弧度 [0, PI]
atan(y, x) arc tangent, 返回弧度 [-PI, PI];
atan(y/x) arc tangent, 返回弧度 [-PI/2, PI/2];
pow(x, y) x的y次方
exp(x) 指数, log(x)
exp2(x) 2的x次方, log2(x)
sqrt(x) x的根号;
inversesqrt(x) x根号的倒数
abs(x) 绝对值
sign(x) 取当前数值的正负符号,返回 1, 0 或 -1 (x>0;x=0;x<0)
floor(x) 底部取整
ceil(x) 顶部取整
fract(x) 取小数部分
mod(x, y) 取模, x - y*floor(x/y)
min(x, y) 取最小值
max(x, y) 取最大值
clamp(x, min, max) min(max(x, min), max);
mix(x, y, a) x, y的线性混叠, x(1-a) + y*a;
step(edge, x) 如 x smoothstep(edge0, edge1, x): threshod smooth transition时使用。 edge0<=edge0时为0.0, x>=edge1时为1.0
length(x) 向量长度
distance(p0, p1) 两点距离, length(p0-p1);
dot(x, y) 点积,各分量分别相乘 后 相加
cross(x, y) 差积 x[1]*y[2]-y[1]*x[2], x[2]*y[0] - y[2]*x[0], x[0]*y[1] - y[0]*x[1]
normalize(x) 归一化 length(x)=1;
faceforward(N, I, Nref) 如 dot(Nref, I)< 0则N, 否则 -N
reflect(I, N) I的反射方向 I -2*dot(N, I)*N, N必须先归一化
refract(I, N, eta) 折射 k=1.0-etaeta(1.0 - dot(N, I) * dot(N, I)); 如k<0.0 则0.0,否则 etaI - (etadot(N, I)+sqrt(k))*N
matrixCompMult(matX, matY) 矩阵相乘, 每个分量 自行相乘 r[j] = x[j]*y[j];
lessThan(vecX, vecY) 向量 每个分量比较 x < y
lessThanEqual(vecX, vecY) 向量 每个分量比较 x<=y
greaterThan(vecX, vecY) 向量 每个分量比较 x>y
greaterThanEqual(vecX, vecY) 向量 每个分量比较 x>=y
equal(vecX, vecY) 向量 每个分量比较 x==y
notEqual(vecX, vexY) 向量 每个分量比较 x!=y
any(bvecX) 只要有一个分量是true, 则true
all(bvecX) 所有分量是true, 则true
not(bvecX) 所有分量取反
发布了134 篇原创文章 · 获赞 37 · 访问量 9万+

Guess you like

Origin blog.csdn.net/lengyoumo/article/details/104231137