使用 SKCropNode 时,我希望添加到cropNode 的图像能够根据相应的蒙版像素 alpha 值调整每个单独的像素 alpha 值。
经过大量研究,我得出的结论是图像像素 alpha 值不会根据蒙版进行调整,但是在继续我的项目后,我注意到一个特定的裁剪节点图像的像素实际上正在褪色蒙版像素 alpha 值???太棒了!然而,在重现这个之后,我不知道它为什么这样做?
import SpriteKit
var textureArray: [SKTexture] = []
var display: SKSpriteNode!
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
backgroundColor = UIColor.greenColor()
fetchTexures()
display = SKSpriteNode()
let image = SKSpriteNode(texture: textureArray[0])
display.addChild(image)
let randomCropNode = SKCropNode()
display.addChild(randomCropNode)
let cropNode = SKCropNode()
cropNode.maskNode = display
let fill = SKSpriteNode(color: UIColor.whiteColor(), size: frame.size)
cropNode.addChild(fill)
cropNode.zPosition = 10
addChild(cropNode)
}
func fetchTexures() {
var x: Int = 0
while x < 1 {
let texture: SKTexture = SKTextureAtlas(named: "texture").textureNamed("\(x)")
textureArray.append(texture)
x += 1
}
}
}
上面的代码给了我想要的效果,但是如果删除下面的代码,图像像素 alpha 值不再根据蒙版进行调整?下面的代码实际上并未在我的项目中使用,但这是我调整像素 alpha 值的唯一方法。
let randomCropNode = SKCropNode()
display.addChild(randomCropNode)
任何人都可以看到是什么导致了这种行为,或者是否有更好的方法来达到我想要的效果?
面具:
结果:
如果删除:
let randomCropNode = SKCropNode()
display.addChild(randomCropNode)
结果:
最佳答案
只有当 Alpha 在 <.5(关闭)和 >=.5(开启)之间变化时,裁剪节点才会打开和关闭像素
然而,要应用淡入淡出,如果您的 alpha 蒙版只是黑色(具有不同的 alpha 级别)且透明,则可以将蒙版作为常规纹理应用到裁剪节点,然后让 alpha 混合处理淡入淡出效果。
至于您的代码问题,您确定您的裁剪节点正在裁剪,而不仅仅是渲染纹理吗?我不知道尝试重现它的纹理是什么样子。
The node supplied to the crop node must not be a child of another node; however, it may have children of its own.
When the crop node’s contents are rendered, the crop node first draws its mask into a private buffer. Then, it renders its children. When rendering its children, each pixel is verified against the corresponding pixel in the mask. If the pixel in the mask has an alpha value of less than 0.05, the image pixel is masked out. Any pixel not rendered by the mask node is automatically masked out. https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKCropNode_Ref/#//apple_ref/occ/instp/SKCropNode/maskNode
关于swift - SKCropNode 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37088465/