Setting More Than Three Texture Splats

Update:

height map now works as it should.

I still have my issue with splatting more than three textures… Can anyone help me figure out what my problem is? I’m very new to JME3 and the SDK but I’m slowly figuring things out.

I’m trying to add a fourth texture to splat onto my terrain using my alpha map (I made sure my colors were RGB). How would I go about adding my fourth texture exactly?

A code snippet or example would be very, very commended.

THANKS!

Current code is as follows:
[java]
/** 1. Create terrain material and load five textures into it. */
mat_terrain = new Material(
assetManager, “Common/MatDefs/Terrain/Terrain.j3md”);

    /** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
    mat_terrain.setTexture("Alpha", assetManager.loadTexture(
        "Textures/game_island_terrain/alphamap/alphamap.png"));

    /** 1.2) Add GRASS texture into the red layer (Tex1). */
    Texture grass = assetManager.loadTexture(
        "Textures/game_island_terrain/textures/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    mat_terrain.setTexture("Tex1", grass);
    mat_terrain.setFloat("Tex1Scale", 64f);

    /** 1.3) Add DIRT texture into the green layer (Tex2) */
    Texture dirt = assetManager.loadTexture(
        "Textures/game_island_terrain/textures/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    mat_terrain.setTexture("Tex2", dirt);
    mat_terrain.setFloat("Tex2Scale", 32f);

    /** 1.4) Add ROAD texture into the blue layer (Tex3) */
    Texture rock = assetManager.loadTexture(
        "Textures/game_island_terrain/textures/road.jpg");
    rock.setWrap(WrapMode.Repeat);
    mat_terrain.setTexture("Tex3", rock);
    mat_terrain.setFloat("Tex3Scale", 128f);

    /** 1.5) Add WATER texture into the yellow layer (Tex4) */
    Texture water = assetManager.loadTexture(
        "Textures/game_island_terrain/textures/water.jpg");
    //water.setWrap(WrapMode.Repeat);
    //mat_terrain.setTexture(yellow.set(255, 255, 0, 0).toString(), water);
    //mat_terrain.setFloat("Tex4Scale", 64f);

[/java]

<cite>@xtgscott said:</cite> Hi all, I'm going off of the hello_terrain example in the beginner tutorials. I created my heightmap (which is completely flat when loaded into the SDK?), and my alphamap.
Been a while since I tried that in the SDK but then I had to scale the height pretty much to see something.
My alphamap contains four colors instead of three.

So instead of:
R = grass,
G = dirt,
B = road.

I have,
R = grass,
G = dirt,
B = road,
YELLOW = water

That’s not really how it works. Yellow is a mix of red and green, so it would be drawn as a mix of grass and dirt. Maybe this helps: http://hub.jmonkeyengine.org/forum/topic/alphamap-helloterrain-confusion/

<cite>@jmaasing said:</cite> Been a while since I tried that in the SDK but then I had to scale the height pretty much to see something.

That’s not really how it works. Yellow is a mix of red and green, so it would be drawn as a mix of grass and dirt. Maybe this helps: http://hub.jmonkeyengine.org/forum/topic/alphamap-helloterrain-confusion/

Thanks for the response.

I’ve reviewed that topic and read what you said, but I don’t quite understand still how I would load a fourth splat texture?

If red and green = yellow, then how would I use that to create my fourth texture splat for water?

Also, how would I scale my height?

Sorry for all of the questions, this is my first real attempt at creating a 3d game.

The trick to having a forth texture is to use the alpha component. Each pixel in “alphamap.png” should be RGBA-format. Alpha is usually used in image graphics to mean how transparent that pixel is, but the terrain shader uses it to select how much of the Tex4 to mix in.

Example,
The terrain shader takes a pixel in alphamap.png. Say it finds that this pixel has the color R: 0.5, G:0.5, B: 0.0, A 0.1
The shader will sample (find a ‘corresponding’ pixel in) each of the textures tex1…tex4. So now it has 4 pixel from the images, it will mix them together to decide what color the pixel should have on the terrain. But it will weight the mixing using alphamap values, although this is not exactly the right math it will do something like:

Tex1 pixel color * 0.5 + Tex2 pixel color * 0.5 + Tex3 pixel color * 0.0 + Tex4 pixel color * 0.1

1 Like

Okay, so, could you give me a code sample of what it would look like? If at all possible.

[Edit:]

Would it be something similar to

[java]
ColorRBGA yellow;

mat_terrain.setTexture(yellow.set(1, 1, 0, 0).toString(), water);
[/java]

bump, updated.

Seems like the current terrain material only supports three textures. You’d have to modify the shaders to have it support more and I suspect based on your misunderstandings so far that this is way over your skill level.

Yeah,creating custom shaders are over my skill level. But I could learn. What if I used a different terrain material?

The ‘Terrain’ material only supports 3 textures. The ‘TerrainLighting’-material supports more (http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/terrain/Common/MatDefs/Terrain/TerrainLighting.j3md)

But I really can’t explain it better than I did. Try creating terrain in the SDK terrain editor, add the materials and play with the editors there. There is no “magic” code other than what is in the tutorials already. Sorry.

What @jmassing said, terrainLighting material supports more than 3 splat textures. The editor in the SDK uses this and will tell you how many texture “layers” you can splat on it.

<cite>@jmaasing said:</cite> The 'Terrain' material only supports 3 textures. The 'TerrainLighting'-material supports more (http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/terrain/Common/MatDefs/Terrain/TerrainLighting.j3md)

But I really can’t explain it better than I did. Try creating terrain in the SDK terrain editor, add the materials and play with the editors there. There is no “magic” code other than what is in the tutorials already. Sorry.

Thanks, jmaasing. That looks like it would work, is there already an example usage of the TerrainLighting material?

<cite>@Sploreg said:</cite> What @jmassing said, terrainLighting material supports more than 3 splat textures. The editor in the SDK uses this and will tell you how many texture "layers" you can splat on it.

Thanks, Sploreg, but as I said, ,is there already an example of theyou terrainLighting material in a tutorial ?

[Edit:]

I was looking through the material definition properties and found the TerrainLighting parameter properties.

I’m presuming if I wanted to use the TerrainLighting class it would look something similar t:
[java]

TerrainLighting terrain_lighting_mat;

terrain_lighting_mat = new Material(assetManager, “path to TerrainLighting.j3md”);

Texture water = assetManager.loadTexture(“path to texture”);
water.setWrap(WrapMode.Repeat);

terrain_lighting_mat.setTexture(“AlphaMap”, water);

rootNode.attachChild(terrain_lighting_mat);
[/java]

TerrainTestAdvanced shows how to use all the different alpha maps. These are the alpha map parameters from that test file:

[java]
// ALPHA map (for splat textures)
matTerrain.setTexture(“AlphaMap”, assetManager.loadTexture(“Textures/Terrain/splat/alpha1.png”));
matTerrain.setTexture(“AlphaMap_1”, assetManager.loadTexture(“Textures/Terrain/splat/alpha2.png”));
// this material also supports ‘AlphaMap_2’, so you can get up to 12 diffuse textures [/java]

<cite>@Sploreg said:</cite> TerrainTestAdvanced shows how to use all the different alpha maps. These are the alpha map parameters from that test file:

[java]
// ALPHA map (for splat textures)
matTerrain.setTexture(“AlphaMap”, assetManager.loadTexture(“Textures/Terrain/splat/alpha1.png”));
matTerrain.setTexture(“AlphaMap_1”, assetManager.loadTexture(“Textures/Terrain/splat/alpha2.png”));
// this material also supports ‘AlphaMap_2’, so you can get up to 12 diffuse textures [/java]

Thanks, Sploreg. That’s exactly what I was looking for. I’ve used the example code provided to produce this,

[java]
/** 1. Create terrain material and load five textures into it. */
mat_terrain = new Material(
assetManager, “Common/MatDefs/Terrain/TerrainLighting.j3md”);
mat_terrain.setBoolean(“useTriPlanarMapping”, false);
mat_terrain.setFloat(“Shininess”, 0.0f);

    /** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
    mat_terrain.setTexture("AlphaMap", assetManager.loadTexture(
        "Textures/game_island_terrain/alphamap/alphamap.png"));

    /** 1.2) Add GRASS texture into the red layer (Tex1). */
    Texture grass = assetManager.loadTexture(
        "Textures/game_island_terrain/textures/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    mat_terrain.setTexture("DiffuseMap", grass);
    mat_terrain.setFloat("DiffuseMap_0_Scale", 64f);

[/java]

Except when I run the game, I get an error thrown saying the material parameter is not defined: DiffuseMap_0_Scale

<cite>@xtgscott said:</cite>

[java]
mat_terrain.setFloat(“DiffuseMap_0_Scale”, 64f);
[/java]

Except when I run the game, I get an error thrown saying the material parameter is not defined: DiffuseMap_0_Scale

Because it is called ‘DiffuseMap_0_scale’. See the link I posted or just use the SDK-editor to set things up.

Is it possible for you to share the code? Im experiencing the same issue.

@TastyLemons said: Is it possible for you to share the code? Im experiencing the same issue.

Experiencing what issue?

1 Like
@pspeed said: Experiencing what issue?

I have created two Alpha Maps, and applied both of them to the Material using the code below, but somehow, they are overwriting each other:

alpha1
http://postimg.org/image/7aa0uzwjd/

alpha2
http://postimg.org/image/erj8a7m2h/

mat_terrain.setTexture(“AlphaMap”, alpha1);
mat_terrain.setTexture(“AlphaMap_1”, alpha2);

@pspeed said: Experiencing what issue?

All right. I summed up my issue in to one final thread. I apologize for being all over the place, but when i have an issue that i cant solve, it eats me up like a disease :confused:

THREAD:
http://hub.jmonkeyengine.org/forum/topic/apply-6-textures-to-randomized-terrain-using-a-generated-alphamap/

Any chance of the last link in this post being repaired ?

thanks

You can just use the search