# Custom Mesh Hex and Texture problem

I am trying to create a Hex by using custom mesh to create a top, bottom and sides so I can texture them accordingly. I am having problems texturing the top face. Using Custom Meshes I cannot figure out how the texture coordinates should work for my mesh.

I think the vertex indexes are correct. I added them in reverse order so I can see the bottom of the mesh. I also think the normals are correct. I added one for each vertex facing the Y axis (up). But the way the texturing should work eludes me.

The way it looks:

The triangle indexes:

The method to create the top mesh (pastebin version)

[java]

private void createTop(Float startX, Float startZ, Float startHeight, Float size) {

Mesh mesh = new Mesh();

Float half = size / 2;

Vector3f[] vertices = new Vector3f[7];

vertices[0] = new Vector3f(startX, startHeight, startZ);

vertices[1] = new Vector3f(vertices[0].x - size, startHeight, vertices[0].z);

vertices[2] = new Vector3f(vertices[1].x + half, startHeight, vertices[0].z - size);

vertices[3] = new Vector3f(vertices[2].x + size, startHeight, vertices[2].z);

vertices[4] = new Vector3f(vertices[3].x + half, startHeight, vertices[3].z + size);

vertices[5] = new Vector3f(vertices[4].x - half, startHeight, vertices[4].z + size);

vertices[6] = new Vector3f(vertices[5].x - size, startHeight, vertices[5].z);

int[] indexes = {

0,1,2, 2,3,0, 0,3,4, 4,5,0, 0,5,6, 6,1,0, // bottom

0,1,6, 6,5,0, 0,5,4, 4,3,0, 0,3,2, 2,1,0 // reverse bottom

};

float[] normals = new float[21];

normals = new float[] {

0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0

};

Vector2f[] texCoord = new Vector2f[4];

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

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

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

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

mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));

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

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

mesh.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes));

mesh.updateBound();

Material mat = new Material(app.getAssetManager(), “Common/MatDefs/Light/Lighting.j3md”);

Geometry geo = new Geometry(“MyMesh”, mesh);

geo.setMaterial(mat);

app.getRootNode().attachChild(geo);

}

[/java]

Texture coordinates are fractions from 0 to 1. (0, 0) would be the bottom left, and (1, 1) would be top right (i think), so modelling it on a square texture would give the best look

(0.5, 0.5 would be the middle) (0)

(0, 0.5 would be the mid left) (1)

(1, 0.5 would be the mid right) (4)

(0.25, 1 would be the top left) (6)

I will let you do the rest

You have 7 vertexes but only 4 texture coordinates. That’s not right. All vertexes must have all attributes… 3 of your vertexes are defaulting to 0,0 because they did not have a texture coordinate specified.

@wezrule said:
Texture coordinates are fractions from 0 to 1. (0, 0) would be the bottom left, and (1, 1) would be top right (i think), so modelling it on a square texture would give the best look

(0.5, 0.5 would be the middle) (0)
(0, 0.5 would be the mid left) (1)
(1, 0.5 would be the mid right) (4)
(0.25, 1 would be the top left) (6)

I will let you do the rest :P
Thanks that pushed me in the right direction. I now got this:

[java]
Vector2f[] texCoord = new Vector2f[7];
texCoord[0] = new Vector2f(0.5f, 0.5f);
texCoord[1] = new Vector2f(0, 0.5f);
texCoord[2] = new Vector2f(0.25f, 0);
texCoord[3] = new Vector2f(0.75f, 0);
texCoord[4] = new Vector2f(1, 0.5f);
texCoord[5] = new Vector2f(0.75f, 1);
texCoord[6] = new Vector2f(0.25f, 1);
[/java]

I wanna construct similar meshes to form a Hexagonal prism with different textures for the sides and bottom. I think this is the right way to go right instead of importing hexagon models from a 3d modeler program?

depends, are you trying to create a hex world, or just have a hexagon in your game? if its the former, then yes custom meshes are the way to go, just make sure to remove vertices from the mesh that are hidden. Your going to want a texture atlas (lots of different “textures” in 1) so that you can batch them by material. Your gonna have to modify those texture coordinates

