Hello Guest

Author Topic: Applying colors to units, reusing collections, minimizing draw calls?  (Read 5508 times)

alcove

  • Newbie
  • *
  • Posts: 5
    • View Profile
If one was making a game with many sprites, but with different armies of various colors, any ideas on what would minimize draw calls with 2d toolkit?

2 possibilities were to have greyscale sprites with tints or shaders applied... or basic colored sprites with some kind of over/underlay showing a team color.

But, I'd like to set this up in a way to maximize batching.....

Any thoughts or links would be highly appreciated!
Thanks,
Scott

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Applying colors to units, reusing collections, minimizing draw calls?
« Reply #1 on: November 13, 2012, 09:07:49 am »
The best way to do this is with a shader and greyscale images. The problem then is to identify the colorized parts and tint them.

You could do this in one of many ways - for instance, if your images don't really have any grayscale components, you could identify how grey a color is and tint appropriately, skipping when there is color.

If you don't need alpha blend (i.e. your sprites could work with cutout shaders), you can then use the alpha channel to store this information - i.e. tint when alpha > 0.

You could also tint when alpha = 255, and scale down to 0 by alpha = 254, so you don't really lose anything else, except your sprites will never truly be solid, but that shouldn't REALLY matter. This could work really well.

Hope one of those helps :)

alcove

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Applying colors to units, reusing collections, minimizing draw calls?
« Reply #2 on: November 13, 2012, 01:42:28 pm »
Thanks! The 254/255 tricks sounds especially intriguing!  :D

ascreator

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Applying colors to units, reusing collections, minimizing draw calls?
« Reply #3 on: January 17, 2014, 08:35:21 am »
Hey and sorry for reviving this old thread, but I need exactly the same and your solution sounds great.

I was just wondering if this solution would also work well with mipmapping, as the mipmaps cannot interpolate between 254 and 255 and so the resulting pixel would be completely tinted or completely untinted.

Imo a possible solution for mipmapping would be: alpha colors 245 to 255 represent tinted pixels with 255 meaning fully tinted and 245 meaning almost no tint. Then the shader would have to use alpha 255 for all alphas 244 to 255 internally and calculate the tinting based on the alpha: tintfactor = (alpha - 244) / 11f;
With this solution everything should work execpt of tinted parts being on alpha blended areas (edges of the sprite). Right?
Would that be possible and performant on a mobile device? Would it be faster than drawing every tinted unit twice, once for the basic sprite and once for the team colored parts, as we are doing it now?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Applying colors to units, reusing collections, minimizing draw calls?
« Reply #4 on: January 17, 2014, 11:07:10 am »
It should work you are always free to modify the range further if needed.
Performance wise, its much harder to guess, as it depends on many factors. If your shader is optimized it shouldn't be too bad, eg. you can do this with just a sign(x - 253/255) * tint to extract a tint value - tools like PVRShaderEditor should give you a very good idea on the complexity of your shader.

ascreator

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Applying colors to units, reusing collections, minimizing draw calls?
« Reply #5 on: January 17, 2014, 12:46:01 pm »
Thanks for the fast answer. I will try it out and report here.
And also thanks for the hint to the shader editor!