public MeshAndColors createMesh(VoxelModelChunk voxelChunk, ShapeNodeChunk shape) { Vector3 ve = rotateVector(new Vector3(voxelChunk.sizeChunk.sizeX, voxelChunk.sizeChunk.sizeY, voxelChunk.sizeChunk.sizeZ), shape.transform); int sizeX = (int)Math.Abs(ve.x); int sizeY = (int)Math.Abs(ve.y); int sizeZ = (int)Math.Abs(ve.z); // WTF? Dont drink and code!!! Vector3 correction = new Vector3((ve.x < 0) ? Math.Abs(ve.x) - 1 : 0, (ve.y < 0) ? Math.Abs(ve.y) - 1 : 0, (ve.z < 0) ? Math.Abs(ve.z) - 1 : 0); RenderVoxel[,,] voxelArray = new RenderVoxel[sizeX, sizeY, sizeZ]; float maxX = 0; float maxY = 0; float maxZ = 0; float minX = sizeX; float minY = sizeY; float minZ = sizeZ; foreach (Voxel voxel in voxelChunk.voxels) { Vector3 rotVec = rotateVector(new Vector3(voxel.x, voxel.y, voxel.z), shape.transform) + correction; voxelArray[(int)rotVec.x, (int)rotVec.y, (int)rotVec.z] = new RenderVoxel(voxel.colorIndex); if (rotVec.x < minX) { minX = rotVec.x; } if (rotVec.y < minY) { minY = rotVec.y; } if (rotVec.z < minZ) { minZ = rotVec.z; } if (rotVec.x > maxX) { maxX = rotVec.x; } if (rotVec.y > maxY) { maxY = rotVec.y; } if (rotVec.z > maxZ) { maxZ = rotVec.z; } } shape.singleCenter = new Vector3((float)Math.Ceiling(minX + (maxX - minX) / 2f), minZ, (float)Math.Ceiling(minY + (maxY - minY) / 2f)); Vector3 shifts = shape.singleCenter; MeshAndColors meshAndColors = createMeshFromVoxelArray(voxelArray, shifts); return(meshAndColors); }
void Run() { string[] splits = input.Split(' '); size = new Vector3Int( Convert.ToInt32(splits[3] + splits[2] + splits[1] + splits[0], 16), Convert.ToInt32(splits[7] + splits[6] + splits[5] + splits[4], 16), Convert.ToInt32(splits[11] + splits[10] + splits[9] + splits[8], 16)); RenderVoxel[,,] voxelArray = new RenderVoxel[size.x, size.y, size.z]; int x = 0; int y = 0; int z = 0; for (int i = 16; i < splits.Length; i += 2) { int n = Convert.ToInt32(splits[i], 16) + 1; int color = Convert.ToInt32(splits[i + 1], 16); for (int k = 0; k < n; k++) { x++; if (x == size.x) { y++; x = 0; if (y == size.y) { y = 0; z++; } } if (color == 0) { continue; } if (z >= size.z) { break; } voxelArray[x, y, z] = new RenderVoxel(color); } } MagicaRenderer renderer = new MagicaRenderer(); MeshAndColors meshAndColors = renderer.createMeshFromVoxelArray(voxelArray, Vector3.zero); GameObject go = new GameObject(); Color32[] colors = new Color32[meshAndColors.colors.Count]; for (int i = 0; i < meshAndColors.colors.Count; i++) { int c = meshAndColors.colors[i]; colors[i] = new Color(c / 256f, c / 256f, c / 256f, 1); } meshAndColors.mesh.colors32 = colors; (go.AddComponent <MeshFilter>()).mesh = meshAndColors.mesh; (go.AddComponent <MeshRenderer>()).material = Resources.Load("VertexShading", typeof(Material)) as Material; }
public MeshAndColors createMeshFromVoxelArray(RenderVoxel[,,] voxelArray, Vector3 shifts) { // gravity first for (int z = 0; z < voxelArray.GetLength(2); z++) { for (int y = 0; y < voxelArray.GetLength(1); y++) { for (int x = 0; x < voxelArray.GetLength(0); x++) { if (voxelArray[x, y, z] == null) { continue; } if (x > 0 && voxelArray[x - 1, y, z] != null) { voxelArray[x, y, z].sides ^= RenderVoxel.LEFT; voxelArray[x - 1, y, z].sides ^= RenderVoxel.RIGHT; } if (y > 0 && voxelArray[x, y - 1, z] != null) { voxelArray[x, y, z].sides ^= RenderVoxel.BACK; voxelArray[x, y - 1, z].sides ^= RenderVoxel.FRONT; } if (z > 0 && voxelArray[x, y, z - 1] != null) { voxelArray[x, y, z].sides ^= RenderVoxel.DOWN; voxelArray[x, y, z - 1].sides ^= RenderVoxel.UP; } } } } List <int> colors; Mesh mesh; List <Vector3> vertices = new List <Vector3>(); List <int> triangles = new List <int>(); List <int> colorOrder = new List <int>(); colors = new List <int>(); for (int _z = 0; _z < voxelArray.GetLength(2); _z++) { for (int _y = 0; _y < voxelArray.GetLength(1); _y++) { for (int _x = 0; _x < voxelArray.GetLength(0); _x++) { RenderVoxel renderVoxel = voxelArray[_x, _y, _z]; if (renderVoxel == null || renderVoxel.sides == 0) { continue; } float x = _x - shifts.x; float y = _y - shifts.z; float z = _z - shifts.y; if (!colorOrder.Contains(renderVoxel.color)) { colorOrder.Add(renderVoxel.color); } if ((renderVoxel.sides & RenderVoxel.DOWN) != 0) { int startIndex = vertices.Count; vertices.Add(new Vector3(x * size, z * size, y * size)); vertices.Add(new Vector3((x + 1) * size, z * size, y * size)); vertices.Add(new Vector3(x * size, z * size, (y + 1) * size)); vertices.Add(new Vector3((x + 1) * size, z * size, (y + 1) * size)); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); triangles.Add(startIndex); triangles.Add(startIndex + 1); triangles.Add(startIndex + 2); triangles.Add(startIndex + 1); triangles.Add(startIndex + 3); triangles.Add(startIndex + 2); } if ((renderVoxel.sides & RenderVoxel.UP) != 0) { int startIndex = vertices.Count; vertices.Add(new Vector3(x * size, (z + 1) * size, y * size)); vertices.Add(new Vector3((x + 1) * size, (z + 1) * size, y * size)); vertices.Add(new Vector3(x * size, (z + 1) * size, (y + 1) * size)); vertices.Add(new Vector3((x + 1) * size, (z + 1) * size, (y + 1) * size)); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); triangles.Add(startIndex); triangles.Add(startIndex + 2); triangles.Add(startIndex + 1); triangles.Add(startIndex + 2); triangles.Add(startIndex + 3); triangles.Add(startIndex + 1); } if ((renderVoxel.sides & RenderVoxel.LEFT) != 0) { int startIndex = vertices.Count; vertices.Add(new Vector3(x * size, z * size, y * size)); vertices.Add(new Vector3(x * size, z * size, (y + 1) * size)); vertices.Add(new Vector3(x * size, (z + 1) * size, (y + 1) * size)); vertices.Add(new Vector3(x * size, (z + 1) * size, y * size)); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); triangles.Add(startIndex); triangles.Add(startIndex + 1); triangles.Add(startIndex + 2); triangles.Add(startIndex + 2); triangles.Add(startIndex + 3); triangles.Add(startIndex); } if ((renderVoxel.sides & RenderVoxel.RIGHT) != 0) { int startIndex = vertices.Count; vertices.Add(new Vector3((x + 1) * size, z * size, y * size)); vertices.Add(new Vector3((x + 1) * size, z * size, (y + 1) * size)); vertices.Add(new Vector3((x + 1) * size, (z + 1) * size, (y + 1) * size)); vertices.Add(new Vector3((x + 1) * size, (z + 1) * size, y * size)); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); triangles.Add(startIndex); triangles.Add(startIndex + 3); triangles.Add(startIndex + 2); triangles.Add(startIndex + 2); triangles.Add(startIndex + 1); triangles.Add(startIndex); } if ((renderVoxel.sides & RenderVoxel.BACK) != 0) { int startIndex = vertices.Count; vertices.Add(new Vector3(x * size, z * size, y * size)); vertices.Add(new Vector3(x * size, (z + 1) * size, y * size)); vertices.Add(new Vector3((x + 1) * size, (z + 1) * size, y * size)); vertices.Add(new Vector3((x + 1) * size, z * size, y * size)); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); triangles.Add(startIndex); triangles.Add(startIndex + 1); triangles.Add(startIndex + 2); triangles.Add(startIndex + 2); triangles.Add(startIndex + 3); triangles.Add(startIndex); } if ((renderVoxel.sides & RenderVoxel.FRONT) != 0) { int startIndex = vertices.Count; vertices.Add(new Vector3(x * size, z * size, (y + 1) * size)); vertices.Add(new Vector3(x * size, (z + 1) * size, (y + 1) * size)); vertices.Add(new Vector3((x + 1) * size, (z + 1) * size, (y + 1) * size)); vertices.Add(new Vector3((x + 1) * size, z * size, (y + 1) * size)); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); colors.Add(renderVoxel.color); triangles.Add(startIndex); triangles.Add(startIndex + 3); triangles.Add(startIndex + 2); triangles.Add(startIndex + 2); triangles.Add(startIndex + 1); triangles.Add(startIndex); } } } } mesh = new Mesh(); mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateNormals(); MeshAndColors meshAndColors = new MeshAndColors(); meshAndColors.mesh = mesh; meshAndColors.colors = colors; return(meshAndColors); }