【发布时间】:2018-10-28 09:36:06
【问题描述】:
我一直在尝试使用 CIFilter 复制 SKSpriteNode 着色方法,以便在另一种类型的 SKNode 上使用它,例如 SKEffectNode。 为 Sprite 节点着色 下的This reference 提供了一种简单地为 sprite 着色的方法。我怎样才能对任何 SKEffectSprite 做同样的事情。
我在 CoreGraphics 中为这种着色重新创建了一个仿真,向 UIImage 添加了一个tinted 函数(见下文)。它工作正常并给出类似的结果。唯一的问题是 CIImage 和 CGImage 在许多版本的 iOS 上都不能很好地运行(错误!)。
我现在转向使用合成过滤器设置标准 CIFilter,但没有成功。
This page 提供了多个选项,但在不知道公式的情况下,似乎很难将我在 CoreGraphics 中所做的与 CoreImage 中的任何特定过滤器相匹配。有没有办法让 CIFilter 现有的复合过滤器链得到与 SKSpriteNode 中的着色算法相似的结果?
extension UIImage {
public func tinted(color: UIColor, colorBlendFactor: CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext() else { return self }
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)
let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
context.setBlendMode(.normal)
UIColor.black.setFill()
context.fill(rect)
context.setBlendMode(.normal)
let cgImage = self.cgImage
context.draw(cgImage!, in: rect)
context.setBlendMode(.color)
color.withAlphaComponent(colorBlendFactor).setFill()
context.fill(rect)
context.setBlendMode(.destinationIn)
context.draw(cgImage!, in: rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
return image!
}
}
【问题讨论】:
-
如果您在 sprite kit 中执行此操作,那么您不想使用 CGContext。你想使用着色器。我不确定我是否在这里关注您的问题,但是为什么不将精灵节点作为效果节点的子节点?
标签: ios sprite-kit cifilter