/// <summary> /// Returns a list of all objects which are overlapped by the specified box. /// </summary> /// <param name="box"> /// The box involved in the overlap query. /// </param> /// <param name="objectOverlapPrecision"> /// The desired overlap precision. For the moment this is not used. /// </param> public List <GameObject> OverlapBox(OrientedBox box, ObjectOverlapPrecision objectOverlapPrecision = ObjectOverlapPrecision.ObjectBox) { // Retrieve all the sphere tree nodes which are overlapped by the box. If no nodes are overlapped, // we can return an empty list because it means that no objects could possibly be overlapped either. List <SphereTreeNode <GameObject> > allOverlappedNodes = _sphereTree.OverlapBox(box); if (allOverlappedNodes.Count == 0) { return(new List <GameObject>()); } // Loop through all overlapped nodes var overlappedObjects = new List <GameObject>(); foreach (SphereTreeNode <GameObject> node in allOverlappedNodes) { // Store the node's object for easy access GameObject gameObject = node.Data; if (gameObject == null) { continue; } if (!gameObject.activeSelf) { continue; } // We need to perform an additional check. Even though the box overlaps the object's node (which is // a sphere), we must also check if the box overlaps the object's world oriented box. This allows // for better precision. OrientedBox objectWorldOrientedBox = gameObject.GetWorldOrientedBox(); if (box.Intersects(objectWorldOrientedBox)) { overlappedObjects.Add(gameObject); } } return(overlappedObjects); }
public List <Vector3> GetOverlappedWorldVerts(OrientedBox box, Matrix4x4 meshTransformMatrix) { if (IsBuildingSilent) { while (IsBuildingSilent) { ; } } if (!_wasBuilt) { Build(); } // Work in mesh model space because the tree daata exists in model space Matrix4x4 inverseTransform = meshTransformMatrix.inverse; box.Transform(inverseTransform); // Retrieve the nodes overlapped by the specified box List <SphereTreeNode <MeshSphereTreeTriangle> > overlappedNodes = _sphereTree.OverlapBox(box); if (overlappedNodes.Count == 0) { return(new List <Vector3>()); } // Loop through all nodes var overlappedWorldVerts = new List <Vector3>(); foreach (var node in overlappedNodes) { // Get the traingle associated with the node int triangleIndex = node.Data.TriangleIndex; Triangle3D modelSpaceTriangle = _editorMesh.GetTriangle(triangleIndex); // Now check which of the triangle points resides inside the box List <Vector3> trianglePoints = modelSpaceTriangle.GetPoints(); foreach (var pt in trianglePoints) { // When a point resides inside the box, we will transform it in world space and add it to the final point list if (box.ContainsPoint(pt)) { overlappedWorldVerts.Add(meshTransformMatrix.MultiplyPoint(pt)); } } } return(overlappedWorldVerts); }