Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
kellessdee

Dynamic Lighting Effects

Recommended Posts

Dynamic Lighting Script

Authors: kellessdee

Version: 1.00

Type: Special Effects


Introduction

 

I noticed a sudden want/need for Lighting scripts and thought: "Hey, maybe I want one too" (Yknow, just to be trendy and cool :P)

So I decided to put one together for myself, and for any one else who is looking for a dynamic lighting script!

 


Features

 

  • Highly customizable and easy to use
  • Can set lighting tone
  • Can set different light image files (to create different effects)
  • Can make flicker or glow, set brightness or radius

 


Screenshots & Video

 

 

gallery_18872_189_533544.png

 

 

http://www.youtube.com/watch?v=44ftmQW6LKw

29 events on the first map (as light sources), no slowdowns.

 


Demo

 

Demo Link


Script

 

Here's the script. Place above Main.

 

#==============================================================================
# ** Dynamic Lights
#------------------------------------------------------------------------------
# by kellessdee
# Version: 1.00
# Date: 23/05/2011
#------------------------------------------------------------------------------
# Version History:
#
#   1.00 - First release
#------------------------------------------------------------------------------
# Description:
#
#   Allows the user to set up dynamic lights on the game map
#------------------------------------------------------------------------------
# Compatibility:
#
#------------------------------------------------------------------------------
# Instructions:
#
#   To create an event as a light source, create an event and set the first
#   command as a comment. The syntax is as follows:
#       render_light filename
#       radius
#       tone_red tone_green tone_blue
#       flicker 
#       glow
#       brightness
#------------------------------------------------------------------------------
# Parameters:
#
#   filename      :the light picture file
#   radius        :radius in pixels
#   tone_red      :tone red value to be mixed (0 - 255)
#   tone_green    :tone green value to be mixed (0 - 255)
#   tone_blue     :tone blue value to be mixed (0 - 255)
#   flicker       :type true if you want the lights to flicker
#   glow          :type true if you want the lights to glow
#   brightness    :the brightness of the light source (0 - 255)
#------------------------------------------------------------------------------
# Notes:
#
#   Any questions, comments or issues you may reach me at:
#
#     kellessdee@gmail.com
#     http://www.rmxpunlimited.com/forums (look for kellessdee)
#     http://wakingdreams.weebly.com/
#
#==============================================================================

#==============================================================================
# ** Sprite_Light_Source
#------------------------------------------------------------------------------
#   Displays the light on the screen
#==============================================================================
class Light_Source < Sprite
 #----------------------------------------------------------------------------
 # * Constants
 #----------------------------------------------------------------------------
 DEFAULT = 'light_render'
 #----------------------------------------------------------------------------
 # * Public Instance Variables
 #----------------------------------------------------------------------------
 attr_accessor :event
 #----------------------------------------------------------------------------
 # * Object Initialization
 #----------------------------------------------------------------------------
 def initialize(event, radius, tone, flicker, glow, brightness, filename)
   super()
   self.z = 1000
   self.tone = tone
   self.opacity = brightness
   self.x = (event.real_x - radius * 2 - $game_map.display_x) / 4 + 12
   self.y = (event.real_y - radius * 2 - $game_map.display_y) / 4 + 6
   bmp = filename == nil ? RPG::Cache.picture(DEFAULT) : RPG::Cache.picture(filename)
   self.bitmap = Bitmap.new(radius, radius)
   self.bitmap.stretch_blt(Rect.new(0, 0, radius, radius), bmp, bmp.rect)
   @radius = radius
   @brightness = brightness
   @event = event
   @flicker = flicker
   @glow = glow
   @step = 0.001
   @flicker_count = -1
   @count = 0
 end
 #----------------------------------------------------------------------------
 # * Update Frame
 #----------------------------------------------------------------------------
 def update
   # Update flicker effect
   if @flicker
     self.opacity += rand(5) * @flicker_count
     @flicker_count = -@flicker_count if self.opacity >= @brightness || self.opacity <= 0
   end
   # Update Glow effect
   if @glow
     self.zoom_x += @step
     self.zoom_y += @step
     @count += 1
     if @count == 220
       @step = -@step
       @count = 0
     end
   end
   self.x = (@event.real_x - (@radius * 2).to_f * self.zoom_x - $game_map.display_x) / 4 + 12
   self.y = (@event.real_y - (@radius * 2).to_f * self.zoom_y - $game_map.display_y) / 4 + 6
 end
