Page 1 of 1

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

Posted: Wed Mar 02, 2016 7:56 pm
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?

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

Posted: Wed Mar 02, 2016 8:47 pm
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.

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

Posted: Wed Mar 02, 2016 9:45 pm
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.

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

Posted: Thu Mar 03, 2016 9:41 am
by molul
Cool! Yes, I thought a stencil wouldn't blend, but I wanted to confirm. Thank you very much :D

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

Posted: Tue Mar 15, 2016 6:52 pm
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 :)

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

Posted: Tue Mar 15, 2016 9:36 pm
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.

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

Posted: Wed Mar 16, 2016 9:19 am
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.

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

Posted: Wed Mar 16, 2016 2:10 pm
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.

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

Posted: Thu Mar 17, 2016 6:43 pm
by molul
I see. Thanks for the explanation :)