Shader实现UV动画

  • Post author:
  • Post category:其他

代码如下:

 

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/UVShader"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_SubTex ("Texture", 2D) = "white" {}
	}

	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

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

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

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

			sampler2D _SubTex;
			sampler2D _MainTex;

			float4 _MainTex_ST;
			
			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 uv_offset = float2(0, 0);
				uv_offset.x = _Time.y * 0.25;
				uv_offset.y = _Time.y * 0.25;
				//对第二张图片进行UV偏移,再采样
				fixed4 linght_color = tex2D(_SubTex, i.uv + uv_offset);
				fixed4 col = tex2D(_MainTex, i.uv) + linght_color;
				return col;
			}
			ENDCG
		}
	}
}

效果:


版权声明:本文为a592733740原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。