Jump to content


Photo

Grid Based Lighting


  • Please log in to reply
10 replies to this topic

#1 wiiowner

wiiowner

    Absolute Zero Team

  • New Member
  • 685 posts

Posted 30 March 2009 - 12:33 AM

Hello I had the idea recently for grid based lighting and decided to try something out. The result was this one script. It is very flexible and runs fairly fast. This does not use surfaces because like probably a lot of people I'm not very good at them. This script also supports hard edged dynamic shadows. Don't expect the shadow to be to good but they work. Here is the script:
//Grid Lighting Script//This script will draw//Usage: scr_grid_lighting(light_object,gridsize,max_lighting_distance,wall_object,draw_grid,draw_shadows)//Example: scr_grid_lighting(obj_player,32,256,obj_wall,0,1)//Note when you are calling this script, for setting draw_grid use a 1 for true and a 0 for false, do not use true or falsefor(i=0; i<room_width/argument1; i+=1)//This is the first for loop which represents the amount of horizontal grid squares{for(j=0; j<room_height/argument1; j+=1)//This is the second for loop which represents the amount of vertical grid squares {alpha_array[i,j] = point_distance(i*argument1+argument1/2,j*argument1+argument1/2,argument0.x,argument0.y)/argument2//This sets the alpha value for each grid point equal to the distance away from the caster divided by the max light distance  if !collision_point(i*argument1+argument1/2,j*argument1+argument1/2,argument3,1,0)//This checks if the grid square does not have a wall object in it   {if argument5//This checks if the user wants shadows to be drawn    {if !collision_line(i*argument1+argument1/2,j*argument1+argument1/2,argument0.x,argument0.y,argument3,1,0)//This checks if there is a wall in between the grid square and the caster     {draw_set_alpha(alpha_array[i,j])}//if so then you set the drawing alpha to that squares alpha     else     {draw_set_alpha(1)}//If not then you set the drawing alpha to 1 (opaque)     }    else    {draw_set_alpha(alpha_array[i,j])}//If the user doesn't want the shadows to be drawn then you set the drawing alpha to that squares alpha   }  else   {draw_set_alpha(alpha_array[i,j])}//If there is a wall object there then you set the drawing alpha to that squares alpha this is so that you can see the walls and they don't show up as shadows  draw_set_color(c_black)//This sets the drawing color to black  draw_rectangle(i*argument1,j*argument1,i*argument1+argument1-abs(argument4-1),j*argument1+argument1-abs(argument4-1),0)}//This draws a black rectange with the indicated alpha the reason for the abs() in there is for the draw_grid option. That option draws each grid square a bit bigger so that they overlap and they get darker.}
It took me about a day to do this. You can set different options such as whether to draw shadows, a grid, the light source, wall object, the grid size and the light radius. I will upload an example soon. This also as you may point out has a second script and that is an attempt to make the shadows by averaging the alpha values of the 4 spaces around it. It doesn't work for some reason and if any of you can figure it out that would make this script a lot better. Where you are supposed to put it is to replace the draw_set_alpha(1). Also in the example if you set the grid size to anything but 32, 16, 8, 4, 2, and 1 it will look weird. This is only because my objects are that size and it will work if your objects are that size. This only credit required is that you PM me if you finish a game using it. I would just like to see how it looks in a game. Otherwise it's all yours, put it to good use. Here is the example:Posted Image If you have any more questions feel free to post here or PM me and I'll try to answer it as best I can.

Edited by wiiowner, 30 March 2009 - 12:41 AM.

  • 0
Completed Projects: Eco-Chrome || Conway's Game of Life - Update that triples framerate!
Scripts: Grid Based Lighting (With Dynamic Soft Shadows) || Mud Physics


#2 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 25708 posts
  • Version:GM:Studio

Posted 02 April 2009 - 10:27 PM

This is a a good, interesting effect, but too slow for most games... using your default example and setting the room speed to 60 I only got an average of 32 fps... And my computer is anything but slow! Maybe someone can optimise this somehow? I looked through the code and it seems good to me, but I´m no expert!

