# How can I optimize meshes for over 20,000 cubes?

**URL:** <https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945>\
**Category:** Questions\
**Tags:** material\
**Created:** [December 26, 2020, 6:22am UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945 "2020-12-26T06:22:52Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 6:22am UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/1 "2020-12-26T06:22:52Z")

</div>

Hello everyone

there are 20,000 cubes in my Block Model, and I used the instance to optimize it but I need much more optimization to get the smooth rendering,  
here is my PG: [https://www.babylonjs-playground.com/#ZEZLAT#51](https://www.babylonjs-playground.com/#ZEZLAT#51)  
is it any way to improve it?

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [December 26, 2020, 7:32am UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/2 "2020-12-26T07:32:30Z")

</div>

There are a lot of ways how and where optimize your scene for the smooth view.  
Did you try any of optimization hints which are described here? - [Optimizing Your Scene | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/scene/optimize_your_scene)

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 11:21am UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/3 "2020-12-26T11:21:41Z")

</div>

> [@labris](#):
>
> Optimizing Your Scene | Babylon.js Documentation

Hi @labris  
Thank you for your recommend , I use all of the method in documentation, but I couldn’t get the desire speed. there is one important limitation in my Model, which I need all the cubes bounding boxes, and I should render about 20,000 cubes with their bounding boxes. I used the instance to improve the model, but it’s not enough. its only works smooth for less than 6000 cubes.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [December 26, 2020, 1:22pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/4 "2020-12-26T13:22:17Z")

</div>

I suppose you have very special application.  
If there are no ways to reduce the number of meshes (which is the main bottleneck) you may try to limit render framerate.  
So, you managed to smooth render about 6000 cubes; if you will render them only every 4th frame, you should be able to have quite smooth render for about 24 000 cubes.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 1:59pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/5 "2020-12-26T13:59:35Z")

</div>

Thank you so much for your recommend.  
How can I limit the render frame rate?

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 2:32pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/6 "2020-12-26T14:32:03Z")

</div>

I used following code for limit the render frame rate, but again there are lag.

```
  window.setTimeout(() => {
    engine.stopRenderLoop();
    let lastTime = new Date().getTime();
    window.setInterval(() => {
      scene.render();
      let curTime = new Date().getTime();
      fpsLabel = (1000 / (curTime - lastTime)).toFixed() + " fps";
      console.log(fpsLabel);
      lastTime = curTime;
    }, 10);
  }, 1000);
```

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [December 26, 2020, 3:45pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/7 "2020-12-26T15:45:15Z")

</div>

