public void Initialize_AutoFitBox() { DMeshSO TargetMeshSO = TargetSO as DMeshSO; // initialize w/ auto-fit box DMesh3 mesh = TargetMeshSO.Mesh; DMeshAABBTree3 spatial = TargetMeshSO.Spatial; meshBounds = mesh.CachedBounds; create_preview_so(); ContOrientedBox3 boxFitter = new ContOrientedBox3( new RemapItr <Vector3d, int>(mesh.TriangleIndices(), (tid) => { return(mesh.GetTriCentroid(tid)); }), new RemapItr <double, int>(mesh.TriangleIndices(), (tid) => { return(mesh.GetTriArea(tid)); })); //DebugUtil.EmitDebugBox("fitbox", boxFitter.Box, Colorf.Red, TargetSO.RootGameObject, false); Box3d fitBox = boxFitter.Box; int longest = 0; if (fitBox.Extent.y > fitBox.Extent.x) { longest = 1; } if (fitBox.Extent.z > fitBox.Extent[longest]) { longest = 2; } Vector3d vTop = fitBox.Center + fitBox.Extent[longest] * fitBox.Axis(longest); Vector3d vBottom = fitBox.Center - fitBox.Extent[longest] * fitBox.Axis(longest); int base_tid = spatial.FindNearestTriangle(vBottom); int top_tid = spatial.FindNearestTriangle(vTop); if (vTop.y < vBottom.y) { int tmp = base_tid; base_tid = top_tid; top_tid = tmp; } Vector3d vBasePt = mesh.GetTriCentroid(base_tid); Vector3d vTopPt = mesh.GetTriCentroid(top_tid); int other1 = (longest + 1) % 3, other2 = (longest + 2) % 3; int front_tid = spatial.FindNearestHitTriangle(new Ray3d(fitBox.Center, fitBox.Axis(other1))); Vector3d vFrontPt = mesh.GetTriCentroid(front_tid); int back_tid = spatial.FindNearestHitTriangle(new Ray3d(fitBox.Center, -fitBox.Axis(other1))); Vector3d vBackPt = mesh.GetTriCentroid(back_tid); int right_tid = spatial.FindNearestHitTriangle(new Ray3d(fitBox.Center, fitBox.Axis(other2))); Vector3d vRightPt = mesh.GetTriCentroid(right_tid); int left_tid = spatial.FindNearestHitTriangle(new Ray3d(fitBox.Center, -fitBox.Axis(other2))); Vector3d vLeftPt = mesh.GetTriCentroid(left_tid); initialFrontPt = (Vector3f)vFrontPt; SetPointPosition_Internal(BasePointID, MeshQueries.SurfaceFrame(mesh, base_tid, vBasePt), CoordSpace.ObjectCoords); SetPointPosition_Internal(FrontPointID, MeshQueries.SurfaceFrame(mesh, front_tid, vFrontPt), CoordSpace.ObjectCoords); SetPointPosition(TopPointID, MeshQueries.SurfaceFrame(mesh, top_tid, vTopPt), CoordSpace.ObjectCoords); }
private void OnDrawGizmos() { // First wehave to convert the Unity Vector3 array // into the g3 type g3.Vector3d var points3d = new Vector3d[transforms.Length]; for (var i = 0; i < transforms.Length; i++) { // Thanks to the g3 library implictely casted from UnityEngine.Vector3 to g3.Vector3d points3d[i] = transforms[i].position; } // BOOM MAGIC!!! var orientedBoundingBox = new ContOrientedBox3(points3d); // Now just convert the information back to Unity Vector3 positions and axis // Since g3.Vector3d uses doubles but Unity Vector3 uses floats // we have to explicitly cast to Vector3 var center = (Vector3)orientedBoundingBox.Box.Center; var axisX = (Vector3)orientedBoundingBox.Box.AxisX; var axisY = (Vector3)orientedBoundingBox.Box.AxisY; var axisZ = (Vector3)orientedBoundingBox.Box.AxisZ; var extends = (Vector3)orientedBoundingBox.Box.Extent; // Now we can simply calculate our 8 vertices of the bounding box var A = center - extends.z * axisZ - extends.x * axisX - axisY * extends.y; var B = center - extends.z * axisZ + extends.x * axisX - axisY * extends.y; var C = center - extends.z * axisZ + extends.x * axisX + axisY * extends.y; var D = center - extends.z * axisZ - extends.x * axisX + axisY * extends.y; var E = center + extends.z * axisZ - extends.x * axisX - axisY * extends.y; var F = center + extends.z * axisZ + extends.x * axisX - axisY * extends.y; var G = center + extends.z * axisZ + extends.x * axisX + axisY * extends.y; var H = center + extends.z * axisZ - extends.x * axisX + axisY * extends.y; // And finally visualize it Gizmos.DrawLine(A, B); Gizmos.DrawLine(B, C); Gizmos.DrawLine(C, D); Gizmos.DrawLine(D, A); Gizmos.DrawLine(E, F); Gizmos.DrawLine(F, G); Gizmos.DrawLine(G, H); Gizmos.DrawLine(H, E); Gizmos.DrawLine(A, E); Gizmos.DrawLine(B, F); Gizmos.DrawLine(D, H); Gizmos.DrawLine(C, G); // And Here we ca just be amazed ;) }
private void OnDrawGizmos() { // First wehave to convert the Unity Vector3 array // into the g3 type g3.Vector3d var points3d = new Vector3d[transforms.Length]; for (var i = 0; i < transforms.Length; i++) { // Thanks to the g3 library implictely casted from UnityEngine.Vector3 to g3.Vector3d points3d[i] = transforms[i].position; } // BOOM MAGIC!!! var orientedBoundingBox = new ContOrientedBox3(points3d); // Now just convert the information back to Unity Vector3 positions and axis // Since g3.Vector3d uses doubles but Unity Vector3 uses floats // we have to explicitly cast to Vector3 var center = (Vector3)orientedBoundingBox.Box.Center; var axisX = (Vector3)orientedBoundingBox.Box.AxisX; var axisY = (Vector3)orientedBoundingBox.Box.AxisY; var axisZ = (Vector3)orientedBoundingBox.Box.AxisZ; var extends = (Vector3)orientedBoundingBox.Box.Extent; // Now we can simply calculate our 8 vertices of the bounding box var A = center - extends.z * axisZ - extends.x * axisX - axisY * extends.y; var B = center - extends.z * axisZ + extends.x * axisX - axisY * extends.y; var C = center - extends.z * axisZ + extends.x * axisX + axisY * extends.y; var D = center - extends.z * axisZ - extends.x * axisX + axisY * extends.y; var E = center + extends.z * axisZ - extends.x * axisX - axisY * extends.y; var F = center + extends.z * axisZ + extends.x * axisX - axisY * extends.y; var G = center + extends.z * axisZ + extends.x * axisX + axisY * extends.y; var H = center + extends.z * axisZ - extends.x * axisX + axisY * extends.y; colliderObject.transform.position = center; Quaternion rotation = Quaternion.LookRotation(axisZ, axisY); colliderObject.transform.rotation = rotation; /*colliderObject.transform.LookAt(center + axisX, Vector3.right); * colliderObject.transform.LookAt(center + axisY, Vector3.up); * colliderObject.transform.LookAt(center + axisZ, Vector3.forward);*/ Bounds b = new Bounds(new Vector3(0, 0, 0), extends * 2); BoxCollider bc = colliderObject.GetComponent <BoxCollider>(); bc.size = b.size; Gizmos.color = Color.red; Gizmos.DrawLine(center, center + axisX); Gizmos.color = Color.green; Gizmos.DrawLine(center, center + axisY); Gizmos.color = Color.blue; Gizmos.DrawLine(center, center + axisZ); // And finally visualize it /*Gizmos.color = Color.white; * Gizmos.DrawLine(A, B); * Gizmos.DrawLine(B, C); * Gizmos.DrawLine(C, D); * Gizmos.DrawLine(D, A); * * Gizmos.DrawLine(E, F); * Gizmos.DrawLine(F, G); * Gizmos.DrawLine(G, H); * Gizmos.DrawLine(H, E); * * Gizmos.DrawLine(A, E); * Gizmos.DrawLine(B, F); * Gizmos.DrawLine(D, H); * Gizmos.DrawLine(C, G);*/ // And Here we ca just be amazed ;) }