Is it possible to apply a mask to a tiledmap with LÖVE?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Is it possible to apply a mask to a tiledmap with LÖVE?

Post by molul »

I'm trying to do this for my platformer game: http://assets.sbnation.com/assets/17402 ... n07_WP.jpg

I've already implemented a function (from one of the setStencilTest examples in the wiki) that uses a stencil map to draw a circle around the player when he's touching any tile in a certain layer in a tiled map (tmx).

My question would be: is it possible to have the circle perimeter blurred as in the picture above?
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Is it possible to apply a mask to a tiledmap with LÖVE?

Post by Davidobot »

You can try combining the stencil function and a mesh that blurs its edges: https://love2d.org/wiki/love.graphics.newMesh
Or have a stencil of a premade circle image with a blur.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Is it possible to apply a mask to a tiledmap with LÖVE?

Post by s-ol »

Davidobot wrote:You can try combining the stencil function and a mesh that blurs its edges: https://love2d.org/wiki/love.graphics.newMesh
Or have a stencil of a premade circle image with a blur.
a stencil won't do as it either discards or uses a pixel, but doesn't blend, so it can't blur. OP needs a shader. For example there is one in the o-ten-one splashscreen but it is rather entangled with the other (LIGHTEN) shader code.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Is it possible to apply a mask to a tiledmap with LÖVE?

Post by molul »

Cool! Yes, I thought a stencil wouldn't blend, but I wanted to confirm. Thank you very much :D
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Is it possible to apply a mask to a tiledmap with LÖVE?

Post by molul »

I'm coming back to this, as I'm not very happy with the current result. Besides the stencil, I'm drawing a dark circle around the character, but the effect, as you can see in the attached 2.png, is not very good.

I was wondering if I could use blending modes to achieve what I want, that is, the dark circle only darkening any tile behind it, and not ay pixel behind it (I don't want the scrolls to darken too).

Any hint is appreciated :)
Attachments
2.png
2.png (236.29 KiB) Viewed 3453 times
1.png
1.png (231.62 KiB) Viewed 3453 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Is it possible to apply a mask to a tiledmap with LÖVE?

Post by s-ol »

molul wrote:I'm coming back to this, as I'm not very happy with the current result. Besides the stencil, I'm drawing a dark circle around the character, but the effect, as you can see in the attached 2.png, is not very good.

I was wondering if I could use blending modes to achieve what I want, that is, the dark circle only darkening any tile behind it, and not ay pixel behind it (I don't want the scrolls to darken too).

Any hint is appreciated :)
Use a shader or blendmode that makes the circle not show up on pixels where the destination alpha value is 0, then either make the background have 0 alpha via a shader, by actually lowering the PNG alpha to zero or by drawing everything but the background to a canvas (which is transparent to begin with) and afterwards drawing that canvas on top of the background.

EDIT: I can't find a blendmode that does that, so you'll need a super simple pixel/fragment shader.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Is it possible to apply a mask to a tiledmap with LÖVE?

Post by molul »

Thanks for the info! I guessed it wouldn't be easy, but fortunately I found a way that more or less does what I wanted:

-If I'm going to draw the tiledmap layer that covers the secret areas (called "secrets"), I enable a stencil circle. This will eras any pixel from that layer that is inside the circle. When I finish drawing that layer, I disable this stencil.

-After drawing the layer, I enable another stencil circle. This time it's made so only pixels inside the circle are drawn.

-Then, I draw dark squares (with alpha = 100) whenever I find a tile in "secrets" layer. Finally, I disable the second stencil circle.



This way I'm able to achieve the same effect Mario had (except for the blurred edge, which would need a shader). For it to work, I have to make sure that when I enter a secret area in the map, I have put enought tiles on top, right, bottom and left edges of the area so the circle doesn't get cut when the character is close to a wall, ceiling or ground tile. It's not perfect, but I can live with that for now.

It would be very cool that a stencil was able to darken or lighten (or even change the alpha) of the pixels it's working on, instead of just allowing to draw them or not. Would this feature request make sense? If so, I'd gladly raise a ticket in the issue tracker.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Is it possible to apply a mask to a tiledmap with LÖVE?

Post by s-ol »

It would be very cool that a stencil was able to darken or lighten (or even change the alpha) of the pixels it's working on, instead of just allowing to draw them or not. Would this feature request make sense? If so, I'd gladly raise a ticket in the issue tracker.
Stencils are an old OpenGl hardware supported feature. In 0.10 with setStencilTest() we now have access to the complete capabilities of the Stencil Buffer; all it can do is discard or keep a pixel based on one comparison with the previous value in the stencil buffer, that's it. Here's the OpenGL documentation for the Stencil buffer: https://www.opengl.org/wiki/Stencil_Test

As you can see there's no way such a feature could be added without emulating it via shaders etc. which wouldn't make sense to be in the LÖVE api.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Is it possible to apply a mask to a tiledmap with LÖVE?

Post by molul »

I see. Thanks for the explanation :)
Post Reply

Who is online

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