Keeping aspect ratio of texture applied to cubiod on all sides

I’m trying to texture a “Box”/cuboid (not necessarily a cube), however my texture shouldn’t be “stretched” to fit each side. It should tiled and should be scaled by the same amount on each side (so the texture is the same size on each side of the cuboid, and is repeated as many times as necessary to cover that side of the cuboid).

How might I achieve this?

Set the texture coordinates properly and make sure that the texture has repeat turned on.

I’m not sure that setting one texture scale (which is shared across all sides of the box) will work unless it is a cube. So if one side is shorter than another, the shorter side’s texture will be “squashed” in comparison:

What I’m trying to avoid is that squashed texture, I’d like the scale and aspect of the texture to be the same on both sides. Is that possible?

I didn’t say to scale the texture coordinates. You will have to set them manually since each side will require a different set of coordinates.

If the cube is twice as wide as it is high then the texture co-rdinates also need to be twice as wide as they are high…

I wasn’t sure how to set the texture coordinates - I had only come across scaling so assumed that’s what “set the texture coordinates” was in reference to. Anyway, I have figured it out, so thought I should reply with an update in order to complete the thread and help others. Please clarify anything I have got wrong?

So my box is 0.25 units tall, 1 unit wide - the texture coordinates need to reflect that. It looks like to set the texture coordinates (the “UV map”), you need to change the values set in one of the box’s buffers, like this:

[java]
Box box = new Box(/etc/);
box.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(new float[]{
1, 0, 0, 0, 0, 0.25f, 1, 0.25f, // back
1, 0, 0, 0, 0, 0.25f, 1, 0.25f, // right
1, 0, 0, 0, 0, 0.25f, 1, 0.25f, // front
1, 0, 0, 0, 0, 0.25f, 1, 0.25f, // left
1, 0, 0, 0, 0, 1, 1, 1, // top
1, 0, 0, 0, 0, 1, 1, 1 // bottom
}));
[/java]

It appears that the coordinates start from “bottom-right” and go around the faces counter-clockwise, with each number being a UV - so the first row above is the UV coords (1,0), (0,0), (0, 0.25), (1,0.25) - one UV coordinate for each of the four vertices which make up one side of the box

The default Box implementation just has each corner’s texture coordinates at 1 or 0, hence the squashing/stretching I was experiencing.

Note: there is already a setBuffer() that will take the array so you don’t need to createFloatBuffer() yourself… just a minor convenience thing.

http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Mesh.html#setBuffer(com.jme3.scene.VertexBuffer.Type,%20int,%20float[])

I know if you were going by this tutorial you might have thought otherwise:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes

…and if you haven’t seen that tutorial then it’s worth a read.