Jump to content


Photo
* * * * * 6 votes

Light and Shadows V2


  • Please log in to reply
53 replies to this topic

#1 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 01 January 2016 - 02:56 PM

  • Title: Light and Shadows V2
  • Description: Light and Shadows V2
  • GM Version: Any version supporting blend modes and surfaces. (at least game maker 7 and higher. Best to use Studio.)
  • Registered: yes
  • File Type: .gmz, .gml and .pdf (Game Maker Studio)
  • File Size: ±20kb
  • File Link: Download full zip
  • Required Extensions: N/A
  • Required DLLs: N/A
  • Tags: Lighting and shadows tutorial

Introduction

Hello dear all of you! 

This is an long promised updated version of the older tutorial I did, all the way back in 2013 ( http://gmc.yoyogames...howtopic=572561 ). The new code and stuff is a lot better organized and will be much easier to use. I also completely overhauled the explaination and made a neat pdf out of it.

 

Screenshot

This is the type of lighting you will get. You can apply this to top down, of platformer, and if you are very brave, you can even try it on isometric!

mqMsm.png

Also, everything in the above example is customizeable. Lights dont need to have a sprite but I added them here for clarity. Same applies for the casters and the background in the picture.

 

Don't get discouraged! Implementing this lighting is actually pretty easy, I might make a video tutorial for clarity soon™. However, I can't promise anything right now.

 

Downloads:


Credits

For this engine, again, you won't need to give me any credit. Feel free to give it though, I'd love to see what people can do with this engine. I also hope my explaination is clear enough so everone can understand how this engine works and why it works.

 

Thanks for reading!

 

Future Requests:

The future request I got so far are listed below, I might implement them sooner or later.

  • Illuminating casters (a bit)
  • Sprite Shadows
  • Performance updates and static lights
  • Caster rotations
  • Soft blur effect
  • Bounding box init sprite

Edited by flokkienathur, 28 February 2016 - 06:46 PM.

  • 4

#2 chance

chance

    GMC Member

  • Global Moderators
  • 8762 posts
  • Version:GM:Studio

Posted 01 January 2016 - 04:38 PM

This is a good addition to the Tutorials Forum.  The light and shadow effects are well done, and the code seems well written and organized. 

 

I will say, however, that beginners might have trouble following this -- even with the PDF tutorial you provided.  One challenge is that you've broken the code into many separate scripts, so following the logic flow is more challenging than with simpler tutorials.

 

But overall, it's a good example with plenty of explanation about how to use it.  Nice job overall.


  • 0

#3 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 03 January 2016 - 02:42 PM

This is a good addition to the Tutorials Forum.  The light and shadow effects are well done, and the code seems well written and organized. 

 

I will say, however, that beginners might have trouble following this -- even with the PDF tutorial you provided.  One challenge is that you've broken the code into many separate scripts, so following the logic flow is more challenging than with simpler tutorials.

 

But overall, it's a good example with plenty of explanation about how to use it.  Nice job overall.

Thanks, and yes, I might need to clarify a few scripts here and there indeed. Thanks for the feedback!


  • 0

#4 NayDeveloper

NayDeveloper

    GMC Member

  • New Member
  • 2 posts
  • Version:GM:Studio

Posted 09 January 2016 - 11:22 PM

Hi flokkie! First I need to say I love it! Well I have a question its very simple but I need to do it. How I can do all outside the radius of the light be Black with alpha = 1? I dont find it in the scripts :c

Can you help me?


  • 0

#5 NayDeveloper

NayDeveloper

    GMC Member

  • New Member
  • 2 posts
  • Version:GM:Studio

Posted 09 January 2016 - 11:56 PM

Hi flokkie! First I need to say I love it! Well I have a question its very simple but I need to do it. How I can do all outside the radius of the light be Black with alpha = 1? I dont find it in the scripts :c

Can you help me?

One thing more how I controll the Circle of the light alpha?


  • 0

#6 YanBG

YanBG

    GMC Member

  • GMC Member
  • 217 posts
  • Version:GM:Studio

Posted 10 January 2016 - 12:32 AM

Can someone working with it upload screenshots, is it top-down, sideways, isometric etc?


  • 0

#7 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 10 January 2016 - 06:49 PM

 

Hi flokkie! First I need to say I love it! Well I have a question its very simple but I need to do it. How I can do all outside the radius of the light be Black with alpha = 1? I dont find it in the scripts :c

Can you help me?

One thing more how I controll the Circle of the light alpha?

 

If I understand your question right, you just want utter blackness unless there is a light. 

There is a script for that ( lightmap_set_ambient ). The ambient light is the light that is everywhere on the screen. So setting this to 0 will result in a completely black screen, unless there is a light (so calling "lightmap_set_ambient(0.3);"). Don't forget that you need to call this on your lightmap object or else it will not work!

 

 

Can someone working with it upload screenshots, is it top-down, sideways, isometric etc?

It is for top down and sideways mostly, but you can apply this to isometric (a friend of mine did this a while ago). However, isometric is almost 3d with lighting, so this will only work in some cases.

I added a screenshot in the origional post. I can't believe I forgot that! Thanks for pointing it out!


  • 0

#8 kricto

kricto

    GMC Member

  • New Member
  • 3 posts
  • Version:GM:Studio

Posted 13 January 2016 - 03:21 PM

How many points can I add to polygon without performance problems?


Edited by kricto, 13 January 2016 - 03:24 PM.

  • 0

#9 Lash94

Lash94

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 13 January 2016 - 11:03 PM

"avgFPS: 143" 

 

Jesus Christ. Man i absolutely love it, but im afraid this + the AI pathfinding + the 3kx3k map + inventory system will absolutely bomb my performance. 


  • 0

#10 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 14 January 2016 - 12:25 PM

How many points can I add to polygon without performance problems?

Depends on how many objects you have in your scene, and how big your lights are. There is still room for optimisation though.

 

 

"avgFPS: 143" 

 

Jesus Christ. Man i absolutely love it, but im afraid this + the AI pathfinding + the 3kx3k map + inventory system will absolutely bomb my performance. 

Well, I think you are right, although, if most of you map is static you might want to not update the lightmap every frame and just draw it. You can probably draw the lightmap surface only on the view, so you won't draw 3kx3k textures. With only static objects and drawing only on the view (ofc depending on your resolution) you can get pretty decent performance (over 700% increase, at least on my pc).


  • 0

#11 Lash94

Lash94

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 14 January 2016 - 02:26 PM

 

 

 

"avgFPS: 143" 

 

Jesus Christ. Man i absolutely love it, but im afraid this + the AI pathfinding + the 3kx3k map + inventory system will absolutely bomb my performance. 

Well, I think you are right, although, if most of you map is static you might want to not update the lightmap every frame and just draw it. You can probably draw the lightmap surface only on the view, so you won't draw 3kx3k textures. With only static objects and drawing only on the view (ofc depending on your resolution) you can get pretty decent performance (over 700% increase, at least on my pc).

 

 

 

I was trying different alternatives yesterday and everything resembling dynamic shadows destroys framerate. Your code is actually really fast in comparison. So i'm going to try and call update every 2~3 steps instead and limit the use of lights.


  • 0

#12 flatalex123

flatalex123

    GMC Member

  • GMC Member
  • 83 posts
  • Version:GM:Studio

Posted 17 January 2016 - 02:24 PM

This is one good lighting engine but i wanted to know if there was any to make a sun which only used one light and gave the whole level shadows

 

Thanks for the help


  • 0

#13 flatalex123

flatalex123

    GMC Member

  • GMC Member
  • 83 posts
  • Version:GM:Studio

Posted 18 January 2016 - 04:03 AM

I made the sun with a day night day night cycle :) if anyone wants to know want i changed to get it to work pm me

 

