private GameObject generateMesh(GameObject parentSphere, List <Vector3[]> allChildrenCorners) { List <CubeGeneratorStatic> meshGenerators = new List <CubeGeneratorStatic> (); int index; //TODO (cmocan): Do foreach instead of for. for (index = 0; index < allChildrenCorners.Count - 1; index++) { GameObject currMesh = new GameObject("newMesh" + index.ToString()); currMesh.transform.parent = this.container.transform; currMesh.transform.localPosition = new Vector3(0, 0, 0); CubeGeneratorStatic meshGenerator = new CubeGeneratorStatic(currMesh); meshGenerator.appendMesh( allChildrenCorners [index] [2], allChildrenCorners [index] [3], allChildrenCorners [index] [1], allChildrenCorners [index] [0], allChildrenCorners [index + 1] [0], allChildrenCorners [index + 1] [2], allChildrenCorners [index + 1] [3], allChildrenCorners [index + 1] [1] ); meshGenerator.finalizeMesh(); meshGenerators.Add(meshGenerator); } CombineInstance[] combine = new CombineInstance[meshGenerators.Count]; Matrix4x4 parentTransform = this.container.transform.worldToLocalMatrix; index = 0; foreach (CubeGeneratorStatic meshGenerator in meshGenerators) { combine [index].mesh = meshGenerator.getMeshFilter().sharedMesh; combine [index].transform = parentTransform * meshGenerator.getMeshFilter().transform.localToWorldMatrix; meshGenerator.getMeshFilter().gameObject.SetActive(false); index++; } GameObject newMesh = new GameObject(MeshGenerationUtils.FULL_MESH_NAME); newMesh.transform.parent = this.container.transform; newMesh.transform.localPosition = new Vector3(0, 0, 0); MeshFilter filter = newMesh.AddComponent <MeshFilter> (); filter.mesh.Clear(); filter.mesh.CombineMeshes(combine); newMesh.gameObject.SetActive(true); Material material = new Material(Shader.Find("Diffuse")); MeshRenderer meshRenderer = newMesh.AddComponent <MeshRenderer> (); meshRenderer.material = material; for (index = 0; index < meshGenerators.Count; index++) { UnityEngine.Object.Destroy(meshGenerators [index].gameObject); } return(newMesh); }
// Use this for initialization void Start() { CubeGeneratorStatic.generateCube(gameObject); }