Unity着色器纹理动画

unity中shader可以采用cg来书写.
此文演示了如何利用纹理坐标来分段街区纹理.

正常的纹理坐标范围[0..1],坐标超出此范围可以用多种方式来处理,如Wrap,Clamp,Mirror等.
假设纹理分为n段,
则,每段对应的纹理坐标为[0..1]/n + index*1/n;
使用此规则就可以将纹理分段来截取了.

cg代码:
float2 nuv = v.uv;
		nuv.x = nuv.x /6;
		nuv.x += 1.0/6.0 * _index;
		float4 texColor = tex2D(_MainTex,nuv);


如此,便可以在shader中实现纹理动画了.

完全代码:

Shader "Level4/alpha1"{
	Properties{
		_MainTex("base rgb",2d) = ""{}
		_alpha("alpha",range(0,1)) = 0.5
		_index("index",float) = 0
	}
	
	CGINCLUDE
	#include "UnityCG.cginc"
	sampler2D _MainTex;
	float _alpha;
	float _index;
	
	struct v2f{
		float4 pos:POSITION;
		float2 uv:TEXCOORD;
	};
	v2f vert(appdata_full v):POSITION{
		v2f o;
		o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
		o.uv = v.texcoord.xy;
		return o;
	}
	
	float4 frag(v2f v):COLOR{
		float2 nuv = v.uv;
		nuv.x = nuv.x /6;
		nuv.x += 1.0/6.0 * _index;
		float4 texColor = tex2D(_MainTex,nuv);
		texColor.a = _alpha;
		return texColor;
	}
	
	ENDCG
	
	SubShader{
		 Tags { "Queue" = "Transparent" } 
		 // ZWrite Off
		 Blend SrcAlpha DstAlpha
		Pass{
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
			ENDCG
		}
	}
}

猜你喜欢

转载自superherosk123.iteye.com/blog/1197820
今日推荐