EDIT i tested the fps for the sun and i was getting 10-25


Edited by flatalex123, 18 January 2016 - 11:49 PM.

  • 0

#14 Eben

Eben

    GMC Member

  • New Member
  • 4 posts
  • Version:GM:Studio

Posted 24 January 2016 - 11:26 PM

Thanks flokkienathur for making this, it's looking great!

 

Would anyone know how to modify this system so that casters can also be illuminated, at least partially? My game looks a bit strange with only the background being illuminated. Here's what it looks like currently:

 

yRzMHpI.png


  • 0

#15 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 25 January 2016 - 04:18 PM

I made the sun with a day night day night cycle :) if anyone wants to know want i changed to get it to work pm me

 

EDIT i tested the fps for the sun and i was getting 10-25

For a day night cycle hmm.. You might want to look into heights (its in the code somewhere). You will need to modify some code, but it is possible to make a sun light without losing too much performance, like you do right now. 

 

If its your only light in the scene I recommend that, instead of clearing to black, you clear to white and draw the shadows on there (directly to the shadow map). And instead of calculating for each caster in what direction the shadow must go, you can just use one sun direction and length (and multiply that length by the casters height probably). You can get loads better performance doing this stuff instead.

 

 

Thanks flokkienathur for making this, it's looking great!

 

Would anyone know how to modify this system so that casters can also be illuminated, at least partially? My game looks a bit strange with only the background being illuminated. Here's what it looks like currently:

 

 

Looks awesome, thanks. 

 

There is one thing you can attempt here (however, it will cost you some performance):

You can make two lightmap objects. One normally, with shadows, and one without the shadows (lightmap_set_shadows_enabled(false) ). Then, make sure the one without the shadows draws first. That will make everything illuminate a bit more where there is a light. It will look a little like this:

 

mJbvu.jpg

 

This was done pretty quickly and your results may look a lot better if you give it more attention. Just fiddle around with the ambient light setting for both of the lightmaps. 

So two identical lightmap objects, with the only difference being:

obj_lightmap_shadows:

 

lightmap_set_shadows_enabled(true);
lightmap_set_ambient(0.8);
 
obj_lightmap_noshadows:
 
lightmap_set_shadows_enabled(false);
lightmap_set_ambient(0.1);
 
Just play with the ambient settings until you get your desired results. (remember, the lightmaps are being multiplied so all your lights will look a bit different and less bright! ).
Also note: this will take a bit away from your performance... :(
 
EDIT: you could also alter my code to draw the light AGAIN after drawing the shadows. This will get even better results with less performance loss. 
 
draw_set_alpha( <<insert your wanted alpha value here >> );

//not sure if you need this but try it
draw_set_blend_mode(bm_add);

//if it is a point light
if(light_type == 0){
     draw_circle_colour(w,h,light_radius,light_color,c_black,false);
}
//if it is a sprite light
if(light_type == 1){
    draw_sprite_ext(light_sprite,light_sprite_index, w,h,1,1,light_rotation,light_color,1);         
}

Paste this code after the drawing of shadows, just before surface_reset_target(); (around line 100).

 

Good luck! EDIT: did not test this code dont kill me pls


Edited by flokkienathur, 25 January 2016 - 04:53 PM.

  • 1

#16 Eben

Eben

    GMC Member

  • New Member
  • 4 posts
  • Version:GM:Studio

Posted 26 January 2016 - 01:53 PM

^ Wow, thanks for the awesome idea! Here's what it looks like after quickly implementing the second method you mentioned:

 

AwPaGLm.png

 

Much more what I was looking for =D


  • 0

#17 Xer0botXer0

Xer0botXer0

    GMC Member

  • GMC Member
  • 770 posts
  • Version:GM:Studio

Posted 29 January 2016 - 05:55 AM

Im liking this.

 

So this can help create day night too as well ? Setting the darkness. 

Im worried that it will just make everything look dark.. I think I'd have to do something about illuminating things in the night, giving it a glow but making it dark. :/


  • 0

//=====================

Good bye, Farewell GMC! :D

See you all in the next TIER!

======================\\


#18 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 29 January 2016 - 11:51 AM

Im liking this.

 

So this can help create day night too as well ? Setting the darkness. 

Im worried that it will just make everything look dark.. I think I'd have to do something about illuminating things in the night, giving it a glow but making it dark. :/

Depends on what type of effect you want. Why don't you try it out and then tell me what you think, and what you would love to see changed. I might be able to help you there!


  • 0

#19 Xer0botXer0

Xer0botXer0

    GMC Member

  • GMC Member
  • 770 posts
  • Version:GM:Studio

Posted 31 January 2016 - 03:44 PM

I'm getting an error regarding the variable "light_radius".

 

Line 47 of lightmap_update script

"        var lr = light_radius; "

 

light_radius doesn't exist apparently..

 

So I just ignored light radius and gave what ever uses it a specified value, as well as the variable light_type that doesn't exist either. just set it to 0.


Edited by Xer0botXer0, 31 January 2016 - 04:04 PM.

  • 0

//=====================

Good bye, Farewell GMC! :D

See you all in the next TIER!

======================\\


#20 TERNOX

TERNOX

    GMC Member

  • New Member
  • 1 posts
  • Version:GM:Studio

Posted 31 January 2016 - 11:13 PM

Hi! This is very cool!
But, how can I make the shadow of the sprite, for example like this? https://www.youtube....h?v=Qo3xNGmIBIU

Not only from a square or circle. 


  • 0

#21 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 01 February 2016 - 02:01 PM

I'm getting an error regarding the variable "light_radius".

 

Line 47 of lightmap_update script

"        var lr = light_radius; "

 

light_radius doesn't exist apparently..

 

So I just ignored light radius and gave what ever uses it a specified value, as well as the variable light_type that doesn't exist either. just set it to 0.

 

I think you forgot to call light_init_whatever(); on your light object. All the light init functions will initialize the light_radius and light_type variables. If you have you light object as a parent object, make sure that you call event_inherited() in the create event.

 

 

Hi! This is very cool!
But, how can I make the shadow of the sprite, for example like this? https://www.youtube....h?v=Qo3xNGmIBIU

Not only from a square or circle. 

 

For this i'd recommend writing a custom light script, or changing the one i provided to work with this. Basically you could draw the sprite with black blending color (see draw_sprite_ext for more information) on the shadowmap for each light. But this will require you to write some code in the light_update script. I might look into this later, for an update on the engine. For now, look at around line 72 in the light_update script, where the shadows are drawn.


  • 0

#22 InventorOfTheBizarre

InventorOfTheBizarre

    GMC Member

  • New Member
  • 4 posts
  • Version:GM:Studio

Posted 02 February 2016 - 05:27 PM

First of all, awesome lighting system! I've tried my best to understand it so I could adapt it to fit my needs, but I'm kinda stuck at the following, if you guys could help me:

 

 

Since it's one object drawing the lightmap (lighting, shadows, the 'remaining darkness'/ambience/whatchamacallit), it's doing all that at one depth (as you can't change the depth during drawing as far as I've found).

 

This means that either a player object's shadow is being drawn on top of him or below him (depending on the depths of the player and the obj_lightmap). The same (respectively top or below) applies to when he steps out of the light and into the 'remaining darkness'.

 

I've made it hard for myself by having sort of 'skewed' sprites so you can see character's faces and backs instead of the top of their head, but nevertheless, even the walls and overhead player character in the example are either covered by the shadows they produce, or stand on them (I like the latter more). If they stand on them however, they are drawn on top of the 'remaining darkness' and thus it's as if they're lit-up (the same would apply to enemies creeping in the dark).

 

This is all true, even when I make a separate object that follows the player object, because if the player (or a wall) is a caster, its sprite is also noticeably darkened (especially if you turn down the ambient, in the example 'settings' it's not as noticeable). I want the game to be eerily dark, but if the sprites are very dark even in the light (both due to the darkening and the shadows being drawn over them), then you barely see anything of the art.

 

In short, for an overhead view, or a skewed view (not isometric as far as I know) it doesn't seem to make sense in what order everything's drawn... or maybe I've just been trying to tweak this thing for too long (I dunno, it's been days, I registered because I couldn't find a (complete) answer in past threads or replies).

 

What I tried:

- splitting the lightmap_update script in two parts (for two objects with two depths), one drawing shadows, one doing all the rest, which still involved duplicating a lot of the code because it calls back to a lot of variables in the original script; this made the player and the lights give off even more light, so I'm thinking there's too much trial and error going on here and I'd need to know which parts do what (I thought I knew, but here we are)

- making a separate object that sticks with the player and does the actual casting (called it obj_shadowfeet); it's sprite is also literally 1 by 1 big. This takes care of the darkening of the sprite. Even if I figured out how to get the darkening off (think I was almost there), the drawing depth problem still remains

 

Edit: Simplest solution if the splitting proves too tricky? drawing the shadow around the player character so it's not on him, which I guess acceptable, just darn hard (I'd have to specify a polygon cone that borders on the hair if the shadow's to the north of the character to make sure it looks believable).

 

If I think of anything else or you'd like me to provide more details, let me know!

 

 

Now, making a script where a part of the sprite is only lit (fake 3D I'd say) when that body part is facing the light is a whole other kind of problem (and don't get me started on multiple lights close to the player), so let's leave that for later.


Edited by InventorOfTheBizarre, 02 February 2016 - 05:37 PM.

  • 0

#23 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 04 February 2016 - 12:26 PM

....

It seems you have kind of answered your own question. But yes, when dealing with isometric (which is basically fake 3d) it is really hard to get the lighting right. Lighting in isometric view is usually what create the illusion of 3d, and adding this lighting engine will kinda kill that i think. If you manage to do it though, kudo's to you, really impressive. I'd recommend using a real 3d lighting engine, even when dealing with 2d sprites in an isometric view. But that is just my opinion ofc. 

 

Furthermore, I don't really understand what you need help with. Could you provide a screenshot showing the problem? The thing I made with the user Eben, a few post above, might also help out a bit here.

 

Anyway, thanks for the compliment though!


  • 0

#24 InventorOfTheBizarre

InventorOfTheBizarre

    GMC Member

  • New Member
  • 4 posts
  • Version:GM:Studio

Posted 04 February 2016 - 08:55 PM



It seems you have kind of answered your own question. But yes, when dealing with isometric (which is basically fake 3d) it is really hard to get the lighting right. Lighting in isometric view is usually what create the illusion of 3d, and adding this lighting engine will kinda kill that i think. If you manage to do it though, kudo's to you, really impressive. I'd recommend using a real 3d lighting engine, even when dealing with 2d sprites in an isometric view. But that is just my opinion ofc. 

 

 

Furthermore, I don't really understand what you need help with. Could you provide a screenshot showing the problem? The thing I made with the user Eben, a few post above, might also help out a bit here.

 

Anyway, thanks for the compliment though!

 

 

I really hope it's not an isometric view, because that would be above what I'm aiming for (I'm striving for simple graphics so I can put in as much content as possible without making the game a juggernaut; also, I suck at graphics), but here goes:

 

Image box doesn't work, so: http://imgur.com/a/uVCwl

 

Side notes: the guy is from a free sprite editor, the white boxes up high is a menu for key rebinding.

 

Before I continue, yes, the thing you and Eben cooked up looks great and adds to the feel. I think it might be part of the solution, but as you'll see below, I'm having trouble figuring out what part of the code does what (and what to keep of the original lightmap script so it doesn't give errors) in order to get the shadows to do their own, separate thing, at a different depth. So, kinda the opposite of what Eben needed? I'm also real fidgety about the performance, wanna keep it as low as possible, as I have or plan on having: pathfinding AI, shooter AI, melee AI, melee-collision detection, huge maps, inventory systems, a big roster of characters, etc. I'll be sure to study the code (sounds like The Matrix up in here) in the meantime.

 

So I put the ambient on 0.05 and as you can see, obj_lightmap has a depth above the player's, causing both the shadows and the 'darkness' to be drawn on top of him. His shadow is provided by a separate object that follows him around, as otherwise, the player's sprite is noticeably darkened.

 

-> haven't figured out why, but the only thing I changed was the ambient, so the order in which everything is drawn (depth wise) is the same as in your first post's example.

 

In the screenshot and the example game file it's harder to tell, but the shadows are indeed drawn on top of the player object, instead of beneath him (as you would expect in a top-down view). Of course, the 'darkness' is also drawn on top of him so he's harder to discern in the darkness, which is what we want.

 

So in short, all of this, can be made even more amazing than it is, by splitting the obj_lightmap into two objects with each a different depth: one drawing above everything (so when players and enemies are in non-lit areas, they're harder to spot because they're covered by darkness; the other drawing their shadows underneath them, as it would appear in real life. For this you'd also have to split up the lightmap script( s ). I tried to do so, by foolishly taking the shadows part out of the main script, getting lots of errors, re-establishing necessary variables from the main script into the new shadows script. Finally got it working and... everything's over-lit. The script is complex, yet elegant so I feel bad butchering it to get what I want. Plus I don't like feeling stupid and going by trial and error.

 

Then I thought, well this is a super-recent topic and the talented creator (and equally talented forum members) are conversing in it. Hence, I've come to request your help! So... which parts of the code draw the shadows (and which part would I need to 'keep' from the original script so I don't get errors)? I'm not being a complete noob, I have tried isolating the code, but it's quite interwoven (which is commendable) and my try... turned out horrible as I said before.

 

Btw, as far as I can tell, writing to two surfaces is very taxing and not recommendable and two objects writing to one surface is something I can't seem to find information on. I'm guessing I've tried and will do the latter (I thiiink I'm doing it... or my laptop is powerful to run two surfaces named the same way, maybe they're not global?), just had it turn out wrong so far. Will put screenshot if needed, but I feel it's just a mess, overlit through mysterious means. Can also put my 'trimmed' version of the code with only the shadows, though it looks more like a Frankensteinian swamp monster than simplified shadow making code.

 

However... One tricky bit remains: when players are in the shadows of objects (and enemies, though that'll tie into my future 'shadow on part of sprite' script that will have to be separate anyway), they should also be covered by these shadows instead of standing on top of them. Since you can make the depths of objects (walls for example) and characters different (because they're separate objects), the only problem would be having a different object draw lifeless objects' shadows than the one drawing people's shadows.

 

So okay, two objects, two lightmap script versions: one is almost the same as the original one, except it doesn't draw people's shadows (maybe if they have a different parent object?) and another small one that only draws people's shadows and nothing else (otherwise the game's drawing almost everything twice at two different depths and that's not only wasteful, it's taxing I'm told).

 

One note superceding all others: what if I just not draw to a surface for people's shadows? But I hear it's a lot less taxing than regular drawing.


Edited by InventorOfTheBizarre, 04 February 2016 - 09:03 PM.

  • 0

#25 karIos

karIos

    GMC Member

  • Banned Users
  • 23 posts
  • Version:Unknown

Posted 04 February 2016 - 09:38 PM

I mght have to give this a crack. It's pretty cool to have another light alternative.



#26 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 06 February 2016 - 01:00 PM

 



...

...

 

You quite answered your own question, but i get what you mean. You might want to look into some other blend modes or stencil bufferin for this, but there is not one global solution (yet). Double lightmapping is very taxing indeed and i would not recommend it. However, it might be possible to kinda bake the shadows for the background. If your background shadows are static (meaning, they won't ever move) you can just not call lightmap_update every draw frame. Also, calling lightmap_update only once every few frames might also help you gain a lot of performance. 

 

But yeah, if you chose to do the double lightmapping, one for background and one for players, you will need to set a different parent object for the second lightmap.

 

I hope this helps!


  • 0

#27 InventorOfTheBizarre

InventorOfTheBizarre

    GMC Member

  • New Member
  • 4 posts
  • Version:GM:Studio

Posted 06 February 2016 - 05:53 PM

 

 



...

...

 

You quite answered your own question, but i get what you mean. You might want to look into some other blend modes or stencil bufferin for this, but there is not one global solution (yet). Double lightmapping is very taxing indeed and i would not recommend it. However, it might be possible to kinda bake the shadows for the background. If your background shadows are static (meaning, they won't ever move) you can just not call lightmap_update every draw frame. Also, calling lightmap_update only once every few frames might also help you gain a lot of performance. 

 

But yeah, if you chose to do the double lightmapping, one for background and one for players, you will need to set a different parent object for the second lightmap.

 

I hope this helps!

 

It certainly did. I did go with double lightmapping, because I realized the moving people (player, allies and enemies) their shadows have to be underneath them, while wall's shadows need to be on top of them, so one object would need all functions anyway. If I can't isolate the script for just drawing shadows and I can't use two objects to draw to ONE surface (without one overwriting the other's work every single frame), I best just draw on two different depths.

 

Calling it only once every few frames is something I'll have to look into (don't know how to make the draw function not draw continuously) and baking shadows as well (only have a vague idea what it even means).

 

I guess I'll learn how this stuff works as I get more ambitious. For now, I'm bodging stuff together. I'm learning though^^ Also have to bodge scripted character movement into a dialogue engine, because the code is too vast for me:p


  • 0

#28 GreenhornGames

GreenhornGames

    GMC Member

  • GMC Member
  • 901 posts
  • Version:GM:Studio

Posted 08 February 2016 - 01:12 PM

Currently trying this engine out, but since my project is a sidescrolling game I'm not using shadows for now and the engine is really easy to understand!


  • 0

#29 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 09 February 2016 - 06:55 PM

 

 

 



...

...

....

It certainly did. I did go with double lightmapping, because I realized the moving people (player, allies and enemies) their shadows have to be underneath them, while wall's shadows need to be on top of them, so one object would need all functions anyway. If I can't isolate the script for just drawing shadows and I can't use two objects to draw to ONE surface (without one overwriting the other's work every single frame), I best just draw on two different depths.

 

Calling it only once every few frames is something I'll have to look into (don't know how to make the draw function not draw continuously) and baking shadows as well (only have a vague idea what it even means).

 

I guess I'll learn how this stuff works as I get more ambitious. For now, I'm bodging stuff together. I'm learning though^^ Also have to bodge scripted character movement into a dialogue engine, because the code is too vast for me:p

 

 

Let me help you on your way:

Not drawing basically means just having a variable in your object updating every step (yes step, not draw) event. Every time it is devideable by 2/3/4/5/6 (whichever you prefer, once ever x frames at least (see modulo in gamemaker help (mod))) you draw a frame.
"Baking" in this context just means update the lightmap only once, and never again. You might need to create your surface as a room sized surface for this. You also might want to use a custom drawing script (see lightmap_draw ) and change it to draw_surface_part (to not draw it in the room, but just in the view).


  • 0

#30 InventorOfTheBizarre

InventorOfTheBizarre

    GMC Member

  • New Member
  • 4 posts
  • Version:GM:Studio

Posted 12 February 2016 - 11:12 AM

That's an excellent idea! I just got that done, albeit in the form of an alarm loop. 3 steps seemed to give acceptable shadow refreshing.

 

Did the baking as well. I'm gonna see what I need to do for big rooms view-wise, but I'll figure it out. Thank you so much for all the help!

 

I'm also gonna show some outdoor areas, as the levels are on the Xth floor of buildings, so it would be real cool to show the depth and I would ironically draw these on top of everything else. Not sure if I can work this into the lightmap drawing, but it might be better to just use tiles^^


  • 0

#31 DrWhitePsyker

DrWhitePsyker

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 17 February 2016 - 09:18 PM

Oh! So glad to see this is updated! I've been using the old version (with modifications, of course) in my current project and have been for about a year now.

This engine was what got me interested in coding and pretty much taught me GML - A genuine 'trial by fire'.  

A few questions, if I may;

  • Could we get an updated feature break down? Is it just a straight up optimisation update?
  • I tweaked the old engine to support shadowcasters being defined by their sprites boundingbox. How hard would it be to replicate modification in this version?
  • On the same note, I also modded in sprite-based lights and moving view support based on some code suggestions in the old thread. Are these features integrated now?
  • My final question is about the old version but perhaps its more fitting as a suggestion for this one - Would it be possible at all to add in a slight surface-blurring effect to the lightmap surface? It would be great to be able to soften those shadow edges just a smidgen.   

Although the viability of these modifications in the new system will ultimately decide if I upgrade or not, I look forward to seeing how this system progresses! 
 


  • 0

#32 Spacedog

Spacedog

    GMC Member

  • GMC Member
  • 21 posts
  • Version:GM:Studio

Posted 20 February 2016 - 06:16 PM

Is it possible to rotate the wall objects in the room editor and have the shadow be updated to match in game with this engine?  It looks like it should work by translating the caster_add_point values for each corner to the new positions but every time I try I get weird looking shadows.

 

I may well be doing something wrong but is this a limitation of the engine?


  • 0

#33 Crazyspawn

Crazyspawn

    GMC Member

  • New Member
  • 4 posts

Posted 23 February 2016 - 11:12 PM

This might be a dumb question but is there a way to tell if an object is under the shadows surface or is everything just one surface? I'm making a top down stealth game and I'm trying to get player visibility to work.


  • 0

#34 DrWhitePsyker

DrWhitePsyker

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 25 February 2016 - 01:02 AM

This might be a dumb question but is there a way to tell if an object is under the shadows surface or is everything just one surface? I'm making a top down stealth game and I'm trying to get player visibility to work.

Hmm, I'm just spit-balling here but here's some hobo-script to that effect.

First try check all relevant lights by getting their 'range' (their radius) and checking it against the distance to the player.
For any lights that 'in range' of the player, check to see if there's any collisions with shadow-casters between that light and the player.
Logically, if you've got a shadowcaster between you and a light that could reach you, you'll be in shadow. Likewise, if a light can reach you and there's no shadow-caster between you and it, then you're lit up. 

For bonus points, you can do a collision_line check for each 'edge' of the player sprite instead of just once from the middle of the sprite. Depending on how many of these lines report collisions, you can work out how much of the player is hidden behind a shadow.


Best of luck!


Edited by DrWhitePsyker, 25 February 2016 - 01:04 AM.

  • 0

#35 Crazyspawn

Crazyspawn

    GMC Member

  • New Member
  • 4 posts

Posted 25 February 2016 - 11:05 PM

This is the code I came up with and it seems to work great.

 

I forgot to mention that I'm running this in the step event of the light object and the radius is defined in the create event.

if collision_line(x,y,oPlayer.x,oPlayer.y,oParent_Solid,false,true) or (distance_to_point(oPlayer.x,oPlayer.y) >= Radius)
{
Hidden = true;
} else {
Hidden = false;
}

Edited by Crazyspawn, 25 February 2016 - 11:16 PM.

  • 0

#36 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 27 February 2016 - 02:27 PM

Is it possible to rotate the wall objects in the room editor and have the shadow be updated to match in game with this engine?  It looks like it should work by translating the caster_add_point values for each corner to the new positions but every time I try I get weird looking shadows.

 

I may well be doing something wrong but is this a limitation of the engine?

 

You can try to modify the caster_x and caster_y (i believe it they are called (see caster_add_point, it modifies these two variables)). The caster_x and caster_y variables are an array with caster corners, so if you want rotation, you could modify these directly. 

 

I might add this in the next update :)


  • 0

#37 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 28 February 2016 - 06:44 PM

Oh! So glad to see this is updated! I've been using the old version (with modifications, of course) in my current project and have been for about a year now.

This engine was what got me interested in coding and pretty much taught me GML - A genuine 'trial by fire'.  

A few questions, if I may;

  • Could we get an updated feature break down? Is it just a straight up optimisation update? Its a complete overhaul to the old engine. Everything changed, and its a lot more stable
  • I tweaked the old engine to support shadowcasters being defined by their sprites boundingbox. How hard would it be to replicate modification in this version? Bounding box shadowcasters are pretty easy to implement. If its the complete sprite it is natively supported currently (with caster_init_sprite). Good point though, I will add this some day, as caster_init_boundingbox. Thanks for the suggestion
  • On the same note, I also modded in sprite-based lights and moving view support based on some code suggestions in the old thread. Are these features integrated now? Sprite based lights are supported now (with light_init_sprite (also, sprite rotations :o )). Moving views are also supported now with a script (can't remember the name, but it kinda speaks for it self (something like lightmap_move_with_view(true), don't have access to a computer with game maker currently, sorry!).
  • My final question is about the old version but perhaps its more fitting as a suggestion for this one - Would it be possible at all to add in a slight surface-blurring effect to the lightmap surface? It would be great to be able to soften those shadow edges just a smidgen. This is not yet supported, nor really planned, but it is VERY possible with a simple blur shader (I haven't looked much into game maker shaders though, but this is one of the more easy one's).

Although the viability of these modifications in the new system will ultimately decide if I upgrade or not, I look forward to seeing how this system progresses! I would recommend using this new engine, but it will take some work to change it in an already working engine. I hope this helped a bit with your choice.
 

 

Hey~! Sorry for the late reaction, I added comment in your quote (as red text). 

 

Good luck!


Edited by flokkienathur, 28 February 2016 - 06:45 PM.

  • 0

#38 LeNoctambul

LeNoctambul

    GMC Member

  • New Member
  • 2 posts
  • Version:Unknown

Posted 28 February 2016 - 08:20 PM

First of all I want to tell you that you did a great job. 

 

I tried to play with your engine and I had a strange behavior in my opinion. When I put a light near an object, the shadow isn't long enough to fill the light radius. That leads to a strange result.

 

I tried to modify light radius and shadow size without success. Maybe I miss something.

 

Is it a usual behavior ? Could you help me with this issue ? 

 

1456690554-shadow-bug-1.png1456690561-shadow-bug-2.png


  • 0

#39 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 29 February 2016 - 10:57 AM

First of all I want to tell you that you did a great job. 

 

I tried to play with your engine and I had a strange behavior in my opinion. When I put a light near an object, the shadow isn't long enough to fill the light radius. That leads to a strange result.

 

I tried to modify light radius and shadow size without success. Maybe I miss something.

 

Is it a usual behavior ? Could you help me with this issue ? 

 

1456690554-shadow-bug-1.png1456690561-shadow-bug-2.png

 

You are correct. This is a bug in the engine. Currently it draws a triangle for each side of the caster. The length of the triangle is a bit larger then the light radius. So the actual triangle ends up overlapping the light (for the most part). The easiest fix is increasing the shadow range, but that means in your case you would need to double the range, which causes bad performance. There should be something that draws the complete circle, but there currently isn't. I might look into this!

For now, there is not really a fix since its a side effect to the way the engine is set up. Splitting the caster up into two sub casters will make this a lot less bad, so that is your only solution currently.

 

TL;DR: Its a bug indeed, sorry!


  • 0

#40 LeNoctambul

LeNoctambul

    GMC Member

  • New Member
  • 2 posts
  • Version:Unknown

Posted 01 March 2016 - 09:10 AM

Thank you for your answer.

 

I will try your solution because actually I can handle the performance problem by generate my lightmap only one time at the start of a level. 

 

But let me know if you can fix it ;)

 

