This is how I did it once (don’t mind the fps without vsync it is around 3000):
package com.jvpichowski.hex;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.BufferUtils;
import jme3tools.optimize.GeometryBatchFactory;
/**
*
*/
public class HexTest extends SimpleApplication {
private final float size = 0.5f;
private final float width = size * 2;
private final float horiz = width * 3/4;
private final float height = 0.25f;
public static void main(String[] args) {
new HexTest().start();
}
@Override
public void simpleInitApp() {
Node root = getRootNode();
addHex(root, 1, new int[]{0,0,2,4,6,0}, new Vector3f(0.75f*width,0,0.5f*horiz));
addHex(root, 2, new int[]{0,0,0,3,4,1}, new Vector3f(0,0, horiz));
addHex(root, 3, new int[]{2,0,0,0,4,4}, new Vector3f(-0.75f*width,0,0.5f*horiz));
addHex(root, 4, new int[]{4,3,0,0,0,5}, new Vector3f(-0.75f*width,0,-0.5f*horiz));
addHex(root, 5, new int[]{6,4,4,0,0,0}, new Vector3f(0,0,-horiz));
addHex(root, 6, new int[]{0,1,4,5,0,0}, new Vector3f(0.75f*width,0,-0.5f*horiz));
addHex(root, 4, new int[]{1,2,3,4,5,6}, new Vector3f(0,0,0));
GeometryBatchFactory.optimize(root);
}
private void addHex(Node node, int center, int[] borders, Vector3f location){
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
//mat.setColor("Color", ColorRGBA.Blue); // set color of material to blue
mat.setTexture("ColorMap", assetManager.loadTexture("Hex.png"));
//mat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
location = location.add(0, height*center, 0);
Vector3f[] vertices = new Vector3f[6];
vertices[0] = new Vector3f(0.5f*width, 0f,0f);
vertices[1] = new Vector3f(0.25f*width, 0f,0.5f*horiz);
vertices[2] = new Vector3f(-0.25f*width, 0f,0.5f*horiz);
vertices[3] = new Vector3f(-0.5f*width, 0f,0f);
vertices[4] = new Vector3f(-0.25f*width, 0f,-0.5f*horiz);
vertices[5] = new Vector3f(0.25f*width, 0f,-0.5f*horiz);
Vector2f[] texCords = new Vector2f[6];
texCords[0] = new Vector2f((0.5f+0.5f)/2, 1-(0.5f+0)/2);
texCords[1] = new Vector2f((0.5f+0.25f)/2, 1-(0.5f+0.5f)/2);
texCords[2] = new Vector2f((0.5f-0.25f)/2, 1-(0.5f+0.5f)/2);
texCords[3] = new Vector2f((0.5f-0.5f)/2, 1-(0.5f+0)/2);
texCords[4] = new Vector2f((0.5f-0.25f)/2, 1-(0.5f-0.5f)/2);
texCords[5] = new Vector2f((0.5f+0.25f)/2, 1-(0.5f-0.5f)/2);
int[] indices = {
5, 1, 0,
5, 4, 1,
4, 2, 1,
4, 3, 2
};
Mesh hex = new Mesh();
hex.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
hex.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCords));
hex.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(indices));
hex.updateBound();
Geometry hexGeom = new Geometry("", hex);
hexGeom.setLocalTranslation(location);
hexGeom.setMaterial(mat); // set the cube's material
node.attachChild(hexGeom);
for(int i = 0; i < borders.length; i++) {
if (borders[i] == center-1) {
Vector3f[] vertices1 = new Vector3f[4];
vertices1[0] = vertices[i].clone();
vertices1[1] = vertices[(i+1)%6].clone();
vertices1[2] = vertices[i].add(0, -height, 0);
vertices1[3] = vertices[(i+1)%6].add(0, -height, 0);
int[] indices1 = {
0, 3, 2,
3, 0, 1
};
Vector2f[] texCords1 = new Vector2f[6];
texCords1[0] = new Vector2f(0f/2, 0.5f);
texCords1[1] = new Vector2f(1f/2, 0.5f);
texCords1[2] = new Vector2f(0f/2, 0);
texCords1[3] = new Vector2f(1f/2, 0);
Mesh s1 = new Mesh();
s1.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices1));
s1.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCords1));
s1.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(indices1));
Geometry s1Geom = new Geometry("", s1);
s1Geom.setMaterial(mat);
s1Geom.setLocalTranslation(location);
node.attachChild(s1Geom);
}else if (borders[i] < center-1){
Vector3f[] vertices1 = new Vector3f[6];
vertices1[0] = vertices[i].clone();
vertices1[1] = vertices[(i+1)%6].clone();
vertices1[2] = vertices[i].add(0, -height, 0);
vertices1[3] = vertices[(i+1)%6].add(0, -height, 0);
vertices1[4] = vertices[i].add(0, -height*(center-borders[i]), 0);
vertices1[5] = vertices[(i+1)%6].add(0, -height*(center-borders[i]), 0);
Vector2f[] texCords1 = new Vector2f[6];
texCords1[0] = new Vector2f(0f/2, 0.5f);
texCords1[1] = new Vector2f(1f/2, 0.5f);
texCords1[2] = new Vector2f(0f/2, 0);
texCords1[3] = new Vector2f(1f/2, 0);
texCords1[4] = new Vector2f(1f/2, 0);
texCords1[5] = new Vector2f(1f/2, 0);
int[] indices1 = {
0, 3, 2,
3, 0, 1,
2, 3, 5,
5, 4, 2
};
Mesh s1 = new Mesh();
s1.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices1));
s1.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCords1));
s1.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(indices1));
Geometry s1Geom = new Geometry("", s1);
s1Geom.setMaterial(mat);
s1Geom.setLocalTranslation(location);
node.attachChild(s1Geom);
}
}
}
}