2D lighting in Defold(SOLVED)

OK, so the projection matrix defines what 3D area that your game will draw from. What you’re trying to do is set it so the render target quad will be drawn over the whole screen. Using an identity matrix for the projection will draw everything in a cube-shaped area from (-1, -1, -1) to (1, 1, 1), so if that’s what you’re using, your flat render quad needs to cover the area from (-1, -1) to (1, 1). You can zoom way in on your main collection to see if that is the case. Possibly you just moved it by a pixel, or you may be using a model that goes from (0, 0) to (1, 1)?

If you want to use a different matrix, the vmath.matrix4_orthographic() function makes it simple, you just give it one coordinate for each side of the cube. So you can use whatever position and size of quad model you want to draw your render targets on, just set the projection matrix so it’s covers the same area.

You can also actually use a sprite, you don’t need a quad model. But the sprite needs to be in its own atlas and should fill the atlas completely (a power of 2 size), so the UVs aren’t weird.

6 Likes

Well I don’t think so, but I have a small demo project here to showw the stuff :-
lighting.zip (1.3 MB)
Could it be the way I extract height and width from the camera ?, playing with wrong coordinates, as the position of objects, when called from factory.create, is also changed.
BTW: here are some ss to show what I mean:


The GUI is rendered properly, both here :point_up: n here :point_down:

(I colored the ambient light red to show everything clearly) But the screen here is scaled half(almost), and most of the sprites too. Moreover the enemies spawn, where they shouldn’t have.
WHy should this happen?

Is this problem only happening after you resize the window? Your example project works perfectly for me, except if I change the proportion of the window, then I get the same problem you have. The one thing that I forgot to mention, that is missing from your render script, is updating the size of the render target when the window changes. You should add the following lines to the part where you update the window size (the if block at the start of update()).

render.set_render_target_size(self.base_target, window_width, window_height)
render.set_render_target_size(self.lights_target, window_width, window_height)

1 Like

No, the problem occurs as soon as I hit ctrl-b on my keyboard

Hmm . . .

If adding those lines didn’t help . . . I think I’m out of ideas. The demo project you put up works fine for me. @britzl, any ideas?

I notice your earlier screenshot didn’t have this problem, do you know what changed between then and now?

Thanks @ross.grams This works as expected now. I was only missing those lines, updating the render(“sigh”).

Just divided the height by 1.3 to have it span the entire screen.

So, screenshot time again :slight_smile: :slight_smile:

4 Likes

Ah! Great!!!

Sorry to necro this thread; was the aforementioned solution regarding dynamic 2d light ever pursued or finished?

3 Likes

This sounds what I’m looking for. The only thing missing is specular mapping for an objects shininess. Would be great if there was a tutorial for it or example

I don’t know if it’s easily doable now without being able to have two textures passed to sprite’s material - or is it guys?