Good luck :) 


  • 0

#41 DrWhitePsyker

DrWhitePsyker

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 02 March 2016 - 09:40 PM

 

...

 

You are correct. This is a bug in the engine. Currently it draws a triangle for each side of the caster. The length of the triangle is a bit larger then the light radius. So the actual triangle ends up overlapping the light (for the most part). The easiest fix is increasing the shadow range, but that means in your case you would need to double the range, which causes bad performance. There should be something that draws the complete circle, but there currently isn't. I might look into this!

For now, there is not really a fix since its a side effect to the way the engine is set up. Splitting the caster up into two sub casters will make this a lot less bad, so that is your only solution currently.

 

TL;DR: Its a bug indeed, sorry!

 

 
Why not just draw a skewed primitive shape based on the caster points for each light in range? Wasn't that what the old system did?

Also really long shadow-casters (think corridor walls) stop blocking light towards their centres. it looks really strange. 


  • 0

#42 tommah

tommah

    GMC Member

  • GMC Member
  • 275 posts
  • Version:GM:Studio

Posted 08 March 2016 - 04:56 PM

I've gotten this to work really well, love the engine.  How would I go about making the lights brighter?  I've tried a few things but it really didn't seem to make a difference.  I don't mean the overall brightness of the room, the brightness of the obj_light.

 

