Shader Basics

                                                   Shader Basics

This is the first time I published something on a blog. This is some of the things I have seen when I learned the basics of shader.

1. Properties feature
Properties that are generally placed to the outside world to modify, such as Color, Texture, 2D, Rect and other properties
Format: _xx{"attribute introduction", attribute category}=value
Variable name ("name displayed in the inspector"  ,Range(min,max))=default value //displayed as a slider
name ("display name" ,Float)=default value//display as a value
2.SubShader (sub shader)
Tag tag (content as follows)
label type value illustrate
Queue Background/Geometry/AlphaTest/Transparent/Overlay The corresponding value is 1000/2000/2450/3000/4000
RenderType Opaque/Transparent/TransparentCutout/
Background/Overlay/TreeOpaque/TreeTransparentCutout/
TreeBillboard/Grass/GrassBillboard
Used for alternate rendering and getting scene depth and normal information
DisableBatching true/false Disable batching
ForceNoShadowCasting true/false No shadow cast
IgnoreProjector true/false Ignore Projector, usually used for translucent objects
CanUseSpriteAtlas true/false Use false when this SubShader is used for Sprite
PreviewType Plane/SkyBox Preview shape

Common Render State Setting Instructions
Cull Back | Front | Off Set polygon culling mode
ZTest (Less | Greater | LEqual | GEqual | Equal | NotEqual | Always) sets the depth test mode
ZWrite On | Off sets deep write mode
Blend color blending mode
The following are common commands for Blend
One(1)
Zero(0)
SrcColor (source color)
SrcAlpha (source transparency)
DstColor (target color)
DstAlpha (target transparency)
OneMinusSrcColor (1 - source color)
OneMinusSrcAlpha (1-source transparency)
OneMinusDstColor (1 - target color)
OneMinusDstAlpha (1 - target transparency)
 Blend Off turns off the color blending mode
 Blend One One Linear Fade
 Blend OnMinusDstColor One Dropout
 Blend Op refers to the use of operators to calculate mixed color values
operator has 
Add (source color + destination color) 
Sub (source color - destination color) 
RevSub (destination color - source color) 
Min (take the smaller of the destination color and the source color)
Max (take the greater of the destination color and the source color)
 Blend On Max
 Blend One One The superposition of the above two lines of code is to brighten
 Blend On Min
 Blend One One is the same as darkening
 Blend DstColor SrcColor Double Multiply (2X Multiply)
 Blend DstColor Zero Multiply Multiply 
 Blend OneMinusDstColor One Soft Additive 
 Blend SrcAlpha OneMinusSrcAlpha normal mode (transparency blending) 
Final color = SrcFactor * source color (calculated fragment color value) + DstFactor * destination color (color value of color buffer)
3.Pass instructions
Name "XXXX", default uppercase, even lowercase will be changed to uppercase 
The main function is to use UsePass to call this named channel in other Shaders
GrabPass {"TextureName"}抓取屏幕并生成纹理,TextureName是该纹理的名字,默认为_GrabTexure
Tag标签
内容如下图
LightModeAlways/ForwardBase/ForwardAdd/Deferred/ShadowCaster/
MotionVectors/PrepassBase/PrepassFinal/Vertex/VertexLMRGBM/VertexLM详见官方文档PassFlagsOnlyDirectional仅传送主平行光、环境光和LightProbe的光照数据RequireOptionsSoftVegetation满足给定条件才渲染该Pass
4.#pragma指令
target 2.0/2.5(default)/3.0/3.5(es3.0)/
4.0/4.5(es3.1)/4.6(gl4.1)/5.0
 
geometry   用于geometry shader,编译目标自动设为4.0
hull   用于tessellation shader,编译目标自动设为4.6
domain   同hull
vertex 顶点函数名 定义顶点函数入口
fragment 片元函数名 定义片元函数入口
only_renderers d3d9/d3d11/opengl/gles/xbox360/p3/flash 指定渲染平台
exclude_renderers 同only_renderers 排除指定平台
multi_compile_fwdadd_fullshadows   开启ForwardAdd中的阴影效果
multi_compile_fwdbase   用于ForwardBase,保证光照衰减等光照变量被正确赋值
multi_compile_fwdadd   用于ForwardAdd,保证光照衰减等光照变量被正确获取

