Regarding the use of InstancedMesh
In Picture 1, I used InstancedMesh to copy the shelf, but when I pushed the camera to the right, it would become like Picture 2. The whole thing crashed. This was caused by the error in Picture 3
Below is my code
loader.load('~@/../static/models/test.glb', function (gltf) {
const model = gltf.scene
const group = new THREE.Group()
const meshes = []
model.traverse(function (child) {
if (child.isMesh) {
const geometry = child.geometry.clone()
geometry.scale(0.009902, 0.009174, 0.009174)
const material = child.material.clone()
meshes.push({geometry, material})
}
})
const count = 9// 实例的数量
const matrix = new THREE.Matrix4()
meshes.forEach(({ geometry, material }) => {
const instancedMesh = new THREE.InstancedMesh(geometry, material, count)
// const instancedMesh = new THREE.InstancedMesh(geometry, material, count)
instancedMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage)
for (let i = 0; i < count; i++) {
const x = 15.37 + 1.46 * i
const y = -4.14
const z = 16.5
matrix.setPosition(x, y, z)
instancedMesh.setMatrixAt(i, matrix)
}
// 旋转实例化网格来修正方向
instancedMesh.rotation.x = Math.PI
instancedMesh.rotation.y = 0
instancedMesh.rotation.z = 0
// scene.add(instancedMesh)
instancedMesh.instanceMatrix.needsUpdate = true
// instancedMesh.computeBoundingSphere()
group.add(instancedMesh)
console.log(instancedMesh)
})
// group.rotateX(Math.PI)
// group.position.set(15.4, 5.2, -17.5)
scene.add(group)
console.log(group)
})