2

I am making a isometric game using Swift and Sprite Kit. I am rethinking my approach about coloring buildings before they are build.

Like in every game, before user builds an object, he has to move it to the free plot, and by moving it, node will change color from green to red, if the object can or cannot be placed.

Here are some pictures for clarification:

enter image description here enter image description here

What I am doing right now, as i am prety new in Sprite Kit:

I make one SKNode - parent

I make 3 nodes and add them to a parent node. Those 3 nodes contain regular building image, red building image and green building image.

If building should be colored red, I hode nodes containing green and regular images. if it needs to be green, I hide other two and sme goes for regular.

My main question is this:

Can I have only regular node, with regular image, and somehow overlay it with green or red color (but only overlay non transparent image parts? I was thinking abut using Pixel Shader but I'm not sure if it's the right tool.

If I cannot do that my next question is: Is this 3 nodes in a parent approach a good one or it's better to change the image of one node, depending if I need to show regular green or red object image?

SteBra
  • 4,188
  • 6
  • 37
  • 68
  • 1
    Have you tried setting the sprite's `color` property to red or green and the `colorBlendFactor` to 1? – 0x141E Dec 14 '16 at 19:09
  • Becareful with colorBlend though, I do not think that alpha blending is working correctly. If you have a blue image(ABGR) (1,1,0,0) and try to blend it with green (1,0,1,0) you should be getting cyan. because alpha blending is SRCrgb*SRCa + DSTrgb*SRCa , so we should be getting (1,1,1,0), but instead I get a dark green (1,0,0.2,0). If I merge blue and red, I end up getting black, If I merge blue and cyan, I get blue – Knight0fDragon Dec 14 '16 at 20:25
  • @Knight0fDragon the docs show Apple uses RGBA format not ABGR. Also, where did you get that expression for alpha blending? – Epsilon Dec 15 '16 at 19:50
  • @Epsilon I was explaining the way my numbers were formatted, not the way the numbers are ordered at the programming level. As for the definition, https://developer.apple.com/reference/spritekit/skblendmode/1483074-alpha – Knight0fDragon Dec 15 '16 at 19:53
  • @Epsilon, also I am pretty sure that ios is little endian, so ABGR would be correct since R would be the lowest part in the word, they just use RGBA to avoid confusion – Knight0fDragon Dec 15 '16 at 19:58
  • @Knight0fDragon that link points to a description not an equation. I've never seen colors blended using `SRCrgbSRCa + DSTrgbSRCa` before. – Epsilon Dec 15 '16 at 20:08
  • there are different methods to define the alpha blending, source alpha, 1 - source alpha, destination alpha, 1-destination alpha, among others – Knight0fDragon Dec 15 '16 at 20:48
  • @Epsilon, regardless of the math, logically, blending blue and red should not get black, while blue and green get a very dark green using alpha blending – Knight0fDragon Dec 15 '16 at 20:50
  • 1
    If you decide to implement this as a simple color manipulation shader, please have a look at my blog post on doing a simple shader under sprite kit, this example shows desaturation applied to a node: http://www.modejong.com/blog/post19_spritekit_bw_shader – MoDJ Dec 15 '16 at 20:59
  • I would recommend doing it as a shader, you get more control and you know the gpu is doing the work not the cpu – Knight0fDragon Dec 16 '16 at 19:56

0 Answers0