Jarron
Jarron

Reputation: 1049

SKCropNode Strange Behaviour

When using SKCropNode, I wanted the image I add to the cropNode to adjust each individual pixel alpha value in accordance to the corresponding mask pixel alpha value.

After a lot of research, I came to the conclusion that the image pixel alpha values were not going to adjust to the mask, however after just continuing with my project, I notice that one specific cropNode image's pixels were in fact fading to the mask pixel alpha value??? Which was great! However after reproducing this, I don't know why it is doing it?

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

        }
    }

} 

The above code gives me my desired effect, however if you remove the below, the image pixel alpha values no longer adjust in accordance with the mask?? The below code is not actually using in my project, but it's the only way I can make the pixel alpha value's adjust.

let randomCropNode = SKCropNode()
display.addChild(randomCropNode)

Can anybody see what is causing this behaviour, or if there a better way of getting my desired effect?

Mask:

enter image description here

Result:

enter image description here

If remove:

let randomCropNode = SKCropNode()
display.addChild(randomCropNode)

Result:

enter image description here

Upvotes: 1

Views: 676

Answers (1)

Knight0fDragon
Knight0fDragon

Reputation: 16827

Crop node will only turn on and off pixels if the alpha varies between <.5 (off) and >=.5(on)

However to apply a fade, if your alpha mask is just black(with various alpha levels) and transparent, you apply the mask as a regular texture to your crop node, and you let alpha blending take care of the fade effect.

As for your issues with the code, are you sure your crop node is cropping, and not just rendering the texture? I do not know what the texture looks like to try and reproduce this.

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

Upvotes: 1

Related Questions