# About the use of InstancedMesh

**URL:** <https://discourse.threejs.org/t/about-the-use-of-instancedmesh/68623>\
**Category:** Questions\
**Tags:** materials, geometry\
**Created:** [July 26, 2024, 11:48am UTC](https://discourse.threejs.org/t/about-the-use-of-instancedmesh/68623 "2024-07-26T11:48:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![panq763](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/panq763/32/50235_2.png) [@panq763](https://discourse.threejs.org/u/panq763)\
**Post date:** [July 26, 2024, 11:48am UTC](https://discourse.threejs.org/t/about-the-use-of-instancedmesh/68623/1 "2024-07-26T11:48:30Z")

</div>

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

 ![1](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/7/7/77e4e5cbf656bfd365501a6209f16b19c2566f7d.jpeg)  
 ![2](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/7/a/7a14ceae23f342306aad199853c6a1396f18b754.jpeg)  
 ![3](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/3/5/359bc8dd1be7553a3ece8ed955a942df33ee4efd.jpeg)

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

```

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [July 26, 2024, 4:34pm UTC](https://discourse.threejs.org/t/about-the-use-of-instancedmesh/68623/2 "2024-07-26T16:34:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![panq763](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/panq763/32/50235_2.png) [@panq763](https://discourse.threejs.org/u/panq763)\
**Post date:** [July 29, 2024, 2:04am UTC](https://discourse.threejs.org/t/about-the-use-of-instancedmesh/68623/3 "2024-07-29T02:04:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [July 29, 2024, 2:17am UTC](https://discourse.threejs.org/t/about-the-use-of-instancedmesh/68623/4 "2024-07-29T02:17:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![panq763](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/panq763/32/50235_2.png) [@panq763](https://discourse.threejs.org/u/panq763)\
**Post date:** [July 29, 2024, 2:33am UTC](https://discourse.threejs.org/t/about-the-use-of-instancedmesh/68623/5 "2024-07-29T02:33:43Z")

</div>

![4](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/c/9/c9a5847ab34281bfd9eafc6c6c2395e38a065f7c.jpeg)  
 ![5](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/5/f/5f8e09b335f925a95e14d4ea13018a726687a307.jpeg)  
sorry

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [July 29, 2024, 3:07am UTC](https://discourse.threejs.org/t/about-the-use-of-instancedmesh/68623/6 "2024-07-29T03:07:33Z")

</div>

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.
