【Unity Shader学习】2D纹理采样

Shader "Sbin/Textures/Texture01"
{
	Properties
	{
		_MainTex("MainTex",2D) = ""{}
		_U("U",Range(-0.001,0.001)) = 0
	}

	SubShader
	{
	pass
	{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"

		sampler2D _MainTex;
		float _U;


		struct v2f
		{
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;

		};

		v2f vert(appdata_base v)
		{
			v2f o;
			o.pos = UnityObjectToClipPos(v.vertex);
			o.uv = v.texcoord.xy;
			return o;
		}

		fixed4 frag(v2f IN) :COLOR
		{
			fixed4 color = tex2D(_MainTex,IN.uv); 
			return color;
		}
		ENDCG
	}
	}
}

效果截图:

 

1.如果在Properties使用2D,CG里要用sampler2D,代表使用的是2维纹理.

2.进行2维纹理贴图时,将图片横向设为U轴,纵向设为V轴,最大值为1.

3.通过tex2D函数进行采样时,采样的第一个参数是一个二维纹理,第二个参数是一个float2类型的uv坐标,如果采样坐标大于1或小于0,采样的图片默认以重复的形式进行计算。

posted @ 2022-07-23 16:39  香菇0_0  阅读(342)  评论(0)    收藏  举报