Getting Angle of terrain and Certain points and Changing Terrain Texture?

Is it possible to change the terrain texture using code? I am wondering because i am trying to make it so that grass can only appear at a certain angle of terrain, and if the terrain is too steep, the grass wont appear to make it a bit more realistic. Is there a way to possibly do this?

Thank you very Much :slight_smile:

I think the easiest way to do this would be with an alpha map which fades out the grass texture in steep areas of the terrain.

For instance, if you use “TerrainLighting.j3md” with the grass texture in “DiffuseMap_1” then you want to load an “AlphaMap” texture that has green in the flat areas and black (or some other color with no green) in the steep areas.

Does that help?

The problem is that my terrain is randomly generated, so i wanted to add stone on mountains, and snow on peaks etc. So im nto sure if an alpha map would be the way to go.

Why not generate the alpha map at the same time you generate the terrain?

Calculating the slope of a terrain triangle is very straightforward: just take one minus the y-coordinate of normal vector. You can get the normal vector using com.jme3.math.Plane.setPlanePoints(Vector3f, Vector3f, Vector3f).getNormal()

2 Likes

Thank you very much. I did as you said. And it works.

1 Like

Thanks Stephen, I got inspiration from your post. I’ll leave this here, which I’m currently using to determine the slope of a given point on a TerrainQuad:

This gives a float between 0 (flat) and 1 (100% vertical) :

[java]
// TerrainQuad tq;
float x = 21; // Whatever X you want to check…
float z = 27; // Whatever Z you want to check…
float ground_slope = 1f - tq.getNormal(new Vector2f(x, z)).y;
[/java]

There is a triplanar texturing option for terrain in JME already.

You also do not need to use anything other than the actual mesh to calculate the normal:

Take a look at https://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core/com/jme3/math/Triangle.java?r=7860

You can grab the info you need directly from the mesh.