Swift 3 (SpriteKit): Parent alpha change that affects all children

Say, for example, I create an 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 children. How to change alpha value of all parent children? Currently only used:

 parent.alpha = 0.5 

does not change the alpha value of its children.

If someone can create a shader that can solve this problem or use any other method, answer. This post is in response to my previous post: ( Swift: make translucent overlapping lines of the same color without changing color when crossing ) I'm trying to make dark spots where the translucent lines intersect if someone can solve this problem.

Here is a sample code:

 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 took a screenshot of the problem when using strings instead of circles:

Thanks.

+5
source share
1 answer

This is the correct and assumed behavior for blendModes SpriteKit, mainly because they do not have a blend mode similar to the how-to principle, which ignores the same thing as itself.

When you reduce the opacity of several nodes that are still drawn sequentially, as they are, you get a cumulative result for any adjacent positions. That is why the brightest part is in the middle.

In my answer to your previous question, I avoided cumulative rendering by “smoothing” both axes of the cross into the texture, and then loading this texture as SpriteNode, a one-time rendering object that does not intersect because it is all one thing.

All that you want to avoid such a cumulative, consistent effect of rendering less than full opacity will require you to “smooth” it with all objects / lines / shapes with 100% opacity first, first, then apply the opacity changes to get alpha effects.

That's why I said in this first “answer” that this is not really a solution to your big problem, just a way to get the desired result.

Without such a function, it ignores the rendering mode, SpriteKit cannot perform blending without cumulative impact on everything that received less than full opacity.

+3
source

Source: https://habr.com/ru/post/1261684/


All Articles