Home  >  Article  >  Web Front-end  >  Take you step by step to develop an atlas packaging tool using node

Take you step by step to develop an atlas packaging tool using node

青灯夜游
青灯夜游forward
2021-11-29 18:53:472284browse

This article will teach you how to use node to handwrite an atlas packaging tool. It has certain reference value. I hope it will be helpful to everyone!

Take you step by step to develop an atlas packaging tool using node

I accidentally discovered a very useful cross-platform image encoding and decoding librarynode-images.

Read its API carefully, The idea of ​​using it to make a sprite atlas came up.

So this toolsprites-pack-tool.

# was born. ##You can view it on github

https://github.com/xdq1553/MySpritesPackTool

You can use npm to install

https://www.npmjs.com /package/sprites-pack-tool


I think everyone is familiar with

Sprite Atlas.

For example, put the following Several pictures are combined into one.

Take you step by step to develop an atlas packaging tool using node

Take you step by step to develop an atlas packaging tool using node

This album is packaged and combined by me using the tools introduced in this article .

The quality of the synthesized pictures is still very high.

Why do we need to use the atlas

web development

We are in web development, every time The browser needs to request server resources once to display a picture.

For example, there is a fundamental difference between

3 requests of 4k each and one request of 12k , and more often than not, a request is not 3 * 4k.

Using the atlas allows us to optimize resource loading and improve the performance of the website.

Game development

In game development, the use of atlases is crucial. Whether it is general frame animation or animation solutions such as svga, resources will not be requested for each picture.

More At that time, we all packaged them into atlases, and the atlas packaging tool

texturepacker was even more popular.

Secondly, there are too many game scenes, and we generally need to load resources step by step, and some When an animation model is created, it involves as few as a dozen pictures or as many as nearly a hundred pictures.

The use of picture albums is indispensable.

Let’s do this below Let’s take a look at how to write an atlas packaging tool.

Tool design

What skills are needed to develop an atlas packaging tool script.

Then we think about how to package an atlas.

  • We need to find the folder that needs to be packaged. There may be multiple or nested folders.

  • The album is composed of multiple scattered pictures Pieced together.

  • The size of the atlas needs to be configurable

  • Compress the atlas space as much as possible so that each picture fits closely

  • Each folder is packaged into an atlas, you need to consider the situation where there are too many pictures

  • You may need to generate the json required for the atlas File, record the picture location information

Start writing the script

Script IO

This is how I design it.

First we need A package object instance

MySpritePackTool, which also supports writing configuration parametersoptions.

/** 图集打包对象 */
const MySpritePackTool = function (opt) {
    this.options = {
        //一个文件夹图片过多或者过长 递归最大次数
        maxCount: opt.maxCount || 2,
        //需要打包图集的文件路径
        assetsPath: opt.assetsPath,
        //输出文件路径
        outPutPath: opt.outPutPath,
        //一张图集打包最大size
        maxSize: { width: 2048, height: 2048 }
    }
};

Then we need to output this object, which can be referenced by other projects.

module.exports = MySpritePackTool;

Traverse files to generate node trees

Our input parameters are as few as possible, so we need our program to traverse folders.

For example, we have the following directory tree:

|--assets
   |--index
      |--img-Take you step by step to develop an atlas packaging tool using node
      |--img-Take you step by step to develop an atlas packaging tool using node
   |--login
      |--img-Take you step by step to develop an atlas packaging tool using node
   |--img-Take you step by step to develop an atlas packaging tool using node
   |--img-Take you step by step to develop an atlas packaging tool using node

We need to package an atlas in each folder.

Thinking: What kind of data structure is needed?

First of all, it is convenient for js For analysis, we agree on an object.

Each layer requires a picture information container

assets;

a contained picture identifier

keys;

A folder name, which is also convenient for us to name the album later

name;

Then put the same object in front of each layer of folders;

The structure is as follows:

