Translating a texture on a mesh

Hi all - I’ve been looking for the JME3 equivalent of what was possible in JME1/2, to translate a texture on a mesh, eg:

(TextureState) texture.getTranslation().x += TRANSLATION_STEP * time;

I can’t find any equivalent to this - is this not possible in JME3?

From the look of this thread I found:

The answer is ‘no’ and ‘do it with shaders’ - so anticipating that - has anyone created a shader that does this? The link from that thread wasn’t obvious what I should be looking for.

Or from that thread, advice to update the texture coordinates - presumably programmatically in the texture coordinate buffer of the mesh? Is that as hard as it sounds…?

(Use case is - I have an effectively infinite surface (sea) the player moves over, so as to avoid reaching an edge, the surface stays put under the player, and the texture is translated over it to give the sense of movement).

1 Like

Ultimately, I think the source code may be your guide if you want to avoid shaders.

Quad.java has this code:

    setBuffer(Type.TexCoord, 2, new float[]{0, 1,
                                                    1, 1,
                                                    1, 0,
                                                    0, 0});

…which is the easiest way to reset the texture coordinates (on a four vertex quad in this case)… though maybe not the most efficient if you do it every frame.

You can do it with in the shader also… but that requires some knowledge of how to work on shaders even if you just fork lighting.j3md or unshaded.j3md and start from there. It is the most efficient way to do this, though… and I can tell you that knowledge of shaders will pay huge dividends later.

Basic unlit vertex shader:

uniform mat4 g_WorldViewProjectionMatrix;
attribute vec3 inPosition;
attribute vec2 inTexCoord;

uniform float m_xoffset;
uniform float m_yoffset;

varying vec2 texCoord;

void main() {
    texCoord = vec2(inTexCoord.x + m_xoffset, inTexCoord.y + m_yoffset);
    
    gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
}

The magic here, of course, being the texCoord = line

I wrote a shader one year ago, modified from jme3 built-in Unshaded shader. It just do exactly as Tryder said.

https://github.com/jmecn/JPsTale/blob/master/JPsTale-Effects/src/main/resources/Shader/Misc/Scroll.j3md

https://github.com/jmecn/JPsTale/blob/master/JPsTale-Effects/src/main/resources/Shader/Misc/Scroll.vert

https://github.com/jmecn/JPsTale/blob/master/JPsTale-Effects/src/main/resources/Shader/Misc/Scroll.frag

I use it like this

    Material mat = new Material(assetManager, "Shader/Misc/Scroll.j3md");
    mat.setFloat("Speed", 1.0f);

    // set texture
    Texture tex = assetManager.loadTexture("Textures/Water/water_diff.png");
    mat.setTexture("ColorMap", tex);
4 Likes

thanks for posting this, this is actually what i need for my fx…

Yes many thanks for the replies and I will give this shader a go…