Efficient sprite interpolation

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
Boarschti
Prole
Posts: 1
Joined: Tue Dec 26, 2017 7:38 pm

Efficient sprite interpolation

Post 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
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Efficient sprite interpolation

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests