# convertToFlatShadedMesh() errors fatally after updatable=false but not after updatable=false

**URL:** <https://forum.babylonjs.com/t/converttoflatshadedmesh-errors-fatally-after-updatable-false-but-not-after-updatable-false/25357>\
**Category:** Questions\
**Tags:** material\
**Created:** [November 16, 2021, 10:14pm UTC](https://forum.babylonjs.com/t/converttoflatshadedmesh-errors-fatally-after-updatable-false-but-not-after-updatable-false/25357 "2021-11-16T22:14:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreasplesch](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/andreasplesch/32/18352_2.png) [@andreasplesch](https://forum.babylonjs.com/u/andreasplesch)\
**Post date:** [November 16, 2021, 10:14pm UTC](https://forum.babylonjs.com/t/converttoflatshadedmesh-errors-fatally-after-updatable-false-but-not-after-updatable-false/25357/1 "2021-11-16T22:14:45Z")

</div>

in case there are less colors than positions/vertices.  
Just a quick note, I will provide PG.  
This is not unreasonable but surprising. Probably the error should be handled gracefully somehow ?

---

<div class="post-metadata">

**Author:** ![andreasplesch](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/andreasplesch/32/18352_2.png) [@andreasplesch](https://forum.babylonjs.com/u/andreasplesch)\
**Post date:** [November 17, 2021, 4:34am UTC](https://forum.babylonjs.com/t/converttoflatshadedmesh-errors-fatally-after-updatable-false-but-not-after-updatable-false/25357/2 "2021-11-17T04:34:55Z")

</div>

error after creating customMesh with updatable=true  
[https://playground.babylonjs.com/#VKBJN#2236](https://playground.babylonjs.com/#VKBJN#2236)

no error after updatable=false  
[https://playground.babylonjs.com/#VKBJN#2237](https://playground.babylonjs.com/#VKBJN#2237)

This may be the intended behavior but it took time to figure out that an updatable mesh insists on the correct number of vertex colors.

---

<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:** [November 17, 2021, 7:58am UTC](https://forum.babylonjs.com/t/converttoflatshadedmesh-errors-fatally-after-updatable-false-but-not-after-updatable-false/25357/3 "2021-11-17T07:58:33Z")

</div>

The mesh is not valid, so `convertToFlatShadedMesh` can’t work.

You have an error in the console log when trying to display the original mesh:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/f/6/f6e174030e0bd7d4d5ff122d49213453c949268b.png)

Babylon.js does not generally check the inputs (for perf reason), you are expected to provide valid data.

It works with “updatable=false” only by side effect I think, you should really provide as many data as the number of vertices.

---

<div class="post-metadata">

**Author:** ![andreasplesch](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/andreasplesch/32/18352_2.png) [@andreasplesch](https://forum.babylonjs.com/u/andreasplesch)\
**Post date:** [November 17, 2021, 12:27pm UTC](https://forum.babylonjs.com/t/converttoflatshadedmesh-errors-fatally-after-updatable-false-but-not-after-updatable-false/25357/4 "2021-11-17T12:27:41Z")

</div>

Thanks. It is reasonable to expect valid inputs. It was the inconsistency which took time to discover.

---

<div class="post-metadata">

**Author:** ![andreasplesch](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/andreasplesch/32/18352_2.png) [@andreasplesch](https://forum.babylonjs.com/u/andreasplesch)\
**Post date:** [November 17, 2021, 7:46pm UTC](https://forum.babylonjs.com/t/converttoflatshadedmesh-errors-fatally-after-updatable-false-but-not-after-updatable-false/25357/5 "2021-11-17T19:46:21Z")

</div>

Since I looked it up let me just make a note here that

> <https://github.com/BabylonJS/Babylon.js/blob/adf9345d417ee6acdc9f2a213580589fde4eb53e/src/Buffers/buffer.ts#L465>

appears to be where the error occurs. The problem is that there already exists a color buffer which is too short for `count` which is based on the number of vertices. So `count` could be limited by the source buffer size to avoid the error but that may not make much sense and provoke other errors later.  
There seem to be some guards against bad input data but due to back compat. I guess they cannot be removed anymore.

My use case was that I wanted to apply the possibly short colors as a default, and then later modify them if necessary, for example after conversion to flat shaded.

Hm, 4.2 does not have the error in the playground.
