private GameObject CreatePatch(PlanetMesh meshGenerator) { // Generate mesh with u,v from class state GameObject patch = meshGenerator.GeneratePatch(this.direction, this.u, this.v); // Set position of node Mesh mesh = patch.GetComponent <MeshFilter>().sharedMesh; Vector3 worldPos = patch.transform.TransformPoint(mesh.vertices[meshGenerator.yVertCount * (meshGenerator.yVertCount / 2) + meshGenerator.xVertCount / 2]); this.position = worldPos; return(patch); }
// leaf becomes parent QuadTreeNode that lacks meshPatch public void SplitNode(QuadTreeNode leaf) { // Create root GO and parent correctly GameObject newRoot = new GameObject(); newRoot.tag = "root"; newRoot.name = "Root_" + PlanetMesh.patches[leaf.direction].name + "_" + leaf.u + "_" + leaf.v; newRoot.transform.parent = leaf.meshPatch.transform.parent; // increase patchcount based on depth meshGenerator.uPatchCount = (int )Mathf.Pow(2.0f, (float )leaf.depth + 1); // LOOK AT THIS meshGenerator.vPatchCount = meshGenerator.uPatchCount; meshGenerator.xVertCount = (int )(((float )meshGenerator.xVertCount)); meshGenerator.yVertCount = (int )(((float )meshGenerator.yVertCount)); // Create child nodes and mesh patches int i = 0; int offset = meshGenerator.uPatchCount / (leaf.depth + 1); for (int u = 0; u < 2; u++) { for (int v = 0; v < 2; v++) { // Create node leaf.children[i] = new QuadTreeNode(i, leaf, meshGenerator); // Create appropriate mesh based on the splitted nodes childnumber switch (leaf.childNumber) { case 0: leaf.children[i].meshPatch = meshGenerator.GeneratePatch(leaf.direction, u, v); break; case 1: leaf.children[i].meshPatch = meshGenerator.GeneratePatch(leaf.direction, u, v + offset); break; case 2: leaf.children[i].meshPatch = meshGenerator.GeneratePatch(leaf.direction, u + offset, v); break; case 3: leaf.children[i].meshPatch = meshGenerator.GeneratePatch(leaf.direction, u + offset, v + offset); break; } // Setup child array of new node and tag GO leaf.children[i].meshPatch.transform.parent = newRoot.transform; leaf.children[i].meshPatch.tag = "patch"; // Set position of node Mesh mesh = leaf.children[i].meshPatch.GetComponent <MeshFilter>().sharedMesh; Vector3 worldPos = leaf.children[i].meshPatch.transform.TransformPoint(mesh.vertices[meshGenerator.yVertCount * (meshGenerator.yVertCount / 2) + meshGenerator.xVertCount / 2]); leaf.children[i].position = worldPos; // Add to flat node list nodes.Add(leaf.children[i]); i++; } } // Disable the splitted node's meshPatch GO leaf.meshPatch.SetActive(false); //GameObject.DestroyImmediate(leaf.meshPatch); this.maxDepth++; }