Page 1 of 1

Efficient sprite interpolation

Posted: Tue Dec 26, 2017 7:48 pm
by Boarschti
Hello,

I'm new to LÖVE.
I want to interpolate between two frames in a sprite animation. The code below is extremly slow when called in love.draw().
What is the best/most efficient way to implement sprite interpolation?

Thank you!

Code: Select all

function InterpolateImage(firstImage, secondImage, tweenFactor)

    local width  = firstImage:getData():getWidth()
    local height = firstImage:getData():getHeight()

    imageData = love.image.newImageData(width, height)

    for x = 0, image:getWidth() - 1 do
        for y = 0, image:getHeight() - 1 do
            r1, g1, b1, a1 = firstImage:getData():getPixel(x,y)
            r2, g2, b2, a2 = secondImage:getData():getPixel(x,y)
            
            -- interolate
            imageData:setPixel(x, y,
                               r1 + (r2 - r1) * tweenFactor,
                               r1 + (r2 - r1) * tweenFactor,
                               r1 + (r2 - r1) * tweenFactor,
                               r1 + (r2 - r1) * tweenFactor)
        end
    end

    return love.graphics.newImage(imageData)
end

Re: Efficient sprite interpolation

Posted: Tue Dec 26, 2017 8:19 pm
by ivan
Draw both images on top of each other.
Interpolate the alpha (setColor(255,255,255,255*tweenFactor) of the top image.
Shaders could be used for more sophisticated effects.