1. 程式人生 > >Unity Shader之波浪效果

Unity Shader之波浪效果

很簡單,注意每次傳入的頂點值是沒有偏移過的

Shader "Custom/Waves"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
	_WaveSpeed("Speed", float) = 0.0
		_WaveStrength("Strength", Range(0.0, 1.0)) = 0.0
	}
		SubShader
	{
		Tags{ "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
		Cull Off

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

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

	fixed4 _LightColor0;


	float _WaveStrength;
	float _WaveSpeed;

	float4 movement(float4 pos, float2 uv) {
		
		pos.y += (sin((uv.x+uv.y)*5+ _Time.y * _WaveSpeed))*(uv.x*2);//最後一個uv.x*2是為了讓越靠近尾巴的地方抖的幅度越大

		return pos;
	}

	v2f vert(appdata_base v) {
		v2f o;
		o.vertex = v.vertex;
		o.pos = mul(UNITY_MATRIX_MVP, movement(v.vertex, v.texcoord));
		o.uv = v.texcoord;
		return o;
	}

	sampler2D _MainTex;

		fixed4 frag(v2f i) : SV_Target{
		fixed4 col = tex2D(_MainTex, i.uv);
		return col;
	
	}

		ENDCG
	}
	}
}