//Recursively search the tree and for each leaf node at the bottom depth populate it's //SDF structure by raycasting to all Scene Geometry and find the closest Surface Point void PopulateTree(Octree.OctreeNode node) { if (!node.IsLeaf()) { var subNodes = node.Nodes; foreach (var subNode in subNodes) { PopulateTree(subNode); } } else { if (node.IsLowestNode()) { //find the closest Surface Point to this Mesh //Unity 5.7 has Physics.ClosestPoint(Vec3 p) //This is my version of it //Vector3 surfacePoint_old = ClosesPointOnCollider(node.Position); Vector3 surfacePoint = ClosesPointOnColliders(node.Position); if (surfacePoint == node.Position) { node.InsideMesh = true; } else { node.InsideMesh = false; node.SDF.mPoint = surfacePoint; node.SDF.mDistance = (surfacePoint - node.Position).magnitude; } } } }
//recursively go through the Octree and convert them into a simple structure void UnpackTree(Octree.OctreeNode node) { TreeNode current = new TreeNode(); var subNodes = node.Nodes; //set this up current.mParent = node.Parent; current.mNode = node; int i = 0; if (subNodes != null) { current.mChildren = new Octree.OctreeNode[8]; foreach (var subNode in subNodes) { current.mChildren[i] = subNode; ++i; } } else { current.mChildren = null; } //assign this node to our array only if It's not inside a mesh if (!current.mNode.InsideMesh) { Unpacked_Tree.Add(current); } //if this Node has children recurse if (!node.IsLeaf()) { foreach (var subNode in subNodes) { UnpackTree(subNode); } } }