Sample average of multiple canvases.

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.
Post Reply
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Sample average of multiple canvases.

Post by GijsB »

Hey everyone,

I'm making this small renderer using shaders. I'm complety at a loss on how to render multiple frames using shaders and afterwards average them together.

I've tried rendering a single frame on a canvas then afterwards adding this canvas to another with

Code: Select all

love.graphics.setBlendMode("add")
which I then render to the screen with a shader which divides by the total amount of frames :

Code: Select all

--initialize Render canvas
Render = love.graphics.newCanvas(
	Width,
	Height,
	{
		type = "2d",
		format = "rgba32f",
		readable = true,
		msaa = 0,
		dpiscale = love.graphics.getDPIScale(),
		mipmaps = "none"
	}
)

--render single Frame
local Frame = love.graphics.newCanvas(Width,Height)
love.graphics.setCanvas(Frame)
	love.graphics.setBlendMode("alpha")
	love.graphics.setShader(RenderShader)
	love.graphics.rectangle("fill",0,0,Width,Heigh)

--add frame to Render
love.graphics.setCanvas(Render)
	love.graphics.setBlendMode("add")
	love.graphics.setShader()
	love.graphics.draw(Frame)
			
--draw average Render to screen 
love.graphics.setCanvas()
	love.graphics.setBlendMode("alpha")
	love.graphics.setShader(PostShader)
	love.graphics.draw(Render)
But the only thing I'm achieving is a black screen :(

Anyone knows a way to do this correctly?
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Sample average of multiple canvases.

Post by pgimeno »

You don't need a shader. If you have say 6 images to average, you can use this:

Code: Select all

  -- Prepare the canvas and draw mode
  love.graphics.setCanvas(averageCanvas)
  love.graphics.clear(0, 0, 0, 0)
  love.graphics.setBlenderMode("alpha", "alphamultiply")

  -- Draw the images, each with an alpha equal to 1/number-of-image
  love.graphics.setColor(1, 1, 1, 1)
  love.graphics.draw(image1)
  love.graphics.setColor(1, 1, 1, 1/2)
  love.graphics.draw(image2)
  love.graphics.setColor(1, 1, 1, 1/3)
  love.graphics.draw(image3)
  love.graphics.setColor(1, 1, 1, 1/4)
  love.graphics.draw(image4)
  love.graphics.setColor(1, 1, 1, 1/5)
  love.graphics.draw(image5)
  love.graphics.setColor(1, 1, 1, 1/6)
  love.graphics.draw(image6)

  -- Stop drawing to the canvas, restore normal colour
  love.graphics.setCanvas()
  love.graphics.setColor(1, 1, 1, 1)

  -- Prepare to draw the canvas to the screen in premultiplied mode
  love.graphics.setBlendMode("alpha", "premultiplied")
  love.graphics.draw(averageCanvas)

  -- Restore normal blend mode
  love.graphics.setBlendMode("alpha", "alphamultiply")
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Sample average of multiple canvases.

Post by GijsB »

User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Sample average of multiple canvases.

Post by pgimeno »

You're welcome!

After reviewing the blend formulas, I've noticed that you don't even need the canvas (maybe you need it anyway for other operations, but for just the averaging it's not necessary):

Code: Select all

  love.graphics.setBlenderMode("alpha", "alphamultiply")

  love.graphics.setColor(1, 1, 1, 1)
  love.graphics.draw(image1)
  love.graphics.setColor(1, 1, 1, 1/2)
  love.graphics.draw(image2)
  love.graphics.setColor(1, 1, 1, 1/3)
  love.graphics.draw(image3)
  love.graphics.setColor(1, 1, 1, 1/4)
  love.graphics.draw(image4)
  love.graphics.setColor(1, 1, 1, 1/5)
  love.graphics.draw(image5)
  love.graphics.setColor(1, 1, 1, 1/6)
  love.graphics.draw(image6)

  love.graphics.setColor(1, 1, 1, 1)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 6 guests