-
-
Save radosinsky/6ac28a2e2c87ddeb0795758d4c95e68c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Animator(){ | |
this.inputs = { | |
object: null, | |
clip: null, | |
animationSpeed: .02 | |
} | |
this.onInit = function(){ | |
if (!this.inputs.object) return; | |
this.setModelAnimation(this.inputs.object, ''); | |
} | |
this.onInputsUpdated = function(oldInputs){ | |
if(!this.inputs.object) return; | |
if (oldInputs.clip != this.inputs.clip || !this.mixer) { | |
this.setModelAnimation(this.inputs.object, this.inputs.clip); | |
} | |
} | |
this.onTick = function(tickDelta){ | |
if (this.mixer) { | |
this.mixer.update(this.inputs.animationSpeed); | |
} | |
} | |
this.onDestroy = function(){} | |
this.setModelAnimation = function(object, clipName){ | |
const THREE = this.context.three; | |
this.mixer = new THREE.AnimationMixer(object); | |
this.clipAction = this.mixer.clipAction(THREE.AnimationClip.findByName(this.findAnimations(object), clipName)); | |
this.clipAction.play(); | |
} | |
this.findAnimations = function(object){ | |
const animations = []; | |
object.traverse(obj => { | |
if (obj.animations){ | |
obj.animations.forEach(anim => animations.push(anim)); | |
} | |
}); | |
return animations; | |
} | |
} | |
export const animatorType = 'mp.animator'; | |
export function makeAnimator(){ | |
return new Animator(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment