public void AddMesh(STLLoader.Mesh mesh, int layer) { this.layer = layer; int vertexCount; vertexCount = mesh.vertices.Count; if (mesh.vertices.Count > 64998) // Unity restricts meshes to have no more than 6500 vertices. { vertexCount = 64998; } vertices = new Vector3[vertexCount]; normals = new Vector3[vertexCount]; indices = new int[vertexCount]; int normalCounter = 0; for (int i = 0; i < vertexCount; ++i) { indices[i] = i; vertices[i] = new Vector3(mesh.vertices[i].x, mesh.vertices[i].y, mesh.vertices[i].z); normals[i] = new Vector3(mesh.normals[(i - normalCounter) / 3].x, mesh.normals[(i - normalCounter) / 3].y, mesh.normals[(i - normalCounter) / 3].z); normalCounter = (normalCounter + 1) % 3; //normals need to be set facewise. } Mesh mesh2 = new Mesh(); GetComponent <MeshFilter>().mesh = mesh2; mesh2.vertices = vertices; mesh2.triangles = indices; mesh2.normals = normals; }
private void CreateMesh(ref STLLoader.Mesh mesh) { GameObject obj = (GameObject)Instantiate(meshPrefab, Vector3.zero, Quaternion.identity); obj.GetComponent <MeshPrefab>().AddMesh(mesh, (int)resolution + 1); obj.transform.parent = gameObject.transform; obj.transform.position = Vector3.zero; obj.transform.localScale = Vector3.one; }
private void CreateIndexedMesh(ref STLLoader.Mesh mesh, int startPos) { if (mesh == null) { return; } indexedVoxelMesh[startPos] = STLLoader.STLLoader.CreateIndexedMesh(mesh, 65000); for (int j = 0; j < indexedVoxelMesh[startPos].Count; ++j) { GameObject obj = (GameObject)Instantiate(meshPrefab, Vector3.zero, Quaternion.identity); obj.GetComponent <MeshPrefab>().AddIndexedMesh(indexedVoxelMesh[startPos][j], startPos); obj.transform.parent = gameObject.transform; obj.transform.localScale = Vector3.one; obj.transform.localPosition = Vector3.zero; obj.name = startPos + "_"; } }