unity material之tiling和offset属性

在Unity中,使用材质或者着色器是通过创建一个材质来实现的,新建一个材质,会要求选择使用哪个着色器,默认的是Diffuse着色器。确定了着色器后,在材质属性面板中就会出现该着色器需要用到的一些属性,一般是贴图及对应的属性。


材质中贴图均有tiling和offset两个属性,它们用来定义当前材质球所使用的贴图区域(

直接将贴图贴到平面上一般会出现X和Y坐标倒置的问题,这个时候可以将tiling取反或者旋转贴图所在平面进行调整)。


offset:指明使用贴图的起始位置,取值范围为0-1

tiling:指明从offset位置处的大小区域,区域的取值范围一般为(-1,1),超过的话部分会按比例生成新的区域拼接上原先的。


也就是说,tiling指明了使用的贴图为输入贴图的多少倍,超过1的会自动拼接,同时offset指明了使用的贴图在输入贴图处的起始位置,假如位置超过了1,就会对1取余。


以一张图片为例,下面分别为原图和直接贴到一个平面上的效果


平面贴图后出现了X和Y方向倒置问题


下面是使用各种不同tiling和offset下的效果


上图中对于不同宽度的tiling,贴在的平面上的宽度没有对应的调整,所以看起来会有点比例失真

可以通过更新材质的offset和tiling属性来制作动画

下图是16个表情格图标,可以每次只显示图标中的某一小块区域来形成动画,参见这里


将该贴图应用到一个平面上面,再在该平面上附加如下代码:
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	// Use this for initialization
	public int colCount =  4;
	public int rowCount =  4;
	 
	//vars for animation
	public int  rowNumber  =  0; //Zero Indexed
	public int colNumber = 0; //Zero Indexed
	public int totalCells = 16;
	public int  fps     = 10;
	  //Maybe this should be a private var
	    private Vector2 offset;
	//Update
	void Update () { SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);  }
	 
	//SetSpriteAnimation
	void SetSpriteAnimation(int colCount ,int rowCount ,int rowNumber ,int colNumber,int totalCells,int fps ){
	 
	    // Calculate index
	    int index  = (int)(Time.time * fps);
	    // Repeat when exhausting all cells
	    index = index % totalCells;
	 
	    // Size of every cell
	    float sizeX = 1.0f / colCount;
	    float sizeY = 1.0f / rowCount;
	    Vector2 size =  new Vector2(sizeX,sizeY);
	 
	    // split into horizontal and vertical index
	    var uIndex = index % colCount;
	    var vIndex = index / colCount;
	 
	    // build offset
	    // v coordinate is the bottom of the image in opengl so we need to invert.
	    float offsetX = (uIndex+colNumber) * size.x;
	    float offsetY = (1.0f - size.y) - (vIndex + rowNumber) * size.y;
	    Vector2 offset = new Vector2(offsetX,offsetY);
	 
            renderer.material.SetTextureScale  ("_MainTex", size);
	    renderer.material.SetTextureOffset ("_MainTex", offset);
	   
	}
	
}

播放后即可得到动态显示每个子图标的动画


### 修改 Unity Shader 中 UV 偏移 Offset 属性的方法 在 Unity 中,可以通过多种方式修改 Shader 的 UV 偏移(Offset属性。一种常见的方式是在 C# 脚本中动态调整材质的 `mainTextureOffset` 参数[^2]。 另一种更灵活且高效的做法是直接在 Shader 或者 Shader Graph 中处理 UV 偏移逻辑。对于手写 HLSL/CG 代码的情况,在顶点着色器阶段应用缩放平移变换可以减少不必要的性能开销[^4]。 #### 使用脚本控制 UV 动画 下面是一个简单的 C# 实现案例,展示了如何创建一个组件来改变指定渲染对象上的纹理坐标偏移: ```csharp using UnityEngine; public class UVAnimator : MonoBehaviour { public float speedX = 0.1f; public float speedY = 0.1f; private Renderer rend; private Material mat; void Start(){ rend = GetComponent<Renderer>(); if(rend != null){ mat = rend.material; } } void Update(){ if(mat != null){ float offsetX = Time.time * speedX % 1; float offsetY = Time.time * speedY % 1; mat.mainTextureOffset = new Vector2(offsetX, offsetY); } } } ``` 这段代码会随着时间推移不断更新材质的 `mainTextureOffset` ,从而让纹理看起来像是在移动而不是单纯地被拉伸。 #### 在 Shader 内部设置 UV 变换 当编写自定义 Shaders 时,可以在顶点着色器部分提前完成 UV 的转换工作,这样能有效降低片段着色器的工作负担并提高效率。具体来说就是利用 `_MainTex_ST` 这样的参数来进行缩放位移运算。 例如,在 HLSL 中你可以这样做: ```hlsl sampler2D _MainTex; float4 _MainTex_ST; // xy表示tiling zw表示offset v2f vert(appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); // 应用了 tiling offset return o; } fixed4 frag(v2f i) : SV_Target{ fixed4 col = tex2D(_MainTex, i.uv); return col; } ``` 上述代码中的 `TRANSFORM_TEX()` 宏自动完成了基于给定 ST 向量对原始 UV 的转换操作。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值