Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 (SpriteKit): Changing the alpha value of the parent which affects all children

Say for instance I create SKShapeNode and create children for it (an example is a cross and its two intersecting lines). The cross is the parent, and the intersecting lines are the children. How could you change the alpha value of all the parent's children? Currently just using:

parent.alpha = 0.5

doesn't change the alpha value of its children.

If anyone is able to maybe create a shader that could solve this issue or use any other way, please reply. This post is in response to my previous post: (Swift: Make translucent overlapping lines of the same color not change color when intersecting) I am trying to make the darker spots where translucent lines intersect disappear if someone is able to solve this issue.

Here is some code as an example:

let nodeParent = SKShapeNode()
let nodeOne = SKShapeNode(circleOfRadius: 5)
let nodeTwo = SKShapeNode(circleOfRadius: 5)

//Set the color of the circles, I'm not sure if this is the right way 
nodeOne.fillColor = UIColor.init(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
nodeTwo.fillColor = UIColor.init(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)

nodeParent.addChild(nodeOne)
nodeParent.addChild(nodeTwo)
addChild(nodeParent)

nodeParent.alpha = 0.5

I have taken a screenshot of the problem while using lines instead of circles:

Thanks.

like image 605
J.Treutlein Avatar asked Dec 22 '16 04:12

J.Treutlein


1 Answers

This is the correct and intended behaviour, for SpriteKit blendModes, largely because they don't have a like-for-like blending mode that ignores anything the same as itself.

When you reduce the opacity of several nodes, that are still drawn sequentially, as these are, you get a cumulative result under any blending positions. That's why the bright part at the middle.

In my answer to your previous question, I was avoiding cumulative rendering by "flattening" both axis of the cross into a texture, then loading that texture as a SpriteNode, a one time rendering object that has no intersections because it's all one thing.

Anything you want to have avoid this kind of cumulative, sequential rendering effect of less than full opacity will require you to first "flatten" it with all objects/lines/shapes at 100% opacity, first, then apply Opacity changes to get alpha effects.

This is why I said, in that first "answer", that it's not really a solution for your bigger problem, just a way to get the desired result.

Without a like-to-like ignores rendering mode, SpriteKit isn't able to do overlays without a cumulative impact on anything that's got less than full opacity.

like image 109
Confused Avatar answered Oct 21 '22 19:10

Confused