{
  assets: [
    {
      id: 'assets/img-Take you step by step to develop an atlas packaging tool using node',
      width: 190,
      height: 187
    },
    ...
  ],
  name: 'assets',
  keys: 'img-Take you step by step to develop an atlas packaging tool using node,img-Take you step by step to develop an atlas packaging tool using node,',
  index: {
    assets: [
        {
            id: 'assets/index/img-Take you step by step to develop an atlas packaging tool using node',
            width: 190,
            height: 187
        },
        ...
    ],
    name: 'index',
    keys: 'img-Take you step by step to develop an atlas packaging tool using node,img-Take you step by step to develop an atlas packaging tool using node,'
  },
  login: {
    assets: [
        {
            id: 'assets/login/img-Take you step by step to develop an atlas packaging tool using node',
            width: 190,
            height: 187
        }
    ],
    name: 'index',
    keys: 'img-Take you step by step to develop an atlas packaging tool using node,'
  },
}

It is not difficult to find that we can already get all the files and folders that need to be packaged.

So how to implement it using a program?

Mainly use the

fs module of nodejs to recursively operate the folder and output the required node tree.

Note that when writing, you need to judge whether it is a picture or a Folder.

MySpritePackTool.prototype.findAllFiles = function (obj, rootPath) {
    let nodeFiles = [];
    if (fs.existsSync(rootPath)) {
        //获取所有文件名
        nodeFiles = fs.readdirSync(rootPath);

        //组装对象
        let nameArr = rootPath.split('/');
        obj["assets"] = [];
        obj["name"] = nameArr[nameArr.length - 1];
        obj["keys"] = "";

        nodeFiles.forEach(item => {
            //判断不是图片路径
            if (!/(.png)|(.jpe?g)$/.test(item)) {
                let newPath = path.join(rootPath, item);
                //判断存在文件 同时是文件夹系统
                if (fs.existsSync(newPath) && fs.statSync(newPath).isDirectory()) {
                    // console.log("获得新的地址", newPath);
                    obj[item] = {};
                    this.findAllFiles(obj[item], newPath);

                } else {
                    console.log(`文件路径: ${newPath}不存在!`);
                }

            } else {
                console.log(`图片路径: ${item}`);
                obj["keys"] += item + ",";
                let params = {};
                params["id"] = path.resolve(rootPath, `./${item}`);
                //获得图片宽高
                params["width"] = images(path.resolve(rootPath, `./${item}`)).width();
                params["height"] = images(path.resolve(rootPath, `./${item}`)).height();
                obj["assets"].push(params);
            }
        })

    } else {
        console.log(`文件路径: ${rootPath}不存在!`);
    }
}

In this way we can get the node tree we need.

Get the new atlas location information

Our operations on the folder have been Completed, now we need to think about.

How to pack these scattered pictures into one picture.

Scattered pictures have two pieces of information, one

widthand A height is actually a rectangle.

我们现在所要做的就是把这些不同面积的矩形放到一个具有最大长宽的大矩形中.


跳开图片, 从矩形放置入手

二维矩形装箱算法有不少, 我这里选用一种比较简单的.

首先得到一个具有最大长宽的矩形盒子.

我们先放入一个矩形A, 这样子, 剩余区域就有两块: 矩形A的右边矩形A的下边.

Take you step by step to develop an atlas packaging tool using node

然后我们继续放入矩形B, 可以先右再下, 然后基于矩形B又有两块空白空间.

Take you step by step to develop an atlas packaging tool using node

依次类推, 我们就可以将合适的矩形全部放入.

举个例子

把左边的散装矩形放入右边的矩形框中, 可以得到:

Take you step by step to develop an atlas packaging tool using node

可以看到, 我们节省了很多空间, 矩形排列紧凑.

如果用代码实现, 是怎么样的呢?

/** 
 * 确定宽高 w h
 * 空白区域先放一个, 剩下的寻找右边和下边
 * 是否有满足右边的, 有则 放入 无则 继续遍历
 * 是否有满足下边的, 有则 放入 无则 继续遍历
 */
