Skip to content

Instantly share code, notes, and snippets.

@radosinsky
Created July 17, 2021 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radosinsky/6ac28a2e2c87ddeb0795758d4c95e68c to your computer and use it in GitHub Desktop.
Save radosinsky/6ac28a2e2c87ddeb0795758d4c95e68c to your computer and use it in GitHub Desktop.
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