private static void AddVertexToVertices(int x, int y, int z, ref VertexPostitionColorPaintNormal[] vertices, ref int verticesSoFar, cornr2AOArrLc corner, ref Vector3 normal, ref byte[] AOarray, byte type, int modelOffsetX, int modelOffsetY, int modelOffsetZ) { VertexPostitionColorPaintNormal Zero = new VertexPostitionColorPaintNormal(); Zero.Position = new Vector3(x + xOffSetsAtCorners[(int)corner], y + yOffSetsAtCorners[(int)corner], z + zOffSetsAtCorners[(int)corner]); Zero.Normal = normal; // NoiseGenerator.Amplitude=2; //float simplex = (float)NoiseGenerator.Noise(x, z)+1; //Zero.PaintColor = new Color((int)(simplex * 255), (int)(simplex * 255), (int)(simplex * 255)); //Random rand = new Random(1+(x + xOffSetsAtCorners[(int)corner]+modelOffsetX) // * (y + yOffSetsAtCorners[(int)corner]+modelOffsetY) // * ( z + zOffSetsAtCorners[(int)corner]+modelOffsetZ)); //Zero.PaintColor = new Color( UIColorPallete.colorArray[type].R + rand.Next(-3,3),UIColorPallete.colorArray[type].G + rand.Next(-3,3),UIColorPallete.colorArray[type].B + rand.Next(-3,3)); Zero.PaintColor = new Color(ColorPallete.getColorFromByte(type).R, ColorPallete.getColorFromByte(type).G, ColorPallete.getColorFromByte(type).B); Zero.Color.R = AOarray[(int)corner]; vertices[verticesSoFar] = Zero; verticesSoFar++; }
public static VerticesAndIndices buildMesh(byte[, ,] array, int spaceWidth, int spaceHeight, int modelOffsetX, int modelOffsetY, int modelOffsetZ) { int numFaces = getNumFaces(array, spaceWidth, spaceHeight); //List<VertexPostitionColorPaintNormal> vertices = new List<VertexPostitionColorPaintNormal>(numFaces * 4); //List<short> indices = new List<short>(numFaces * 6); VertexPostitionColorPaintNormal[] vertices = new VertexPostitionColorPaintNormal[numFaces * 4]; short[] indices = new short[numFaces * 6]; int USETOStoreVertexCountSoFar = 0; int verticesSoFar = 0; int indicesSoFar = 0; for (int x = 0; x < spaceWidth - 1; x++) { for (int y = 0; y < spaceHeight - 1; y++) { for (int z = 0; z < spaceWidth - 1; z++) { if (array[x, y, z] != (byte)PaintedCubeSpace.AIR) { if (array[x, y + 1, z] == (byte)PaintedCubeSpace.AIR) { drawFace(x, y, z, ref array, vertices, ref verticesSoFar, indices, ref indicesSoFar, cornersCoveredAlongforAOArray(x, y, z, array, spaceWidth, spaceHeight)[1], topIndices, topNormal, topCorners, ref USETOStoreVertexCountSoFar, modelOffsetX, modelOffsetY, modelOffsetZ); } if (array[x + 1, y, z] == (byte)PaintedCubeSpace.AIR) { drawFace(x, y, z, ref array, vertices, ref verticesSoFar, indices, ref indicesSoFar, cornersCoveredAlongforAOArray(x, y, z, array, spaceWidth, spaceHeight)[0], leftIndices, leftNormal, leftCorners, ref USETOStoreVertexCountSoFar, modelOffsetX, modelOffsetY, modelOffsetZ); } if (array[x, y, z + 1] == (byte)PaintedCubeSpace.AIR) { drawFace(x, y, z, ref array, vertices, ref verticesSoFar, indices, ref indicesSoFar, cornersCoveredAlongforAOArray(x, y, z, array, spaceWidth, spaceHeight)[2], backIndices, backNormal, backCorners, ref USETOStoreVertexCountSoFar, modelOffsetX, modelOffsetY, modelOffsetZ); } } else //xyz is air { if (array[x, y + 1, z] != (byte)PaintedCubeSpace.AIR) { drawFace(x, y + 1, z, ref array, vertices, ref verticesSoFar, indices, ref indicesSoFar, cornersCoveredAlongforAOArray(x, y + 1, z, array, spaceWidth, spaceHeight)[1], bottomIndices, bottomNormal, bottomCorners, ref USETOStoreVertexCountSoFar, modelOffsetX, modelOffsetY, modelOffsetZ); } if (array[x + 1, y, z] != (byte)PaintedCubeSpace.AIR) { drawFace(x + 1, y, z, ref array, vertices, ref verticesSoFar, indices, ref indicesSoFar, cornersCoveredAlongforAOArray(x + 1, y, z, array, spaceWidth, spaceHeight)[0], rightIndices, rightNormal, rightCorners, ref USETOStoreVertexCountSoFar, modelOffsetX, modelOffsetY, modelOffsetZ); } if (array[x, y, z + 1] != (byte)PaintedCubeSpace.AIR) { drawFace(x, y, z + 1, ref array, vertices, ref verticesSoFar, indices, ref indicesSoFar, cornersCoveredAlongforAOArray(x, y, z + 1, array, spaceWidth, spaceHeight)[2], frontIndices, frontNormal, frontCorners, ref USETOStoreVertexCountSoFar, modelOffsetX, modelOffsetY, modelOffsetZ); } } } } } for (int x = 0; x < spaceWidth; x++) { for (int z = 0; z < spaceWidth; z++) { int y = spaceHeight - 1; createFacesOnBlockWithWithinChecks(array, spaceWidth, spaceHeight, vertices, ref verticesSoFar, indices, ref indicesSoFar, USETOStoreVertexCountSoFar, x, y, z, modelOffsetX, modelOffsetY, modelOffsetZ); } } for (int y = 0; y < spaceHeight; y++) { for (int z = 0; z < spaceWidth; z++) { int x = spaceWidth - 1; createFacesOnBlockWithWithinChecks(array, spaceWidth, spaceHeight, vertices, ref verticesSoFar, indices, ref indicesSoFar, USETOStoreVertexCountSoFar, x, y, z, modelOffsetX, modelOffsetY, modelOffsetZ); } } for (int x = 0; x < spaceWidth; x++) { for (int y = 0; y < spaceHeight; y++) { int z = spaceWidth - 1; createFacesOnBlockWithWithinChecks(array, spaceWidth, spaceHeight, vertices, ref verticesSoFar, indices, ref indicesSoFar, USETOStoreVertexCountSoFar, x, y, z, modelOffsetX, modelOffsetY, modelOffsetZ); } } for (int x = 0; x < spaceWidth; x++) { for (int z = 0; z < spaceWidth; z++) { int y = -1; createFacesOnBlockWithWithinChecks(array, spaceWidth, spaceHeight, vertices, ref verticesSoFar, indices, ref indicesSoFar, USETOStoreVertexCountSoFar, x, y, z, modelOffsetX, modelOffsetY, modelOffsetZ); } } for (int y = 0; y < spaceHeight; y++) { for (int z = 0; z < spaceWidth; z++) { int x = -1; createFacesOnBlockWithWithinChecks(array, spaceWidth, spaceHeight, vertices, ref verticesSoFar, indices, ref indicesSoFar, USETOStoreVertexCountSoFar, x, y, z, modelOffsetX, modelOffsetY, modelOffsetZ); } } for (int x = 0; x < spaceWidth; x++) { for (int y = 0; y < spaceHeight; y++) { int z = -1; createFacesOnBlockWithWithinChecks(array, spaceWidth, spaceHeight, vertices, ref verticesSoFar, indices, ref indicesSoFar, USETOStoreVertexCountSoFar, x, y, z, modelOffsetX, modelOffsetY, modelOffsetZ); } } int test = verticesSoFar; if (vertices.Length != verticesSoFar || indices.Length != indicesSoFar) { throw new Exception("number of elements in vertex array does not match number of vertices"); } return(new VerticesAndIndices(vertices, indices)); }