Page 1 of 2

Canvas & Stencil in Love 11.0

Posted: Mon Apr 02, 2018 7:24 pm
by Ref
Error message:
Error
physics.lua:435: Drawing to the stencil buffer with a Canvas active requires either stencil=true or a custom stencil-type Canvas to be used, in setCanvas.
Code referenced:

Code: Select all

texCanvas = function( canW, canH, image, x, y, a, sx, sy, wd, ht  )
	local canvas = love.graphics.newCanvas( canW, canH )
	local stencilFunction = function()
		gr.polygon( 'fill', polymask )
	end
	gr.setCanvas( canvas )
		gr.stencil( stencilFunction, 'replace', 1 )
		gr.setStencilTest( 'greater',  0 )
			gr.setColor( 1, 1, 1 )
			gr.draw( image, x, y, a, sx, sy, image:getWidth()/2, image:getHeight()/2 )
		gr.setStencilTest()
	gr.setCanvas()
	return canvas
end
OK!
Works on Love 10 but not on Love 11.
Where do I stick 'stencil=true' or get a "custom stencil-type Canvas" from?

Re: Canvas & Stencil in Love 11.0

Posted: Mon Apr 02, 2018 10:26 pm
by raidho36
Try

Code: Select all

gr.setCanvas( canvas. { stencil = true } )
or try browsing source code and hopefully work it out that way.

Re: Canvas & Stencil in Love 11.0

Posted: Tue Apr 03, 2018 3:10 am
by zorg
That dot should be a comma though.

Re: Canvas & Stencil in Love 11.0

Posted: Wed Apr 04, 2018 7:28 am
by Countzero
So (just got it from the wiki, thought) the right way (or at least a way that works) is

Code: Select all

love.graphics.setCanvas({canvas, stencil=true})
or just withouth the round brackets

Code: Select all

love.graphics.setCanvas{canvas, stencil=true}
Tested and working.

Re: Canvas & Stencil in Love 11.0

Posted: Wed Apr 04, 2018 1:55 pm
by Ref
Glad to hear you got it to work.
Used your suggestion and got the same error message for the line mentioned above.
Must be something else in the code that is causing this Love 10 script to fail in Love 11.
More head scratching required!

Re: Canvas & Stencil in Love 11.0

Posted: Wed Apr 04, 2018 2:42 pm
by pgimeno
Ref wrote: Wed Apr 04, 2018 1:55 pm Glad to hear you got it to work.
Used your suggestion and got the same error message for the line mentioned above.
Must be something else in the code that is causing this Love 10 script to fail in Love 11.
More head scratching required!
Really? Does this code gives an error for you? (texCanvas is a direct copy/paste of your function, with the change suggested by Countzero)

Code: Select all

local gr = love.graphics
local polymask = {0,0,1,1,0,1}

texCanvas = function( canW, canH, image, x, y, a, sx, sy, wd, ht  )
	local canvas = love.graphics.newCanvas( canW, canH )
	local stencilFunction = function()
		gr.polygon( 'fill', polymask )
	end
	gr.setCanvas( {canvas, stencil=true} )
		gr.stencil( stencilFunction, 'replace', 1 )
		gr.setStencilTest( 'greater',  0 )
			gr.setColor( 1, 1, 1 )
			gr.draw( image, x, y, a, sx, sy, image:getWidth()/2, image:getHeight()/2 )
		gr.setStencilTest()
	gr.setCanvas()
	return canvas
end

texCanvas(100,100,love.graphics.newImage(love.image.newImageData(1,1)), 10, 10, 1, 1, 1, 1, 1)

function love.keypressed(k) if k == "escape" then love.event.quit() end end

Re: Canvas & Stencil in Love 11.0

Posted: Wed Apr 04, 2018 8:54 pm
by Ref
Bingo!

Code: Select all

gr.setCanvas( {canvas, stencil=true} )
does work.
Just have to get the brackets in the right places.
Thanks for the help!

Re: Canvas & Stencil in Love 11.0

Posted: Thu Apr 05, 2018 10:55 pm
by MissDanish
btw canvases are broken in 11.0 unless you use the fix smile linked in the 11.0 release thread. Sometimes they refuse to draw on screen

Re: Canvas & Stencil in Love 11.0

Posted: Sun Jun 30, 2019 3:08 pm
by Jack Dandy
Hey fellas, I ran into the same problem.

https://www.reddit.com/r/love2d/comment ... th_ingame/
Hi.

I made a game that uses a Stencil to create a 'Fog of War' style effect. It worked great.

However, when I tried using the 'Scrale' scaling library to also have a nice fullscreen option, it gave me this error:

(image on reddit thread)

Scrale uses the canvas mechanic to work.

(I understand that this is a problem Love2D version 11.0 and up are having- canvases and stencils colliding.)

(image on reddit thread)

I checked out the implementation of scrale, found that it only uses 'setCanvas' in one place, and changed it from 'love.graphics.setCanvas(scrale.canvas)'

to 'love.graphics.setCanvas({scrale.canvas, stencil=true}' ).

But then, it breaks the Fog of War effect. Things that were previously hidden by it, are now always displayed (Even when they're supposed to be hidden). See below - every sprite in the greyed-out and blacked-out zone is not supposed to appear. It worked just fine before.


(image on reddit thread)


In short- anyone have a clue on how to combine Scrale with using stencils?
Can anyone help please?

Re: Canvas & Stencil in Love 11.0

Posted: Sat Jul 06, 2019 8:41 am
by Jack Dandy
Bump? All it involves is a canvas and a stencil that is drawn & applied inside it.