Lit: a light engine


(Alex Larioza) #1

@jasonhpickering was looking for a lighting engine so here’s one that I created for my previous game Infested Space. I also must give credits to @Noel as I used his library from the old FlashPunk forums as a starting point. Basic usage can be found in the readme and also the demo provided.

GITHUB
DEMO
WEBPAGE


Dynamic Lighting, huge performance issuses
Draw.withCode or using Sprites?
Flashpunk and lighting?
Most efficient way to fake lighting
Flashpunk and lighting?
A more efficient way to do tile-based lighting?
(Alex Larioza) #2

Added a dedicated page to my website along with a demo! Check the original post for links.


(TheHuckleberryWill) #3

:smile: Great! Will be using this in my game, thanks so much!


(Alex Larioza) #5

@brendyntodd I’m not exactly sure what edges you are talking about. The lights are created by images in the demo’s asset folder. If you want them to “fade” smoother, you’d simply improve the quality of the gradient.

Note that the light images in the demo are just quick placeholders, you can use any image you want.


(Suyash Mohan) #6

Great!! This is what I was looking for. I have an Idea for my next game in my mind and this library will surely help me :smile:


(John Andersson) #7

Hey. I tried adding this to my game, but even with only one light, the fps drops by 15! Is there any updated version somewhere or something? The performance is really lacking.

Any way to add colored lighting?


(Alex Larioza) #8

That doesn’t sound right. Can you post the relevant code?


(John Andersson) #9

GameWorld:

	public var lighting:Lighting;
	public var mouseLight:Light;

override public function begin():void 
{
    add(lighting = new Lighting(FP.screen.width, FP.screen.height));

    var image:Image = new Image(SPR_LIGHT_CIRCLE_GRADIENT);
    image.centerOO();
    mouseLight = new Light(0, 0, image, 4);
    lighting.add(mouseLight);
}

override public function update():void 
{
super.update();	
mouseLight.x = mouseX;
mouseLight.y = mouseY;
}

That’s it!

Also, I tried making an entity which emits light (a lantern, but I can’t figure out how to make it work!) I tried this:

Lantern:

	public var lighting:Lighting;
	public var lanternLight:Light;
	
	public function CommonersLantern(xt:Number, yt:Number)
	{
		layer = 1;

		x = xt;
		y = yt;
		
		setHitbox(Assets.LANTERN, Assets.LANTERN);
		
		spritemap.add("glow", [0, 1, 2, 3, 4, 5, 6, 7], 15, true);
		
		graphic = spritemap;

		_held = false;
		
		var image:Image = new Image(SPR_LIGHT_CIRCLE_GRADIENT);
		image.centerOO();
		lanternLight = new Light(0, 0, image, 4);
		GameWorld.lighting.add(lanternLight);
	}
	
	override public function update():void
	{
		spritemap.play("glow");
		GameWorld.lanternLight.x = x;
		GameWorld.lanternLight.y = y;
		
		super.update();
	}

But yeh, then I had to make “lighting” a static variable, but that just made it all weird.

Is colored lighting something you can use with this?? And how do you increase the intensity of the light? I know you can scale it and change alpha, but how can I make it brighter?


(John Andersson) #10

Any ideas? :stuck_out_tongue: I would really like to use this


(John Andersson) #11

So I tried adding it into a fresh new game, and it works great. The FPS is at a solid 60. BUT, now there is a weird issue. At some places on the map, the one light cuts off the other one. They don’t blend!


(Alex Larioza) #12

Sorry about the delay in responses. Our of curiosity, did you figure out why the FPS was so low in the other project? Maybe you were adding multiple lighting objects?

As for your new problem, it looks like you’ve set the blend mode to normal. When you say “some places on the map” where exactly does this happen? I might be relevant in tracking down what causes this.


(John Andersson) #13

No problem :smiley:

It seems to be at random places, some places blend just fine, some don’t. If I increase the scale of every light, then there are way more intersections. I never figured out the FPS issue.

It was apparently at SUBTRACT. If I set it to NORMAL, then it looks like this


(Jacob Albano) #14

It’s supposed to be SUBTRACT.

What are the dimensions of your screen? If they’re to high and/or you have a lot of lights, the performance would naturally take a hit.


(John Andersson) #15

There is no performance hit as I’ve added it in a new game, the only problem now is the whole “shadow appears as a line instead of blending with the other light source”


(Alex Larioza) #16

Are you sure you don’t have two lighting objects added to the scene?


(John Andersson) #17

Ehm, let me show you the code:

	[Embed(source="../assets/graphics/gradients/circle_3.png")]
	public static const GRADIENT_CIRCLE_3:Class;
	
	private var lightsource:Image = new Image(GRADIENT_CIRCLE_3);

	public var lighting:Lighting;


            add(lighting = new Lighting(FP.screen.width, FP.screen.height));
		lightsource.centerOO();

            if (tile.@gid == GID_torch_1)
	{
		FP.world.add(new Torch(new Point(int(tileX * 16), int(tileY * 16 ))));
		lighting.add(new Light(tileX * 16 + 8, tileY * 16 + 16, lightsource));
	}

a total of 8 light sources


(Alex Larioza) #18

Well this code is out of context, I just wanted to make sure you weren’t accidentally adding multiple instances of the Lighting class to the game and I can’t tell from the snippets you posted.


(John Andersson) #19

how can I see how many instances of the lighting class are active?


(John Andersson) #20

please respond before I eat you


(John Andersson) #21

hello? :stuck_out_tongue: I would really like to use this