水波效果

作者:追风剑情 发布于:2017-5-22 20:57 分类:Shader

一、创建SinWater.shader

Shader "Custom/SinWater"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		//控制波动周期长度
		_DistanceFactor ("Distance Factor", float) = 1.0
		//控制波动速率(值越大越快)
		_TimeFactor ("Time Factor", float) = 0
		_AlphaScale ("Alpha Scale", float) = 1.0
	}
	SubShader
	{
		Tags { "Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}
		LOD 100

		Pass
		{
			Tags {"LightMode"="ForwardBase"}

			ZWrite Off
			//开启Alpha混合才能调节透明度
			Blend SrcAlpha OneMinusSrcAlpha
			
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#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;
			float4 _MainTex_ST;
			float _DistanceFactor;
			float _TimeFactor;
			float _AlphaScale;
			
			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
			{
				float2 dv = float2(0.5, 0.5) - i.uv;
				//归一化
				float2 dv1 = normalize(dv);

				//计算像素离中心点的距离
				float dis = sqrt(dv.x * dv.x + dv.y * dv.y);

				//利用sin函数让像素波动
				float sinFactor = sin(dis*_DistanceFactor + _Time.y * _TimeFactor)*0.01;

				//计算像素偏移量(让像素的x、y坐标一起做正弦波动)
				float2 offset = dv1 * sinFactor;
				
				fixed4 col = tex2D(_MainTex, offset + i.uv);
				//col.a *= _AlphaScale;
				//return col;
				return fixed4(col.rgb, col.a * _AlphaScale);
			}
			ENDCG
		}
	}
}


11111.jpg


运行效果

gaollg3.GIF

标签: Shader

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号