文章

shader案例-LOGO 闪光

shader案例-LOGO 闪光

LOGO 闪光

Flash

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
//Logo 闪光shader
Shader "Practice/FlashTestMat"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        //间隔
        _Interval("Interval",float) = 5.0
        _BrightNess("BrightNess",float)=10000000
        _BrightWidth("BrightWidth",Range(0,0.5))=0.1
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off
       // Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Interval;
            float _BrightNess;
            float _BrightWidth;
            struct appdata
            {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };

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

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
                return o;
            }
            //闪光
            //可以在这里丰富闪光的形状,其他逻辑等
            fixed4 blinn(float2 uv)
            {
                //当前时间段
                fixed delta = fmod(_Time.y,_Interval);
                float dis =saturate(_BrightWidth- abs(uv.x+0.5-delta));
                float cur_brightness=_BrightNess*dis;
                fixed3 rgb = fixed3(1,1,1)*cur_brightness;
                return fixed4(rgb,1);
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                //透明不闪光
                clip(col.a-0.1);
                fixed4 brighness = blinn(i.uv);
                col = col+brighness;
                return col;
            }
            ENDCG
        }
    }
    Fallback "UI/Default"
}

增加调试参数

为了策划方便调试,这里丰富下 shader 的参数,让光柱进行一定的偏移,给定闪光频率。 Flash

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
83
84
85
//Logo 闪光shader
Shader "Practice/FlashTestMat"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        //冷却
        _Cold("Cold",Range(0,3.0)) = 1.0
        //完成闪烁时间
        _FinishTime("FinishTime",Range(1.0,4.0))=2.0
        //光亮度
        _BrightNess("BrightNess",float)=100
        //光柱宽度
        _BrightWidth("BrightWidth",Range(0,0.5))=0.1
        //偏移速率
        _Rad("Rad",Range(0,3.14))=1.0 //偏移弧度
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off
       // Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _FinishTime;
            float _BrightNess;
            float _BrightWidth;
            float _Rad;
            float _Cold;
            struct appdata
            {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };

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

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
                return o;
            }
            //闪光
            fixed4 blinn(float2 uv)
            {
                //当前时间段
                fixed delta = _Time.y/(_FinishTime+_Cold);
                delta =delta - floor(delta);
                delta = delta*(_FinishTime+_Cold)/_FinishTime;
                //根据y轴做一定的偏移s
                fixed _offset_x=uv.y*tan(_Rad);
                float dis =saturate(_BrightWidth- abs(uv.x+0.5+_offset_x-delta));
                float cur_brightness=_BrightNess*dis;
                fixed3 rgb = fixed3(1,1,1)*cur_brightness;
                return fixed4(rgb,1);
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                //透明不闪光
                clip(col.a-0.1);
                fixed4 brighness = blinn(i.uv);
                col = col+brighness;
                return col;
            }
            ENDCG
        }
    }
    Fallback "UI/Default"
}

本文由作者按照 CC BY 4.0 进行授权