Unity Shader编程(3)uv操作

Unity Shader编程(3)uv操作

1、shader示意图

Unity Shader编程(3)uv操作_第1张图片


Unity Shader编程(3)uv操作_第2张图片


2、uv常用操作

①缩放

Shader "Custom/NewShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_scaleX("scaleX",float)=1//X方向
		_scaleY("scaleY",float)=1//Y方向
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		float _scaleX;
		float _scaleY;

		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
		float2 uv = IN.uv_MainTex;
		uv = uv*float2(1.0/_scaleX,1.0/_scaleY);//缩放
		
			half4 c = tex2D (_MainTex, uv);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}
Unity Shader编程(3)uv操作_第3张图片  Unity Shader编程(3)uv操作_第4张图片 


②平移

Shader "Custom/NewShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		//_scaleX("scaleX",float)=1//X方向
		//_scaleY("scaleY",float)=1//Y方向
		_px("px",float) = 0//平移
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		//float _scaleX;
		//float _scaleY;
		float _px;
		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
		float2 uv = IN.uv_MainTex;
		//uv = uv*float2(1.0/_scaleX,1.0/_scaleY);//缩放
		uv = uv + float2(_px,0);//平移
			half4 c = tex2D (_MainTex, uv);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}


③旋转

Shader "Custom/NewShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		//_scaleX("scaleX",float)=1//X方向
		//_scaleY("scaleY",float)=1//Y方向
		//_px("px",float) = 0//平移
		_angle("angle",float) = 0
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		//float _scaleX;
		//float _scaleY;
		//float _px;
		float _angle;
		
		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
		float2 uv = IN.uv_MainTex;
		//uv = uv*float2(1.0/_scaleX,1.0/_scaleY);//缩放
		//uv = uv + float2(_px,0);//平移
		uv =float2((cos(_angle)*uv.x-sin(_angle)*uv.y),(cos(_angle)*uv.y+sin(_angle)*uv.x));
				half4 c = tex2D (_MainTex, uv);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}
Unity Shader编程(3)uv操作_第5张图片


====================================================================================

结束。

你可能感兴趣的:(unity3d,shader)