Author Topic: How to achieve additive blending?  (Read 1925 times)

hima

  • Member
  • **
  • Posts: 96
  • Karma: +0/-0
    • View Profile
    • My Dev Blog
How to achieve additive blending?
« on: Tue, Oct 13, 2009 »
Hello all,
  I was wondering how to do additive/subtractive blending in ActionScript3? Or how to integrate that into flixel? I've tried googling it but it doesn't really give me anything @_@; 

 Thank you in advance!

biomechanic

  • Active Member
  • ***
  • Posts: 107
  • Karma: +0/-0
    • View Profile
Re: How to achieve additive blending?
« Reply #1 on: Wed, Oct 14, 2009 »
Try these links:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BlendMode.html
http://www.lostinactionscript.com/blog/index.php/2009/05/26/custom-blend-modes-for-flash-10/

Here's an explanation of blend modes' behaviours:
http://en.wikipedia.org/wiki/Blend_modes#Addition

My take on making a custom blending thingy from scratch would be to take background and foreground pixel value (for each pixel in the blending sprite) and then do whatever is needed for the specific mode I want.

hima

  • Member
  • **
  • Posts: 96
  • Karma: +0/-0
    • View Profile
    • My Dev Blog
Re: How to achieve additive blending?
« Reply #2 on: Sat, Oct 17, 2009 »
Thank you, biomechanic! That really helps a lot :) I can get blending to work in my game now.

mklee

  • Member
  • **
  • Posts: 68
  • Karma: +0/-0
    • View Profile
Re: How to achieve additive blending?
« Reply #3 on: Sat, Oct 17, 2009 »
Changing the blend mode for FlxSprite is pretty easy actually. What I do is add a string variable called blendMode (which defaults to BlendMode.Normal). In the render function for FlxSprite add a check in the if statement to see if BlendMode is set to something other than BlendMode.Normal (that way if the blend mode is the default setting it copies the FlxSprite using copyPixels. Finally change the FlxG.buffer.draw function to include the blendMode variable.

In the end it should look something like this in render():

Code: [Select]
if((angle != 0) || (scale.x != 1) || (scale.y != 1) || (blendMode != BlendMode.NORMAL))
{
...
FlxG.buffer.draw(_pixels,mtx,null,blendMode);
}

The only issue is that draw is way slower than copyPixels. Ideally this is used sparingly in your code (or if someone has a faster way of doing this).