Edit*

Nevermind, I set the like at c_white color and it looks great.  Go figure that blending may reduce brightness, derp.


Edited by tommah, 08 March 2016 - 06:20 PM.

  • 0

#43 Jesus_3k

Jesus_3k

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 14 March 2016 - 11:31 PM

Hi, flokkienathur. Absolutely love your engine. Very easy to understand and use. So useful for my current project.

f343117df5efd3f2645fd3006f346c39.png

 

But here is a huge question\request, is there a chance to make short shadows for a small objects like barrels\crates\rocks etc. I mean that shadows must be longer from far and shorter at close distances. Maybe different parents for short and long casters.  Here is example what i'm talking about:

 

0e73f99644a9961cb3ed05d96e54603b.png


  • 0

#44 flokkienathur

flokkienathur

    GMC Member

  • GMC Member
  • 35 posts
  • Version:GM:Studio

Posted 17 March 2016 - 05:33 PM

Hi, flokkienathur. Absolutely love your engine. Very easy to understand and use. So useful for my current project.

f343117df5efd3f2645fd3006f346c39.png

 

But here is a huge question\request, is there a chance to make short shadows for a small objects like barrels\crates\rocks etc. I mean that shadows must be longer from far and shorter at close distances. Maybe different parents for short and long casters.  Here is example what i'm talking about:

 

