UV動畫:UV映射 + UV旋轉 + UV偏移 實現 太空旅行、煙雲內旋,旋渦效果

Uv的 旋轉 偏移 拉伸操作的組合可以實現非常多的效果。

先來幾張普通的紋理:





下邊是效果圖:







效果上如果需要更精細,需要製作一些更精細的貼圖,相信效果會更好。

最後附上代碼:

Shader "MyShaders/內收漸變效果"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_RotateRadius ("rotateRate", Range(0,3.1415926)) = 0.1
		_RotateSpeed ("RotateSpeed", Range(0,1)) = 0.1
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

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

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

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float _RotateRadius;
			float _RotateSpeed;
			//旋轉矩陣

			half4x4 translateA = {
			1,0,0,-0.5,
			0,1,0,-0.5,
			0,0,1,0,
			0,0,0,1
			};

			half4x4 translateB = {
			1,0,0,0.5,
			0,1,0,0.5,
			0,0,1,0,
			0,0,0,1
			};

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{

			    float2 newuv = i.uv;

			    //計算UV映射

			    float2 AOHeader = float2(i.uv.x - 0.5, i.uv.y - 0.5);

			    float2 xLine = float2(1, 0);

			    float radius = acos(dot(normalize(AOHeader), xLine));

			    if (i.uv.y  > 0.5)
			    {
			        radius += 3.1415926;
			        radius = 3 * 3.1415926 - radius; 
			    }

			    float r = distance(i.uv, float2(0.5,0.5));

			    newuv.x = radius / (2 * 3.1415926); //radius * r/ (2 * 3.1415926 * r)

			    newuv.y = 1 - r / abs(2);

			    //UV旋轉
			    float rotationRadius = _RotateRadius * ((0.5 - r) / 0.5);
			    
			    float4x4 rotationMatrix = {
			    cos(rotationRadius), -sin(rotationRadius),0,0,
			    sin(rotationRadius), cos(rotationRadius),0,0,
			    0,0,1,0,
			    0,0,0,1
			    };

			    float4 newUVVector = float4(newuv.x, newuv.y, 0, 1);

			    newUVVector = mul(translateA, newUVVector);
			    newUVVector = mul(rotationMatrix, newUVVector);
			    newUVVector = mul(translateB, newUVVector);
			    newuv.x = newUVVector.x;
			    newuv.y = newUVVector.y;

			    //uv偏移應在旋轉矩陣操作之後,避免時間引起uv偏移,被矩陣影響
			    newuv.y -= _RotateSpeed * _Time.y; 

				// sample the texture
				fixed4 col = tex2D(_MainTex, newuv.xy);

				return col;
			}
			ENDCG
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章