文章

shader入门基础-动画

shader入门基础-动画

Unity 内置时间变量

名称类型描述
_Timefloat4t 是自然该场景加载开始所经过的时间,(t/20,t,2t,3t)
_SinTimefloat4t 是时间的正弦值,(t/8,t/4,t/2,t)
_CosTimefloat4t 是时间的余弦值,(t/8,t/4,t/2,t)
unity_DeltaTimefloat4dt 是时间增量,(dt,1/dt,smoothDt,1/smoothDt)

纹理动画

序列动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Shader "Practice/SequenceAnim"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _RowCount("行数",int)=4
        _ColCount("列数",int)=4
        _Speed("播放速度",Range(0,100))=1.0
    }
    SubShader
    {
        // No culling or depth
        Tags {"Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}

        Pass
        {
            Tags {"LightModel"="ForwardBase"}
            Blend SrcAlpha OneMinusSrcAlpha
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Speed;
            half _RowCount;
            half _ColCount;
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv,_MainTex) ;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float length = _Speed*_Time.y;
                half curRow = floor(length/_ColCount);
                half curCol = floor(length-curRow*_ColCount);
                half2 uv =float2(i.uv.x/_ColCount,i.uv.y/_RowCount);
                uv.x+= curCol/_ColCount;
                uv.y-= curRow/_RowCount;
                fixed4 col =tex2D(_MainTex,uv);
                return  col;
            }
            ENDCG
        }
    }
}

滚动的背景

滚动的背景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Shader "Practice/BgAnim"
{
    Properties
    {
        _FirstBG ("FirstBG", 2D) = "white" {}
        _SecondBG ("FirstBG", 2D) = "white" {}
        _ScrollX("_ScrollX",float) = 1
        _ScrollX2("_ScrollX2",float)=1.1
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            sampler2D _FirstBG;
            sampler2D _SecondBG;
            fixed4 _FirstBG_ST;
            fixed4 _SecondBG_ST;
            float _ScrollX2;
            float _ScrollX;
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy =TRANSFORM_TEX(v.uv,_FirstBG);
                o.uv.zw = TRANSFORM_TEX(v.uv,_SecondBG);
                return o;
            }


            fixed4 frag (v2f i) : SV_Target
            {
                float2 uv_offset = frac(float2(_ScrollX,0.0)*_Time.y);
                float2 uv_offset2 = frac(float2(_ScrollX2,0.0)*_Time.y);
                fixed4 col = tex2D(_FirstBG, i.uv.xy+uv_offset);
                fixed4 col2 =tex2D(_SecondBG,i.uv.zw+uv_offset2);
                fixed4 final_col=lerp(col,col2,col2.a);
                return final_col;
            }
            ENDCG
        }
    }
}

顶点动画

DisableBatching: 批处理会合并所有相关的模型,而这些模型各自的模型空间会丢失。我们需要在物体的模型空间下对顶点位置进行偏移,因此需要取消 批处理操作。

滚动的背景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Properties {
		_MainTex ("Main Tex", 2D) = "white" {}
		_Color ("Color Tint", Color) = (1, 1, 1, 1)
		_Magnitude ("Distortion Magnitude", Float) = 1
 		_Frequency ("Distortion Frequency", Float) = 1
 		_InvWaveLength ("Distortion Inverse Wave Length", Float) = 10
 		_Speed ("Speed", Float) = 0.5
	}
	SubShader {
		// Need to disable batching because of the vertex animation
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"}

		Pass {
			Tags { "LightMode"="ForwardBase" }

			ZWrite Off
			Blend SrcAlpha OneMinusSrcAlpha
			Cull Off

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;
			fixed4 _Color;
			float _Magnitude;
			float _Frequency;
			float _InvWaveLength;
			float _Speed;

			struct a2v {
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
			};

			v2f vert(a2v v) {
				v2f o;

				float4 offset;
				offset.yzw = float3(0.0, 0.0, 0.0);
				offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
				o.pos = UnityObjectToClipPos(v.vertex + offset);

				o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
				o.uv +=  float2(0.0, _Time.y * _Speed);

				return o;
			}

			fixed4 frag(v2f i) : SV_Target {
				fixed4 c = tex2D(_MainTex, i.uv);
				c.rgb *= _Color.rgb;

				return c;
			}

			ENDCG
		}
	}
	FallBack "Transparent/VertexLit"

