UnityShader-ShaderLab

UnityShader-ShaderLab

本文用于放置一些ShaderLab中的基本概念和属性速查表

Unity Shader的基本框架

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Shader "MyShader" {
Properties {
_MyTexture ("My Texture", 2D) = "white" { }
// place other properties here, such as colors or vectors.
}
SubShader {
// place the shader code here for your:
// - surface shader,
// - vertex and program shader, or
// - fixed function shader
}
SubShader {
// a simpler version of the subshader above goes here.
// this version is for supporting older graphics cards.
}
}

Properties

image-20210819195901522

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Float类型,下面对应变量可以用flaot,half,fixed  
_Name("Inspector Name", Float) = defaultValue
//Float类型,可以用一个滑动条控制范围,下面对应变量可以用float,half,fixed
_Name("Inspector Name", Range(min, max)) = defaultValue
//颜色类型,下面对应变量可以用float4,half4,fixed4,如果是颜色,尽量fixed4
_Name("Inspector Name", Color) = (defaultValue.r, defaultValue.g, defaultValue.b, defaultValue.a)
//2D纹理类型,默认纹理可以为空,白,黑,灰,凹凸,下面对应变量sampler2D
_Name("Inspector Name", 2D) = "" / "white" / "black" / "gray" / "bump"{options}
//长方形纹理,非2次方大小的纹理,其同上
_Name("Inspector Name", Rect) = "" / "white" / "black" / "gray" / "bump"{options}
//立方体贴图CubeMap
_Name("Inspector Name", Cube) = "" {options}
//传递一个Vector4向量
_Name("Inspector Name", Vector) = (defaultValue.x, defaultValue.y, defaultValue.z, defaultValue.w)

//注:上面纹理后面{}里面是一些纹理TexGen,LightmapMode光照模式等内容

SubShader

1
2
3
4
5
6
7
SubShader {
[Tags]
[RenderSetup]
Pass {

}
}

状态设置

image-20210819200033406

标签类型

image-20210819200122349

Pass

1
2
3
4
5
6
Pass {
[Name]
[Tags]
[RenderSetup]
// Other code
}

标签类型

image-20210819200315169

特殊Pass:

  • UsePass:服用其他Unity Shader中的Pass,必须使用大写形式的名字

    1
    UsePass "MyShader/MYPASSNAME"
  • GrabPass:抓取屏幕并将结果存储在一张纹理中,以用于后续的Pass处理

Fallback

1
Fallback "name"

备用Shader,同时会影响阴影的投射


UnityShader-ShaderLab
http://example.com/2021/07/12/UnityShader-ShaderLab/
作者
Chen Shuwen
发布于
2021年7月12日
许可协议