gpt4 book ai didi

javascript - 在 three.js 中使用 THREE.GLTFLoader 导入和播放多个动画

转载 作者:行者123 更新时间:2023-11-29 16:38:13 25 4
gpt4 key购买 nike

当我将 .glb 文件放入 glTF 查看器 (gltf-viewer.donmccurdy.com) 时,我使用 gltfExporter 从 Blender 导出了一个动画模型(宇宙飞船)和一个动画相机作为 .glb 一切有效,所以我知道问题不在于模型/ blender 。

问题是当我尝试在我的脚本中播放动画时。我既可以让模型完美地制作动画,也可以让相机完美地制作动画,但是当我尝试这样做时,相机和模型都会变得疯狂。我想这可能是我对动画混合器缺乏了解?也许我只需要为一个文件使用一个混音器?无论如何,这就是我正在做的事情:

我创建了两个独立的动画混合器,一个用于宇宙飞船,一个用于相机:

gltfStore.mixer = new THREE.AnimationMixer(gltf.cameras[0]);
gltfStore.mixerShip = new THREE.AnimationMixer(gltf.scene.children[2]);

我使用 clipAction 播放动画:

gltfStore.mixer.clipAction(gltfStore.animations[0]).play(); 
gltfStore.mixerShip.clipAction(gltfStore.animations[0]).play();

在我的循环中,我渲染动画:

gltfStore.mixer.update(clock.getDelta());
gltfStore.mixerShip.update(clock.getDelta());

它们单独工作,但一起工作时不工作。我注意到在 glTF 动画对象中导入的动画数据包括 gltf.animations[0] 下相机和模型的动画。我的意思是 gltf.animations[0] 有一个 tracks 数组,其中包含 6 个项目,每个项目的位置、四元数和比例。是吗?

为方便起见,这里是主要的js文件:

var scene = new THREE.Scene();      
var mixer, animationClip;
var clock = new THREE.Clock();

var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

//LIGHTS
var light = new THREE.PointLight( 0xffffff, 1, 200 );
light.position.set( 10, -10, 0 );
scene.add( light )

//OBJECT TO STORE THE GLTF ASSETS WHEN LOADED
var gltfStore = {};

var loader = new THREE.GLTFLoader();

// LOAD GLTF ASSETS
var gltfCamera = loader.load(
'spaceship.glb',
function ( gltf ) {

scene.add( gltf.scene );
gltfStore.animations = gltf.animations;
gltfStore.ship = gltf.scene.children[2];
gltfStore.cam = gltf.cameras[0];

gltfStore.mixer = new THREE.AnimationMixer(gltf.cameras[0]);
gltfStore.mixerShip = new THREE.AnimationMixer(gltf.scene.children[2]);

gltfStore.mixer.clipAction(gltfStore.animations[0]).play();
gltfStore.mixerShip.clipAction(gltfStore.animations[0]).play();
}
);


function animate() {
requestAnimationFrame( animate );
if(gltfStore.mixer && gltfStore.cam){
//gltfStore.mixer.update(clock.getDelta());
gltfStore.mixerShip.update(clock.getDelta());
renderer.render(scene, gltfStore.cam);
}
};

animate();

感谢您的任何帮助/想法!

最佳答案

因此,在进一步检查示例后,我找到了答案。

简单地说,没有必要单独提取和尝试为模型和相机制作动画,所需要的只是从场景本身创建一个 AnimatioMixer:

gltfStore.mixer = new THREE.AnimationMixer(gltf.scene);
gltfStore.mixer.clipAction(gltfStore.animations[0]).play();

上面的动画完美的所有元素。

关于javascript - 在 three.js 中使用 THREE.GLTFLoader 导入和播放多个动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49342306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com