0e73f99644a9961cb3ed05d96e54603b.png

 

Hey! Your game (and art work) looks amazing! Good work!

 

This may sound pretty stupid, but i remember implementing this. I think light_set_height and caster_set_height does this. if the light is higher than the caster, the shadow will be shorter (I think). I am very sorry, I'm not 100% positive that this works flawlessly, but I'm pretty sure it does work!

 

If it doesn't, please let me know!


  • 0

#45 Jesus_3k

Jesus_3k

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 18 March 2016 - 01:56 AM

I think light_set_height and caster_set_height does this.

 

Yes, yes, yes! Awesome. It works perfect for me! Thanks :wub:

038dc367aefdacee8f4636a6ae42303e.gif


  • 0

#46 tommah

tommah

    GMC Member

  • GMC Member
  • 275 posts
  • Version:GM:Studio

Posted 20 March 2016 - 12:59 AM

I'm have an issue that I feel I should know, but I've tried everything I can think of

 

5fe53c2a47b85ad115931c17eab1d003.png

 

sorry for the horrible picture but let me narrate.  The top part of the platform works perfect because it is a right angle.  the lower part of the pic is a slant that  is blocking out for the whole image rather than the sprite mask.  I have precision box check, I'm using "caster_init_sprite()" in my "floor" object but I can't get it... WHAT am I missing? 

 

