纹理贴图移动(水流、电流效果)

纹理贴图移动特效产生岩浆、瀑布效果

原理是改变动态改变纹理坐标uv的值,使之移动  

首先准备好一张贴图

 

建立一个shader 变量一览: _MainTex 主纹理贴图 _MainTint 主要颜色   _ScrollXSpeed x轴移动速度 _ScrollYSpeed  y轴移动速度 

	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_MainTint ("Diffuse Tint", Color) = (1, 1, 1, 1) 
		_ScrollXSpeed ("X Scroll Speed", Range(0, 10)) = 2
		_ScrollYSpeed ("Y Scroll Speed", Range(0, 10)) = 2
	} 

主要是在surf函数里进行操作 fixed xScrollValue = _ScrollXSpeed * _Time; fixed yScrollValue = _ScrollYSpeed * _Time; x、y偏移量随时间增加 

		void surf (Input IN, inout SurfaceOutput o) {
			fixed2 scrolledUV = IN.uv_MainTex;
			fixed xScrollValue = _ScrollXSpeed * _Time;
			fixed yScrollValue = _ScrollYSpeed * _Time;
			scrolledUV += fixed2(xScrollValue, yScrollValue);
			half4 c = tex2D (_MainTex, scrolledUV);
			o.Albedo = c.rgb * _MainTint;
			o.Alpha = c.a;
		} 

 _Time是 unity shaderlab内置值 float4 _Time : Time (t/20, t, t*2, t*3) 是一个四维向量
scrolledUV += fixed2(xScrollValue, yScrollValue); uv的x,y值随时间累加偏移量
最后整合uv与主颜色到当前纹理上 产生了这种移动的效果

 

 

shader代码如下

Shader "Custom/testShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_MainTint ("Diffuse Tint", Color) = (1, 1, 1, 1) 
		_ScrollXSpeed ("X Scroll Speed", Range(0, 10)) = 2
		_ScrollYSpeed ("Y Scroll Speed", Range(0, 10)) = 2
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert
		fixed4 _MainTint;
		fixed _ScrollXSpeed;
		fixed _ScrollYSpeed;
		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			fixed2 scrolledUV = IN.uv_MainTex;
			fixed xScrollValue = _ScrollXSpeed * _Time;
			fixed yScrollValue = _ScrollYSpeed * _Time;
			scrolledUV += fixed2(xScrollValue, yScrollValue);
			half4 c = tex2D (_MainTex, scrolledUV);
			o.Albedo = c.rgb * _MainTint;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

 

posted @ 2015-09-16 18:50  王者归来1936  阅读(1118)  评论(0编辑  收藏  举报