How can I use multiple textures with a custom material?

Hello,

I am trying to write a higher performance terrain renderer, which requires a custom material. This renderer, however, stores almost all of its data in the form of textures (Texture2D in the .j3md files). But, when each having its own name, and each being set with material.setTexture, using the proper name, the first texture to be set appears to be applying to all of the 5 textures my shader needs to use. How can I prevent this from happening?

Vertex shader:
attribute vec3 inPosition;

uniform sampler2D HeightMap;
uniform mat4 g_WorldViewProjectionMatrix;

out vec3 position;

void main(){
    float y = texture(HeightMap, vec2(inPosition.x/TERRAINSIZE, inPosition.z/TERRAINSIZE)).r*16;
    position = vec3(inPosition.x, y, inPosition.z);
    gl_Position = g_WorldViewProjectionMatrix*vec4(position.xyz, 1);
}

Fragment shader:

uniform sampler2D AlphaMap;
uniform sampler2D RedChannel;
uniform sampler2D GreenChannel;
uniform sampler2D BlueChannel;

in vec3 position;

out vec4 outColor;

float toTexCoord(float f){
    return mod(f, 1);
}

void main(){
    vec4 alphaTexture = texture(AlphaMap, (position.xz)*(1/TERRAINSIZE));
    float alphaFilterTotal = alphaTexture.r + alphaTexture.g + alphaTexture.b;

    vec3 color = vec3(0.0, 0.0, 0.0);
    vec2 textureCoordinates = vec2(toTexCoord(position.x), toTexCoord(position.z));

    color += (texture(RedChannel, textureCoordinates).rgb)*(alphaTexture.r/alphaFilterTotal);
    color += (texture(GreenChannel, textureCoordinates).rgb)*(alphaTexture.g/alphaFilterTotal);
    color += (texture(BlueChannel, textureCoordinates).rgb)*(alphaTexture.b/alphaFilterTotal);

    outColor = vec4(color.rgb, 1.0);
}

.j3md file:

MaterialDef Solid Color {
    MaterialParameters{
        Texture2D AlphaMap
        Texture2D RedChannel
        Texture2D GreenChannel
        Texture2D BlueChannel
        Texture2D HeightMap
        Float TerrainSize
    }
    Technique {
        VertexShader GLSL330: assets/SimpleTerrainVert.glsl
        FragmentShader GLSL330: assets/SimpleTerrainFrag.glsl
        WorldParameters{
            WorldViewProjectionMatrix
        }
        Defines {
            TERRAINSIZE : TerrainSize
        }
    }
    Technique FixedFunc {}
}

This is not a material parameter.

This is:
uniform sampler2D m_AlphaMap;

Put m_ in front of them.

Thank you! That worked :slight_smile: I am wondering though, if it isn’t a material parameter, what is it? I imagine that it is something in jME because this wiki page gives examples without any prefix.

Which examples do you mean?. The attributes without any prefix are built in attributes as it is said in the end of the page (you can see a table with the built-ins):

It was the built-in attributes, thank you. I guess that is what I get for not reading as thoroughly as I should.