Skip to content
Snippets Groups Projects
loop-animation.js 1.29 KiB
Newer Older
import { findAncestorWithComponent } from "../utils/scene-graph";
/**
 * Loops the given clip using this entity's animation mixer
 * @component loop-animation
 */
AFRAME.registerComponent("loop-animation", {
  schema: {
  init() {
    this.mixerEl = findAncestorWithComponent(this.el, "animation-mixer");
      console.warn("loop-animation component could not find an animation-mixer in its ancestors.");
Robert Long's avatar
Robert Long committed

    if (oldData.clip !== this.data.clip && this.mixerEl) {
    const { mixer, animations } = this.mixerEl.components["animation-mixer"];
    const clipName = this.data.clip;

    if (animations.length === 0) {
    if (!clipName) {
      clip = animations[0];
    } else {
      clip = animations.find(({ name }) => name === clipName);
    }

    if (!clip) {
      return;
    const action = mixer.clipAction(clip, this.el.object3D);
    action.enabled = true;
    action.setLoop(THREE.LoopRepeat, Infinity).play();
    this.currentAction = action;
    if (this.currentAction) {
      this.currentAction.enabled = false;
      this.currentAction.stop();
    }