Unity Shader编程uv操作

Posted 胡胜同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity Shader编程uv操作相关的知识,希望对你有一定的参考价值。

Unity Shader编程(3)uv操作

1、shader示意图

技术分享

技术分享

技术分享


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"
}
技术分享 技术分享 技术分享


②平移

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编程uv操作的主要内容,如果未能解决你的问题,请参考以下文章

Unity Shader ------ UV动画原理及简易实现

Unity中Shader的三种基本类型

Unity 报错解决方法:Standard Shader Invalid Subscript uv2 error

Unity Shader 赛博小人01UV准备 SD制作特效贴图

Unity Shader Graph 制作车流效果(UV滚动)

Unity 报错解决方法:Standard Shader Invalid Subscript uv2 error