【发布时间】:2019-09-10 14:53:50
【问题描述】:
如何将threejs中的gltf模型从定义的场景中独立移动?
在我的代码中,我更新了一个 gltf 模型,并尝试使用 KeyboardState.js 移动它。使用的控件是 OrbitControl.js。
KeyboardState.js:https://searchcode.com/codesearch/view/69477532/
这是我使用的加载函数:
function loadGLTF(x,y,z,mesh,url){
var loader = new THREE.GLTFLoader();
// model
loader.load(
// resource URL
url,
// called when the resource is loaded
function ( gltf ) {
mesh = gltf.scene;
mesh.position.set(x,y,z);
scene.add(mesh);
},
// called while loading is progressing
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log(error);
console.log( 'An error happened' );
}
);
}
这是我用于键盘输入的更新功能:
function update()
{
keyboard.update();
var moveDistance = 50 * clock.getDelta();
if ( keyboard.down("W") )
mesh.translateY( moveDistance );
if ( keyboard.down("S") )
mesh.translateY( -moveDistance );
if ( keyboard.down("A") ){
console.log("entered in A");
mesh.translateX( -moveDistance );
}
if ( keyboard.down("D") ){
console.log("entered in D");
mesh.translateX( moveDistance );
}
controls.update();
stats.update();
}
但实际上发生的是对象随着整个场景移动,而不是独立于场景移动。如果我使用相同的代码来移动例如球体,它就可以工作。为什么?也许 gltf 文件在内部以隐式方式依赖于场景,而不是简单的 THREE.SphereGeometry?
【问题讨论】:
-
不是您问题的答案,但您可能会发现 this article 有用
标签: javascript three.js gltf