5.Unity内置函数
float4 UnityObjectToClipPos(float3 pos) 将顶点坐标从模型空间转换到裁剪空间
float3 UnityObjectToViewPos(float3 pos) 将顶点坐标从模型空间转换到观察空间
TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw) 根据贴图的tilling和offset值计算uv,需要定义变量“贴图名称_ST”,例:_MainTex_ST
ComputeScreenPos(float4 pos) 计算顶点在屏幕空间的位置
float3 WorldSpaceViewDir(float4 v) 输入顶点位置,输出世界空间下顶点到摄像机的方向
float3 UnityWorldSpaceViewDir(float4 v) 输入一个世界顶点,输出世界空间下该点到摄像机的方向
float3 ObjSpaceViewDir(float4 v) 输入顶点位置,输出模型空间下顶点到摄像机的方向
float3 WorldSpaceLightDir(float4 v) 仅用于前向渲染,输入顶点位置,输出世界空间下顶点到光源的方向
float3 UnityWorldSpaceLightDir(float4 v) 仅用于前向渲染,输入一个世界坐标,输出世界空间下该点到光源的方向
float3 ObjSpaceLightDir(float4 v) 仅用于前向渲染,输入顶点位置,输出模型空间下顶点到光源的方向
float3 UnityObjectToWorldNormal(float3 norm) 将法线方向从模型空间转换到世界空间
float3 UnityObjectToWorldDir(in float3 dir) 将方向从模型空间转换到世界空间
float3 UnityWorldToObjectDir(float3 dir) 将方向从世界空间转换到模型空间
SHADOW_COORDS(i) 放在FrowardBase渲染路径下顶点函数的输出结构体中,需引用”AutoLight.cginc”,i是可用寄存器索引
用于声明阴影纹理的坐标
TRANSFER_SHADOW(o) 计算结果即为阴影纹理的坐标,o为输出结构体,需放在顶点函数中
SHADOW_ATTENUATION(i) 计算阴影衰减值,需放在片元函数中,i为片元函数的输入结构体,即上式中的o
注意:以上三个函数使用时,需保证顶点函数的输入结构体变量为v,结构体的顶点坐标为vertex,输出结构体中的顶点坐标为pos
float2 ParallaxOffset (half h, half height, half3 viewDir) 为视差法线贴图计算UV偏移
fixed Luminance (fixed3 c) 将颜色转换为亮度(灰度)
fixed3 DecodeLightmap (fixed4 color) 从Unity光照贴图解码颜色(基于平台为RGBM 或dLDR)
float4 EncodeFloatRGBA (float v) 为储存低精度的渲染目标,编码[0..1)范围的浮点数到RGBA颜色
float DecodeFloatRGBA (float4 enc) 解码RGBA颜色到float
float2 EncodeFloatRG (float v) 类似EncodeFloatRGBA
float DecodeFloatRG (float2 enc) 类似DecodeFloatRGBA
float2 EncodeViewNormalStereo (float3 n) 编码视图空间法线到在0到1范围的两个数
float3 DecodeViewNormalStereo (float4 enc4) 从enc4.xy解码视图空间法线

6、Unity内置变量

