shader案例-LOGO 闪光

LOGO 闪光

image

//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 的参数,让光柱进行一定的偏移,给定闪光频率。
image

//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"
}

posted @ 2025-07-22 14:58  晓叔  阅读(11)  评论(0)    收藏  举报