public List <Vector3> GetOverlappedModelVerts(Box modelBox) { if (!_wasBuilt) { Build(); } OrientedBox meshSpaceOOBB = new OrientedBox(modelBox); List <SphereTreeNode <MeshSphereTreeTriangle> > overlappedNodes = _sphereTree.OverlapBox(meshSpaceOOBB); if (overlappedNodes.Count == 0) { return(new List <Vector3>()); } HashSet <int> usedIndices = new HashSet <int>(); var overlappedModelVerts = new List <Vector3>(50); foreach (var node in overlappedNodes) { int triangleIndex = node.Data.TriangleIndex; MeshTriangleInfo triangleInfo = _octave3DMesh.GetMeshTriangleInfo(triangleIndex); List <Vector3> trianglePoints = triangleInfo.ModelSpaceTriangle.GetPoints(); for (int ptIndex = 0; ptIndex < trianglePoints.Count; ++ptIndex) { if (usedIndices.Contains(triangleInfo.VertIndices[ptIndex])) { continue; } Vector3 point = trianglePoints[ptIndex]; if (meshSpaceOOBB.ContainsPoint(point)) { overlappedModelVerts.Add(point); usedIndices.Add(triangleInfo.VertIndices[ptIndex]); } } } return(overlappedModelVerts); }
/// <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> public List <GameObject> OverlapBox(OrientedBox box) { // 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); }
/// <summary> /// Returns the world space vertices overlapped by the specified box. /// </summary> public List <Vector3> GetOverlappedWorldVerts(OrientedBox box, TransformMatrix meshTransformMatrix) { // If the tree was not yet build, we need to build it because we need // the triangle information in order to perform the raycast. if (!_wasBuilt) { Build(); } // Work in mesh model space because the tree data exists in model space OrientedBox meshSpaceBox = new OrientedBox(box); Matrix4x4 inverseTransform = meshTransformMatrix.ToMatrix4x4x.inverse; meshSpaceBox.Transform(inverseTransform); // Used to avoid duplicate indices since there can be triangles which share the same vertices // and we don't want to return the same vertex multiple times. HashSet <int> usedIndices = new HashSet <int>(); // Retrieve the nodes overlapped by the specified box List <SphereTreeNode <MeshSphereTreeTriangle> > overlappedNodes = _sphereTree.OverlapBox(meshSpaceBox); if (overlappedNodes.Count == 0) { return(new List <Vector3>()); } // Loop through all nodes var overlappedWorldVerts = new List <Vector3>(50); foreach (var node in overlappedNodes) { // Get the triangle associated with the node int triangleIndex = node.Data.TriangleIndex; MeshTriangleInfo triangleInfo = _octave3DMesh.GetMeshTriangleInfo(triangleIndex); List <Vector3> trianglePoints = triangleInfo.ModelSpaceTriangle.GetPoints(); for (int ptIndex = 0; ptIndex < trianglePoints.Count; ++ptIndex) { if (usedIndices.Contains(triangleInfo.VertIndices[ptIndex])) { continue; } Vector3 point = trianglePoints[ptIndex]; if (meshSpaceBox.ContainsPoint(point)) { overlappedWorldVerts.Add(meshTransformMatrix.MultiplyPoint(point)); usedIndices.Add(triangleInfo.VertIndices[ptIndex]); } } /*Triangle3D modelSpaceTriangle = _octave3DMesh.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); }