The simplest example is here - [https://playground.babylonjs.com/#3A2PUF#2](https://playground.babylonjs.com/#3A2PUF#2)  
It renders the scene only when pointerDown.  
Another example with RollingAverage - [Babylon.js Playground](https://playground.babylonjs.com/#9W2NDV)  
But without knowing your specific application it is difficult to give you the exact answer.  
This subject has a lot of discussions, so I would suggest you to search first; and, then, to find the best method for your case.  
Here just a couple of links - there are dozens of them:  
[What's the difference between engine.getDeltaTime and scene.getAnimationRatio? - Questions - Babylon.js](https://forum.babylonjs.com/t/whats-the-difference-between-engine-getdeltatime-and-scene-getanimationratio/61)  
[Decoupling Frame rate from movement speed issues - Questions - Babylon.js](https://forum.babylonjs.com/t/decoupling-frame-rate-from-movement-speed-issues/9611)  
[Which Best Method for Mesh Rendering (Updated Every Frame) - Questions - Babylon.js](https://forum.babylonjs.com/t/which-best-method-for-mesh-rendering-updated-every-frame/2834)  
From old Forum - [Controlling the render loop - Questions & Answers - HTML5 Game Devs Forum](https://www.html5gamedevs.com/topic/18215-controlling-the-render-loop/)

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 4:18pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/9 "2020-12-26T16:18:45Z")

</div>

> [@labris](#):
>
> [https://playground.babylonjs.com/#3A2PUF#2](https://playground.babylonjs.com/#3A2PUF#2)

Thank you so much for your help 🙏 🙏 🙏 I am going to search and learn much more about this topic.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 4:22pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/10 "2020-12-26T16:22:11Z")

</div>

Hi @Necips, Thanks a lot for your recommend , but I didn’t understand how can I calculate the data beforehand.  
In my model I get the data with the json file, and how can I calculate it beforehand. is it possible to give me some hint how can I do it?

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [December 26, 2020, 4:34pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/11 "2020-12-26T16:34:26Z")

</div>

Here are some hints for so called Object Pool - [Object Pool · Optimization Patterns · Game Programming Patterns](http://gameprogrammingpatterns.com/object-pool.html)  
They don’t relate to JS or Babylon directly, but to know all these tips is very useful because they explain the logic inside your application.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 4:39pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/12 "2020-12-26T16:39:36Z")

</div>

> [@labris](#):
>
> Object Pool · Optimization Patterns · Game Programming Patterns

Thank you so much @labris for devoting your time to answering my question and helping me to increase my knowledge. 🙏 🙏 I’m going to read this article. 🙏 🙏

---

<div class="post-metadata">

**Author:** ![sokorototo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sokorototo/32/11061_2.png) [@sokorototo](https://forum.babylonjs.com/u/sokorototo)\
**Post date:** [December 26, 2020, 5:53pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/13 "2020-12-26T17:53:20Z")

</div>

Maybe try using LOD to change the cubes to a lower detail mesh, e.g a plane at a certain distance. Or to not render them completely if far enough. Check this [link](https://doc.babylonjs.com/divingDeeper/mesh/LOD)

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 9:25pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/15 "2020-12-26T21:25:39Z")

</div>

> [@sokorototo](#):
>
> link

Thank you for your recommend 🙏 🙏

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 9:27pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/16 "2020-12-26T21:27:24Z")

</div>

Thank you so much, its a great idea 🙏 🙏  
I am going to work on it.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 26, 2020, 9:54pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/17 "2020-12-26T21:54:26Z")

</div>

should I do it in the following format? I didn’t understand what should I do with time , should I set interval for each mesh separately?

```
scene.registerBeforeRender(function () {
for (let i = 0; i < cubes.length; i++) {
  let instance = cube.createInstance("cube" + cubes[i].key);
  instance.position.x = cubes[i].X;
  instance.position.y = cubes[i].Y;
  instance.position.z = cubes[i].Z;
  instance.instancedBuffers.color = new BABYLON.Color4(
    0.3,
    0.4,
    cubes[i].G
  );

  avgX += cubes[i].X / cubes.length;
  avgY += cubes[i].Y / cubes.length;
  avgZ += cubes[i].Z / cubes.length;

  arrCubes.push(instance);

  // **************End Fetch Cubes********** /
}

  });

```

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#ZEZLAT%2352)**

---

<div class="post-metadata">

**Author:** ![csimms](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/csimms/32/12498_2.png) [@csimms](https://forum.babylonjs.com/u/csimms)\
**Post date:** [December 27, 2020, 2:55am UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/18 "2020-12-27T02:55:09Z")

</div>

I don’t know your application much, but I know that [Thin Instances](https://doc.babylonjs.com/divingDeeper/mesh/copies/thinInstances) can definitely handle 20,000 easily. You will lose bounding boxes, but I guess you would have to write more code to make up for that.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 27, 2020, 10:07am UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/20 "2020-12-27T10:07:18Z")

</div>

Thank you so much for your explain 🙏 🙏 🙏 I will try to find a way 🙏

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 27, 2020, 4:11pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/22 "2020-12-27T16:11:04Z")

</div>

> [@Necips](#):
>
> Merging Meshes | Babylon.js Documentation

I need each mesh separately because I have a Gizmo box which move through the block model and deleted the cubes that are inside the Gizmo box.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [December 27, 2020, 8:17pm UTC](https://forum.babylonjs.com/t/how-can-i-optimize-meshes-for-over-20-000-cubes/16945/24 "2020-12-27T20:17:41Z")

</div>

I think it is difficult , However it’s great idea. I should work on it. 🙏
