Code Review for my Keyframe Animation
Hey, just getting into scripting for Lens and I'm nearly positive there is a better way to do what I'm doing here via scripting. Curious if anyone knowledgable could take a look :)
//@input Component.Image image
//@input Asset.Texture[] frames
//@input int fps = 30
//@input float delay = 0.0
//@input bool loop = false
var frameIndex = 0
var frameRate = 1 / script.fps
var image = script.image
// delay
waitForSeconds(script.delay, function() {
runAnimation()
})
function runAnimation() {
waitForSeconds(frameRate, function() {
setTexture(script.image, script.frames[frameIndex])
frameIndex = frameIndex + 1
if (frameIndex == script.frames.length) {
frameIndex=0
if (script.loop == false) {
return
}
}
runAnimation()
})
}
function waitForSeconds(seconds, callback) {
var delayedEvent = script.createEvent('DelayedCallbackEvent')
delayedEvent.bind(function(eventData) {
callback()
})
delayedEvent.reset(seconds)
}
function setImageTexture(image, texture) {
image.mainPass.baseTex = texture
}
Hey Drew,
If would be helpful if you described what you are trying to accomplish with your script. Are you trying to play an animated image sequence? If that's the case, you can create your 2D animation in the Resources panel by clicking Add New > 2D Animation From Files. Then just select all the images you want in the animation and it will create the animation for you!
Let me know if this helps
Ben
Thanks, thats exactly what I meant.. I knew there was an easier way, thank you