Anyway, I like this and it´s nice to see someone taking a different approach to doing things. When I finish my current WIP I may come back to this as I had an idea for a puzzle game while playing around with it...

Keep thinking out of the box!
  • 0

U1FVsm3.png

40799.png


#3 wiiowner

wiiowner

    Absolute Zero Team

  • New Member
  • 685 posts

Posted 02 April 2009 - 10:37 PM

Thanks a lot. Your fps seems to be odd as I had 100+ with dynamic shadows (I think). It is fairly slow though seeing as it's a brute force method. I tried a different approach to soft shadows than just a cheap blend but it didn't end that well. It used 8 collision line functions for each grid square and used a loop to run through collisions on a line across the diameter of the light source. Then for each successful collision_line it added 1 to a counter which then was modified to be the alpha. The result was awful framerates and it only blended a couple squares for each shadow. I might post that but I don't think so. Thanks for the comment though!
  • 0
Completed Projects: Eco-Chrome || Conway's Game of Life - Update that triples framerate!
Scripts: Grid Based Lighting (With Dynamic Soft Shadows) || Mud Physics


#4 reigalius

reigalius

    Generally Awesome

  • New Member
  • 1170 posts

Posted 03 April 2009 - 08:36 PM

I love it but I was getting 9 - 15 fps! There's better ways I'm sure...
  • 0

#5 wiiowner

wiiowner

    Absolute Zero Team

  • New Member
  • 685 posts

Posted 03 April 2009 - 11:14 PM

There probably are. Can I ask what settings you were using (like gridsize, shadows that kind of thing)?
  • 0
Completed Projects: Eco-Chrome || Conway's Game of Life - Update that triples framerate!
Scripts: Grid Based Lighting (With Dynamic Soft Shadows) || Mud Physics


#6 reigalius

reigalius

    Generally Awesome

  • New Member
  • 1170 posts

Posted 06 April 2009 - 08:51 PM

There probably are. Can I ask what settings you were using (like gridsize, shadows that kind of thing)?

I just downloaded it and pressed play lol.
  • 0

#7 wiiowner

wiiowner

    Absolute Zero Team

  • New Member
  • 685 posts

Posted 06 April 2009 - 10:23 PM

You should try tweaking the settings you might be able to get a better framerate. It is fairly slow though :whistle:.
  • 0
Completed Projects: Eco-Chrome || Conway's Game of Life - Update that triples framerate!
Scripts: Grid Based Lighting (With Dynamic Soft Shadows) || Mud Physics


#8 reigalius

reigalius

    Generally Awesome

  • New Member
  • 1170 posts

Posted 15 April 2009 - 09:47 PM

It's not a horrible framerate, I can't tell its slow unless I already know how much it should be. If there was no framerate counter, it would look perfect to me! xD Blissfully Ignorant as Always
  • 0

#9 wiiowner

wiiowner

    Absolute Zero Team

  • New Member
  • 685 posts

Posted 15 April 2009 - 10:30 PM

Yep what you don't know won't hurt you :D That's the thing that I did notice is that it may be lagging but without the counter you really can't tell (unless it's like 8fps or lower).
  • 0
Completed Projects: Eco-Chrome || Conway's Game of Life - Update that triples framerate!
Scripts: Grid Based Lighting (With Dynamic Soft Shadows) || Mud Physics


#10 Krisando

Krisando

    GMC Member

  • New Member
  • 1351 posts

Posted 25 April 2009 - 08:08 PM

Interesting Concept.
  • 0

Posted Image


#11 wiiowner

wiiowner

    Absolute Zero Team

  • New Member
  • 685 posts

Posted 26 April 2009 - 06:29 PM

Thank you in a week or so I may try and optimize it so that the shadows look better and run faster but that may push it over one script, which is alright.
  • 0
Completed Projects: Eco-Chrome || Conway's Game of Life - Update that triples framerate!
Scripts: Grid Based Lighting (With Dynamic Soft Shadows) || Mud Physics