end
#==============================================================================
# ** Spriteset_Map
#------------------------------------------------------------------------------
#   Redefine initialization to create light array
#   Redefine update to update light array
#   Redefine dispose to destroy light array
#   Added render light method to set up light array
#==============================================================================
class Spriteset_Map
 #----------------------------------------------------------------------------
 # * Method Aliases
 #----------------------------------------------------------------------------
 unless self.method_defined?(:new_light_render_init)
   alias :new_light_render_init :initialize 
 end
 unless self.method_defined?(:new_light_render_updt)
   alias :new_light_render_updt :update
 end
 unless self.method_defined?(:new_light_render_disp)
   alias :new_light_render_disp :dispose
 end
 #----------------------------------------------------------------------------
 # * Object Initialization
 #----------------------------------------------------------------------------
 def initialize
   @light_source = []
   render_light_source
   new_light_render_init
 end
 #----------------------------------------------------------------------------
 # * Update Frame
 #----------------------------------------------------------------------------
 def update
   @light_source.each {|light| light.update } unless @light_source == []
   new_light_render_updt
 end
 #----------------------------------------------------------------------------
 # * Dispose Object
 #----------------------------------------------------------------------------
 def dispose
   new_light_render_disp
   @light_source.each {|light| light.dispose }
   @light_source = []
 end
 #----------------------------------------------------------------------------
 # * Render Light Source
 #----------------------------------------------------------------------------
 def render_light_source
   $game_map.events.values.each {|event|
     next if event.list == nil
     if event.list[0].code == 108 && event.list[0].parameters[0].split[0] == 'render_light'
       # Get file name
       filename = event.list[0].parameters[0].split[1]
       parms = []
       (1..5).each {|i| 
         if event.list[i].code == 108
           parms << event.list[i].parameters
         end
       }
       5.times {|i| parms[i] = [0] if parms[i] == [nil] }
       # Get Radius
       rad = parms[0][0].to_i
       t = parms[1][0].split
       ton = Tone.new(-t[0].to_i, -t[1].to_i, -t[2].to_i)
       fli = (parms[2] == ['true'])
       glo = (parms[3] == ['true'])
       bri = parms[4][0].to_i
       @light_source << Light_Source.new(event, rad, ton, fli, glo, bri, filename)
       @light_source << Light_Source.new(event, rad + 50, ton, fli, glo, bri - 25, filename)
       @light_source << Light_Source.new(event, rad + 100, ton, fli, glo, bri - 50, filename)
     end
   }
 end
end

 

 


Instructions

 

  • Place script below default scripts, and above "Main".
  • Set up light graphic; if you want you can use mine (see author notes) just place in pictures.
  • Create A "light source" event and create 6 comments

Syntax for setting up light source:
render_light filename
radius_in_pixels
tone_red tone_green tone_blue
flicker?
glow?
brightness

 

ex.
render_light light_render
125
0 0 0
true
false
64

 

ex2.

gallery_18872_189_3040.png

 

if you want to specify the filename just write it in after render_light (in the same comment)

NOTE: all syntax must be lowercase to work!

 


Compatibility

 

No known compatibility issues.

 


Credits and Thanks

 

  • kellessdee

(Special thanks to ForeverZer0 for his AWESOME script layout and because of you I have discovered drop box!)


Author's Notes

 

You can either grab my render_light file from the demo or use this:

gallery_18872_189_490.png

http://rmxpunlimited.net/forums/uploads/gallery/album_189/gallery_18872_189_490.png

 

Or use your own!

 

