Oct*_*tor 3 javascript animation blender three.js gltf
从 Blender 中,我使用 gltfExporter 将动画模型(宇宙飞船)和动画相机导出为 .glb,当我将 .glb 文件放入 glTF 查看器(gltf-viewer.donmccurdy.com)时,一切正常,所以我知道问题不在于模型/搅拌机。
问题是当我尝试在我的脚本中播放动画时。我可以让模型完美地动画,或者我可以让相机完美地动画,但是当我尝试同时做相机和模型时,我会发疯。我想这可能是我对动画混合器缺乏了解?也许我只需要为一个文件使用一个 Mixer?无论如何,这就是我正在做的事情:
我创建了两个单独的动画混合器,一个用于飞船,一个用于相机:
gltfStore.mixer = new THREE.AnimationMixer(gltf.cameras[0]);
gltfStore.mixerShip = new THREE.AnimationMixer(gltf.scene.children[2]);
Run Code Online (Sandbox Code Playgroud)
我使用clipAction播放动画:
gltfStore.mixer.clipAction(gltfStore.animations[0]).play();
gltfStore.mixerShip.clipAction(gltfStore.animations[0]).play();
Run Code Online (Sandbox Code Playgroud)
在我的循环中,我渲染动画:
gltfStore.mixer.update(clock.getDelta());
gltfStore.mixerShip.update(clock.getDelta());
Run Code Online (Sandbox Code Playgroud)
他们单独工作,但他们不一起工作。我注意到的一点是,在 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();
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助/想法!
因此,在进一步检查示例后,我找到了答案。
简而言之,无需提取并尝试分别为模型和相机设置动画,所需要做的就是AnimatioMixer从场景本身创建一个:
gltfStore.mixer = new THREE.AnimationMixer(gltf.scene);
gltfStore.mixer.clipAction(gltfStore.animations[0]).play();
Run Code Online (Sandbox Code Playgroud)
上面完美地动画了所有元素。
| 归档时间: |
|
| 查看次数: |
4062 次 |
| 最近记录: |