/// <summary> /// Finds closest edge to ray, including principal axes of the target object. /// </summary> /// <param name="ray">The ray.</param> /// <param name="triangleEdge">Triangle edge from raycast result.</param> /// <param name="principalEdgeExtension">Extension of principal axes relative to bounding box or object faces.</param> /// <returns>Edge (principal or triangle) closest to the given ray.</returns> private AGXUnity.Edge FindClosestEdgeIncludingTargetPrincipalAxes(Ray ray, AGXUnity.Edge triangleEdge, float principalEdgeExtension = 10.0f) { if (m_collectedData.Target == null) { return(new AGXUnity.Edge()); } var edges = new AGXUnity.Edge[4]; var shape = m_collectedData.Target.GetComponent <Shape>(); var shapeUtils = shape?.GetUtils(); if (shapeUtils != null) { Array.Copy(shapeUtils.GetPrincipalEdgesWorld(principalEdgeExtension), edges, 3); } else { var mesh = shape is AGXUnity.Collide.Mesh ? (shape as AGXUnity.Collide.Mesh).SourceObjects.FirstOrDefault() : m_collectedData.Target.GetComponent <MeshFilter>()?.sharedMesh; var halfExtents = 0.5f * Vector3.zero; if (mesh != null) { halfExtents = mesh.bounds.extents; } Array.Copy(ShapeUtils.ExtendAndTransformEdgesToWorld(m_collectedData.Target.transform, new AGXUnity.Edge[] { new AGXUnity.Edge() { Start = BoxShapeUtils.GetLocalFace(halfExtents, ShapeUtils.Direction.Negative_X), End = BoxShapeUtils.GetLocalFace(halfExtents, ShapeUtils.Direction.Positive_X), Normal = ShapeUtils.GetLocalFaceDirection(ShapeUtils.Direction.Positive_Y), Type = AGXUnity.Edge.EdgeType.Principal }, new AGXUnity.Edge() { Start = BoxShapeUtils.GetLocalFace(halfExtents, ShapeUtils.Direction.Negative_Y), End = BoxShapeUtils.GetLocalFace(halfExtents, ShapeUtils.Direction.Positive_Y), Normal = ShapeUtils.GetLocalFaceDirection(ShapeUtils.Direction.Positive_Z), Type = AGXUnity.Edge.EdgeType.Principal }, new AGXUnity.Edge() { Start = BoxShapeUtils.GetLocalFace(halfExtents, ShapeUtils.Direction.Negative_Z), End = BoxShapeUtils.GetLocalFace(halfExtents, ShapeUtils.Direction.Positive_Z), Normal = ShapeUtils.GetLocalFaceDirection(ShapeUtils.Direction.Positive_X), Type = AGXUnity.Edge.EdgeType.Principal } }, principalEdgeExtension), edges, 3); } edges[3] = triangleEdge; return(ShapeUtils.FindClosestEdgeToSegment(ray.origin, ray.GetPoint(5000.0f), edges).Edge); }