UnityShader学习教程之<流光效果+渐变灰色>

Shader流光+渐变灰色

Properties面板

	Properties
	{
		_MainTex ("Base (RGB)", 2D) = "black" {}
		_FlashColor ("Flash Color", Color) = (1,1,1,0)  
		_Alpha ("Flash Alpha", Range(0, 1)) = 0.7  
        _Angle ("Flash Angle", Range(0, 360)) = 45  
        _Width ("Flash Width", Range(0, 1)) = 0.2  
        _Speed ("Speed", Float) = 1  
        _Interval ("Time Interval", Float) = 2
		_Gary("Gary",Range(-3,3)) = 2
	}

Tags标签

		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
			"DisableBatching" = "True"
		}

Pass

		Pass
		{
			Cull Off
			Lighting Off
			ZWrite Off
			Fog { Mode Off }
			Offset -1, -1
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag			
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float4 _FlashColor;  
			float _Alpha; 
	        float _Angle;  
	        float _Width;  
	        float _Speed;
	        float _Interval;  
			float _Gary;
	
			struct appdata_t
			{
				float4 vertex : POSITION;
				float2 texcoord : TEXCOORD0;
				fixed4 color : COLOR;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};
	
			struct v2f
			{
				float4 vertex : SV_POSITION;
				half2 texcoord : TEXCOORD0;
				fixed4 color : COLOR;
				UNITY_VERTEX_OUTPUT_STEREO
			};
	
			v2f o;

			float inFlash(half2 uv)  
	        {     
	            float brightness = 0;  
	              
	            float angleInRad = _Angle;  
	            float tanInverseInRad = 1.0 / acos(angleInRad);  

		        float currentTime = _Time.y;  
	            bool onLeft = (tanInverseInRad > 0);  
		        float xBottomFarLeft = onLeft? 0.0 : tanInverseInRad;  
		        float xBottomFarRight = onLeft? (1.0 + tanInverseInRad) : 1.0;  

	            float xBottomRightBound = xBottomFarLeft + _Speed * (xBottomFarRight - xBottomFarLeft);
	            float xBottomLeftBound = xBottomRightBound - _Width;  
	              
	            float xProj = uv.x + uv.y * -tanInverseInRad;  
	              
	            if(xProj > xBottomLeftBound && xProj < xBottomRightBound)  
	            {  
	                brightness = 1.0 - abs(2.0 * xProj - (xBottomLeftBound + xBottomRightBound)) / _Width;  
	            }  
	            return brightness;  
	        }

			v2f vert (appdata_t v)
			{
				UNITY_SETUP_INSTANCE_ID(v);
				UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.texcoord = v.texcoord;
				o.color = v.color;
				return o;
			}
				
			fixed4 frag (v2f IN) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
				if(col.a == 1)
				{
					col += inFlash(IN.texcoord) * _Alpha;
				}
				float gray = 0.2125 * col.r + 0.7154 * col.g + 0.0721 * col.b;
				float3 grayColor = fixed3(gray, gray, gray);
				float3 avgcolor = float3(0.5, 0.5, 0.5);
				col.rgb = lerp(grayColor, col.rgb, saturate(1 - IN.texcoord.y - _Gary));
				col.rgb = lerp(grayColor, col.rgb, saturate(IN.texcoord.x - _Gary));
				return col;
			}
			ENDCG
		}

解释inFlash函数<用于计算流光>:

			float inFlash(half2 uv)  
	        {     
	            float brightness = 0;  
	              
	            float angleInRad = _Angle;  
	            float tanInverseInRad = 1.0 / acos(angleInRad);  

		        float currentTime = _Time.y;  
	            bool onLeft = (tanInverseInRad > 0);  
		        float xBottomFarLeft = onLeft? 0.0 : tanInverseInRad;  
		        float xBottomFarRight = onLeft? (1.0 + tanInverseInRad) : 1.0;  

	            float xBottomRightBound = xBottomFarLeft + _Speed * (xBottomFarRight - xBottomFarLeft);
	            float xBottomLeftBound = xBottomRightBound - _Width;  
	              
	            float xProj = uv.x + uv.y * -tanInverseInRad;  
	              
	            if(xProj > xBottomLeftBound && xProj < xBottomRightBound)  
	            {  
	                brightness = 1.0 - abs(2.0 * xProj - (xBottomLeftBound + xBottomRightBound)) / _Width;  
	            }  
	            return brightness;  
	        }

解释灰色渐变:

				float gray = 0.2125 * col.r + 0.7154 * col.g + 0.0721 * col.b;
				float3 grayColor = fixed3(gray, gray, gray);
				float3 avgcolor = float3(0.5, 0.5, 0.5);
				col.rgb = lerp(grayColor, col.rgb, saturate(1 - IN.texcoord.y - _Gary));
				col.rgb = lerp(grayColor, col.rgb, saturate(IN.texcoord.x - _Gary));

shader重要的不是实现效果,而是在于shader的性能!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值