I forgot to mention as well, my light file is set as the default in the script (filename after render_light is optional)

you can change the default heading over to line 60 and set it to the name of the file you wish to set as default!

 

LINE60:

DEFAULT = 'filename'

 

If anyone has any issues please let me know.

Share this post


Link to post
Share on other sites

Awesome Kellessdee! :alright:

 

I'm gonna test it out now. :33

 

*Does the edits I made prior to this have to be undone? Or is it fine just leaving the edits? :sweatdrop: *

Share this post


Link to post
Share on other sites

Thanks :D

 

It's all up to you really, if you still wanna use the overlays you could actually (probably anyways..I haven't tested it) use both...

If you don't plan on using the other overlays, then you may as well remove them. But It shouldn't make a difference.

 

EDIT: It seems you can do about 60-80 individual (updating, flickering + glowing) light sources before any noticeable slowdowns.

Share this post


Link to post
Share on other sites

Ah man, I love the flickering and glowing effect, absolutely awesome!

 

Can I leave radius_in_pixels blank? I have a image to place over the map for the lighting effect, but I'm not sure its working for me (is a lame novice). :sweatdrop:

Share this post


Link to post
Share on other sites

I'm glad you like it :P

 

It may give you an error if you leave the radius blank...i never made it default :o I think my first update will be modifying the code so you don't have to put EVERY single parameter...

 

However, what is it you are trying to do exactly? do you not want the image to stretch?

Share this post


Link to post
Share on other sites

I made a light image the size of the map (25x20), which I plan on doing for almost all maps, but I can't seem to get it to work...

Share this post


Link to post
Share on other sites

ooohhh i see! yea, this may not work with that. This script draws just a single light source, centered on the location. If you want to use the overlays the other modification I made for you would probably be better...which reminds me I totally forgot about fixing it up :sweatdrop: but if system architect takes any longer to load up I may have a few more scripts done rrrrr.

Share this post


Link to post
Share on other sites

Ah, I see then :P

 

Still, this is an amazing script. I'm gonna use it for fireplaces and flickering effects. :biggrin_002:

Share this post


Link to post
Share on other sites

Really nice script, but I have one question;

 

Is it possible to make your screen tone black, and have only areas that are lit by scripted lights, visible as normal?

 

I've tried to no avail so far, black screen tone obfuscates all graphics except for the lights themselves.

I just think it would be a really nice touch for some of those darker/scary scenes.

Share this post


Link to post
Share on other sites

this is really cool, following what total sticks said, i would like to do a similar thing where everything is blacked out but you light a candle from your inventory and the radius around you lights up, and also follows you when you move around the room, many thanks :)

Share this post


Link to post
Share on other sites

Really nice script, but I have one question;

 

Is it possible to make your screen tone black, and have only areas that are lit by scripted lights, visible as normal?

 

I've tried to no avail so far, black screen tone obfuscates all graphics except for the lights themselves.

I just think it would be a really nice touch for some of those darker/scary scenes.

 

