简易Shader实践记录3-背景滚动与星空旋转

学习传送门:
Unity Shader 学习笔记(18)纹理动画、顶点动画、广告牌技术

1.简单的UV滚动,可以做背景展示

需要注意

图片格式记得选择 repat

具体实现

Shader "Unlit/MoveUV"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_XSpeed("X轴方向的纹理滚动速度:", Range(0, 10)) = 1
		_YSpeed("Y轴方向的纹理滚动速度:", Range(0, 10)) = 1
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

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

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

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

			sampler2D _MainTex;
			float4 _MainTex_ST;
			fixed _XSpeed;
			fixed _YSpeed;

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed2 UV = i.uv;
				fixed xV = _XSpeed * _Time.x;
				fixed yV = _YSpeed * _Time.y;
				UV += fixed2(xV, yV);
				fixed4 col = tex2D(_MainTex, UV);
				return col;
			}
			ENDCG
		}
	}
}

2.星空旋转(UV)

Shader "Unlit/RotationUV"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_RotationSpeed("Rotation Speed", Float) = 2.0
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

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

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

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

			sampler2D _MainTex;
			uniform float _RotationSpeed;
			float4 _MainTex_ST;

			float4 CalculateRotation(float4 pos)
			{
				//先计算距离位置点距离原点的值
				float2 div= (_MainTex_ST.xy / 2.0) + _MainTex_ST.zw;
				float rot = _RotationSpeed * _Time.y;

				pos.xy -= div;

				float s, c;
				sincos(radians(rot), s, c);
				float2x2 rotMatrix = float2x2(c, -s, s, c);
				pos.xy = mul(pos.xy, rotMatrix);

				pos.xy += div;
				return pos;
			}
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv.xy = CalculateRotation(v.uv);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				return col;
			}
			ENDCG
		}
	}
}

3.顶点旋转

Shader "Unlit/RotataeVertices"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_RotationSpeed ("Rotation Speed", Float) = 2.0
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

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

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

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

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float _RotationSpeed;
			
			float4 CalculateRotation(float4 pos)
			{
			    float rotation=_RotationSpeed*_Time.y;
			    float s,c;
				sincos(radians(rotation), s, c);
			    float2x2 rotMatrix=float2x2(c,-s,s,c);
			    pos.xy=mul(pos.xy,rotMatrix);
			    
			    return pos;
			}
			
			v2f vert (appdata v)
			{
				v2f o;
				float4 k=float4(v.vertex.x,v.vertex.z,1.0,1.0);
				k=CalculateRotation(k);
				o.vertex = UnityObjectToClipPos(float4(k.x,v.vertex.y,k.y,v.vertex.w));
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				return col;
			}
			ENDCG
		}
	}
}

你可能感兴趣的:(简易shader实践)