Everything else is working perfectly EXCEPT this and I'm dying here.

 

*Edit* here is a better picture

80a75745fd9729a820ab185ff9429977.png


Edited by tommah, 20 March 2016 - 01:03 AM.

  • 0

#47 Jesus_3k

Jesus_3k

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 20 March 2016 - 12:27 PM

tommah, don't use "caster_init_sprite()". Its just size of the sprite. Use polygon caster ;)


  • 0

#48 tommah

tommah

    GMC Member

  • GMC Member
  • 275 posts
  • Version:GM:Studio

Posted 20 March 2016 - 12:56 PM

I am probably using it incorrectly but I changed the caster_init_sprite to caster_init_polygon(); and it crashed

trying to index a variable which is not an array
 at gml_Script_lightmap_update (line 91) -                         caster_point_x[caster_point_count-1] + x - lx + w, caster_point_y[caster_point_count-1] + y - ly + h, 

  • 0

#49 Jesus_3k

Jesus_3k

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 20 March 2016 - 01:02 PM

 

I am probably using it incorrectly but I changed the caster_init_sprite to caster_init_polygon(); and it crashed

trying to index a variable which is not an array
 at gml_Script_lightmap_update (line 91) -                         caster_point_x[caster_point_count-1] + x - lx + w, caster_point_y[caster_point_count-1] + y - ly + h, 

Yep, first u need caster_init_polygon(); then write caster points with caster_add_point(x,y):  counter-clockwise, like:

 

ec9808d89d565156db3d76601693a621.png


  • 0

#50 tommah

tommah

    GMC Member

  • GMC Member
  • 275 posts
  • Version:GM:Studio

Posted 20 March 2016 - 02:20 PM

oooooooh ok.  are the points based off the sprite image or the object coordinates?


  • 0