在 THREE.Js 的场景中添加同一导入模型的多个副本
我已经将3D模型导入了我一直在研究的三个项目中,我想在现场添加几份副本。这是我一直使用的代码来复制它:
let ball = new THREE.Mesh();
loader.load( './ball.gltf', function ( gltf ) {
gltf.scene.traverse(function(model) { //for gltf shadows!
if (model.isMesh) {
model.castShadow = true;
model.material = sphereMaterial;
}
});
ball = gltf.scene;
scene.add( ball );
}, undefined, function ( error ) {
console.error( error );
} );
const ball2 = ball.clone()
ball2.position.set(0.5, 0.5, 0.5)
scene.add(ball2)
但是,从 loader.load()
呼叫中仅显示一个“球”中的一个“球”。有人碰巧知道我应该做些什么以成功复制模型吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
>网格是克隆的。
onload()
handler(函数(GLTF){...}
)是异步的,并且clonesball ball
的代码() const ball2 = ball.clone()
)在使用GLTF数据初始化Ball
之前执行。因此,在当时ball.clone()
执行,ball
只是您在加载GLTF之前创建的空的网格
,而该空我怀疑您在某个时候是在不确定的
clone
上遇到的控制台错误,这就是为什么您将行添加到空的ball
中添加到空的网格
,这是不必要的。有几种方法可以处理此操作,但最简单的是将克隆
ball
的代码移动到onload
handler( ie )之后,之后代码> scene.add(ball))。概念证明在这里。
Your
onLoad()
handler (function ( gltf ) {...}
) is asynchronous, and the code that clonesball
(const ball2 = ball.clone()
) is executing beforeball
is initialized with the GLTF data. So at the timeball.clone()
executes,ball
is simply the emptyMesh
you created before loading the GLTF, and that emptyMesh
is what gets cloned.I suspect you were, at some point, getting a console error relating to reading
clone
on undefined, and that's why you added the line to initializeball
to an emptyMesh
, which is unnecessary.There are a few ways to handle this, but the simplest is to move the code that clones
ball
into theonLoad
handler (i.e., afterscene.add( ball )
).Proof of concept here.