const Packer = function (w, h) {
    this.root = { x: 0, y: 0, width: w, height: h };
    // /** 匹配所有的方格 */
    Packer.prototype.fit = function (blocks) {

        let node;
        for (let i = 0; i < blocks.length; i++) {
            let block = blocks[i];
            node = this.findNode(this.root, block.width, block.height);
            if (node) {
                let fit = this.findEmptyNode(node, block.width, block.height);
                block.x = fit.x;
                block.y = fit.y;
                block.fit = fit;
            }
        }

    }

    /** 找到可以放入的节点 */
    Packer.prototype.findNode = function (node, w, h) {
        if (node.used) {
            return this.findNode(node.rightArea, w, h) || this.findNode(node.downArea, w, h);
        } else if (node.width >= w && node.height >= h) {
            return node;
        } else {
            return null;
        }
    }

    /** 找到空位 */
    Packer.prototype.findEmptyNode = function (node, w, h) {
        //已经使用过的 删除 
        node.used = true;
        //右边空间
        node.rightArea = {
            x: node.x + w,
            y: node.y,
            width: node.width - w,
            height: h
        };

        //下方空位
        node.downArea = {
            x: node.x,
            y: node.y + h,
            width: node.width,
            height: node.height - h
        }
        return node;
    }
}

使用递归, 代码量很少, 但是功能强大.

但是有一个问题, 如果超出定长定宽, 或者一个矩形装不完, 我们的算法是不会放入到大矩形中的.

这样子就有点不满足我们的图集打包思路了.

所以我们还需要将这个算法改进一下;

加入两个变量, 一个记录使用的总的区域, 一个记录未被装入的矩形.

//记录使用的总的区域
this.usedArea = { width: 0, height: 0 };
//记录未被装入的矩形
this.levelBlocks = [];

详细代码可以查看源码中的packing.

当然, 这里只是最简单的一种二维装箱算法

还有一种加强版的装箱算法, 我放在源码里了, 这里就不赘述了, 原理基本一致

现在, 我们已经可以将矩形合适的装箱了, 那怎么使用去处理成图集呢?

定义一个dealImgsPacking方法, 继续去处理我们的节点树.

这里用到了我们的配置项maxCount, 就是为了一张图集装不完, 多打出几张图集的作用.

然后我们打包出来的图集命名使用文件夹 + 当前是第几张的形式.

`${obj[&#39;name&#39;] + (count ? "-" + count : &#39;&#39;)}`

具体方法如下:

MySpritePackTool.prototype.dealImgsPacking = function (obj) {
    let count = 0;
    if (obj.hasOwnProperty("assets")) {
        let newBlocks = obj["assets"];
        obj["assets"] = [];

        while (newBlocks.length > 0 && count < this.options.maxCount) {
            let packer1 = new Packer(this.options.maxSize.width, this.options.maxSize.height);
            packer1.fit(newBlocks);

            let sheets1 = {
                maxArea: packer1.usedArea,
                atlas: newBlocks,
                fileName: `${obj[&#39;name&#39;] + (count ? "-" + count : &#39;&#39;)}`
            };
            newBlocks = packer1.levelBlocks;
            obj["assets"].push(sheets1);
            count++;
        }
    }
    for (let item in obj) {
        if (obj[item].hasOwnProperty("assets")) {
            this.dealImgsPacking(obj[item]);
        }
    }
}

通过这个方法我们改造了之前的节点树;

将之前节点树中的assest变为了一个数组, 每个数组元素代表一张图集信息.

