# \[SOLVED\] Terrain generation | Falling through mesh

**URL:** <https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697>\
**Category:** Help & Support\
**Tags:** shaders\
**Created:** [July 21, 2022, 4:39pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697 "2022-07-21T16:39:30Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 21, 2022, 4:39pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/1 "2022-07-21T16:39:30Z")

</div>

Please scroll to the last post, thank you!

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 21, 2022, 4:43pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/2 "2022-07-21T16:43:23Z")

</div>

Update: Managed to fix vertex, now Im getting err on frag

`{message: "'=' : dimension mismatch", line: 6, source: 'precision highp float;\nvarying float vAmount;\n\nvoi…0, 1.0) );\n`

```javascript
varying float vAmount;

void main()

{

    vec4 water = (smoothstep(0.01, 0.25, vAmount) - smoothstep(0.24, 0.26, vAmount)) * vec3 (0.0, 0.0, 1.0) );

    gl_FragColor = vec4(water, 1.0);

}

```

---

<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:** [July 21, 2022, 6:39pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/3 "2022-07-21T18:39:55Z")

</div>

Hey, I think you are getting that because you are declaring a vec4 variable, but you only assign a vec3 there. Try this instead:

```javascript
vec3 water = (smoothstep(0.01, 0.25, vAmount) - smoothstep(0.24, 0.26, vAmount)) * vec3 (0.0, 0.0, 1.0) );

gl_FragColor = vec4(water, 1.0);

```

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 21, 2022, 6:47pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/4 "2022-07-21T18:47:45Z")

</div>

Thanks, have figured this out, your example has ) at the end that is not needed, but hey, new problem, terrain from heightmap gets sort of destroyed once applied, is it due shader or…?

 ![Fiksavimas](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/6/9/69a77d989be7d6762c91f54a5c30dce8b97f32b8.png)

I’m applying this on basic terrain example from grayscale height map, shader uses the same gray map  
any ideas?

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 21, 2022, 7:29pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/5 "2022-07-21T19:29:40Z")

</div>

I thought somethin is wrong with the way material is applied to terrain, so I’ve loaded shader first, then created terrain with that material, same results 🤨

Update: So i fixed it by changing bumpScale to 1, not sure what it does to be honest, it seems like heightmap in shader is upside down, not confirmed yet (will continue testing)

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 22, 2022, 4:52pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/6 "2022-07-22T16:52:34Z")

</div>

Also, how do I colorize that black?

```javascript
vec3 water = (smoothstep(0.0, 0.35, vAmount) - smoothstep(0.34, 0.35, vAmount)) * vec3 (0.0, 0.2, 1.0);

```

Updated with: black is now water

```javascript
vec3 water = (smoothstep(-0.5, 0.35, vAmount) - smoothstep(0.34, 0.35, vAmount)) * vec3 (0.0, 0.2, 1.0);

```

I thougt the values are from 0.00 to 1.0? How do I know if my vAmount can be more than 1? Edit heightmap and add a 100% white spot thus testing in game?

---

<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:** [July 22, 2022, 7:29pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/7 "2022-07-22T19:29:28Z")

</div>

To flip an image just do this at the point where you grab the heightmap color:

```javascript
float heightmapColor = 1.0 - texture2D( heightmapTexture, uv ).r;

```

Not sure what do you mean by vAmount being more than 1. Try sharing your full shader code so we can look at that in context.

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 23, 2022, 8:17am UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/8 "2022-07-23T08:17:13Z")

</div>

```javascript
    vec4 bumpData = 1.0 - texture2D(bumpTexture, vUv0);
 vAmount = bumpData.b;

```

Hey, it flips splating instead, water becomes terrain tops

---

<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:** [July 23, 2022, 8:31am UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/9 "2022-07-23T08:31:43Z")

</div>

Yeah that is just the theory on how to flip image color in a shader.

In your case you need to share more details on how you use those variables.

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 23, 2022, 9:20am UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/10 "2022-07-23T09:20:31Z")

</div>

Well, I’m keeping the fliped texture until I get to know how it works, I have a question

```javascript
vec3 water = (smoothstep(0.0, 0.35, vAmount) - smoothstep(0.34, 0.35, vAmount)) * vec3 (0.0, 0.2, 1.0);

```

How can achive coloring without smoothstep gradients?  
Something like:

```javascript
vec3 water min-height max-height * color 

```

So I have hard edges without gradients everywhere?

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 23, 2022, 7:43pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/11 "2022-07-23T19:43:09Z")

</div>

I have used step instead of smoothstep to get the effect I wanted, tommorow I will try to change it to textures instead of colors, any suggestions performance vise? Thanks

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 24, 2022, 10:50am UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/12 "2022-07-24T10:50:25Z")

</div>

**Update:** I’ve been testing the waters with terrain generation, even with higher subdivision count I can fall trough mesh sometimes, on higher mountains mostly, why does this happen? Holes in the mesh?  
Increasing subdivisions drastically increases triangles 🙄

---

<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:** [July 24, 2022, 7:43pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/13 "2022-07-24T19:43:29Z")

</div>

Hi @Newbie_Coder,

That’s quite an old problem with the Bullet version PlayCanvas is currently using.

Increasing subdivisions may help but ultimately it won’t fully fix it. There are 2-3 fixes that have worked for me in the past (either as a partial or full fix):

1. Use a sphere collider instead of capsule
2. Use a compound collider with multiple spheres to simulate a capsule
3. Upgrade to Ammo3, that may be simpler than it sounds, there is some discussion and a build on this closed PR on the official Ammo.js repo: [https://github.com/kripken/ammo.js/pull/366](https://github.com/kripken/ammo.js/pull/366)

Hope that helps!

---

<div class="post-metadata">

**Author:** ![LeXXik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/lexxik/32/6953_2.png) [@LeXXik](https://forum.playcanvas.com/u/LeXXik)\
**Post date:** [July 24, 2022, 11:31pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/14 "2022-07-24T23:31:10Z")

</div>

If you use a heightmap, go for what Leonidas suggested. You don’t have other options, unless you change a physics engine.

If you use a mesh collider, you can also try to find the offending quad (2 adjacent triangles), where the fall-through happens, and change the diagonal direction in Blender. If the diagonal was from top left to bottom right, change to “bottom left - top right”.

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 25, 2022, 10:42am UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/15 "2022-07-25T10:42:18Z")

</div>

Does a size of a sphere collider plays a role in this situation? Any step-by-step guides upgrading to Ammo3? Thank you for help guys

---

<div class="post-metadata">

**Author:** ![Newbie\_Coder](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/newbie_coder/32/21065_2.png) [@Newbie\_Coder](https://forum.playcanvas.com/u/Newbie_Coder)\
**Post date:** [July 28, 2022, 5:43pm UTC](https://forum.playcanvas.com/t/solved-terrain-generation-falling-through-mesh/26697/16 "2022-07-28T17:43:22Z")

</div>

We can mark this as solved, thank you guys and especially @Leonidas
