呼吸灯效果

作者:追风剑情 发布于:2024-3-19 19:04 分类:Unity3d

Shader

//呼吸灯效果
Shader "Custom/BreathLight"
{
    Properties
    {
        //主纹理
        _MainTex ("Texture", 2D) = "white" {}
        //漫反射颜色
        _Albedo ("Albedo", Color) = (1,1,1,1)
        //呼吸灯颜色
        _LightColor ("Light Color", Color) = (1,1,1,1)
        //呼吸速度
        _Speed ("Speed", Float) = 3
        //呼吸灯开关
        [Toggle(ENABLED)] 
        _Enabled ("Enabled", Float) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            //生成多版本shader
			#pragma multi_compile __ ENABLED

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

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

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Albedo;
            fixed4 _LightColor;
            half _Speed;

            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
            {
                fixed4 col = tex2D(_MainTex, i.uv) * _Albedo;

                #ifdef ENABLED
                fixed t = sin(_Time.y * _Speed);
                //映射到区间[0,1]
                t = (t + 1) / 2;
                col = lerp(col, _LightColor, t);
                #endif

                return col;
            }
            ENDCG
        }
    }
}

1111.png

运行效果
11111113.gif

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号