结构如下:

  assets: [
    { 
        maxArea: { width: 180,height: 340 }, 
        atlas: [
            {
                id: &#39;assets/index/img-Take you step by step to develop an atlas packaging tool using node&#39;,
                width: 190,
                height: 187,
                x: 0,
                y: 0
            }
        ], 
        fileName: &#39;assets&#39; },
        ...
  ]

我们可以清晰的得到, 打包之后的图集, 最大宽高是maxArea, 每张图宽高位置信息是atlas,以及图集名称fileName.

接下来, 就是最后一步了, 绘制新的图片, 并输出图片文件.

注意
我们在使用打包算法的时候, 可以先进行一下基于图片大小的排序
这样以来打包出来的图集会留白更小

图集打包并输出

这里图集的绘制和输出均是使用了node-images的API;

遍历之前得到的节点树, 首先绘制一张maxArea大小的空白图像.

images(item["maxArea"].width, item["maxArea"].height)

然后遍历一张图集所需要的图片信息, 将每一张图片绘制到空白图像上.

//绘制空白图像
let newSprites = images(item["maxArea"].width, item["maxArea"].height);
//绘制图集
imgObj.forEach(it => {
    newSprites.draw(images(it["id"]), it["x"], it["y"]);
});

然后绘制完一张图集输出一张.

newSprites.save(`${this.options.outPutPath}/${item[&#39;fileName&#39;]}.png`);

最后对节点树递归调用, 绘制出所有的图集.

具体代码如下:

MySpritePackTool.prototype.drawImages = function (obj) {
    let count = 0;
    if (obj.hasOwnProperty("assets")) {
        //打包出一个或者多个图集
        let imgsInfo = obj["assets"];
        imgsInfo.forEach(item => {
            if (item.hasOwnProperty("atlas")) {
                let imgObj = item["atlas"];
                // console.log("8888",imgObj)
                //绘制一张透明图像
                let newSprites = images(item["maxArea"].width, item["maxArea"].height);
                imgObj.forEach(it => {
                    newSprites.draw(images(it["id"]), it["x"], it["y"]);
                });

                newSprites.save(`${this.options.outPutPath}/${item[&#39;fileName&#39;]}.png`);
                count++;
            }
        })
    }

    for (let item in obj) {
        if (obj[item].hasOwnProperty("assets")) {
            this.drawImages(obj[item]);
        }
    }

}

这样子, 我们就大功告成了,

运行测试一下, 可以得到如下的图集:

Take you step by step to develop an atlas packaging tool using node

Take you step by step to develop an atlas packaging tool using node

效果还不错.

如何使用

安装

npm i sprites-pack-tool

使用

const MySpritePackTool = require("sprites-pack-tool");
const path = require("path");
/** 打包最多递归次数 */
const MAX_COUNT = 2;
//需要合成的图集的路径
const assetsPath = path.resolve(__dirname, "./assets");

/** 图集打包工具配置*/
const mySpritePackTool = new MySpritePackTool({
    //一个文件夹图片过多或者过长 递归最大次数
    maxCount: MAX_COUNT,
    //需要打包图集的文件路径
    assetsPath: assetsPath,
    //输出文件路径
    outPutPath: path.resolve(__dirname, "./res"),
    //一张图集打包最大size
    maxSize: { width: 2048,height: 2048}
});
/** 图集打包 */
mySpritePackTool.Pack2Sprite();

展望

当然, 这个工具只是初版, 后续还会继续优化并增加新的功能.

  • 算法可以继续优化, 现在留白也挺多.

  • 文件夹操作,可以优化. 比如写入图片可以每个文件夹下一张图集.

  • 增加更多配置项, 比如开启图片压缩

  • 增加json文件

  • ...

我大致看了下, 市场上有几款图集打包工具, 要么基于texturePacker, 要么基于imagemagick;

使用这俩应用开放的API也是可以打包图集的, 效果品质可能更好.

但是你还得额外安装一个应用.

同样的, 你也可以使用webpack的一些loader或者plugins. 目的都是打包图集.

The tools introduced in this article are relatively lightweight, but they can be used once and they can be used right out of the box.

Afterword

I haven’t written an article for a while, but I accidentally wanted to write one this weekend Such a tool has been put into practice, and the results are not bad.

If you have a better way, you can leave your comments, thank you very much~.

Welcome to take photos Correction, the author's skills are still shallow, please correct me if there is any inappropriateness.

Source code

You can view it on github: https://github. com/xdq1553/MySpritesPackTool

You can use npm to install: https://www.npmjs.com/package/sprites-pack-tool

Reference

node-images:https://github.com/zhangyuanwei/node-images

spritesheet:https://github.com/krzysztof-o/spritesheet.js

The article is superficial, I hope you will spare your comments and likes~

Original address: https://juejin.cn/post/7035809483666227230

Author: Bears since childhood

For more node-related knowledge, please visit: nodejs tutorial! !

The above is the detailed content of Take you step by step to develop an atlas packaging tool using node. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete