Use of RawTexture.CreateRGBATexture according to terrain elevation (AutoTexture)

Hello,

On my editor project, I would like to add the possibility to automatically texture a terrain depending on the elevation.

I use mixMaterial which allows the use of texture in RGBA. mixMap

On my project, I manually paint on the canvas (in RGBA) then generate the mixMap texture. (example of field painting)

But I’d like to try making an autoTexture that will create the same for me faster based on terrain elevation to then refine with manual painting.

For example:

  • Sand when values ​​below 0
  • Ground when the values ​​are above 0 and the slope is less than 15% (variable values).
  • Rock when slope is greater than 15% (variable values).
  • Snow if the height exceeds 30 units high…

In the end, the goal is to create a small autoTexture editor where the user would define the minimum and maximum height of each texture (or RGBA color) to paint the terrain automatically.

But I have no idea how to do this. I thought that RawTexture could be a good start, but I don’t know how to use it and generate an RGBA texture according to the elevation and then retrieve this texture to save it.

Does anyone have a simple PG example that does this.

I started a PG, but I didn’t understand what I should do.

Thanks for your help.

1 Like

Here’s a PG that should get you started:

If you want to choose the color based on the slope, you will have to compute it by looking up the elevation of the adjacent vertices and by computing some differences.

1 Like

Thank you very much Evgeni

I tried to make another more complete PG that uses mixMaterial. I added to the PG also the possibility to download the mixMap texture. So far everything is fine.

But I still have a lot of trouble understanding what you said to me by :

If you want to choose the color based on the slope, you will have to compute it by looking up the elevation of the adjacent vertices and by computing some differences.

I may have understood how to apply the different colors, but not according to the slopes (if we are flat, we have more than 20%).

You can try something like that:

The slope is calculated by a difference between the elevations around the current point: there’s a slopeX and a slopeY, for the X and Y direction respectively. I simply take the max of the two (absolute) values as the final slope.

Then, the slope is used to mix the rock texture with the other one.

In this snipped:

if (slope > 1) {
    const f = BABYLON.Scalar.Clamp(slope / 3, 0, 1);

    g = Math.floor(255 * f);
    r = Math.floor(r * (1 - f));
    b = Math.floor(b * (1 - f));
    a = 255 - Math.floor((255 - a) * (1 - f));
}

The 1 in slope > 1 and 3 in BABYLON.Scalar.Clamp(slope / 3, 0, 1) are arbitrary values, you can change them based on your liking.

1 Like

It looks awesome. Although I still have a hard time understanding all of his math, it works. I will try to play with the different values ​​to try to understand.

Thank you very much for your precious help Evgeni.