在three.js中为FBX加载的对象制作动画

时间:2018-08-09 21:51:13

标签: javascript three.js

使用THREE.FBXLoader,如何在动画函数中调用对象旋转? (未捕获的ReferenceError:未定义对象)是错误,所以我怎么知道如何引用它?

如果我定义对象,则没有错误,但仍然没有动画:

var object = {
    rotation: {x: 0}
};


fbx_loader = new THREE.FBXLoader();
  fbx_loader.load(fbx[fbx_counter], function (object) {
  scene.add(object);
  object.name = 'new_fbx'
  object.getObjectById(12).rotation.x += 0.01
  console.log('id: ' + object.id);
  console.log('name: ' + object.name);
});

function animate() {
    requestAnimationFrame( animate );
  // this is where the problem is:
  //object.rotation.x += 0.01
  //object.rotateX += 0.01
    renderer.render( scene, camera );
}

1 个答案:

答案 0 :(得分:0)

似乎您正在全局范围内创建一个object变量,但没有将其分配给您正在加载的模型。

尝试做类似的事情:

var loadedModel;


fbx_loader = new THREE.FBXLoader();
  fbx_loader.load(fbx[fbx_counter], function (object) {
  loadedModel = object;
  loadedModel.name = 'new_fbx';
  loadedModel.getObjectById(12).rotation.x += 0.01
  scene.add(loadedModel);

  animate();
});

function animate() {
    requestAnimationFrame( animate );
    loadedModel.rotation.x += 0.01
    renderer.render( scene, camera );
}