广告牌技术

根据视角方向来旋转一个被纹理着色的多边形,使得多边形看起来好像总是面对摄像机。广告牌技术的本质是构建旋转矩阵。 广告牌

为了避免使用模型空间的中心作为锚点,我们可以利用顶点颜色存储每个顶点到锚点的距离值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Shader "Practice/BillBoard"
{
   Properties {
		_MainTex ("Main Tex", 2D) = "white" {}
		_Color ("Color Tint", Color) = (1, 1, 1, 1)
		_VerticalBillboarding ("Vertical Restraints", Range(0, 1)) = 1
	}
	SubShader {
		// 关闭批处理,批处理会导致模型各自的模型空间丢失
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"}

		Pass {
			Tags { "LightMode"="ForwardBase" }

			ZWrite Off
			Blend SrcAlpha OneMinusSrcAlpha
            //关闭剔除让广告牌每个面都能显示
			Cull Off

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#include "Lighting.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;
			fixed4 _Color;
			fixed _VerticalBillboarding;

			struct a2v {
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
			};

			v2f vert (a2v v) {
				v2f o;
				//模型空间位置原点
				float3 center = float3(0, 0, 0);
                //将摄像机位置转换到模型空间坐标
				float3 viewer = mul(unity_WorldToObject,float4(_WorldSpaceCameraPos, 1));
				// 摄像机指向原点的方向作为法线方向
				float3 normalDir = viewer - center;
                //_VerticalBillboarding == 1 我们使用摄像机指向原点的方向作为法线方向
                //_VerticalBillboarding == 0 则固定方向作为法线方向
				normalDir.y =normalDir.y * _VerticalBillboarding;
				normalDir = normalize(normalDir);
				// 获取up方向
				float3 upDir = abs(normalDir.y) > 0.999 ? float3(0, 0, 1) : float3(0, 1, 0);
                //叉积后获取指向右方的向量
				float3 rightDir = normalize(cross(upDir, normalDir));
				upDir = normalize(cross(normalDir, rightDir));

				// 使用3个正焦急矢量做相对于锚点旋转
				float3 centerOffs = v.vertex.xyz - center;
				float3 localPos = center + rightDir * centerOffs.x + upDir * centerOffs.y + normalDir * centerOffs.z;

				o.pos = UnityObjectToClipPos(float4(localPos, 1));
				o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);

				return o;
			}

			fixed4 frag (v2f i) : SV_Target {
				fixed4 c = tex2D (_MainTex, i.uv);
				c.rgb *= _Color.rgb;

				return c;
			}

			ENDCG
		}
	}
	FallBack "Transparent/VertexLit"
}

对包含顶点动画的物体添加阴影

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
		Pass {
            //阴影投射pass
			Tags { "LightMode" = "ShadowCaster" }

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#pragma multi_compile_shadowcaster

			#include "UnityCG.cginc"

			float _Magnitude;
			float _Frequency;
			float _InvWaveLength;
			float _Speed;

			struct v2f {
			    V2F_SHADOW_CASTER;
			};

			v2f vert(appdata_base v) {
				v2f o;

				float4 offset;
				offset.yzw = float3(0.0, 0.0, 0.0);
				offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
				v.vertex = v.vertex + offset;

				TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)

				return o;
			}

			fixed4 frag(v2f i) : SV_Target {
			    SHADOW_CASTER_FRAGMENT(i)
			}
			ENDCG
		}
本文由作者按照 CC BY 4.0 进行授权