Help with "fade out" effect

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
erty
Prole
Posts: 2
Joined: Fri May 03, 2024 1:28 pm

Help with "fade out" effect

Post by erty »

So I tend to overengineer things, so please tell me if there's a more efficient/better way of accomplishing this!

I want my characters to leave "slime trails", and so my thought was this: I'll create two canvases. Every frame, I'll flip between the two canvases, copying the "front" buffer into the "back" buffer with some small transparency multiplier. This would make anything drawn to either canvas slowly fade out.

However, I get a really weird effect where some frames it draws the slime trail at full transparency, and some frames it draws at half transparency.

Here's where I copy over the slime trails from the previous frame

Code: Select all

function CopySlimeCanvas(fromCanvas, toCanvas)
    love.graphics.setCanvas(toCanvas)
    love.graphics.clear()
    -- Set premultiplied alpha blend mode since we've
    -- already rendered the maze to canvas
    love.graphics.setBlendMode("alpha", "premultiplied")
    love.graphics.setColor(255, 255, 255, 0.5) -- draw the previous "back" buffer at half transparency (???)
    love.graphics.draw(fromCanvas, 0, 0)
    love.graphics.reset()
end
Here's where I draw the players' slime trails

Code: Select all

function StatePlaying:drawSlimeCanvas()
    love.graphics.setCanvas(self.slimeCanvases[self.currentSlimeCanvas]) -- currentSlimeCanvas switches between 1 and 2 every frame
    for _, player in ipairs(self.players) do
        love.graphics.setColor(255, 0, 255, 0.5) -- Draw the slime trail at half opacity
        love.graphics.circle("fill", player.x, player.y, SLIME_RADIUS)
    end
    love.graphics.reset()
end
I've attached my .love file - the relevant code is in stateplaying.lua, starting on line 322.

Thanks!

Erty
Attachments
love-game.love
(1.57 MiB) Downloaded 69 times
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Help with "fade out" effect

Post by BrotSagtMist »

That .love is packaged incorrectly.

I see no fading at all here and i am not sure what exactly you need.
But you dont need to have multiple canvases for it.
If you just want a fading trail then the smartest solution is to have a table full of positions that you just prepend (table insert) the current position of your snail in and then you iterate over it drawing the trails with sinking alpha.
If the trail is static then just draw it half transparent on one canvas but use the replace pixel method instead drawing on top of the old pixel.
obey
erty
Prole
Posts: 2
Joined: Fri May 03, 2024 1:28 pm

Re: Help with "fade out" effect

Post by erty »

Ah, of course! Redrawing is a great way to do it. Thanks!
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Help with "fade out" effect

Post by knorke »

Your game looks nice so far. :awesome:

This is not a real solution but maybe slightly useful:
I made a "everything leaves a fading trail" effect a while ago.
I tried to re-create it in the attached doodle.
First draw the trails to a canvas. Then every few moments draw a black but slightly transparent rectangle over the whole canvas.
Of course this has the limitation that everything fades out at the same rate.
Also needs some blendMode or stencil so that the black parts do not cover everything.

I think the canvas-methode might have potential if you want long trails for lots of things without creating large tables for all the past positions.

It could also be used to give trails to https://love2d.org/wiki/ParticleSystem
There is no way to read the positions of emitted love-particles so storing their past positions is not possible.
Attachments
fadeTrails.love
(650 Bytes) Downloaded 45 times
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests