# Sprites vs 3D models cost

**URL:** <https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919>\
**Category:** Questions\
**Created:** [June 22, 2020, 10:12pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919 "2020-06-22T22:12:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Djani\_Daud](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/djani_daud/32/8184_2.png) [@Djani\_Daud](https://forum.babylonjs.com/u/Djani_Daud)\
**Post date:** [June 22, 2020, 10:12pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/1 "2020-06-22T22:12:45Z")

</div>

So I have this huge ground mesh and I wanted to make it more interesting by adding some grass spots over it. I initially used a 3D model - I loaded the model and created around 1000 instances, and spread them around the perimeter. I then realised that maybe using grass sprites will not only do the same job but will be cheaper too. I loaded the sprites but for some reason, my total frame increased by ~5ms. I  
know that I have a “limit” of 16ms for optimal performance so it just seemed strange that using sprites turned up so expensive. I checked the example PG([https://www.babylonjs-playground.com/#9RI8CG#0](https://www.babylonjs-playground.com/#9RI8CG#0)) on the sprites how-to-page of babylon.js and surprisingly the scene has 2000 loaded trees and everything runs in ~2ms. So I must be doing something wrong, but I have no idea what. My sprites are 64x64 and the source image is only 12KB (I dont know if that even matters but I reckoned I should mention it).

---

<div class="post-metadata">

**Author:** ![PirateJC](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/piratejc/32/1003_2.png) [@PirateJC](https://forum.babylonjs.com/u/PirateJC)\
**Post date:** [June 22, 2020, 11:48pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/2 "2020-06-22T23:48:44Z")

</div>

Hey @Djani_Daud, do you have a playground that we can look at to help trouble shoot?

---

<div class="post-metadata">

**Author:** ![Djani\_Daud](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/djani_daud/32/8184_2.png) [@Djani\_Daud](https://forum.babylonjs.com/u/Djani_Daud)\
**Post date:** [June 23, 2020, 2:07pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/3 "2020-06-23T14:07:45Z")

</div>

I do not have a playground but here is a link to the scene:  
[http://interactivetable-com.stackstaging.com/game/optimised.php](http://interactivetable-com.stackstaging.com/game/optimised.php)

Here is a screenshot (in case it turns out that I am the only one who experiences the performance drop)

 ![Screenshot 2020-06-23 at 17.02.33](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/2/238cfb63b22bdda1c9af18de76126984c2cba154.jpeg)

I will try to replicate it in a playground and I will share the link ASAP.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 23, 2020, 2:15pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/4 "2020-06-23T14:15:04Z")

</div>

Can you see the same performance drop here where 2000 sprites are in use ? [https://playground.babylonjs.com/#9RI8CG#0](https://playground.babylonjs.com/#9RI8CG#0)

Also in your case instances might be a bit faster and even use a smaller buffer if the geometry is really small 🙂

---

<div class="post-metadata">

**Author:** ![Djani\_Daud](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/djani_daud/32/8184_2.png) [@Djani\_Daud](https://forum.babylonjs.com/u/Djani_Daud)\
**Post date:** [June 23, 2020, 2:21pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/5 "2020-06-23T14:21:02Z")

</div>

The scene you have sent me runs in ~1.2ms. That is what surprises me the most. I am starting to believe that the problem comes from the source image that I am using.

---

<div class="post-metadata">

**Author:** ![Djani\_Daud](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/djani_daud/32/8184_2.png) [@Djani\_Daud](https://forum.babylonjs.com/u/Djani_Daud)\
**Post date:** [June 23, 2020, 5:05pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/6 "2020-06-23T17:05:48Z")

</div>

So I tried using another texture… nothing changed. I also created a playground and filled it with 2000 sprites of my texture and the performance was great - around ~2ms. So this means that something is wrong in the scene that I am working on.  
Now the question is, am I the only one who experiences the performance drop, because if that is the case, then I would continue using sprites. Here is the link to my scene [PolyKingdom](http://interactivetable-com.stackstaging.com/game/optimised.php).

---

<div class="post-metadata">

**Author:** ![Raggar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raggar/32/20672_2.png) [@Raggar](https://forum.babylonjs.com/u/Raggar)\
**Post date:** [June 23, 2020, 5:54pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/7 "2020-06-23T17:54:31Z")

</div>

I get about 27-30fps. Turning off sprites in the inspector results in 60fps. There is definitely an error or issue somewhere, as you have a relatively low amount of sprites.  
I had a project(now gone). With 30.000 objects like trees, rocks and vegetation very densely populated.  
I used a worker to determine the distance from the player to the objects. If the objects were farther away than some threshold, I used sprites, otherwise I used instances. And that project could handle thousands of sprites and hundreds of instances at a smooth 60fps. Have you tried replacing your sprite with the tree one from BabylonJS?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 23, 2020, 6:17pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/8 "2020-06-23T18:17:22Z")

</div>

The thing is that the Babylon sprites are user-friendly and let you do a number of things (like setting the visibility on a per-frame basis for each sprite) that leads to reconstructing the vertex buffer each frame: in your PG, most of the time is spent in the `Sprite.render` function because of that.

Optimizing sprite rendering has already been discussed in this thread [SpriteManager WebGL transparency draw issues and improves](https://forum.babylonjs.com/t/spritemanager-webgl-transparency-draw-issues-and-improves/8991) and a PR has been alluded to but never created.

---

<div class="post-metadata">

**Author:** ![Djani\_Daud](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/djani_daud/32/8184_2.png) [@Djani\_Daud](https://forum.babylonjs.com/u/Djani_Daud)\
**Post date:** [July 11, 2020, 1:14pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/9 "2020-07-11T13:14:09Z")

</div>

So I found the reason why the scene is so expensive:  
When I was building the scene, I wanted to give the field a bit more variety so I randomized the width and height of each grass sprite. I commented that part of the code and the scene now runs in ~1ms instead of ~6ms! I do not know if this is a bug, or what causes this behaviour but maybe someone should take a look at it.  
I recreated the bug in a PG:  
[https://www.babylonjs-playground.com/#9RI8CG#161](https://www.babylonjs-playground.com/#9RI8CG#161)

Note that:  
To recreate the bug I used: [https://www.babylonjs-playground.com/#9RI8CG#0](https://www.babylonjs-playground.com/#9RI8CG#0). The scene runs in ~1ms (as expected).

I then added the 3 lines of code that randomize each sprite’s size and the scene started running in ~4ms.

However, when I **commented out the 3 lines of code, the scene was still running in ~4ms** even though **I was not changing the sprite size.** I find this to be extremely strange 😃

---

<div class="post-metadata">

**Author:** ![rdw1](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/rdw1/32/9212_2.png) [@rdw1](https://forum.babylonjs.com/u/rdw1)\
**Post date:** [July 11, 2020, 4:49pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/10 "2020-07-11T16:49:12Z")

</div>

If you change the sprite size manually, it also doesn’t appear to cause any slowdown. Perhaps all of them being a different size is the problem, as all the individual instances might require some different computation that isn’t needed when all use the same scaling factor? Or perhaps some optimization can be applied when they’re identical that doesn’t work when they aren’t?

I only took a brief look, so I don’t know for sure 😛

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [July 11, 2020, 5:04pm UTC](https://forum.babylonjs.com/t/sprites-vs-3d-models-cost/11919/11 "2020-07-11T17:04:39Z")

</div>

Lose the `.toFixed(2)` in you random number generator and you will see a difference.

By the way you could use `BABYLON.Scalar.RandomRange(0.4, 1)`

Note: adding toFixed(2) to this also slows things down. Numbers are faster than strings.
