private void RealizeNode(MCMesh m) { Debug.Log("Realizing node!"); GameObject clone = UnityEngine.Object.Instantiate(MeshPrefab, new Vector3(0, 0, 0), Quaternion.identity); Color c = UtilFuncs.SinColor(m.nodeDepth * 3f); clone.GetComponent <MeshRenderer>().material.color = new Color(c.r, c.g, c.b, 0.9f); clone.transform.localScale = Vector3.one * (WorldSize * m.nodeSize / Resolution); clone.name = "Node " + m.nodeID + ", Depth " + m.nodeDepth; MeshFilter mf = clone.GetComponent <MeshFilter>(); Mesh um = new UnityEngine.Mesh(); um.SetVertices(m.Vertices); um.SetNormals(m.Normals); um.triangles = m.Triangles; mf.mesh = um; clone.GetComponent <Transform>().SetParent(Parent); clone.GetComponent <Transform>().SetPositionAndRotation(m.nodePosition * WorldSize, Quaternion.identity); UnityObjects[m.nodeID] = clone; }
public static MCMesh PolyganizeNode(Node node, float worldSize, int resolution) { float mul = node.Size; UtilFuncs.Sampler sample = (float x, float y, float z) => UtilFuncs.Sample( (((x * node.Size) / resolution) + node.Position.x) * worldSize, (((y * node.Size) / resolution) + node.Position.y) * worldSize, (((z * node.Size) / resolution) + node.Position.z) * worldSize); /*Node[] neighbors = FindNeighbors(root, node); * * int currentSide = 1; * * for(int i = 0; i < 6; i++) { * if(neighbors[i] != null && neighbors[i].Depth < node.Depth) { * lod |= (byte)currentSide; * } * currentSide = currentSide << (byte)1; * }*/ //node.LODSides = 0; sbyte[][][][] data = GenerateChunkData(resolution, sample); MCMesh m = SE.MarchingCubes.PolygonizeArea(new Vector3(0, 0, 0), node.LODSides, resolution, data); //Mesh m2 = new Mesh(); //m2.SetVertices(m.Vertices); //m2.SetNormals(m.Normals); //m2.triangles = m.Triangles; return(m); }
void TestTransvoxel(int sF) { int resolution = 12; int res1 = resolution + 1; MCMesh m = new MCMesh(); List <Vector3> vertices = new List <Vector3>(); List <int> triangles = new List <int>(); int sampleFn = 1; byte lod = 63; UtilFuncs.Sampler fn = (float x, float y, float z) => sampleFunctions[sampleFn](x, y, z, 16); SE.Transvoxel.Transvoxel.GenerateChunk(new Vector3(0, 0, 0), vertices, triangles, resolution, fn, lod); Debug.Log("0 tris"); if (triangles.Count > 0) { m.Triangles = triangles.ToArray(); m.Vertices = vertices; Mesh(m); } }
public void MeshNode(Node node, ref float totalPolyganizeNodeTime, ref float totalAllBeforeTime, System.Diagnostics.Stopwatch sw) { sw.Start(); GameObject clone = Object.Instantiate(MeshPrefab, new Vector3(0, 0, 0), Quaternion.identity); Color c = UtilFuncs.SinColor(node.Depth * 3f); clone.GetComponent <MeshRenderer>().material.color = new Color(c.r, c.g, c.b, 0.9f); clone.transform.localScale = Vector3.one * (WorldSize * node.Size / Resolution); clone.name = "Node " + node.ID + ", Depth " + node.Depth; MeshFilter mf = clone.GetComponent <MeshFilter>(); sw.Stop(); totalAllBeforeTime += (float)sw.ElapsedMilliseconds / 1000f; sw.Reset(); sw.Start(); MCMesh mcm = SE.Octree.Ops.PolyganizeNode(node, WorldSize, Resolution); Mesh m = new Mesh(); m.SetVertices(mcm.Vertices); m.SetNormals(mcm.Normals); m.triangles = mcm.Triangles; mf.mesh = m; sw.Stop(); totalPolyganizeNodeTime += (float)sw.ElapsedMilliseconds / 1000f; clone.GetComponent <Transform>().SetParent(Parent); clone.GetComponent <Transform>().SetPositionAndRotation(node.Position * WorldSize, Quaternion.identity); UnityObjects[node.ID] = clone; }
public async Task <MCMesh> MeshNode(Node node) { MCMesh m = await Task.Run(() => SE.Octree.Ops.PolyganizeNode(node, WorldSize, Resolution)); m.nodeDepth = node.Depth; m.nodeID = node.ID; m.nodeSize = node.Size; m.nodePosition = node.Position; return(m); }
void Mesh(MCMesh m) { Object.Destroy(CurrentMesh); GameObject clone = Object.Instantiate(MeshPrefab, new Vector3(0, 0, 0), Quaternion.identity); clone.name = "Test mesh"; MeshFilter mf = clone.GetComponent <MeshFilter>(); UnityEngine.Mesh m2 = new Mesh(); m2.SetVertices(m.Vertices); m2.SetNormals(m.Normals); m2.triangles = m.Triangles; mf.mesh = m2; //m2.RecalculateNormals(); CurrentMesh = clone; }
public static MCMesh PolygonizeArea(Vector3 min, float size, int resolution, byte lod, sbyte[][][] data) { MCMesh m = new MCMesh(); List <Vector3> Vertices = new List <Vector3>(); Hashtable ExistingVertices = new Hashtable(); /*Map<Vector3, int> map = Collections.synchronizedMap( * new LinkedHashMap<String, String>());*/ //List<Vector3> Vertices = new List<Vector3>(); List <int> Triangles = new List <int>(); int ntriang, i; int trinum = 0; Vector3[] VertList = new Vector3[12]; //Debug.Log(data[0][8][64]); sbyte[] densities = new sbyte[8]; for (int x = 0; x < resolution; x++) { for (int y = 0; y < resolution; y++) { for (int z = 0; z < resolution; z++) { byte caseCode = 0; densities[0] = data[x][y][z]; densities[1] = data[x + 1][y][z]; densities[2] = data[x + 1][y + 1][z]; densities[3] = data[x][y + 1][z]; densities[4] = data[x][y][z + 1]; densities[5] = data[x + 1][y][z + 1]; densities[6] = data[x + 1][y + 1][z + 1]; densities[7] = data[x][y + 1][z + 1]; if (densities[0] < 0) { caseCode |= 1; } if (densities[1] < 0) { caseCode |= 2; } if (densities[2] < 0) { caseCode |= 4; } if (densities[3] < 0) { caseCode |= 8; } if (densities[4] < 0) { caseCode |= 16; } if (densities[5] < 0) { caseCode |= 32; } if (densities[6] < 0) { caseCode |= 64; } if (densities[7] < 0) { caseCode |= 128; } if (x == 0 && y == 0 && z == 0) { Debug.Log("case code for xyz 0: " + caseCode); } if (caseCode == 0 || caseCode == 255) { continue; } if ((Tables.edgeTable[caseCode] & 1) == 1) { VertList[0] = Lerp(densities[0], densities[1], x, y, z, x + 1, y, z); } //vertlist[0] = UtilFuncs.Lerp(isovalue,cell.points[0],cell.points[1]); if ((Tables.edgeTable[caseCode] & 2) == 2) { VertList[1] = Lerp(densities[1], densities[2], x + 1, y, z, x + 1, y + 1, z); } //vertlist[1] = UtilFuncs.Lerp(isovalue,cell.points[1],cell.points[2]); if ((Tables.edgeTable[caseCode] & 4) == 4) { VertList[2] = Lerp(densities[2], densities[3], x + 1, y + 1, z, x, y + 1, z); } //vertlist[2] = UtilFuncs.Lerp(isovalue,cell.points[2],cell.points[3]); if ((Tables.edgeTable[caseCode] & 8) == 8) { VertList[3] = Lerp(densities[3], densities[0], x, y + 1, z, x, y, z); } //vertlist[3] = UtilFuncs.Lerp(isovalue,cell.points[3],cell.points[0]); if ((Tables.edgeTable[caseCode] & 16) == 16) { VertList[4] = Lerp(densities[4], densities[5], x, y, z + 1, x + 1, y, z + 1); } //vertlist[4] = UtilFuncs.Lerp(isovalue,cell.points[4],cell.points[5]); if ((Tables.edgeTable[caseCode] & 32) == 32) { VertList[5] = Lerp(densities[5], densities[6], x + 1, y, z + 1, x + 1, y + 1, z + 1); } //vertlist[5] = UtilFuncs.Lerp(isovalue,cell.points[5],cell.points[6]); if ((Tables.edgeTable[caseCode] & 64) == 64) { VertList[6] = Lerp(densities[6], densities[7], x + 1, y + 1, z + 1, x, y + 1, z + 1); } //vertlist[6] = UtilFuncs.Lerp(isovalue,cell.points[6],cell.points[7]); if ((Tables.edgeTable[caseCode] & 128) == 128) { VertList[7] = Lerp(densities[7], densities[4], x, y + 1, z + 1, x, y, z + 1); } //vertlist[7] = UtilFuncs.Lerp(isovalue,cell.points[7],cell.points[4]); if ((Tables.edgeTable[caseCode] & 256) == 256) { VertList[8] = Lerp(densities[0], densities[4], x, y, z, x, y, z + 1); } //vertlist[8] = UtilFuncs.Lerp(isovalue,cell.points[0],cell.points[4]); if ((Tables.edgeTable[caseCode] & 512) == 512) { VertList[9] = Lerp(densities[1], densities[5], x + 1, y, z, x + 1, y, z + 1); } //vertlist[9] = UtilFuncs.Lerp(isovalue,cell.points[1],cell.points[5]); if ((Tables.edgeTable[caseCode] & 1024) == 1024) { VertList[10] = Lerp(densities[2], densities[6], x + 1, y + 1, z, x + 1, y + 1, z + 1); } //vertlist[10] = UtilFuncs.Lerp(isovalue,cell.points[2],cell.points[6]); if ((Tables.edgeTable[caseCode] & 2048) == 2048) { VertList[11] = Lerp(densities[3], densities[7], x, y + 1, z, x, y + 1, z + 1); } //vertlist[11] = UtilFuncs.Lerp(isovalue,cell.points[3],cell.points[7])) /*ntriang = 0; * for (i=0; Tables.triTable[caseCode][i] != -1; i += 3 ) { * triangles[ntriang].p[0] = vertlist[Tables.triTable[cubeindex][i ]]; * triangles[ntriang].p[1] = vertlist[Tables.triTable[cubeindex][i+1]]; * triangles[ntriang].p[2] = vertlist[Tables.triTable[cubeindex][i+2]]; * ntriang++; * }*/ for (i = 0; Tables.triTable[caseCode][i] != -1; i++) { int tri = Tables.triTable[caseCode][i]; Vector3 vert = VertList[tri]; if (!ExistingVertices.Contains(vert)) { Vertices.Add(vert); ExistingVertices.Add(vert, trinum); Triangles.Add(trinum); trinum++; } else { Triangles.Add((int)ExistingVertices[vert]); } } //m.Vertices.Add(new Vector3(x, y, z)); } } } /*int[] Triangles = new int[Vertices.Count]; * for(i = 0; i < Triangles.Length; i++) { * Triangles[i] = i; * }*/ m.Vertices = Vertices; m.Triangles = Triangles.ToArray(); return(m); }