About the use of InstancedMesh

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)
    })

When you console.log the value of meshes - do all values of geometry appear correct? Technically speaking Meshes can be created without a geometry / with an empty geometry, so that if (child.isMesh) { wouldn’t save in such case.

Uploading: 4.jpg…
Uploading: 5.jpg…
The result of the console is this. I didn’t find any problem.

Unless it’s my network that’s super laggy, the images don’t seem to have been uploaded :face_with_monocle:



sorry

  1. Neither InstancedMesh nor Group is a geometry - is this what appears when you log the value of meshes.geometry ?

  2. One of the logs is collapsed - what’s the value of geometry in the collapsed InstancedMesh?

  3. If that Group is one of the values in meshes - it’s likely causing the issue. Since groups do not have geometries - you may be attempting to destructure geometry from a Group, receive undefined, and proceed to create an InstancedMesh with an undefined geometry.