[Unity]模擬雨水的折射效果


用GrabPass做的小玩具。

並不是真的計算了折射,只是簡單地擾動了uv,對於雨水來說效果已經足夠好了。

Shader代碼:

Shader "Unlit/Rain"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
		_Opaque("Opaque",float) = 0.02
	}
	SubShader
	{
		Tags{
			"RenderType" = "Opaque"
			"Queue" = "Transparent"
		}
		LOD 100

		Blend SrcAlpha OneMinusSrcAlpha
		GrabPass{ "GrabTexture" }
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

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

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

			};

			sampler2D _MainTex;
			sampler2D GrabTexture;
			float4 _MainTex_ST;
			float _Opaque;

			v2f vert(appdata v) {
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.screenuv = ComputeGrabScreenPos(o.vertex);
				return o;
			}

			fixed4 frag(v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				float2 suv = i.screenuv / i.screenuv.w;
				//512和128是測試出來比較正常的魔數。
				//越大則擾動越劇烈。
				float2 distort = float2(sin(suv.x * 512 + _Time.w * 10),sin(suv.y * 128 + _Time.w * 10)) / 128;
				col = _Opaque + tex2D(GrabTexture, suv + distort);
				col.a = tex2D(_MainTex, i.uv).a;
				return col;
			}
			ENDCG
		}
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM