UnityShader的形态
SurfaceShader
💡 这是一种在早期Unity版本中被鼓励使用的Shader,在Unity中创建Shader代码时,默认创建的就是SurfaceShader 💡 图形渲染管线能够识别的就只有两种Shader,VertexShader和FragmentShader SurfaceShader是在Vertex and Fragment Shader基础之上进行的一种包装 最终Unity引擎还是会把SurfaceShader编译成Vertex and Fragment Shader
Vertex and Fragment Shader
💡 顶点片元着色器 渲染管线最终可以执行并且处理的两种基本的Shader 可以使用Cg和GLSL语言去编写 它是镶嵌在ShaderLab中的代码片段
Fixed Function Shader
💡 固定功能Shader,在可编程渲染管线出现之前,很多光照都会在硬件级实现 对于这样的功能,一般都会用固定功能的Shader实现,这类Shader相对来说比较保守 功能也很固定,例如开关光照之类的,但是能够在绝大多数的设备上运行
ShaderLab基本结构
shader "name”{
[Properties]
SubShaders
[FallBack]
}
Properties
💡 在Unity的Material Inspector面板中,Shader可以定一个参数列表,以供美术或者开发人员进行调整Shader的参数
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShaders
💡 专门为GPU渲染编写的具体算法片段
一个Shader中,可以有多个SubShader,且至少要有一个SubShader 显卡去处理的时候,只能选择一个SubShader处理 作为Shader有可能存在某一种算法某一种指令在当前硬件不能得到支持 当一个Shader文件被执行运行时,会去检测当前硬件能不能够很好的完整支持 如果第一个SubShader能够很好的被完整支持,就会选择第一个SubShader执行 当不确定,或者不能很好的进行支持时,就选选择第二个SubShader进行执行
💡 所以在设计SubShader时,越往下编写的,运算指令和算法就要越简化,为了支持更多不兼容的硬件
SubShader
{
Tags { "ExampleSubShaderTagKey" = "ExampleSubShaderTagValue" }
LOD 100
// ShaderLab commands that apply to the whole SubShader go here.
Pass
{
Name "ExamplePassName"
Tags { "ExamplePassTagKey" = "ExamplePassTagValue" }
// ShaderLab commands that apply to this Pass go here.
// HLSL code goes here.
}
}
Fallback
💡 如果所有SubShader都不能被当前的硬件执行,那么就会自动进入Fallback回滚
Build-In Shader
- Unlit. This is just a texture, not affected by any lighting. 不发光。这只是一个纹理,不被任何光照果响
- VertexLit. 顶点光照
- Diffuse.漫反射
- Normal mapped. This is a bit more expensive than Diffuse: it adds one more texture (normal map), and a couple of shader instructions. 法线贴图,比没反射更昂贵:增加了一个或更多纹理(法线见图)和几个著色器结构
- Specular. This adds specular highlight calculation. 高光。这增加了特殊的高光计算
- Normal Mapped Specular. Again, this is a bit more expensive than Specular. 高光法线贴图。这比高光更昂贵一点
- Parallax Normal mapped. This adds parallax normal-mapping calculation. 视差法线贴图。这增加了视差法贴图计算
- Parallax Normal Mapped Specular. This adds both parallax normal-mapping and specular highlight calculation. 视差高光法织圖。这增加了视差法线贴图和資面高光计算
CustomEditor
💡 自定义编辑器 可以使用CustomEditor “name” 自定义Shader属性展现的方式
Shader "example"{
// properties and subshaders here..•
CuatomEditor "MyGustomEditoz"
}


...