# Textures of the Box

How to set different textures for different faces of Box?

uv map a model.

@zarch said:
uv map a model.

Can I do this from code?

yes, modify the texCoords buffer of the Box Mesh

I would recommend you use a texture atlas, and do this in a 3D model editor (i.e blender)

@denosaur said:
Can I do this from code?

Yes.

OK, I created custom box mesh.

What I must to do with texture coordinates to set different textures for box faces ?

[java]Mesh m = new Mesh();

// Vertex positions in space

Vector3f [] vertices = new Vector3f[8];

vertices[0] = new Vector3f(0,0,0);

vertices[1] = new Vector3f(0,0,1);

vertices[2] = new Vector3f(1,0,0);

vertices[3] = new Vector3f(1,0,1);

vertices[4] = new Vector3f(0,1,0);

vertices[5] = new Vector3f(0,1,1);

vertices[6] = new Vector3f(1,1,0);

vertices[7] = new Vector3f(1,1,1);

// Texture coordinates

Vector2f [] texCoord = new Vector2f[4];

texCoord[0] = new Vector2f(0,0);

texCoord[1] = new Vector2f(1,0);

texCoord[2] = new Vector2f(0,1);

texCoord[3] = new Vector2f(1,1);

// Indexes. We define the order in which mesh should be constructed

int[] indexes = { 2,0,1, 1,3,2, 2,3,1, 1,0,2, 6,4,5, 5,7,6, 6,7,5, 5,4,6,

4,0,1, 1,5,4, 1,0,4, 4,5,1, 5,1,3, 3,7,5, 3,1,5, 5,7,3, 7,3,2, 2,6,7,

2,3,7, 7,6,2, 6,2,0, 0,4,6, 0,2,6, 6,4,0

};

// Setting buffers

m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));

m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));

m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));

m.updateBound();[/java]

Your gonna want 4 vertices for each face. You need a texCoord for each vertex. And then assuming you have a 2x4 texture, with 6 different faces, and 2 buffer ones (that way you avoid recursive decimals in the texCoords) i.e :

| |

| |

| |

| |

(although this does seem a waste, perhaps theres a better way, idk)

Then your first texCoord (bottom left square) would be something like (0, 0), (0.5, 0), (0, 0.25), (0.5, 0.25)

This isn’t really multiple textures, just 1 texture with multiple “subtextures”. If you want loads of textures, then you’ll have to write your own custom shader, to join multiple textures together.

Looks something like this

http://i.imgur.com/Ssrkr.png

wrong texCoords

Same result with other texCoords

post your texCoords, do you have the same amount of texCoords as Vertices?

do you have the same amount of texCoords as Vertices?

No.
@wezrule said:
You need a texCoord for each vertex

Can you get me exapmle?

I suggest taking a step back and describing what you are trying to achieve and why. There may be a simpler way to achieve what you want…

@denosaur said:
Same result with other texCoords

Dude, this is the solution, just work a bit on it.. This won't be the hardest thing you'll encounter during game development, good chance to test your skills.

@denosaur said:
Can you get me exapmle?

An example is given in the link that was posted. If you want other people to write your game for you please make a payment offer..

I found this class

[java]import com.jme3.math.Vector3f;

import com.jme3.scene.VertexBuffer.Type;

import com.jme3.scene.shape.Box;

/**

• The used Texture is 1 Unit wide and 8 Units high. (to avoid rescaling)<br>
• The first 6 squares are mapped to the sides of the Box.<br>
• Everything else is exactly as in {@link Box}.<br>

*/

public class MultiFaceBox extends Box {

public MultiFaceBox() {

super();

remap();

}

public MultiFaceBox(final Vector3f center, final float xExtent, final float yExtent, final float zExtent) {

super(center, xExtent, yExtent, zExtent);

remap();

}

public MultiFaceBox(final Vector3f min, final Vector3f max) {

super(min, max);

remap();

}

private void remap() {

final FloatBuffer fb = getFloatBuffer(Type.TexCoord);

fb.rewind();

for (int i = 0; i < 6; i++) {

final float top = i / 8f;

final float bottom = (i + 1) / 8f;

final float[] tex = new float[] { 1, bottom, 0, bottom, 0, top, 1, top };

fb.put(tex);

}

}

}

[/java]

But how to set textures for faces?

Check out the TextureAtlas class that comes with jme. Also its source and especially javadoc.

yeh. They use a 1x8, which is similar to the 2x4 that i suggested, both use empty buffer space