# Terrain Generator trims mesh if exceeding 65k vertices

**URL:** <https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737>\
**Category:** Help & Support\
**Created:** [March 10, 2021, 2:45pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737 "2021-03-10T14:45:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![underlight](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/underlight/32/8332_2.png) [@underlight](https://forum.playcanvas.com/u/underlight)\
**Post date:** [March 10, 2021, 2:45pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/1 "2021-03-10T14:45:30Z")

</div>

Hello guys!

I’ve tried this terrain generator: [https://developer.playcanvas.com/en/tutorials/terrain-generation/](https://developer.playcanvas.com/en/tutorials/terrain-generation/)  
But it seems like it starts trimming the mesh once subdivisions go \> 255 (number of vertices \> 65k)

This doc claims it should be possible to generate meshes with \>65k of vertices / indices: [https://developer.playcanvas.com/en/api/pc.Mesh.html#setIndices](https://developer.playcanvas.com/en/api/pc.Mesh.html#setIndices)

But seems like it’s not the case. Is it a bug or smth?

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [March 10, 2021, 2:48pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/2 "2021-03-10T14:48:44Z")

</div>

When you say ‘trim’, do you mean the collision mesh or the visible render?

---

<div class="post-metadata">

**Author:** ![underlight](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/underlight/32/8332_2.png) [@underlight](https://forum.playcanvas.com/u/underlight)\
**Post date:** [March 10, 2021, 2:51pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/3 "2021-03-10T14:51:45Z")

</div>

The visible render also!

I didn’t try to reproduce it on original project, but in my project using this script the trimming happens. Should I provide a public example?

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [March 10, 2021, 2:54pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/4 "2021-03-10T14:54:11Z")

</div>

There is a limit on the number of vertices in a number of older devices that don’t support 32-bit index buffers. I am not sure though that’s the issue in your case.

I’ve tried the default terrain example with 300 subdivisions and it seems to work quite alright:

 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/5/5e27881f2d5584559d489e215fe56603bc426f62.jpeg)

---

<div class="post-metadata">

**Author:** ![underlight](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/underlight/32/8332_2.png) [@underlight](https://forum.playcanvas.com/u/underlight)\
**Post date:** [March 10, 2021, 2:58pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/5 "2021-03-10T14:58:57Z")

</div>

@Leonidas thank you for quick look!  
I was trying my project in desktop environment, so it’s not mobile issue…  
I’ll try reproducing this issue in original tutorial project then.

---

<div class="post-metadata">

**Author:** ![underlight](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/underlight/32/8332_2.png) [@underlight](https://forum.playcanvas.com/u/underlight)\
**Post date:** [March 10, 2021, 3:25pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/6 "2021-03-10T15:25:53Z")

</div>

@Leonidas @yaustar the trimming happens in tutorial project as well  
to notice it you just need to set width / depth to 10, max height to 1  
and then compare 250 subdivs vs 400, for example.

Cloned tutorial project with 400 subdivs:  
[https://playcanvas.com/project/773098/](https://playcanvas.com/project/773098/)

Seems like something is wrong with pc.createMesh

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [March 10, 2021, 3:32pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/7 "2021-03-10T15:32:52Z")

</div>

You are right, but what’s interesting is that the collision mesh works. Here is me standing mid air because the collision mesh was fully generated:

 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/4/420dcd55ad99b3c1b2ae5158708c32ea3bee510c.jpeg)

@mvaligursky do you any idea what may the issue here?

---

<div class="post-metadata">

**Author:** ![underlight](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/underlight/32/8332_2.png) [@underlight](https://forum.playcanvas.com/u/underlight)\
**Post date:** [March 10, 2021, 3:35pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/8 "2021-03-10T15:35:03Z")

</div>

Yeah, the collision is generated with 0.5 \* subdiv, so it will not be trimmed until subdiv = 512

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [March 10, 2021, 4:12pm UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/9 "2021-03-10T16:12:15Z")

</div>

pc.CreateMesh only supports 16bit index buffers:

> <https://github.com/playcanvas/engine/issues/2951>
>
> I noticed createMesh function only creates UInt16 array and doesn't go for a big indices Uint32 array format if it's supported....

We need to port it to use new mesh api which does not have that limitation.

Alternatively, you can do one of these:

- use Mesh API directly when generating terrain, instead of createMesh function. Note that as mentioned, some old mobile devices do not support 32bit index buffer

- generates terrain in multiple patches, where each path does not go over 65535 vertices

---

<div class="post-metadata">

**Author:** ![underlight](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/underlight/32/8332_2.png) [@underlight](https://forum.playcanvas.com/u/underlight)\
**Post date:** [March 12, 2021, 9:33am UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/10 "2021-03-12T09:33:55Z")

</div>

@mvaligursky Thank you!

@yaustar @Leonidas Replacing

```javascript
var mesh = pc.createMesh (this.app.graphicsDevice, vertexData.positions,
{
    normals: vertexData.normals,
    uvs: vertexData.uvs,
    indices: vertexData.indices
});

```

with this:

```javascript
var mesh = new pc.Mesh (this.app.graphicsDevice);
mesh.setPositions (vertexData.positions);
mesh.setNormals (vertexData.normals);
mesh.setIndices (vertexData.indices);
mesh.setUvs (0, vertexData.uvs);
mesh.update ();

```

basically solves this issue

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [March 12, 2021, 9:34am UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/11 "2021-03-12T09:34:54Z")

</div>

Thanks for sharing @underlight! Good to know.

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [March 12, 2021, 9:54am UTC](https://forum.playcanvas.com/t/terrain-generator-trims-mesh-if-exceeding-65k-vertices/18737/12 "2021-03-12T09:54:47Z")

</div>

Added ticket to track the need to update the tutorial: [https://github.com/playcanvas/developer.playcanvas.com/issues/247](https://github.com/playcanvas/developer.playcanvas.com/issues/247)

Many thanks @underlight!
