有没有办法创建SKSpriteNode的CGPath匹配大纲? [英] Is there a way to create a CGPath matching outline of a SKSpriteNode?

查看:86
本文介绍了有没有办法创建SKSpriteNode的CGPath匹配大纲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建一个与SKSpriteNode轮廓相匹配的CGPath。

My goal is to create a CGPath that matches the outline of a SKSpriteNode.

这对于创建SKSpriteNodes的发光/轮廓以及物理路径非常有用。

This would be useful in creating glows/outlines of SKSpriteNodes as well as a path for physics.

一以为我有过,但我对CIImage一点都没有用,所以我不知道是否有办法在像素级别上访问/修改图像。

One thought I have had, but I have not really worked much at all with CIImage, so I don't know if there is a way to access/modify images on a pixel level.

那么也许我可以将这样的东西移植到Objective-C:

Then maybe I would be able to port something like this to Objective-C :

http://www.sakri.net/blog/2009/05/28/detecting-edge- pixel-with-marching-squares-algorithm /

同样非常开放的其他方法使这个过程自动化,而不是为每个精灵创建形状路径我用于物理或轮廓/发光效果。

Also very open to other approaches that make this process automated as opposed to me creating shape paths for every sprite I make for physics or outline/glow effects.

推荐答案

您正在寻找的是一种轮廓追踪算法即可。 摩尔邻居追踪很受欢迎,适用于图片和拼贴图。但请查看备选方案,因为它们可能更适合您的用途。

What you're looking for is called a contour tracing algorithm. Moore neighbor tracing is popular and works well for images and tilemaps. But do check out the alternatives because they may better fit your purposes.

AFAIK行进方块和轮廓跟踪密切相关,如果不是相同(类)算法。

AFAIK marching squares and contour tracing are closely related, if not the same (class of) algorithms.

瓷砖地图(从瓷砖创建物理形状)的实现包含在 Kobold Kit 中。该算法的主体位于 KKTilemapLayerContourTracer.m的traceContours 方法

An implementation for tilemaps (to create physics shapes from tiles) is included in Kobold Kit. The body of the algorithm is in the traceContours method of KKTilemapLayerContourTracer.m.

它看起来比实际上更复杂,另一方面需要一段时间才能绕过它,因为它是一种行走算法,意味着结果在当前步骤中使用先前步骤来做出决定。

It looks more complex than it really is, on the other hand it takes a while to wrap your head around it because it is a "walking" algorithm, meaning the results of prior steps is used in the current step to make decisions.

KK实现还包括一些专门用于tilemaps的小修复(即两个或多个水平或垂直连接的tile成为一行,而不是将行划分为tile-size段)。它也是使用自定义点数组结构创建的,当我将它移植到SK时,我决定继续使用它并且只在最后将点数组转换为CGPath对象。

The KK implementation also includes a few minor fixes specifically for tilemaps (ie two or more horizontally or vertically connected tiles become a single line instead of dividing the line into tile-sized segments). It was also created with a custom point array structure, and when I ported it to SK I decided it would be easier to continue with that and only at the end convert the point arrays to CGPath objects.

如果您可以安全地假设您尝试跟踪的形状不会触及边框,并且不能有任何仅对角连接的图块,则可以进行某些优化。当你为自己的目的实际实现算法时,所有这一切都变得更加清晰。

You can make certain optimizations if you can safely assume that the shape you're trying to trace is not going to touch the borders, and there can not be any tiles that are only connected diagonally. All of this becomes clearer when you're actually implementing the algorithm for your own purposes.

但是,就现成的,适合所有目的的解决方案而言:没有。

But as far as a ready-made, fits-all-purposes solution goes: there ain't none.

这篇关于有没有办法创建SKSpriteNode的CGPath匹配大纲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