【发布时间】:2022-01-13 22:51:37
【问题描述】:
我正在尝试制作一个随机放置我导入的一些云的功能,但问题是,它生成了云的数量,但位置相同!我随机化了位置,但是当代码运行时,我在同一位置有 10 朵云。我能做些什么?这是代码:
loader.load('/clouds/clouds1/scene.gltf', function (clouds1) {
var clouds1array = []
function addClouds1(){
for (var i = 1; i < 10; i++) {
const clouds1Mesh = clouds1.scene
const clouds1Position = clouds1Mesh.position
clouds1Position.x = Math.random() * 10
clouds1Position.y = Math.random() * 10
clouds1Position.z = (Math.random() - 0.5 ) * 300
clouds1Mesh.scale.setX(0.05)
clouds1Mesh.scale.setY(0.05)
clouds1Mesh.scale.setZ(0.05)
scene.add(clouds1Mesh)
clouds1array.push(clouds1Mesh)
}
}
addClouds1()
})
我不知道为什么它有这么多的孩子,我试图用答案来解决,但它仍然不起作用。最后的第三个孩子包含网格,我尝试使用它来工作,但它说我不能在 scene.add() 函数中使用它
编辑:我解决了这个问题!我只需要将 for 循环放在加载函数之外
for(let i = 0; i < 30; i+= 3)
loader.load('/clouds/clouds1/scene.gltf', function (clouds1) {
const cloud = clouds1.scene
const child1 = clouds1.scene.children[0].children[0].children[0].children[2].children[0]
child1.material = new THREE.MeshStandardMaterial({ emissive: 'white', emissiveIntensity: 0.5})
cloud.scale.set(0.05, 0.05, 0.05)
cloud.position.x = (Math.random() - 0.5) * 500
cloud.position.y = (Math.random() + ((Math.random() + 20 ) + 70))
cloud.position.z = (Math.random() - 1) * 500
cloud.rotation.x = Math.random()
cloud.rotation.y = Math.random()
cloud.rotation.z = Math.random()
scene.add(cloud)
})
【问题讨论】:
-
请注意,按照您现在的设置方式,three.js 将为每个云加载一个 GLTF 模型。由于浏览器缓存,这将很快,但内存效率低。您可以研究的一些技术包括:使用
Object3D.clone()、在多个网格中重复使用几何体和材质,以及InstancedMesh。 -
@TheJim01 谢谢!我将会。我是编程新手,所以我有点想探索
标签: javascript for-loop random three.js mesh