[SOLVED] Terrain generation | Falling through mesh

Please scroll to the last post, thank you!

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

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);

}

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

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);

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…?

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

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 :face_with_raised_eyebrow:

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)

Also, how do I colorize that black?

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

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?

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

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.

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

Hey, it flips splating instead, water becomes terrain tops

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.

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

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:

vec3 water min-height max-height * color 

So I have hard edges without gradients everywhere?

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

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 :roll_eyes:

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

Hope that helps!

2 Likes

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”.

2 Likes

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

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

1 Like