Page 1 of 1

Sample average of multiple canvases.

Posted: Sun Sep 23, 2018 3:37 pm
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?

Re: Sample average of multiple canvases.

Posted: Sun Sep 23, 2018 7:59 pm
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")

Re: Sample average of multiple canvases.

Posted: Mon Sep 24, 2018 8:11 am
by GijsB

Re: Sample average of multiple canvases.

Posted: Mon Sep 24, 2018 10:35 am
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)