首页 >Unity3D频道 >【Unity3D拓展编辑器】 > Unity3D研究院之静态合并贴图(一百一十六)
2020
06-07

Unity3D研究院之静态合并贴图(一百一十六)

最近研究了一下Texture2D.PackTextures方法。它可以在运行时或者编辑时对贴图进行合并。运行时它不支持任意贴图格式的合并,比如现在主流的ASTC,它会合并成RGBA32格式。而且无法指定贴图合并到大图具体某个位置上。

动态合并贴图的方法可以参考我之前的文章,这篇主要考虑静态合并,也就是编辑模式下合并

Unity3D研究院之运行时合并ETC1、ETC2、ASTC、DXT1、DXT5、PVRTC贴图

既然是编辑模式下合并,我们肯定不希望每张小贴图也需要标记read/write enable 那样就太麻烦了。如下代码所示,提供需要合并的小图,以及小图所在大图的偏移即可。

using System;
using System.IO;
using UnityEditor;
using UnityEngine;

public class TextureCombine 
{
    [MenuItem("Tools/TextureCombine")]
    static void Combine()
    {
        Texture2D tex512 = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/512.jpg");
        Texture2D tex128 = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/128.jpg");
        Texture2D @out  = Combine(
            new[] { tex512, tex128 }, //贴图
            new[] { (0, 0), (600, 600) } //偏移
            ,1024);//最终贴图大小

        File.WriteAllBytes("Assets/out.jpg", @out.EncodeToJPG());
        AssetDatabase.Refresh();
    }

    static Texture2D Combine(Texture2D []texs, ValueTuple<int,int>[]offests,int size)
    {
        Texture2D @out = new Texture2D(size, size, TextureFormat.RGBA32, true);
        for (int i = 0; i < texs.Length; i++)
        {
            var tex = texs[i];
            var offest = offests[i];
            var width = tex.width;
            var height = tex.height;
            RenderTexture tmp = RenderTexture.GetTemporary(width,height,0,RenderTextureFormat.Default,RenderTextureReadWrite.Linear);
            Graphics.Blit(tex, tmp);
            RenderTexture previous = RenderTexture.active;
            RenderTexture.active = tmp;
            Texture2D @new = new Texture2D(width, height);
            @new.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            @new.Apply();
            @out.SetPixels(offest.Item1, offest.Item2, width, height, @new.GetPixels());
            RenderTexture.active = previous;
            RenderTexture.ReleaseTemporary(tmp);
        }
        return @out;
    }
   
}

最后合并的结果

Snip20200607_1

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

留下一个回复

你的email不会被公开。 必填项已用 * 标注。