@wezrule said:
depends, are you trying to create a hex world, or just have a hexagon in your game? if its the former, then yes custom meshes are the way to go, just make sure to remove vertices from the mesh that are hidden. Your going to want a texture atlas (lots of different "textures" in 1) so that you can batch them by material. Your gonna have to modify those texture coordinates

I wanna do a hex world. So would I be better off creating the 3d hex with 14 vertices and using a texture atlas to get the texture on the correct side, top and bottom? Or should I create 8 separate meshes (top, bottom and 6 sides) to look like a 3d hex ?

The one on the left, and your first texture.png has the right idea, but if you want more “hexagons textures” for other hexagons, you gonna want to tessellate them better (think honeycomb).

http://austinreed.files.wordpress.com/2008/09/honeycomb1.gif

Should I add 4 extra vertices per side to be able to texture the sides correctly? For instance the side at 2,1,8,9 ? Currently they have normals at Y axis pointing up. Also currently the 14 vertices are used to texture the bottom and top.

yeh you need to add extra vertices

Just remember all the things people say about box worlds apply to hex worlds too. (Read up on voxels).

@wezrule said:
The one on the left, and your first texture.png has the right idea, but if you want more "hexagons textures" for other hexagons, you gonna want to tessellate them better (think honeycomb).

http://austinreed.files.wordpress.com/2008/09/honeycomb1.gif

Do you mean the way the hexes are placed next to each other? I could either achieve this by rotating my current mesh and place them in a honeycomb layout or start drawing with the vertexes different ? I will start reading about voxels and box worlds. Is this a similar concept like building all the hexes as much as one mesh whenever possible instead of different meshes ?

Yes, essentially.

I’m not sure what I’m doing wrong here.

As I’m trying to get the same result as you I end up with this as a test:

[java]public class Main extends SimpleApplication {

private final int size = 5;

private int half = size/2;

private final int height = 0;

public static void main(String[] args) {

Main app = new Main();

app.start();

}

@Override

public void simpleInitApp() {

Mesh mesh = new Mesh();

Vector3f[] vertices = new Vector3f[7];

vertices[0] = Vector3f.ZERO;

vertices[1] = new Vector3f(vertices[0].x - size, height, vertices[0].z);

vertices[2] = new Vector3f(vertices[1].x + half, height, vertices[0].z - size);

vertices[3] = new Vector3f(vertices[2].x + size, height, vertices[2].z);

vertices[4] = new Vector3f(vertices[3].x + half, height, vertices[3].z + size);

vertices[5] = new Vector3f(vertices[4].x - half, height, vertices[4].z + size);

vertices[6] = new Vector3f(vertices[5].x - size, height, vertices[5].z);

int[] indexes = {

0,1,2, 2,3,0, 0,3,4, 4,5,0, 0,5,6, 6,1,0, // bottom

0,1,6, 6,5,0, 0,5,4, 4,3,0, 0,3,2, 2,1,0 // reverse bottom

};

float[] normals = new float[21];

normals = new float[] {

0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0

};

Vector2f[] texCoord = new Vector2f[7];

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

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

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

texCoord[3] = new Vector2f(0.75f, 0);

texCoord[4] = new Vector2f(1, 0.5f);

texCoord[5] = new Vector2f(0.75f, 1);

texCoord[6] = new Vector2f(0.25f, 1);

mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));

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

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

mesh.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes));

mesh.updateBound();

Material mat = new Material(this.getAssetManager(), “Common/MatDefs/Light/Lighting.j3md”);

Geometry geo = new Geometry(“MyMesh”, mesh);

geo.setMaterial(mat);

this.getRootNode().attachChild(geo);

}

@Override

public void simpleUpdate(float tpf) {

}

@Override

public void simpleRender(RenderManager rm) {

}

}[/java]

But for some reason it’s an empty world. I can seem to find my texture no where.

The dirt texture is a simple 400X400px dirt picture.

Regards

@goowikns

do your stats show any triangles or objects?

if so, it might be there, just invisible…

try adding a light to the scene, or use an unshaded material.