Material 的 Tiling 和 Offset

一、Tiling 和 Offset 概述

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

  • Offset

    表示贴图 UV 坐标的起始位置,取值范围是 0~1。超过 1 的部分取余,so Offset 值为 1.0 和 0 时的效果是一样。Offset > 0 时,向右 Offset % 1 为起始位置;Offset < 0 时,向左 |Offset| % 1 为起始位置。

  • Tiling

    表示从 Offset 指定的起始位置开始,分别在 X/Y 轴,截取 指定比列大小 的原始贴图,作为输出贴图。超过 1 的部分会自动拼接。如果 Texture 的 Wrap Mode 是 Repeat,则超出 1 的部分由原始贴图拼接;如果 Wrap Mode 是 Clamp,则超出 1 的部分由空白拼接。


二、示例

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

这里写图片描述

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

下面是使用各种不同 Tiling 和 Offset 下的效果,Texture 的 Wrap Mode 为 Repeat。

这里写图片描述

可以通过更新材质的 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);  

    }  

}  

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


三、SetTextureScale 和 SetTextureOffset

SetTextureScale 和 SetTextureOffset 分别用于设置材质’s各种贴图纹理的 缩放量偏移量

Shader 相当于配色方案,将材质的各种贴图整合 Mix。不同的 Shader,需要的贴图数量和种类也不一样。为此,在修改材质纹理属性的时候,为区别各种贴图,这两个方法的第一个参数常用于区别各种贴图。

  • “_MainTex”

    主要的漫反射纹理,也能通过 mainTextureScale/mainTextureOffset 属性访问。

  • “_BumpMap”

    法线贴图。

  • “_Cube”

    反射cubemap。(盒子贴图)


参考

[1].http://blog.csdn.net/kfqcome/article/details/19343323

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值