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

查看:26
本文介绍了有没有办法创建 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-pixels-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 中包含了 tilemaps 的实现(从 tile 创建物理形状).算法主体在 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 实现还包括一些专门针对瓷砖地图的小修复(即两个或多个水平或垂直连接的瓷砖变成一条线,而不是将线分成瓷砖大小的段).它也是使用自定义点数组结构创建的,当我将其移植到 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天全站免登陆