Lighting Basics

So, recently I was playing through a friends project demo and I noticed he was using the 'classic' Khas 'Awesome' Light Effects.

Now, The Khas script does boast some excellent features; coupled with the fact it is now free for commercial games tempted me to insert it into my own project.

Shortly after inserting the script, once I had continued development of my own project, I noticed some severe lag issues occurring. Immediately, I assumed that this was due to the small script I wrote to show a lantern (like, in the players hand) by using a simple script call...

To give you idea how this looks...

Spoiler
1394400137108028_zps62505b77.png
After turning off my simply little lantern graphic script, I realized that the lag was actually being caused by Khas light script. Even though my map is 'tiny' (24x26) with only one light source on it (the players lantern)

So, this gave me the motivation to write my own 'simple' lighting script.

The features I need are fairly simple as it is only to add a little detail when in caves, and to make use of my 'show lantern' script mentioned previously.

Anyway...

I seem to have 'hit a snag' that I cant quite put my finger on.

I wrote a little snippet to simply create a layer of 'darkness' and another to create a 'light'. I was using the light images found in khas awesome light demo (as i figured they would work too)

Now, when I create both images, I cant seem to get the 'light' to 'override' the 'darkness'

It looks like this...

Spoiler
1394466648007672_zps0d38b7f8.png
As you can see, my 'light' is simply shown above the darkness layer. Additionally, it does not look 'light' in any sense of the term..

My first thought was to simply 'invert' the light image... So I did...

Spoiler
1394466703798863_zpsf8f095a6.png
As you can see, it is still not looking right at all..

I have tried adjusting the 'blend_type' to various values for both the darkness and the light.

I have tried other various things I could think of, such as setting the color.alpha to high/low values, tried turning opacity / visibility on/off etc. and It just doesn't seem to want to show my images correctly..

For the meantime (until I can resolve this issue) I have resorted to a 'pokemon - flash' style light

Spoiler
1394466776688032_zpsbc9360f0.png
As you can see, this is also not ideal; therefore, I would greatly appreciate any help anyone may have into this issue.

For your perusal, I have included my script in this final spoiler ^_^

Spoiler
if true # << Make true to use this script, false to disable.#===============================================================================module Easy_Lights#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  Folder  = "Graphics/Lights/"                            #####################                            # CUSTOMISATION END #end                         ######################===============================================================================module Cache#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def self.lights(filename, hue = 0)    load_bitmap(Easy_Lights::Folder, filename, hue)  endend#===============================================================================class Sprite_Darkness < Sprite#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def initialize(viewport)    super(viewport)    gw = Graphics.width    gh = Graphics.height    co = Color.new(0,0,0,200)    self.bitmap = Bitmap.new(gw,gh)    self.bitmap.fill_rect(0,0,gw,gh,co)    self.visible = true    self.blend_type = 0  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def dispose    super  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def update    super  endend#===============================================================================class Sprite_Lights < Sprite#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def initialize(viewport,image)    super(viewport)    self.bitmap = Cache.lights(image)    self.blend_type = 0    self.ox = (self.bitmap.width/2)    self.oy = (self.bitmap.height/2)+12    self.visible = true#    self.color.alpha = 255  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def dispose    super  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def update    super  endend#===============================================================================class Spriteset_Map#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  alias :init_ezaylight :initialize  alias :disp_ezaylight :dispose  alias :updt_ezaylight :update  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def initialize    init_ezaylight    init_darkness    init_lights  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def dispose    disp_ezaylight    disp_darkness    disp_lights  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def update    updt_ezaylight    updt_darkness    updt_lights  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def init_darkness    @darkness = Sprite_Darkness.new(@viewport2)    updt_darkness  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def disp_darkness    return unless @darkness    @darkness.dispose    @darkness = nil  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def updt_darkness    return unless @darkness    @darkness.update    @darkness.z = $game_player.screen_z-1  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def init_lights    @lights = Sprite_Lights.new(@viewport2,"light_s")    updt_lights  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def disp_lights    return unless @lights    @lights.dispose    @lights = nil  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def updt_lights    return unless @lights    @lights.update    @lights.x = $game_player.screen_x    @lights.y = $game_player.screen_y    @lights.z = $game_player.screen_z-1  end end#==============================================================================##                      http://dekitarpg.wordpress.com/                         ##==============================================================================#end  # if true # << Make true to use this script, false to disable.
This script will autorun when you insert it into your project. You must ensure that you have a folder called Lights within the graphics folder and the image "light_s" from Khas' awsome lights demo, which can be found HERE.

So, if anyone can shed some light onto this dull situation it would enlighten my mood substantially ^_^

Also, if you need any more info / have questions, feel free to let me know 🙂
I have my own one... I think the main lag cause with Khas is the things done per frame, like updating lights, dynamic shadows etc.


The lighting system works because the sprite in which the color is painted is using the "negative/subtract" blend mode... then you simply blt the light into this sprite, so since it was set to subtract, the blt'd image removes parts of the sprite rather than adding to it.


Basically, this is the outline:


1) Create a new sprite and fill it with the darkness color


2) Make sure it was set to blending mode for subtract (it was 2 I think)


3) use the sprite class' bitmap.blt or stretch_blt method to put the light image into the surface sprite


4) Adjust them (per frame or if needed only)


The main thing here is that the darkness and the light should be on just 1 sprite which uses the subtract blend mode.
omg of course...  I feel so silly now lmao.

I just kept looking at the code and thinking 'im doing something wrong here' and just couldnt put my finger on it. I ended up coding it to work the same as the pokemon style shown above, but doing it 'properly' will mean I can add another cool feature i wanted.

Thanks for pointing this out. It seems so obvious now aha ^_^
Back when I was making it, it took me quite a lot of time to realize it needed to be subtract mode (I noticed the blt part quite a bit early on reading the script). sometimes it's really a bit hard to pinpoint things. I was even actually afraid at first of looking into Khas' script.
lol I had a look at khas script and its very unpleasing code to read. its all crammed in together and such. But I didt notice the blt :/

Gonna have a little mess around with it tomorrow as im currently implementing new features in my equip scene 🙂

Again, thanks for pointing this out ^_^
well yeah, it took time to actually make a sense as to what each line does...

Original thread: https://forums.rpgmakerweb.com/threads/lighting-basics.24815/