# Turn geometry into small cubes

**URL:** <https://discourse.threejs.org/t/turn-geometry-into-small-cubes/52719>\
**Category:** Questions\
**Tags:** geometry\
**Created:** [June 12, 2023, 1:07pm UTC](https://discourse.threejs.org/t/turn-geometry-into-small-cubes/52719 "2023-06-12T13:07:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weijian-zhu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/weijian-zhu/32/25688_2.png) [@weijian-zhu](https://discourse.threejs.org/u/weijian-zhu)\
**Post date:** [June 12, 2023, 1:07pm UTC](https://discourse.threejs.org/t/turn-geometry-into-small-cubes/52719/1 "2023-06-12T13:07:40Z")

</div>

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/e/4/e42a637e21281a448afca2043317713f43813d0e.jpeg)  
Hello community, I want to implement the geometry to be made up of many cubes, like in the picture, but I don’t have any ideas, can you give me some ideas, examples, anything

---

<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:** [June 12, 2023, 1:17pm UTC](https://discourse.threejs.org/t/turn-geometry-into-small-cubes/52719/2 "2023-06-12T13:17:28Z")

</div>

By doing [this](https://doc.babylonjs.com/toolsAndResources/utilities/IsInside) - which kinda really boils down just to [these 2-3 lines of code](https://github.com/mjurczyk/openvdb/blob/7cd895a49cebf1d721cdb6d5b4f0b715d5ea6dbf/src/openvdb/utils/primitives/MeshVolume.js#L158).

```js
const position = new Three.Vector3();
const voxelOffset = 0.5;

for (let x = 0; x < 100; x++) {
  for (let y = 0; y < 100; y++) {
    for (let z = 0; z < 100; z++) {

      position.set(x, y, z).multiplyScalar(voxelOffset);

      const pointIsInsideGeometry = /* NOTE Do the raycasting here */
      
      if (pointIsInsideGeometry) {
        const voxel = new Three.Mesh(boxGeometry, boxMaterial);
        voxel.position.copy(position);

        scene.add(voxel);
      }
    }
  }
}

```

* * *

Keep in mind:

1. Use [InstancedMesh](https://threejs.org/docs/index.html?q=instan#api/en/objects/InstancedMesh) if you have more than 10x10x10 grid of cubes.
2. You kinda absolutely have to use [three-mesh-bvh](https://github.com/gkjohnson/three-mesh-bvh) - it requires only 3 lines of code to be added, and makes the raycasting part 1 billion times faster (not really an exaggeration, especially for non-triavial models like the car in the screenshot 😅)
3. This works best for [watertight meshes](https://davidstutz.de/a-formal-definition-of-watertight-meshes/) - should work ok also for otherz, provided openings are not too big.

---

<div class="post-metadata">

**Author:** ![weijian-zhu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/weijian-zhu/32/25688_2.png) [@weijian-zhu](https://discourse.threejs.org/u/weijian-zhu)\
**Post date:** [June 12, 2023, 1:50pm UTC](https://discourse.threejs.org/t/turn-geometry-into-small-cubes/52719/3 "2023-06-12T13:50:01Z")

</div>

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/6/9/69d4e74303b8015eb025a135bd881926b8da5718.jpeg)

[https://gkjohnson.github.io/three-mesh-bvh/example/bundle/raycast.html](https://gkjohnson.github.io/three-mesh-bvh/example/bundle/raycast.html)  
From the information you provided, I found the effect I wanted very close, thank you very much
