Preventing lighting from being drawn on the background

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

Preventing lighting from being drawn on the background

Post by Whatthefuck »

So I have lighting in my game, however I came across an issue - the shadows are drawn on the background. I need a way of preventing the shadows from being drawn on the background. Here's the problem I came across:

Image

The orange'ish color is the background, and obviously, it should be void of any light being drawn over it.
How do I prevent that? Use a shader? And if so, what kind? Any examples?

Now, not all backgrounds should be void of the lighting. Here's an example of the background that does receive the lighting and should stay that way:

Image
User avatar
Zer0
Citizen
Posts: 59
Joined: Sat Oct 06, 2012 9:55 am
Location: Sweden
Contact:

Re: Preventing lighting from being drawn on the background

Post by Zer0 »

Probably using a canvas for the foreground (everything BUT the background) that is shaded and drawing it ontop of the unshaded background.
[wiki]Canvas[/wiki]
EDIT: I sure hope you're using spritebatches for those tiles. (Just mentioning because alot of draw calls is WAAAY more expensive than one draw call with a spritebatch)
If you can't fix it, Kill it with fire. ( Preferably before it lays eggs. )
Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

Re: Preventing lighting from being drawn on the background

Post by Whatthefuck »

Zer0 wrote:Probably using a canvas for the foreground (everything BUT the background) that is shaded and drawing it ontop of the unshaded background.
[wiki]Canvas[/wiki]
That won't do anything.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Preventing lighting from being drawn on the background

Post by micha »

What method are you using for drawing the lighting? Is it a shader or a mesh or something else?
Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

Re: Preventing lighting from being drawn on the background

Post by Whatthefuck »

micha wrote:What method are you using for drawing the lighting? Is it a shader or a mesh or something else?
It's a spritebatch.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Preventing lighting from being drawn on the background

Post by kikito »

Show us the code. Give us a .love file showing the issue. Otherwise it's too difficult to help you.
When I write def I mean function.
User avatar
Zer0
Citizen
Posts: 59
Joined: Sat Oct 06, 2012 9:55 am
Location: Sweden
Contact:

Re: Preventing lighting from being drawn on the background

Post by Zer0 »

Whatthefuck wrote:
micha wrote:What method are you using for drawing the lighting? Is it a shader or a mesh or something else?
It's a spritebatch.
A spriteBatch like 1 sprite per pixel? *cough* inefficient *cough*
I was guessing for a shader and you did not really explain how you did the lightning.
1. Draw the background
2. Start drawing to canvas with lightning shader
3. Draw everything shaded
4. Draw the canvas

If you don't want to use a shader for some reason use a stencil.
1. Draw background without lightning
2. Use the foreground as a stencil and draw it.
If you can't fix it, Kill it with fire. ( Preferably before it lays eggs. )
Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

Re: Preventing lighting from being drawn on the background

Post by Whatthefuck »

Zer0 wrote:
Whatthefuck wrote:
micha wrote:What method are you using for drawing the lighting? Is it a shader or a mesh or something else?
It's a spritebatch.
A spriteBatch like 1 sprite per pixel? *cough* inefficient *cough*
I was guessing for a shader and you did not really explain how you did the lightning.
1. Draw the background
2. Start drawing to canvas with lightning shader
3. Draw everything shaded
4. Draw the canvas

If you don't want to use a shader for some reason use a stencil.
1. Draw background without lightning
2. Use the foreground as a stencil and draw it.
1 sprite per 20x20 pixels, it's actually pretty efficient (not THE most efficient method but for a person who has no clue how to write shaders and in the way I use it, it produces good results). And no, I don't use any shaders.
The background is just the default one with the colors being changed via love.graphics.setBackgroundColor.
How would I use a stencil for that?
For reference, the finalized lighting is drawn into a separate canvas, which is then upscaled.
Last edited by Whatthefuck on Fri Jul 25, 2014 8:41 am, edited 2 times in total.
Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

Re: Preventing lighting from being drawn on the background

Post by Whatthefuck »

kikito wrote:Show us the code. Give us a .love file showing the issue. Otherwise it's too difficult to help you.
There's so much code and stuff that I'd have to spend hours to pack everything into a separate .love file.

Edit: argh, no automerge. ;_;
User avatar
Zer0
Citizen
Posts: 59
Joined: Sat Oct 06, 2012 9:55 am
Location: Sweden
Contact:

Re: Preventing lighting from being drawn on the background

Post by Zer0 »

Whatthefuck wrote:How would I use a stencil for that?

Code: Select all

    draw_background()
    love.graphics.setStencil(draw_foreground())
    draw_foreground()
    love.graphics.setStencil()
[wiki]love.graphics.setStencil[/wiki]
setStencil says you ONLY draw to where the stencil draws. (the stencil is just a function with stuff that is drawn)
If you can't fix it, Kill it with fire. ( Preferably before it lays eggs. )
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 206 guests