1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

Trying to implement Real-time lighting in 2d

Discussion in 'Programming' started by Palocles, Jan 26, 2020.

  1. Palocles

    Palocles Member

    Joined:
    Dec 3, 2019
    Posts:
    30
    I’ve just run through this tutorial: https://www.yoyogames.com/blog/419/realtime-2d-lighting-in-gamemaker-studio-2-part-1 and part 2 and i’m having some pretty weird results.

    Is anyone familiar with the tutorial? Have it working in their games?

    If you can point me in the right direction to fix the following I would appreciate it.

    1. Game runs super slow when light source object exists.

    2. Shadows are not cast from objects.

    3. Top left of the light circle almost has correct fade but other quarters fade from dark to light as they move away from the light source.

    Additionally, i’d Like to make all objects outside the light radius hidden by darkness.

    Could I achieve a similar effect to the circle of light (but not shadows cast from objects) by overlaying a sprite to the light source which is a black mask with a faded circle of transparency in the middle?

    Thanks.
     
  2. Palocles

    Palocles Member

    Joined:
    Dec 3, 2019
    Posts:
    30
    Well, I answered my last question in the previous post, it is easier to masking sprite with a transparent hole in it.

    Was a bit of a struggle in Aseprite but far less hassle than the above coding exercise.

    I thought it would be cool to have the objects cast shadows too, so would still be interested in solutions.
     
  3. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    296
    Casting real time shadows can get a bit complicated, if you're happy to spend a little coin the best prebuilt system is probably Aurora from nocturne, look it up on the market. Another option is fg lighting, it works okay but many light sources can get dicey. If you want to build your own do a search for gm dynamic shadows in Google, there is a whiole blog post on yoyo site about how to do it using surface and shader.

    With that said, I thought I would mention something. All of these shadow systems produce smooth aliased adges, even when you turn it off you don't get pixelated edges, you just get more blurry edges. So, I mention this because if you are making a pixel art game and you want shadows to have pixelated edges to match your art style you are going to run into issues.

    The solution is to draw the shadow surface at the games base resolution and scale it back up to the applicatio surface, but the problem there is that drawing the shadow surface at base resolution means you lose sub pixels and so when scaled back up the shadows appear to have jagged animations which you may not want. That happens because the shadow surface is the size of the view and follows the camera. To fix that you can make the surface the size of the room and use draw_surface_part instead, but then that too has problems and not the best for fps. If you can live with the aliased edges then you'll be good.
     
  4. Palocles

    Palocles Member

    Joined:
    Dec 3, 2019
    Posts:
    30
    Aliased edges wouldn’t be an issue.

    I think the tutorial/blog post you mentioned is the one I tried and linked.

    Actually, what I would find more useful with it is not the objects casting shadows but the multiple light sources. Then I could have one following the player and others to light up specific things in the room that the player needs to see or would have their own light. Like the fire ball shooting traps.

    Anyway, I think I can live with the masking sprite i’m using for now. I’ll redo it when I can use Gimp, to increase the size of the fade area as I can only go up to 64 pixels in Aseprite, the fade is very abrupt.
     

Share This Page