Help needed with 2d platformer lights

I am working on a 2d platformer where once a player goes below y=0 world darkens and they shoulb be able to use a flashlight to see in the dungeon. I followed 2D lights and shadows sample - #20 by Haath to get where i am with my implementation today. I am very new to game dev (few months) and Defold is my first game engine.

  • Currently the light cone is rendered correctly and light collisions with tiles is working as expected
  • I am also rendering entire world to RT and applying ambient color in the shader to make entire screen darker when user goes below y=0

My issue: I cannot figure out a way how to render the world behind the light cone when the whole screen is dark.

  • I have tried passing a world texture to the fragment shader that draws the light, which works but the issue is that this texture is an entire screen whereas i only need it to be whats behind the light.
  • I have attempted to render a small square around the player to an RT in hopes to pass that as texture but i honestly have no idea if its possible or not and it didnt really work…

Below area. a few screenshots of the current state. Any tips or help is appreciated.


Here is the branch for my code https://github.com/BetweenTwoPines/subterra-scavengers/compare/main...lighting-poc

It a bit of a mess as i probably have refactored everything a million times and at this point its pure frustration…

1 Like

I am relatively new t lights myself. Do you have an example of what it is you wish to accomplish?

Yeah, in the forum post that i linked above, the very last post has the image that shows light cone with visible background inside of it while everything else is dark

You can see in my game examples above, the light cone kind of works, but its sampling the entire world rather whats behind it.

what is the color level / value of your ambient color? If you set it to complete blackout { vmath.vector4(0,0,0,1) }, what happens?

If i set my ambient color to blackout this is what i get

this is the desired behavior but i want it to only show stuff thats behind the cone

the rightmost tree in the cone of light is actually a tree thats all the way on the rightmost of screen and is not actually “behind the cone”

So the render target has incorrect tile / object information. That I would have to understand the game itself to better help.

Is the black out (or close)) the darkness you were looking for?

Making screen darker as player moves below y-axis is working as expected. My issue is not being able to render the correct world information inside of the cone of light. I dont know how to pass a correct texture to the shader that is drawing the actual light (aka i am currently passing the entire world/screen vs just the area around the player)

Okay. I jave to see how I handle it in my game once I make it home… then maybe we can figure it out

1 Like

Home now. Do you have a test project? I see you are trying to grab image info from a screen location and size. My way uses the whole scene. But I suppose we can still try.

yeah you can get a project at on this branch https://github.com/BetweenTwoPines/subterra-scavengers/tree/lighting-poc

1 Like

not sure what this means? I did not withdraw anything

I did. deleted message

@pixelpusherninja - just grabbed the project link.
Hope to have a look sometime today - have a few other things to do atm.

Ahh. I think I see what you mean about how you want it to work. You need a collision mask (for the empty areas and obstacles) and I assume the light will have a shadowed look.

This feels like a little raycast shader might be best. Then you can pass in the collision mask (as a texture) and the lighting can be adjusted based on it. Will dig in later today.

Oh. Before I have to head out. A couple of questions about specific features (will impact solution):

  • What sort of objects do you want to shadow from the torch? (if any)
  • Are there going to be other light sources (like wall lights in mine or something)?
  • Does the whole lighting want to be applied as you go down, or would you prefer lighting around the player to get darker as you do down?

Sorry got busy with work and just getting back to this.

  • What sort of objects do you want to shadow from the torch? (if any)

For now its just tiles (in my game tiles are just game objects) and i think for the intial implementation thats all its gonna be.

  • Are there going to be other light sources (like wall lights in mine or something)?

Potentially, but not right away at least.

  • Does the whole lighting want to be applied as you go down, or would you prefer lighting around the player to get darker as you do down?

Player will have an ability to turn on and off the flashlight while being underground. So i guess only apply the lighting when the flashlight is on

I like that you have an idea of how this may work, but even after re-reading this message a couple times i am not really sure where to even start to attempt doing what you suggested. I probably need to learn about raycasting and collision masks.

Hi again. I have something like this:


This is the same as what you have with a little more complex shader for the torch.

What I think would work well:

  • Convert this torch to a raymarch type (this will give you shadows automagically)
  • You will also need a mask.

I’m looking at your tiles, and you could build a bit mask of “empty” tiles into a texture that you pass to the shader. And in the torch shader it checks that mask for where the torch rays can be traced.

I’ll zip what I have here and if I get time tomorrow I’ll add in some examples for building a texture (prob only needs to be 8 bit greyscale) for setting your tiles in it. Will need to add tile map offset and so on, so it accurately matches what the camera is looking at (similar to the torch uv setup).

subterra-scavengers.zip (5.9 MB)

Side note: The overall lighting isnt working because the torch shader just overrides it :slight_smile: … can easily fix by using that better in the shader. Will clean that up too.

2 Likes

Oh wow, that looks awesome! thank you so much for taking a look at it, i really appreciate it. I have spent so much time at this without any luck and this looks a step in the direction i want to go towards! I will take a closer look at the zipped file that you attached later today once i am back home from office.

2 Likes

Ok. I have something thats got a level mask happening. I didnt really quite understand how your tiling was working so it really just a sample with what can be done:

Attached zip as well of project with shader, material and mask creation (mainly in main.lua and render script).
subterra-scavengers.zip (5.9 MB)

Ive layered the mask texture (note that its not aligned - again this is because I really didnt get how you were doing tile lookups. I was also often “portaled” into the middle of the dirt!! :slight_smile:

But if you notice the torch blocks when it isnt in a masked area. You can use these masks for all sorts of things. I added a texture (bottom right) that shows up somtimes (again, not sure whats happening here) where you can see the whole mask.

To make shadows a raycast can be added - essentially a loop that casts from the light position outwards in a cone, and if it isnt in a mask, then its hit something and it will stop casting the light ray.
I wont do all of this since I think its a good way to learn shaders and how they interop with defold, but I hope this helps in some way. Feel free to ask questions about it here. Its not a great solve, but you can see how it would work.

Oh additional note. I moved the darkening based on your tile y position as you go down. So the quad shader now does:

  • Torch/lighting
  • Light level
  • Light occlusion (with the mask)

Just for completeness. Heres what it looks like with the mask coloring removed.

2 Likes

That…is…awesome! Thank you, so much again. i will be going over the code and if i have any questions i will post them here. As far as tile generation being wonky - its the first time working with perlin and dynamically generated map.