public bool CornersContains(Vector3 point, BoundCorners corners) { // Create a vector from max to min Vector3 min = corners.FrontBottomLeft; Vector3 max = corners.BackTopRight; // Determine whether it has a positive dot product with all three nearest sides // Repeat for the max side Vector3 v = point - min; Vector3 up = corners.FrontTopLeft - min; Vector3 back = corners.BackBottomLeft - min; Vector3 right = corners.FrontBottomRight - min; if (Vector3.Dot(v, up) >= 0 && Vector3.Dot(v, back) >= 0 && Vector3.Dot(v, right) >= 0) { v = point - max; up = corners.BackBottomRight - max; back = corners.FrontTopRight - max; right = corners.BackTopLeft - max; if (Vector3.Dot(v, up) >= 0 && Vector3.Dot(v, back) >= 0 && Vector3.Dot(v, right) >= 0) { return(true); } } return(false); }
public void Update() { if (GOTrigger) { VisualizeGameObjectBounds(); } if (MinMaxTrigger) { VisualizeBoxFromMinAndMax(); } if (CornersTrigger) { VisualizeBoxFromBoxCorners(); } BoundCorners goCorners = CalculateCorners(GetBoundsFor(MeshObjectToDisplayBoundsFor), MeshObjectToDisplayBoundsFor); BoundCorners minMaxCorners = CalculateCorners(MinAndMax[0], MinAndMax[1]); MinMaxIntersect = Intersects(goCorners, minMaxCorners); if (MinMaxIntersect) { LineColor = ContainedColor; } else { LineColor = NotContainedColor; } }
private bool getVisibilityRecommendation(GameObject go, BoundCorners querySpaceCorners) { Bounds b = visualizer.GetBoundsFor(go); BoundCorners goCorners = visualizer.CalculateCorners(b, go); return(getVisibilityRecommendation(goCorners, querySpaceCorners)); }
public void VisualizeGameObjectBounds(GameObject go) { GameObjBounds = GetBoundsFor(go); BoundCorners corners = CalculateCorners(GameObjBounds, go); VisualizeBox(corners); }
public BoundCorners CalculateCorners(Bounds bounds, GameObject go) { Vector3 cornerMin = bounds.min; // Front, Bottom, Left Vector3 cornerFTL = bounds.center + new Vector3(-bounds.extents.x, bounds.extents.y, -bounds.extents.z); // Front, Top, Left Vector3 cornerFTR = bounds.center + new Vector3(bounds.extents.x, bounds.extents.y, -bounds.extents.z); // Front, Top, Right Vector3 cornerFBR = bounds.center + new Vector3(bounds.extents.x, -bounds.extents.y, -bounds.extents.z); // Front, Bottom, Right Vector3 cornerBBL = bounds.center + new Vector3(-bounds.extents.x, -bounds.extents.y, bounds.extents.z); // Back, Bottom, Left Vector3 cornerBTL = bounds.center + new Vector3(-bounds.extents.x, bounds.extents.y, bounds.extents.z); // Back, Top, Left Vector3 cornerMax = bounds.max; // Back, Top, Right Vector3 cornerBBR = bounds.center + new Vector3(bounds.extents.x, -bounds.extents.y, bounds.extents.z); // Back, Bottom, Right if (go != null) { cornerMin = RotateBoundingBoxPoint(cornerMin, go); cornerFTL = RotateBoundingBoxPoint(cornerFTL, go); cornerFTR = RotateBoundingBoxPoint(cornerFTR, go); cornerFBR = RotateBoundingBoxPoint(cornerFBR, go); cornerBBL = RotateBoundingBoxPoint(cornerBBL, go); cornerBTL = RotateBoundingBoxPoint(cornerBTL, go); cornerMax = RotateBoundingBoxPoint(cornerMax, go); cornerBBR = RotateBoundingBoxPoint(cornerBBR, go); } BoundCorners corners = new BoundCorners(cornerMin, cornerFTL, cornerFTR, cornerFBR, cornerBBL, cornerBTL, cornerMax, cornerBBR); return(corners); }
private void updateMinMaxCorners() { if (minMaxCornersNeedsUpdate()) { cacheMinMax(); minMaxCorners = visualizer.CalculateCorners(cachedMin, cachedMax); } }
public void VisualizeGameObjectBounds() { Bounds b = GetBoundsFor(MeshObjectToDisplayBoundsFor); BoundCorners corners = CalculateCorners(b, MeshObjectToDisplayBoundsFor); VisualizeBox(corners); //VisualizeBoxWithRays(corners); }
public BoundCorners CalculateCorners(Vector3 min, Vector3 max) { Vector3 center = min + ((max - min) / 2); Vector3 size = max - min; Bounds b = new Bounds(center, size); BoundCorners corners = CalculateCorners(b, null); return(corners); }
public BoundCorners CalculateCorners(Vector3 min, Vector3 max) { Vector3 size = max - min; Vector3 extents = size / 2; Vector3 center = min + extents; // Vector3 center = min + ((max - min) / 2); BBBounds = new Bounds(center, size); BoundCorners corners = CalculateCorners(BBBounds, null); return(corners); }
public BoundCorners GetBoundCorners(Vector3 frontBottomLeft, Vector3 frontTopLeft, Vector3 frontTopRight, Vector3 frontBottomRight, Vector3 backBottomLeft, Vector3 backTopLeft, Vector3 backTopRight, Vector3 backBottomRight) { BoundCorners corners = new BoundCorners( frontBottomLeft, frontTopLeft, frontTopRight, frontBottomRight, backBottomLeft, backTopLeft, backTopRight, backBottomRight); return(corners); }
private bool getVisibilityRecommendation(BoundCorners goCorners, BoundCorners querySpaceCorners) { //bool MeshBBCornerInQuerySpaceBB = true; //bool fbl = visualizer.CornersContains(goCorners.FrontBottomLeft, querySpaceCorners); //if (!fbl) //{ // bool ftl = visualizer.CornersContains(goCorners.FrontTopLeft, querySpaceCorners); // if (!ftl) // { // bool ftr = visualizer.CornersContains(goCorners.FrontTopRight, querySpaceCorners); // if (!ftr) // { // bool fbr = visualizer.CornersContains(goCorners.FrontBottomRight, querySpaceCorners); // if (!fbr) // { // bool bbl = visualizer.CornersContains(goCorners.BackBottomLeft, querySpaceCorners); // if (!bbl) // { // bool btl = visualizer.CornersContains(goCorners.BackTopLeft, querySpaceCorners); // if (!btl) // { // bool btr = visualizer.CornersContains(goCorners.BackTopRight, querySpaceCorners); // if (!btr) // { // bool bbr = visualizer.CornersContains(goCorners.BackBottomRight, querySpaceCorners); // if (!bbr) // { // MeshBBCornerInQuerySpaceBB = false; // } // } // } // } // } // } // } //} //if (MeshBBCornerInQuerySpaceBB) //{ // return true; //} return(visualizer.Intersects(goCorners, querySpaceCorners)); }
public bool IsGoContainedInMinMaxCalc() { BoundCorners goCorners = CalculateCorners(GetBoundsFor(MeshObjectToDisplayBoundsFor), MeshObjectToDisplayBoundsFor); BoundCorners minMaxCorners = CalculateCorners(MinAndMax[0], MinAndMax[1]); bool fbl = IsLesserThanPoint(goCorners.FrontBottomLeft, MinAndMax[0]) && IsGreaterThanPoint(goCorners.FrontBottomLeft, MinAndMax[1]); if (!fbl) { bool ftl = IsLesserThanPoint(goCorners.FrontTopLeft, MinAndMax[0]) && IsGreaterThanPoint(goCorners.FrontTopLeft, MinAndMax[1]); if (!ftl) { bool ftr = IsLesserThanPoint(goCorners.FrontTopRight, MinAndMax[0]) && IsGreaterThanPoint(goCorners.FrontTopRight, MinAndMax[1]); if (!ftr) { bool fbr = IsLesserThanPoint(goCorners.FrontBottomRight, MinAndMax[0]) && IsGreaterThanPoint(goCorners.FrontBottomRight, MinAndMax[1]); if (!fbr) { bool bbl = IsLesserThanPoint(goCorners.BackBottomLeft, MinAndMax[0]) && IsGreaterThanPoint(goCorners.BackBottomLeft, MinAndMax[1]); if (!bbl) { bool btl = IsLesserThanPoint(goCorners.BackTopLeft, MinAndMax[0]) && IsGreaterThanPoint(goCorners.BackTopLeft, MinAndMax[1]); if (!btl) { bool btr = IsLesserThanPoint(goCorners.BackTopRight, MinAndMax[0]) && IsGreaterThanPoint(goCorners.BackTopRight, MinAndMax[1]); if (!btr) { bool bbr = IsLesserThanPoint(goCorners.BackBottomRight, MinAndMax[0]) && IsGreaterThanPoint(goCorners.BackBottomRight, MinAndMax[1]); if (!bbr) { return(false); } } } } } } } } return(true); }
//public void VisualizeBoxWithRays(BoundCorners corners) //{ // Debug.Log("Corner0 = " + corners.FrontBottomLeft); // Debug.Log("Corner1 = " + corners.FrontTopLeft); // Debug.Log("Corner2 = " + corners.FrontTopRight); // Debug.Log("Corner3 = " + corners.FrontBottomRight); // Debug.Log("Corner4 = " + corners.BackBottomLeft); // Debug.Log("Corner5 = " + corners.BackTopLeft); // Debug.Log("Corner6 = " + corners.BackTopRight); // Debug.Log("Corner7 = " + corners.BackBottomRight); // // Connect front face // Debug.DrawRay(corners.FrontBottomLeft, (corners.FrontTopLeft - corners.FrontBottomRight), LineColor, (corners.FrontTopLeft - corners.FrontBottomLeft).magnitude); // Debug.DrawLine(corners.FrontBottomLeft, corners.FrontTopLeft, LineColor); // Debug.DrawLine(corners.FrontTopLeft, corners.FrontTopRight, LineColor); // Debug.DrawLine(corners.FrontTopRight, corners.FrontBottomRight, LineColor); // Debug.DrawLine(corners.FrontBottomRight, corners.FrontBottomLeft, LineColor); // // Connect back face // Debug.DrawLine(corners.BackBottomLeft, corners.BackTopLeft, LineColor); // Debug.DrawLine(corners.BackTopLeft, corners.BackTopRight, LineColor); // Debug.DrawLine(corners.BackTopRight, corners.BackBottomRight, LineColor); // Debug.DrawLine(corners.BackBottomRight, corners.BackBottomLeft, LineColor); // // Connect faces // Debug.DrawLine(corners.FrontBottomLeft, corners.BackBottomLeft, LineColor); // Debug.DrawLine(corners.FrontTopLeft, corners.BackTopLeft, LineColor); // Debug.DrawLine(corners.FrontTopRight, corners.BackTopRight, LineColor); // Debug.DrawLine(corners.FrontBottomRight, corners.BackBottomRight, LineColor); //} public void VisualizeBox(BoundCorners corners) { // Connect front face Debug.DrawLine(corners.FrontBottomLeft, corners.FrontTopLeft, LineColor); Debug.DrawLine(corners.FrontTopLeft, corners.FrontTopRight, LineColor); Debug.DrawLine(corners.FrontTopRight, corners.FrontBottomRight, LineColor); Debug.DrawLine(corners.FrontBottomRight, corners.FrontBottomLeft, LineColor); // Connect back face Debug.DrawLine(corners.BackBottomLeft, corners.BackTopLeft, LineColor); Debug.DrawLine(corners.BackTopLeft, corners.BackTopRight, LineColor); Debug.DrawLine(corners.BackTopRight, corners.BackBottomRight, LineColor); Debug.DrawLine(corners.BackBottomRight, corners.BackBottomLeft, LineColor); // Connect faces Debug.DrawLine(corners.FrontBottomLeft, corners.BackBottomLeft, LineColor); Debug.DrawLine(corners.FrontTopLeft, corners.BackTopLeft, LineColor); Debug.DrawLine(corners.FrontTopRight, corners.BackTopRight, LineColor); Debug.DrawLine(corners.FrontBottomRight, corners.BackBottomRight, LineColor); }
private bool EdgeBoxIntersect(KeyValuePair <Vector3, Vector3> edge, BoundCorners boxCorners) { Vector3 fbl = boxCorners.FrontBottomLeft; Vector3 ftl = boxCorners.FrontTopLeft; Vector3 ftr = boxCorners.FrontTopRight; Vector3 fbr = boxCorners.FrontBottomRight; Vector3 bbl = boxCorners.BackBottomLeft; Vector3 btl = boxCorners.BackTopLeft; Vector3 btr = boxCorners.BackTopRight; Vector3 bbr = boxCorners.BackBottomRight; // Generate a face for each of the box faces List <Vector3> frontFaceCorners = new List <Vector3>(new Vector3[] { fbl, ftl, ftr, fbr }); List <Vector3> backFaceCorners = new List <Vector3> (new Vector3[] { bbr, btr, btl, bbl }); List <Vector3> leftFaceCorners = new List <Vector3> (new Vector3[] { bbl, btl, ftl, fbl }); List <Vector3> rightFaceCorners = new List <Vector3> (new Vector3[] { fbr, ftr, btr, bbr }); List <Vector3> topFaceCorners = new List <Vector3> (new Vector3[] { ftl, btl, btr, ftr }); List <Vector3> bottomFaceCorners = new List <Vector3> (new Vector3[] { fbl, bbl, bbr, fbr }); if (!EdgeFaceIntersect(edge, frontFaceCorners.ToArray())) { if (!EdgeFaceIntersect(edge, backFaceCorners.ToArray())) { if (!EdgeFaceIntersect(edge, leftFaceCorners.ToArray())) { if (!EdgeFaceIntersect(edge, rightFaceCorners.ToArray())) { if (!EdgeFaceIntersect(edge, topFaceCorners.ToArray())) { if (!EdgeFaceIntersect(edge, bottomFaceCorners.ToArray())) { return(false); } } } } } } return(true); }
public void VisualizeRoomPieceIfAppropriate(GameObject roomPiece, bool displayBoundingBox) { if (roomPiece != null) { Bounds b = visualizer.GetBoundsFor(roomPiece); BoundCorners corners = visualizer.CalculateCorners(b, roomPiece); bool shouldBeVisible = getVisibilityRecommendation(corners, minMaxCorners); if (shouldBeVisible) { roomPiece.SetActive(true); if (displayBoundingBox) { visualizer.VisualizeBox(corners); } } else { roomPiece.SetActive(false); } } }
private bool AnyEdgeBoxIntersect(BoundCorners corners, BoundCorners otherCorners) { List <KeyValuePair <Vector3, Vector3> > edges = GetEdges(corners); List <KeyValuePair <Vector3, Vector3> > otherEdges = GetEdges(otherCorners); foreach (var edge in edges) { if (EdgeBoxIntersect(edge, otherCorners)) { return(true); } } foreach (var edge in otherEdges) { if (EdgeBoxIntersect(edge, corners)) { return(true); } } return(false); }
private List <KeyValuePair <Vector3, Vector3> > GetEdges(BoundCorners corners) { List <KeyValuePair <Vector3, Vector3> > edgeList = new List <KeyValuePair <Vector3, Vector3> > (); // Front face edgeList.Add(GetEdge(corners.FrontBottomLeft, corners.FrontTopLeft)); edgeList.Add(GetEdge(corners.FrontTopLeft, corners.FrontTopRight)); edgeList.Add(GetEdge(corners.FrontTopRight, corners.FrontBottomRight)); edgeList.Add(GetEdge(corners.FrontBottomRight, corners.FrontBottomLeft)); // Back face edgeList.Add(GetEdge(corners.BackBottomLeft, corners.BackTopLeft)); edgeList.Add(GetEdge(corners.BackTopLeft, corners.BackTopRight)); edgeList.Add(GetEdge(corners.BackTopRight, corners.BackBottomRight)); edgeList.Add(GetEdge(corners.BackBottomRight, corners.BackBottomLeft)); // Connectors edgeList.Add(GetEdge(corners.FrontBottomLeft, corners.BackBottomLeft)); edgeList.Add(GetEdge(corners.FrontTopLeft, corners.BackTopLeft)); edgeList.Add(GetEdge(corners.FrontTopRight, corners.BackTopRight)); edgeList.Add(GetEdge(corners.FrontBottomRight, corners.BackBottomRight)); return(edgeList); }
public void VisualizeBoxFromBoxCorners() { BoundCorners corners = GetBoundCorners(BoxCorners[0], BoxCorners[1], BoxCorners[2], BoxCorners[3], BoxCorners[4], BoxCorners[5], BoxCorners[6], BoxCorners[7]); VisualizeBox(corners); }
public void VisualizeBoxFromMinAndMax() { BoundCorners corners = CalculateCorners(MinAndMax[0], MinAndMax[1]); VisualizeBox(corners); }
public void Start() { visualizer = new BoundingBoxVisualizer(); cacheMinMax(); minMaxCorners = visualizer.CalculateCorners(cachedMin, cachedMax); }
public bool Intersects(BoundCorners goCorners, BoundCorners spaceCorners) { //// Calculate if a corner of the mesh bounding box is in the minmax bounding box //bool MeshBBCornerInMinMaxBB = true; //bool fbl = IsLesserThanPoint(goCorners.FrontBottomLeft, MinAndMax[0]) && IsGreaterThanPoint(goCorners.FrontBottomLeft, MinAndMax[1]); // if (!fbl) // { // bool ftl = IsLesserThanPoint(goCorners.FrontTopLeft, MinAndMax[0]) && IsGreaterThanPoint(goCorners.FrontTopLeft, MinAndMax[1]); // if (!ftl) // { // bool ftr = IsLesserThanPoint(goCorners.FrontTopRight, MinAndMax[0]) && IsGreaterThanPoint(goCorners.FrontTopRight, MinAndMax[1]); // if (!ftr) // { // bool fbr = IsLesserThanPoint(goCorners.FrontBottomRight, MinAndMax[0]) && IsGreaterThanPoint(goCorners.FrontBottomRight, MinAndMax[1]); // if (!fbr) // { // bool bbl = IsLesserThanPoint(goCorners.BackBottomLeft, MinAndMax[0]) && IsGreaterThanPoint(goCorners.BackBottomLeft, MinAndMax[1]); // if (!bbl) // { // bool btl = IsLesserThanPoint(goCorners.BackTopLeft, MinAndMax[0]) && IsGreaterThanPoint(goCorners.BackTopLeft, MinAndMax[1]); // if (!btl) // { // bool btr = IsLesserThanPoint(goCorners.BackTopRight, MinAndMax[0]) && IsGreaterThanPoint(goCorners.BackTopRight, MinAndMax[1]); // if (!btr) // { // bool bbr = IsLesserThanPoint(goCorners.BackBottomRight, MinAndMax[0]) && IsGreaterThanPoint(goCorners.BackBottomRight, MinAndMax[1]); // if (!bbr) // { // MeshBBCornerInMinMaxBB = false; // } // } // } // } // } // } // } // } // Calculate if a corner of the mesh bounding box is in the minmax bounding box bool MeshBBCornerInMinMaxBB = true; bool fbl = CornersContains(goCorners.FrontBottomLeft, spaceCorners); if (!fbl) { bool ftl = CornersContains(goCorners.FrontTopLeft, spaceCorners); if (!ftl) { bool ftr = CornersContains(goCorners.FrontTopRight, spaceCorners); if (!ftr) { bool fbr = CornersContains(goCorners.FrontBottomRight, spaceCorners); if (!fbr) { bool bbl = CornersContains(goCorners.BackBottomLeft, spaceCorners); if (!bbl) { bool btl = CornersContains(goCorners.BackTopLeft, spaceCorners); if (!btl) { bool btr = CornersContains(goCorners.BackTopRight, spaceCorners); if (!btr) { bool bbr = CornersContains(goCorners.BackBottomRight, spaceCorners); if (!bbr) { MeshBBCornerInMinMaxBB = false; } } } } } } } } if (MeshBBCornerInMinMaxBB) { return(true); } // Calculate if a corner of the minmax bounding box is in the mesh bounding box //bool MinMaxBBCornerInMeshBB = true; //fbl = IsLesserThanPoint(minMaxCorners.FrontBottomLeft, goCorners.FrontBottomLeft) && IsGreaterThanPoint(minMaxCorners.FrontBottomLeft, goCorners.BackTopRight); //if (!fbl) //{ // bool ftl = IsLesserThanPoint(minMaxCorners.FrontTopLeft, goCorners.FrontBottomLeft) && IsGreaterThanPoint(minMaxCorners.FrontTopLeft, goCorners.BackTopRight); // if (!ftl) // { // bool ftr = IsLesserThanPoint(minMaxCorners.FrontTopRight, goCorners.FrontBottomLeft) && IsGreaterThanPoint(minMaxCorners.FrontTopRight, goCorners.BackTopRight); // if (!ftr) // { // bool fbr = IsLesserThanPoint(minMaxCorners.FrontBottomRight, goCorners.FrontBottomLeft) && IsGreaterThanPoint(minMaxCorners.FrontBottomRight, goCorners.BackTopRight); // if (!fbr) // { // bool bbl = IsLesserThanPoint(minMaxCorners.BackBottomLeft, goCorners.FrontBottomLeft) && IsGreaterThanPoint(minMaxCorners.BackBottomLeft, goCorners.BackTopRight); // if (!bbl) // { // bool btl = IsLesserThanPoint(minMaxCorners.BackTopLeft, goCorners.FrontBottomLeft) && IsGreaterThanPoint(minMaxCorners.BackTopLeft, goCorners.BackTopRight); // if (!btl) // { // bool btr = IsLesserThanPoint(minMaxCorners.BackTopRight, goCorners.FrontBottomLeft) && IsGreaterThanPoint(minMaxCorners.BackTopRight, goCorners.BackTopRight); // if (!btr) // { // bool bbr = IsLesserThanPoint(minMaxCorners.BackBottomRight, goCorners.FrontBottomLeft) && IsGreaterThanPoint(minMaxCorners.BackBottomRight, goCorners.BackTopRight); // if (!bbr) // { // MinMaxBBCornerInMeshBB = false; // } // } // } // } // } // } // } //} bool MinMaxBBCornerInMeshBB = true; fbl = CornersContains(spaceCorners.FrontBottomLeft, goCorners); if (!fbl) { bool ftl = CornersContains(spaceCorners.FrontTopLeft, goCorners); if (!ftl) { bool ftr = CornersContains(spaceCorners.FrontTopRight, goCorners); if (!ftr) { bool fbr = CornersContains(spaceCorners.FrontBottomRight, goCorners); if (!fbr) { bool bbl = CornersContains(spaceCorners.BackBottomLeft, goCorners); if (!bbl) { bool btl = CornersContains(spaceCorners.BackTopLeft, goCorners); if (!btl) { bool btr = CornersContains(spaceCorners.BackTopRight, goCorners); if (!btr) { bool bbr = CornersContains(spaceCorners.BackBottomRight, goCorners); if (!bbr) { MinMaxBBCornerInMeshBB = false; } } } } } } } } if (MinMaxBBCornerInMeshBB) { return(true); } // Calculate if an edge of the mesh bounding box exists in the minmax bounding box // or vice versa bool edgeBoxIntersect = AnyEdgeBoxIntersect(goCorners, spaceCorners); if (edgeBoxIntersect) { return(true); } return(false); }