hmmmm actually I think I may have an idea for this! I am gonna play with this script a bit, make it a lil easier to use and post an update. (I think I will make parameters optional so you won't have to specify everything)

 

I think if I make a new function for darkening the screen, and rather than changing the screen tone (as what that does is just blends different colors into the screen) I can probably draw a layer of black then wherever light is rendered, erase an circular area from the overlay.

 

EDIT: @diagostimo: I can definitely add it so the player is a light source as well if you'd like.

Share this post


Link to post
Share on other sites

Kell, take a look at these...

 

http://www.hbgames.org/forums/viewtopic.php?f=155&t=74395

 

http://www.hbgames.org/forums/viewtopic.php?f=11&t=74494

 

First one is a discussion on 'subtractive' lighting & how to implement it

Second one is the script that resulted from said discussion.

 

Might give you some insight / ideas...

 

Be Well

Share this post


Link to post
Share on other sites

That's awesome brewmeister! that helps alot. I was messing with just subtracting a circle area from the overlay...and while it works, it doesn't look very nice/smooth...using graphics to erase the black is a great idea, looks much nicer.

 

I hope you guys don't mind if I use your bitmap sub method and rect overlap method, you will all be included in the credits.

 

The sub method is more what i need albeit modified, I have almost got this working the way I want it! I fixed the parameters (so you only need to put what you want to change, and so it doesn't have to be lower case) and got the subtraction working pretty good.

Share this post


Link to post
Share on other sites

:3 Thank you, even though I should have been working on setting up your battles + homework etc.

 

WHY DO I GET DISTRACTED SO EASILY. I see a few all nighters in my near future.

Share this post


Link to post
Share on other sites

Hey there, just checking in to see if any progress had been made, cant wait to use this for all sorts of things. :)

Share this post


Link to post
Share on other sites

Actually, I have made some progress so far! I found a faster way of subtracting the darkness (the sub effect I found in the other lighting script was too slow for what I was trying to achieve)

 

and have made it not only easier to set up the events (you no longer need to specify everything in a specific order etc), I have given more control: such as quality (how layered the lights appear) and blend types.

 

http://www.youtube.com/watch?v=TRCGGKACEB4&feature=channel_video_title

(sorry for bad quality; I am trying to find either a better recording software or better codecs to use with camstudio)

 

I still need to:

 

(1) Make it so only the light's that are visible update

(2) Make it so you can create the player as a light source

(3) Make it so if you set the light source on a separate page (as in called by a switch or something) will activate the light. (thank you to kevin.ds for pointing this issue out)

Share this post


Link to post
Share on other sites

Actually, I have made some progress so far! I found a faster way of subtracting the darkness (the sub effect I found in the other lighting script was too slow for what I was trying to achieve)

 

and have made it not only easier to set up the events (you no longer need to specify everything in a specific order etc), I have given more control: such as quality (how layered the lights appear) and blend types.

 

http://www.youtube.com/watch?v=TRCGGKACEB4&feature=channel_video_title

(sorry for bad quality; I am trying to find either a better recording software or better codecs to use with camstudio)

 

I still need to:

 

(1) Make it so only the light's that are visible update

(2) Make it so you can create the player as a light source

(3) Make it so if you set the light source on a separate page (as in called by a switch or something) will activate the light. (thank you to kevin.ds for pointing this issue out)

 

 

 

E..P...I...C :D cant wait !

Share this post


Link to post
Share on other sites

Wow this is really nice work man. I dont need this now but I know for sure I will be needing this in the near future. :alright:

Share this post


Link to post
Share on other sites

bump, did you sort the fading of the light? if so could we get an upadate please :)

Edited by diagostimo

Share this post


Link to post
Share on other sites

Requests for script updates are popping up on other forums as well.

 

http://forum.chaos-project.com/index.php/topic,13800.0.html

 

---

 

Video is marked as private.  Is it still available?

 

 

So I was looking at the script on this page and I think it will be handy for the firefly event that I've made.

Here is my problem, since they are always called by switches or self-switches (Day/Night cycle using CCTS), they don't light up.

Take the switches off and it reads that page first calling the lighiting.

Here is an excerpt from that page:

"I still need to:

(1) Make it so only the light's that are visible update
(2) Make it so you can create the player as a light source
(3) Make it so if you set the light source on a separate page (as in called by a switch or something) will activate the light. (thank you to kevin.ds for pointing this issue out)"

So yeah number 3 is the only issue I can't work with. I read that F0 helped him and I was curious if any of you know whether or not something like this can be done.

 

I'll add some things to the list of requested features:

 

Erased Events get rid of light source.

Light Properties can be changed on the Fly.  Comment Config for initial settings only.

Flexibility in Comment Config to allow compatability with other Scripts that use Comments for Config.

Other updates you already mentioned.

 

If this is no longer being maintained, you could post what you have, even if its not complete / functional and we can add to what exists.

 

---

 

Legal:  I'd also like to include this script, old and / or new version in my "Collection" to be distributed with your permission.  May I have permission to distribute this Script in my "Collection"?

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...