如何实现一种可以通过透明度将纹理切片为多个精灵的算法?

-3 graphics 2d sprite unity-game-engine

我想实现Unity具有的功能:http : //docs.unity3d.com/Manual/SpriteEditor.html
“将切片类型设置为自动,编辑器将尝试通过透明度来猜测精灵元素的边界”,什么是实现这一点的最简单方法吗?

Krz*_*rko 5

您必须找到精灵的边界框。为此,您必须将像素标记为已使用和尚未使用。

在伪代码中:

set every transparent pixel as used
// now you should have separate sprites marked as unused

while at least one pixel not used:

  position=find first unused pixel

  bounds=flood fill from position as used, saving bounds of the fill

  if bounds intersect any previous found sprite position:
    throw error

  save bounds as a sprite position
Run Code Online (Sandbox Code Playgroud)

示例(精灵源):

首先你有一个透明背景的精灵表:

示例步骤 1

然后按照使用情况填充背景:

示例步骤 2

然后找到第一个未使用的像素:

示例步骤 3

然后洪水填充精灵,你的洪水填充的边界就是你的精灵的边界。

示例步骤 4


您可能想要修改第一步,以便为精灵留出余量 - 这样分离效果会更好,并且不会留下任何悬空像素。

示例分离