[java]
public Tile(AssetManager manager, double h0, double h1, double h2, double h3) {
assetManager = manager;
meshOne = new Mesh();
meshTwo = new Mesh();
geometryOne = new Geometry("wireframeGeometry", meshOne);
geometryTwo = new Geometry("wireframeGeometry", meshTwo);
material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
this.h0 = h0;
this.h1 = h1;
this.h2 = h2;
this.h3 = h3;
calculateDistance();
calculateColors();
createMesh();
createGeometry();
tileNode = new Node("tileNode");
tileNode.attachChild(geometryOne);
tileNode.attachChild(geometryTwo);
GeometryBatchFactory.optimize(tileNode);
}
private static void calculateDistance() {
double diff1 = Math.abs(h0 - h3) * Math.abs(h0 - h3);
double diff2 = Math.abs(h1 - h2) * Math.abs(h1 - h2);
double ans1 = Math.sqrt(2 + diff1);
double ans2 = Math.sqrt(2 + diff2);
if (ans1 <= ans2) {
start = 1;
color = 1;
} else {
start = 0;
color = 1;
}
if ((h0 == h1 && h2 == h3) || (h0 == h2 && h1 == h3)) {
color = 1;
}
}
private static void createMesh() {
Vector3f[] verticesOne = new Vector3f[3];
Vector2f[] texCoordOne = new Vector2f[3];
Vector3f[] verticesTwo = new Vector3f[3];
Vector2f[] texCoordTwo = new Vector2f[3];
int[] indexesOne = new int[3];
int[] indexesTwo = new int[3];
float[] normalsOne = new float[9];
float[] normalsTwo = new float[9];
if (start == 0) {
verticesOne[0] = new Vector3f(0 * scale, (float) (h0 * scale) / 10, 0 * scale);
verticesOne[1] = new Vector3f(1 * scale, (float) (h1 * scale) / 10, 0 * scale);
verticesOne[2] = new Vector3f(0 * scale, (float) (h2 * scale) / 10, 1 * scale);
verticesTwo[0] = new Vector3f(0 * scale, (float) (h2 * scale) / 10, 1 * scale);
verticesTwo[1] = new Vector3f(1 * scale, (float) (h3 * scale) / 10, 1 * scale);
verticesTwo[2] = new Vector3f(1 * scale, (float) (h1 * scale) / 10, 0 * scale);
texCoordOne[0] = new Vector2f(0, 0);
texCoordOne[1] = new Vector2f(1, 0);
texCoordOne[2] = new Vector2f(0, 1);
texCoordTwo[0] = new Vector2f(0, 1);
texCoordTwo[1] = new Vector2f(1, 1);
texCoordTwo[2] = new Vector2f(1, 0);
int[] tempOne = {2, 1, 0};
int[] tempTwo = {0, 1, 2};
indexesOne = tempOne;
indexesTwo = tempTwo;
Vector3f edgeOne1 = verticesOne[2].subtract(verticesOne[0]);
Vector3f edgeOne2 = verticesOne[1].subtract(verticesOne[0]);
Vector3f normalOne = edgeOne1.cross(edgeOne2);
Vector3f edgeTwo1 = verticesTwo[0].subtract(verticesTwo[2]);
Vector3f edgeTwo2 = verticesTwo[1].subtract(verticesTwo[2]);
Vector3f normalTwo = edgeTwo1.cross(edgeTwo2);
float getXOne = normalOne.get(0);
float getYOne = normalOne.get(1);
float getZOne = normalOne.get(2);
float getXTwo = normalTwo.get(0);
float getYTwo = normalTwo.get(1);
float getZTwo = normalTwo.get(2);
normalsOne = new float[] {getXOne,getYOne,getZOne, getXOne,getYOne,getZOne, getXOne,getYOne,getZOne};
normalsTwo = new float[] {getXTwo,getYTwo,getZTwo, getXTwo,getYTwo,getZTwo, getXTwo,getYTwo,getZTwo};
} else {
verticesOne[0] = new Vector3f(0 * scale, (float) (h0 * scale) / 10, 0 * scale);
verticesOne[1] = new Vector3f(0 * scale, (float) (h2 * scale) / 10, 1 * scale);
verticesOne[2] = new Vector3f(1 * scale, (float) (h3 * scale) / 10, 1 * scale);
verticesTwo[0] = new Vector3f(1 * scale, (float) (h3 * scale) / 10, 1 * scale);
verticesTwo[1] = new Vector3f(1 * scale, (float) (h1 * scale) / 10, 0 * scale);
verticesTwo[2] = new Vector3f(0 * scale, (float) (h0 * scale) / 10, 0 * scale);
texCoordOne[0] = new Vector2f(0, 0);
texCoordOne[1] = new Vector2f(0, 1);
texCoordOne[2] = new Vector2f(1, 1);
texCoordTwo[0] = new Vector2f(1, 1);
texCoordTwo[1] = new Vector2f(1, 0);
texCoordTwo[2] = new Vector2f(0, 0);
int[] tempOne = {0, 1, 2};
int[] tempTwo = {0, 1, 2};
indexesOne = tempOne;
indexesTwo = tempTwo;
Vector3f edgeOne1 = verticesOne[0].subtract(verticesOne[2]);
Vector3f edgeOne2 = verticesOne[1].subtract(verticesOne[2]);
Vector3f normalOne = edgeOne1.cross(edgeOne2);
Vector3f edgeTwo1 = verticesTwo[0].subtract(verticesTwo[2]);
Vector3f edgeTwo2 = verticesTwo[1].subtract(verticesTwo[2]);
Vector3f normalTwo = edgeTwo1.cross(edgeTwo2);
float getXOne = normalOne.get(0);
float getYOne = normalOne.get(1);
float getZOne = normalOne.get(2);
float getXTwo = normalTwo.get(0);
float getYTwo = normalTwo.get(1);
float getZTwo = normalTwo.get(2);
normalsOne = new float[] {getXOne,getYOne,getZOne, getXOne,getYOne,getZOne, getXOne,getYOne,getZOne};
normalsTwo = new float[] {getXTwo,getYTwo,getZTwo, getXTwo,getYTwo,getZTwo, getXTwo,getYTwo,getZTwo};
}
meshOne.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(verticesOne));
meshOne.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoordOne));
meshOne.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexesOne));
meshOne.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normalsOne));
meshOne.updateBound();
meshTwo.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(verticesTwo));
meshTwo.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoordTwo));
meshTwo.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexesTwo));
meshTwo.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normalsTwo));
meshTwo.updateBound();
}
private static void createGeometry() {
material.setBoolean("UseMaterialColors", true);
if (color == 0) {
material.setColor("Diffuse", colorRGB1);
geometryOne.setMaterial(material);
material.setColor("Diffuse", colorRGB2);
geometryTwo.setMaterial(material);
} else {
material.setColor("Diffuse", colorRGB1);
geometryOne.setMaterial(material);
material.setColor("Diffuse", colorRGB2);
geometryTwo.setMaterial(material);
}
}
private static void calculateColors() {
double totalOne = 0;
double totalTwo = 0;
double[] valOne = new double[3];
double[] valTwo = new double[3];
if (color == 1) {
totalOne = (h0 + h1 + h2 + h3) / 4;
totalTwo = totalOne;
} else if (start == 1) {
totalOne = ((h0 * 45) + (h2 * 90) + (h3 * 45)) / 180;
totalTwo = ((h0 * 45) + (h1 * 90) + (h3 * 45)) / 180;
} else if (start == 0) {
totalOne = ((h1 * 45) + (h0 * 90) + (h2 * 45)) / 180;
totalTwo = ((h1 * 45) + (h3 * 90) + (h2 * 45)) / 180;
}
valOne[0] = (140 - (totalOne % 32)) / 255;
valOne[1] = (155 - (totalOne % 16)) / 255;
valOne[2] = (140 - (totalOne % 8)) / 255;
valTwo[0] = (140 - (totalTwo % 32)) / 255;
valTwo[1] = (155 - (totalTwo % 16)) / 255;
valTwo[2] = (140 - (totalTwo % 8)) / 255;
colorRGB1 = new ColorRGBA((float) valOne[0], (float) valOne[1], (float) valOne[2], 1);
colorRGB2 = new ColorRGBA((float) valTwo[0], (float) valTwo[1], (float) valTwo[2], 1);
}
[/java]
Take a look at the materials just in case I’m not describing it all correctly. This is the tile class, which is what makes up a chunk; the chunk optimizes the geometry.