码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-12-08 12:27:58      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:游戏开发   unity3d   shader   cg语言   屏幕特效   

纹理贴图移动特效产生岩浆、瀑布效果
原理是改变动态改变纹理坐标uv的值,使之移动

首先准备好一张贴图

bubuko.com,布布扣



建立一个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与主颜色到当前纹理上
产生了这种移动的效果

bubuko.com,布布扣



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"
}


可以看看我的这篇文章也运用了这个知识unity3d 老电影式的屏幕特效 


                                                                                                  --------by wolf96


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

标签:游戏开发   unity3d   shader   cg语言   屏幕特效   

原文地址:http://blog.csdn.net/wolf96/article/details/41801117

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!