Unity Shader:闪烁

2 篇文章 0 订阅
文章介绍了如何通过自定义UnityShader实现UI闪烁、物体闪烁和半透明闪烁效果,分别针对UI、物体和纯色透明对象,通过改变颜色和透明度随时间变化来实现闪烁。
摘要由CSDN通过智能技术生成

还是一样的分为UI闪烁和物体闪烁,其中具体可分为:UI闪烁、物体闪烁与半透明闪烁

1,UI闪烁

对于UI 还是一样的,改写UI本身的shader:

Shader "UI/YydUIShanShder"
{
	Properties
	{
		[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
	_Color("Tint", Color) = (1,1,1,1)

		_StencilComp("Stencil Comparison", Float) = 8
		_Stencil("Stencil ID", Float) = 0
		_StencilOp("Stencil Operation", Float) = 0
		_StencilWriteMask("Stencil Write Mask", Float) = 255
		_StencilReadMask("Stencil Read Mask", Float) = 255

		_ColorMask("Color Mask", Float) = 15
		[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
	
	    [Toggle]_Switch("Switch", Float) = 0

		_value("Speed",Range(1,3)) = 1
		_overlayCol("OtherColor",Color) = (0.2146,1,0,0.6039)
	
	}

		SubShader
	{
		Tags
	{
		"Queue" = "Transparent"
		"IgnoreProjector" = "True"
		"RenderType" = "Transparent"
		"PreviewType" = "Plane"
		"CanUseSpriteAtlas" = "True"
	}

		Stencil
	{
		Ref[_Stencil]
		Comp[_StencilComp]
		Pass[_StencilOp]
		ReadMask[_StencilReadMask]
		WriteMask[_StencilWriteMask]
	}

		Cull Off
		Lighting Off
		ZWrite Off
		ZTest[unity_GUIZTestMode]
		Blend SrcAlpha OneMinusSrcAlpha
		ColorMask[_ColorMask]

		Pass
	{
		Name "Default"
		CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0

#include "UnityCG.cginc"
#include "UnityUI.cginc"

#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP

		struct appdata_t
	{
		float4 vertex   : POSITION;
		float4 color    : COLOR;
		float2 texcoord : TEXCOORD0;
		UNITY_VERTEX_INPUT_INSTANCE_ID
	};

	struct v2f
	{
		float4 vertex   : SV_POSITION;
		fixed4 color : COLOR;
		float2 texcoord  : TEXCOORD0;
		float4 worldPosition : TEXCOORD1;
		half4  mask : TEXCOORD2;
		UNITY_VERTEX_OUTPUT_STEREO
	};

	sampler2D _MainTex;
	fixed4 _Color;
	fixed4 _TextureSampleAdd;
	float4 _ClipRect;
	float4 _MainTex_ST;
	float _UIMaskSoftnessX;
	float _UIMaskSoftnessY;


	float _value;
	float4 _overlayCol;

	half _Switch;


	v2f vert(appdata_t v)
	{
		v2f OUT;
		UNITY_SETUP_INSTANCE_ID(v);
		UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
		float4 vPosition = UnityObjectToClipPos(v.vertex);
		OUT.worldPosition = v.vertex;
		OUT.vertex = vPosition;

		float2 pixelSize = vPosition.w;
		pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));

		float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
		float2 maskUV = (v.vertex.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
		OUT.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
		OUT.mask = half4(v.vertex.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy)));

		OUT.color = v.color * _Color;
		return OUT;
	}

	fixed4 frag(v2f IN) : SV_Target
	{
		half4 color = IN.color * (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd);

#ifdef UNITY_UI_CLIP_RECT
		half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
		color.a *= m.x * m.y;
#endif

#ifdef UNITY_UI_ALPHACLIP
		clip(color.a - 0.001);
#endif
		half _g = dot(color.rgb, unity_ColorSpaceLuminance);

		//这部分是自定义处理的
		if (_Switch == 1)
		{
			fixed4 col = _overlayCol;
			float t = abs(sin(_Time.w * _value));
			col.a = lerp(0, _overlayCol.a, t);

			color = lerp(color, col, col.a);//将col叠加在color上
		}

		return color;
	}
		ENDCG
	}
	}
}

  实现原理我们可以理解为,在原有的图片上,叠加了一层会随着时间变化而变化透明度的纯色。

  和原image的shader相比,基本没做太大改动。只是为了实现闪烁效果,自定义了如下部分:

//这部分是自定义处理的
		if (_Switch == 1)
		{
			fixed4 col = _overlayCol;
			float t = abs(sin(_Time.w * _value));
			col.a = lerp(0, _overlayCol.a, t);

			color = lerp(color, col, col.a);//将col叠加在color上
		}

2,物体闪烁

  原理相同,实现代码如下:

Shader "YaDong/YydShanShader"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}

        [Header(twinkle)]
        _twinkleCol("闪光色",Color) = (0.7735,0.7735,0.7735,1)
        _twinkleValue("闪烁 Speed",float) = 1


       _middleValue("中间值",float) = 0.5
       _volatilityValue("波动",float) = 0.6
     
    }
        SubShader
        {
            Tags { "RenderType" = "Opaque" }
            LOD 100

            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                // make fog work
                //#pragma multi_compile_fog

                #include "UnityCG.cginc"

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

                struct v2f
                {
                    float2 uv : TEXCOORD0;
                    //UNITY_FOG_COORDS(1)
                    float4 vertex : SV_POSITION;
                };

                sampler2D _MainTex, _YydChangeInterval;
                float4 _MainTex_ST;

                float4 _twinkleCol;
                float _twinkleValue;

                float _middleValue;
                float _volatilityValue;

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

                 fixed4 frag(v2f i) : SV_Target
                 {
                     // sample the texture
                     fixed4 col = tex2D(_MainTex, i.uv);

                     float t = abs(sin(_Time.w * _twinkleValue)* _volatilityValue)+ _middleValue;// +0.5;
                     col.rgb = lerp(col.rgb, saturate(col.rgb + _twinkleCol.rgb), t);

                     return col;
                 }
                 ENDCG
             }
        }
}

3,纯色半透明闪烁

  与上述物体闪烁不同的是,这个只是在颜色基础上加了半透明闪烁,实现代码如下:

Shader "YaDong/YydGreenShader"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _value("速度",Range(1,3)) = 1
        _overlayCol("叠加色",Color) = (0.2146,1,0,0.6039)
    }
        SubShader
    {
        //Tags { "RenderType" = "Opaque"  }
        Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
        zWrite off
        blend srcAlpha one
        //LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            //#pragma multi_compile_fog

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                //UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex, _YydChangeInterval;
            float4 _MainTex_ST;
          
            float _value;
            float4 _overlayCol;

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

            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 col = _overlayCol;
                float t = abs(sin(_Time.w * _value));
                col.a = lerp(0.3, _overlayCol.a, t);
                
                return col;
             }
             ENDCG
         }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值