<commit_before>---
layout: post
title: Shader笔记
categories: unity shader
description: Shader笔记
---

# [Shader笔记](http://blog.lingkeyang.com/2017/09/28/shader)

## Properties支持的属性类型

|属性类型|默认值定义语法 |例子|
|-----------|:-----------:|:-----------:|
|Int|number|_Int("Int", Int)=2|
|Float|number|_Float("Float", Float)=1.5|
|Range(min, max)|number|_Range("Range", Range(0.0, 5.0))=3.0|
|Color|(number,number,number,number)|_Color("Color", Color)=(1,1,1,1)|
|Vector|(number,number,number,number)|_Vector("Vector", Vector)=(1,1,1,1)|
|2D|"defaulttexture"{}|_2D("2D", 2D)=""{}|
|Cube|"defaulttexture"{}|_Cube("Cube", Cube)="white"{}|
|3D|"defaulttexture"{}|_3D("3D", 3D)="black"{}|


- defaulttexture
    + white
    + black
    + gray
    + bump

- [MaterialPropertyDrawer](https://docs.unity3d.com/ScriptReference/MaterialPropertyDrawer.html "unity document")
    + \[KeywordEnum(None, Add, Multiply)\] ` ( [KeywordEnum(None, Add, Multiply)] _Overlay("Overlay mode", Float) = 0) `
    + \[Toggle\] ` ([Toggle] _Invert("Invert color?", Float) = 0) `
    + \[HideInInspector\] ` does not show the property value in the material inspector. `
    + \[Normal\] ` indicates that a texture property expects a normal-map. `

## SubShader语义块

```csharp
// unity shader
SubShader{
    // optional
    [Tags]

    // optional
    [RenderSetup]

    Pass{

    }

    // other passes
}

```

## SubShader渲染状态设置选项

|状态名称|设置指令|解释|
|---|:---:|:---:|
|Cull|Cull Back\|Front\|Off|设置剔除模式：剔除背面/正面/关闭|
|ZTest|ZTest Less\|Greater\|LEqual\|GEqual\|Equal\|NotEqual\|Always|设置深度测试时使用的函数|
|ZWrite|ZWrite On\|Off|开启/关闭深度写入|
|Blend|Blend SrcFactor DstFactor|开启并设置混合模式|


- BlendMode
    + Zero ` Blend factor is (0,0,0,0) `
    + One ` Blend factor is (1,1,1,1) `
    + DstColor ` Blend factor is (Rd, Gd, Bd, Ad) `
    + SrcColor ` Blend factor is (Rs, Gs, Bs, As) `
    + OnMinusDstColor ` Blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad) `
    + SrcAlpha ` Blend factor is (As, As, As, As) `
    + OneMinusSrcColor ` Blend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As) `
    + DstAlpha ` Blend factor is (Ad, Ad, Ad, Ad) `
    + OneMinusDstAlpha ` Blend factor is (1 - Ad, 1 - Ad, 1 - Ad, 1 - Ad) `
    + SrcAlphaSaturate ` Blend factor is (f,f,f,1) where f = min(As, 1 - Ad) `
    + OneMinusSrcAlpha ` Blend factor is (1 - As, 1 - As, 1 - As, 1 - As) `


## SubShader的标签类型

` Tags { "TagName1" = "Value1" "TagName2" = "Value2" } `

|标签类型|说明|例子|
|---|:--------:|:---:|
|Queue|控制渲染顺序，指定该物体属于哪一个渲染队列，通过这种方式可以保证所有的透明物体可以在所有不透明物体后面被渲染，我们也可以自定义使用的渲染队列来控制物体的渲染顺序|Tags { "Queue" = "Transparent" }|
|[RenderType](https://docs.unity3d.com/Manual/SL-ShaderReplacement.html)|对着色器进行分类：不透明着色器 or 透明着色器 etc|Tags { "RenderType" = "Transparent" }|
|DisableBatching|指明是否对该 *SubShader* 使用批处理| Tags { "DisableBatching" = "True" }|
|ForceNoShadowCasting|控制使用该 *SubShader* 的物体是否会投射阴影| Tags { "ForceNoShadowCasting" = "True" }|
|IgnoreProjector|控制使用该 *SubShader* 的物体是否受 **Projector** 的影响，通常用于半透明物体| Tags { "IgnoreProjector" = "True" }|
|CanUseSpriteAtlas|当该 *SubShader* 是用于sprites时，将该标签设为“False”| Tags { "CanUseSpriteAtlas" = "False" }|
|PreviewType|指明材质面板预览模式，默认为球形| Tags { "PreviewType" = "Plane"\|"SkyBox" }|

- Queue
    + Background
    + Geometry (default)
    + AlphaTest
    + Transparent
    + Overlay

- RenderType
    + Opaque
    + Transparent
    + TransparentCutout
    + Background
    + Overlay
    + TreeOpaque
    + TreeTransparentCutout
    + TreeBillboard
    + Grass
    + GrassBillboard

- 上述标签只能在 ***SubShader*** 中申明

## Pass语义块

```csharp
// unity shader
Pass{
    [Name]
    [Tags]
    [RenderSetup]
    // other code
}
```

- Pass中 ` Name "MyPassName" `

- 可通过 ` UsePass "ShaderName/MYPASSNAME" ` 来复用Pass代码 (PASSNAME 大写)

## Pass的标签类型

|标签类型|说明|例子|
|---|:---:|:---:|
|LightMode|光照模型| Tags { "LightMode" = "ForwardBase" }|
|RequireOptions|满足条件才渲染该Pass| Tags { "RequireOptions" = "SoftVegetation" }|


- [LightMode](https://docs.unity3d.com/Manual/SL-PassTags.html)
    + Always
    + ForwardBase
    + ForwardAdd
    + Deferred
    + ShadowCaster
    + MotionVectors
    + PrepassBase
    + PrepassFinal
    + Vertex
    + VertexLMRGBM
    + VertexLM

## 一个简单的 ***Surface Shader***

``` csharp
// unity shader
Shader "Custom/Simple Surface Shader"{
    SubShader{
        Tags { "RenderType" = "Opaque" }
        CGPROGRAM
        #pragma surface surf Lambert    // 定义光照模型
        struct Input{
            float4 color: COLOR;
        }
        void surf (Input IN, inout SurfaceOutput o){
            o.Albedo = 1;
        }
        ENDCG
    }
    Fallback "Diffuse"
}
```

- Cg/HLSL语法结构
- 定义在 *SubShader*（非 *Pass* ）语义块

## 一个简单的 ***Vertex/Fragment Shader***

``` csharp
// unity shader
Shader "Custom/Simple VertexFragment Shader"{
    SubShader{
        Pass{
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            float4 vert(float4 v : POSITION) : SV_POSITION{
                return mul(UNITY_MATRIX_MVP, v);
            }
            fixed4 frag() : SV_Target{
                return fixed4(1.0, 0.0, 0.0, 1.0);
            }
        }
    }
}
```

- Cg/HLSL语法结构

## 固定函数着色器<commit_msg>修正一处错误<commit_after>---
layout: post
title: Shader笔记
categories: unity shader
description: Shader笔记
---

# [Shader笔记](http://blog.lingkeyang.com/2017/09/28/shader)

## Properties支持的属性类型

|属性类型|默认值定义语法 |例子|
|-----------|:-----------:|:-----------:|
|Int|number|_Int("Int", Int)=2|
|Float|number|_Float("Float", Float)=1.5|
|Range(min, max)|number|_Range("Range", Range(0.0, 5.0))=3.0|
|Color|(number,number,number,number)|_Color("Color", Color)=(1,1,1,1)|
|Vector|(number,number,number,number)|_Vector("Vector", Vector)=(1,1,1,1)|
|2D|"defaulttexture"{}|_2D("2D", 2D)=""{}|
|Cube|"defaulttexture"{}|_Cube("Cube", Cube)="white"{}|
|3D|"defaulttexture"{}|_3D("3D", 3D)="black"{}|


- defaulttexture
    + white
    + black
    + gray
    + bump

- [MaterialPropertyDrawer](https://docs.unity3d.com/ScriptReference/MaterialPropertyDrawer.html "unity document")
    + \[KeywordEnum(None, Add, Multiply)\] ` ( [KeywordEnum(None, Add, Multiply)] _Overlay("Overlay mode", Float) = 0) `
    + \[Toggle\] ` ([Toggle] _Invert("Invert color?", Float) = 0) `
    + \[HideInInspector\] ` does not show the property value in the material inspector. `
    + \[Normal\] ` indicates that a texture property expects a normal-map. `

## SubShader语义块

```csharp
// unity shader
SubShader{
    // optional
    [Tags]

    // optional
    [RenderSetup]

    Pass{

    }

    // other passes
}

```

## SubShader渲染状态设置选项

|状态名称|设置指令|解释|
|---|:---:|:---:|
|Cull|Cull Back\|Front\|Off|设置剔除模式：剔除背面/正面/关闭|
|ZTest|ZTest Less\|Greater\|LEqual\|GEqual\|Equal\|NotEqual\|Always|设置深度测试时使用的函数|
|ZWrite|ZWrite On\|Off|开启/关闭深度写入|
|Blend|Blend SrcFactor DstFactor|开启并设置混合模式|


- BlendMode
    + Zero ``` Blend factor is (0,0,0,0) ```
    + One ` Blend factor is (1,1,1,1) `
    + DstColor ` Blend factor is (Rd, Gd, Bd, Ad) `
    + SrcColor ` Blend factor is (Rs, Gs, Bs, As) `
    + OnMinusDstColor ` Blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad) `
    + SrcAlpha ` Blend factor is (As, As, As, As) `
    + OneMinusSrcColor ` Blend factor is (1 - Rs, 1 - Gs, 1 - Bs, 1 - As) `
    + DstAlpha ` Blend factor is (Ad, Ad, Ad, Ad) `
    + OneMinusDstAlpha ` Blend factor is (1 - Ad, 1 - Ad, 1 - Ad, 1 - Ad) `
    + SrcAlphaSaturate ` Blend factor is (f,f,f,1) where f = min(As, 1 - Ad) `
    + OneMinusSrcAlpha ` Blend factor is (1 - As, 1 - As, 1 - As, 1 - As) `


## SubShader的标签类型

` Tags { "TagName1" = "Value1" "TagName2" = "Value2" } `

|标签类型|说明|例子|
|---|:--------:|:---:|
|Queue|控制渲染顺序，指定该物体属于哪一个渲染队列，通过这种方式可以保证所有的透明物体可以在所有不透明物体后面被渲染，我们也可以自定义使用的渲染队列来控制物体的渲染顺序|Tags { "Queue" = "Transparent" }|
|[RenderType](https://docs.unity3d.com/Manual/SL-ShaderReplacement.html)|对着色器进行分类：不透明着色器 or 透明着色器 etc|Tags { "RenderType" = "Transparent" }|
|DisableBatching|指明是否对该 *SubShader* 使用批处理| Tags { "DisableBatching" = "True" }|
|ForceNoShadowCasting|控制使用该 *SubShader* 的物体是否会投射阴影| Tags { "ForceNoShadowCasting" = "True" }|
|IgnoreProjector|控制使用该 *SubShader* 的物体是否受 **Projector** 的影响，通常用于半透明物体| Tags { "IgnoreProjector" = "True" }|
|CanUseSpriteAtlas|当该 *SubShader* 是用于sprites时，将该标签设为“False”| Tags { "CanUseSpriteAtlas" = "False" }|
|PreviewType|指明材质面板预览模式，默认为球形| Tags { "PreviewType" = "Plane"\|"SkyBox" }|

- Queue
    + Background
    + Geometry (default)
    + AlphaTest
    + Transparent
    + Overlay

- RenderType
    + Opaque
    + Transparent
    + TransparentCutout
    + Background
    + Overlay
    + TreeOpaque
    + TreeTransparentCutout
    + TreeBillboard
    + Grass
    + GrassBillboard

- 上述标签只能在 ***SubShader*** 中申明

## Pass语义块

```csharp
// unity shader
Pass{
    [Name]
    [Tags]
    [RenderSetup]
    // other code
}
```

- Pass中 ` Name "MyPassName" `

- 可通过 ` UsePass "ShaderName/MYPASSNAME" ` 来复用Pass代码 (PASSNAME 大写)

## Pass的标签类型

|标签类型|说明|例子|
|---|:---:|:---:|
|LightMode|光照模型| Tags { "LightMode" = "ForwardBase" }|
|RequireOptions|满足条件才渲染该Pass| Tags { "RequireOptions" = "SoftVegetation" }|


- [LightMode](https://docs.unity3d.com/Manual/SL-PassTags.html)
    + Always
    + ForwardBase
    + ForwardAdd
    + Deferred
    + ShadowCaster
    + MotionVectors
    + PrepassBase
    + PrepassFinal
    + Vertex
    + VertexLMRGBM
    + VertexLM

## 一个简单的 ***Surface Shader***

``` csharp
// unity shader
Shader "Custom/Simple Surface Shader"{
    SubShader{
        Tags { "RenderType" = "Opaque" }
        CGPROGRAM
        #pragma surface surf Lambert    // 定义光照模型
        struct Input{
            float4 color: COLOR;
        }
        void surf (Input IN, inout SurfaceOutput o){
            o.Albedo = 1;
        }
        ENDCG
    }
    Fallback "Diffuse"
}
```

- Cg/HLSL语法结构
- 定义在 *SubShader*（非 *Pass* ）语义块

## 一个简单的 ***Vertex/Fragment Shader***

``` csharp
// unity shader
Shader "Custom/Simple VertexFragment Shader"{
    SubShader{
        Pass{
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            float4 vert(float4 v : POSITION) : SV_POSITION{
                return mul(UNITY_MATRIX_MVP, v);
            }
            fixed4 frag() : SV_Target{
                return fixed4(1.0, 0.0, 0.0, 1.0);
            }
            ENDCG
        }
    }
}
```

- Cg/HLSL语法结构

## 固定函数着色器
{'fraction_non_alphanumeric': 0.17280813214739518, 'fraction_numerical': 0.016029713615482357, 'mean_word_length': 2.9937548790007806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4793741', 'n_tokens_mistral': 4449, 'n_tokens_neox': 4525, 'n_words': 774}