码迷,mamicode.com
首页 > 编程语言 > 详细

Unity Shader编程(3)uv操作

时间:2016-03-03 14:53:46      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

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编程(3)uv操作

标签:

原文地址:http://blog.csdn.net/husheng0/article/details/50787626

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!