Carregando modelo 3D com animação usando CreateFromMorphTargetSequence Three.js

Nov 07 2020

Estou tentando carregar este arquivo GBL e reproduzir a animação. Este código retorna o seguinte erro:

TypeError: Não é possível ler a propriedade 'length' de indefinido em Function.CreateFromMorphTargetSequence

function Animate() {
    if(window.anim_flag)
    {
    // Hotspot_Ring_Anim();
    requestAnimationFrame(Animate);
        renderer.clear();
        TWEEN.update();
        orbit.update();
        if (mixer.length > 0) {
        var delta = clock.getDelta();
        for (var i = 0; i < mixer.length; i++) {
            mixer[i].update(delta);
            }
        }
        renderer.render(scene, camera);

    }
    
}


function Add_Hotspot_Rings(id,px,py,pz,rx,ry,rz,sx,sy,sz) {
  const loader = new GLTFLoader();

  // Optional: Provide a DRACOLoader instance to decode compressed mesh data
  const dracoLoader = new DRACOLoader();
  dracoLoader.setDecoderPath(  '../jsm/draco/'  );
  loader.setDRACOLoader( dracoLoader );

  loader.load( '../Models/ABB_Clinic_AnimatedRings_Lowpoly_02.glb', function ( gltf ) {
    const model = gltf.scene;
    model.name = 'hotspot_rings';

    model.position.set(px,py,pz);
    model.rotation.set(0,ry,rz);
    model.scale.set(0.90, 0.3, 0.90);
    scene.add(model);

    // MORPH
    const mixerr = new THREE.AnimationMixer( model );
    const clips = model.animations;

    const morphClip = THREE.AnimationClip.CreateFromMorphTargetSequence( 'RingsRising', model.morphTargets );
    mixerr.clipAction(morphClip).setDuration(1).play();

    mixer.push(mixerr);
    window.anim_flag= true;
    Animate();

  }, undefined, function ( error ) {

    console.error( error );

  } );
}

Como posso resolver esse erro e carregar o modelo com a animação em execução?

Respostas

2 Mugen87 Nov 08 2020 at 11:14

const morphClip = THREE.AnimationClip.CreateFromMorphTargetSequence ('RingsRising', model.morphTargets);

As instâncias de Object3Dnão têm uma morphTargetspropriedade.

A reprodução de uma animação de um ativo glTF deve ser assim:

const animations = gltf.animations;
const mixer = new THREE.AnimationMixer( model );
mixer.clipAction( animations[ 0 ] ).setDuration( 1 ).play();

Exemplo ao vivo: https://threejs.org/examples/webgl_morphtargets_horse