1

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

Jarron
  • 1,049
  • 2
  • 13
  • 29

1 Answers1

1

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

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • The behaviour I'm getting is it is actually adjusting to the pixel alpha value, if my mask pixel alpha value is 0.25, my image pixel alpha value will go to 0.25, whereas before if the mask value was 0.25, the image value would be 1.0. When I add the randomCropNode I get my desired effect, but from what Ive read on the internet, the pixels of the image shouldn't adjust to the mask. Sorry this probably doesn't make sense. Basically I want the pixels to adjust to the mask, but I'm not sure why it is doing it in this instance only. – Jarron May 07 '16 at 13:10
  • Like I said, it could be just rendering your mask as a normal texture instead of actually being a mask – Knight0fDragon May 07 '16 at 13:12
  • Sorry I get what your saying, but no it's not, I'll attach the image shortly. The image is 4 shades of black and then I add a white fill and it turns to 4 shades of white. – Jarron May 07 '16 at 13:13
  • Is there any holes (0 alpha) – Knight0fDragon May 07 '16 at 13:14
  • 4 shades of black on top of white will give you 4 shades of white when rendering, does not mean cropping is happening – Knight0fDragon May 07 '16 at 13:16
  • yes cropping is happening, sorry I'm just getting the images now – Jarron May 07 '16 at 13:18
  • Added the images now – Jarron May 07 '16 at 13:25
  • Yes I see it now, That is definitely weird, and it only happens when adding a crop node, not another node. May be a bug with how 2 crop nodes get rendered, but I have answered the reason as to why it doesn't work without adding it – Knight0fDragon May 07 '16 at 13:34
  • Yes it only happens when I add the extra crop node. That extra crop node doesn't serve any purpose, so it is strange that it then renders the image the way I want it to once added? I'll just continue to add an additional crop node so that I get my effect, but I still not sure why it doesn't perform this way all the time, because if I wanted a solid image cropped, I would use a solid mask. – Jarron May 07 '16 at 13:47
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111294/discussion-between-knight0fdragon-and-jarron). – Knight0fDragon May 07 '16 at 13:48
  • 1
    It is not built to work that way, and may/could get fixed in future versions of iOS – Knight0fDragon May 07 '16 at 13:48
  • Well your last comment pretty much summed up what I was chasing. I was just checking to see if I was getting my desired effect the wrong way, but from what I've read on the internet and what you have just told me, this effect shouldn't happen regardless. If Apple does fix it, hopefully they still give you an option to adjust your image's pixels in accordance to the mask pixel alpha value. Thanks for your comments and time! – Jarron May 07 '16 at 14:01