函数 说明
UNITY_MATRIX_MVP 将顶点/方向从模型空间转换到裁剪空间
UNITY_MATRIX_MV 将顶点/方向从模型空间转换到观察空间
UNITY_MATRIX_V 将顶点/方向从世界空间转换到观察空间
UNITY_MATRIX_P 将顶点/方向从观察空间转换到裁剪空间
UNITY_MATRIX_VP 将顶点/方向从世界空间转换到裁剪空间
UNITY_MATRIX_T_MV UNITY_MATRIX_MV的转置矩阵
UNITY_MATRIX_IT_MV UNITY_MATRIX_MV的逆转置矩阵,可将法线从模型空间转换到观察空间
_Object2World 将顶点/方向从模型空间转换到世界空间,已修改为unity_ObjectToWorld
_World2Object 将顶点/方向从世界空间转换到模型空间,已修改为unity_WorldToObject
_WorldSpaceCameraPos 当前渲染的相机在世界空间中的位置
_ProjectionParams x=1(-1),y=near,z=far,w=1+1/far,near和far是近远裁屏幕到相机的距离
_ScreenParams x=width,y=height,z=1+1/width,w=1+1/height
_ZBufferParams x=1-far/near,y=far/near,z=x/far,w=y/far
unity_OrthoParams x=正交投影相机的宽度,y=正交投影相机的高度,z未定义,w=1(正交相机)/0(透视相机)
unity_CameraProjection 该相机的投影矩阵
unity_CameraInvProjection 该相机的投影矩阵的逆矩阵
unity_CameraWorldClipPlanes[6] 该相机的6个裁剪屏幕在世界空间下的等式,存储顺序:左右下上近远
UNITY_LIGHTMODEL_AMBIENT 前向渲染下的环境光信息
_LightColor0 光源颜色
_WorldSpaceLightPos0 光源方向
_LightMatrix0 世界空间到光源空间的变换矩阵
unity_4LightPosX0 仅用于ForwardBase,前4个非重要光源的世界空间下的x坐标
unity_4LightPosY0 仅用于ForwardBase,前4个非重要光源的世界空间下的y坐标
unity_4LightPosZ0 仅用于ForwardBase,前4个非重要光源的世界空间下的z坐标
unity_4LightAtten0 仅用于ForwardBase,前4个非重要光源的衰减因子
unity_LightColor 数组,仅用于ForwardBase,前4个非重要光源的颜色
_Time (t/20,t,2t,3t)
_SinTime (t/8,t/4,t/2,t)
_CosTime (t/8,t/4,t/2,t)
unity_DeltaTime (dt, 1/dt, smoothDt, 1/smoothDt)
_CameraDepthTexture 深度纹理,使用SAMPLE_DEPTH_TEXTURE函数获取深度值,使用Linear01Depth线性化深度值
_CameraDepthNormalsTexture 深度法线纹理,使用DecodeViewNormalStereo(tex2D(_CameraDepthNormalsTexture,i.uv).xy)获取[-1,1]之间的法线值
unity_SpecCube0 天空盒贴图
unity_SpecCube0_HDR 用来解码天空盒颜色:DecodeHDR(UNITY_SAMPLE_TEXCUBE(unity_SpecCube0,i.worldRefl),unity_SpecCube0
7.宏定义
UNITY_UV_STARTS_AT_TOP  判断当前平台是否DirectX,一般配合_MainTex_TexelSize.y(负数说明开启了抗锯齿)进行uv反转
USING_DIRECTIONAL_LIGHT 是否使用平行光
8.Category 
 是对其下的命令的逻辑分组。例如,如果你的shader有多个subshader,并且每一个subshader都需要关闭雾效和以及一样的blending mode ,就可以使用Category:

如果你在学shader的话,cgprogramminginunity这本书作为入门是比较合适的,最好不要翻译本(当然你也找不到),锻炼一丝丝英语能力和查找资料的能力,这个随便找就能能找到pdf,虽然有些用法有点过时,不过入门来说是比较好的,同时还锻炼一些找bug的能力,因为照着打你肯定会发现warning(别问我怎么知道的=-=),望于君可堪

参考资料:https://blog.csdn.net/qq_34469717/article/details/78427241
参考资料:https://www.cnblogs.com/HangZhe/p/7220969.html
参考文献:CgProgramming in unity
                                                                                                                                                                                                      时间:2018年4月21日13:09:55
                                                                                                                                                                                                        作者:JeffreyCsj

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324671095&siteId=291194637