1

UPDATE It appears this happens with any blendmode - not just erase

I've been working on a crude lighting engine for a game of mine in Adobe AIR, written in pure AS3. How it works is there is a bitmap data the size of the screen, and at the beginning of each frame it is set to a black transparent rectangle. During that step, things can subtract from the bitmap data and then when it is drawn on the screen those areas will appear lighter.

Setting that up and everything has gone smoothly. Currently I'm messing around with a basic radial gradient from the light source, and setting that up was pretty easy

_circ.graphics.lineStyle();
_circ.graphics.beginGradientFill(GradientType.RADIAL, [0x000000, 0x000000], [.9, 0], [0, 255]);
_circ.graphics.drawCircle(0, 0, 100);
_circ.graphics.endFill()

Then to draw (well, anti-draw) it onto the bitmap data, I just do this

FP.matrix.identity();
FP.matrix.tx = x + 30;
FP.matrix.ty = y + 5;
WorldGame.darkBit.draw(_circ, FP.matrix, null, BlendMode.ERASE, null, true);
FP.matrix.identity();

FP.matrix is just a generic global matrix (I'm using Flashpunk). Now this ALMOST works, except for one tiny problem.

bad things

If you look very closely you'll be able to see a very thin black line going around the gradient, which is so very frustrating. I have no idea what's causing it - I've tried making the gradient smaller than the circle, I've tried making it linear, tried making it a different colour. It doesn't happen on blend modes other than ERASE as well, and as far as I can tell, it doesn't happen to non-gradients either (still erasing).

Any suggestions?

Pinpickle
  • 984
  • 2
  • 9
  • 18
  • Try to change linestyle color to red, will it be visible? – alxx Nov 23 '12 at 05:54
  • @alxx That didn't work sorry. – Pinpickle Nov 23 '12 at 08:19
  • Ok, alpha of 0 in linestyle doesn't change anything too? – alxx Nov 23 '12 at 08:27
  • Well the line is visible, but it's all grainy and full of weird artifacts like the thin line in the example - just thicker. Weird as the alpha is set to 0 – Pinpickle Nov 23 '12 at 09:04
  • Maybe dumb idea, but... what if you increase circle radius? Gradient is controlled by beginGradientFill params, so it won't be affected. Of course, this may degrade performance. If that edge goes out of the window, it's no problem anymore. – alxx Nov 23 '12 at 09:07
  • Just to clarify - having the stroke with an alpha of 1 and a colour of red is visible the same way it is with alpha of 0, just all grainy as well. What I meant is that it didn't get rid of the hairline width artefact. – Pinpickle Nov 23 '12 at 09:07
  • I'd really rather not do something like that (increase circle radius). It seems like a huge workaround and in some cases that'd need to be a really wide circle to make it invisible. – Pinpickle Nov 23 '12 at 09:17

1 Answers1

1

Strange behaviour when using BlendMode "erase" in flash AS3

Somehow I missed this in my earlier searching, guess I didn't use the right keywords. The solution is just to set bitmap caching to true on the sprite that is being used as the "eraser". For example:

_circ.cacheAsBitmap = true;

And that's all there is to it!

Community
  • 1
  • 1
Pinpickle
  • 984
  • 2
  • 9
  • 18