Exemplo n.º 1
0
        private void OnDrawGizmos()
        {
            Gizmos.color  = Color.green;
            Gizmos.matrix = transform.localToWorldMatrix;
            Gizmos.DrawLine(Triangle[0], Triangle[1]);
            Gizmos.DrawLine(Triangle[1], Triangle[2]);
            Gizmos.DrawLine(Triangle[2], Triangle[0]);
            if (m_PlaneDirectionCheck)
            {
                Gizmos_Extend.DrawArrow(Triangle.GetUVPoint(Vector2.one * .25f), Quaternion.LookRotation(Triangle.normal), .5f, .1f);
            }

            float distance = 2f;

            if (UGeometry.RayDirectedTriangleIntersect(Triangle, m_Ray, m_RayDirectionCheck, m_PlaneDirectionCheck, out float rayDistance))
            {
                distance     = rayDistance;
                Gizmos.color = Color.red;
                Gizmos.DrawSphere(m_Ray.GetPoint(distance), .05f);
            }
            Gizmos.color = Color.white;
            if (m_RayDirectionCheck)
            {
                Gizmos_Extend.DrawArrow(m_Ray.origin, Quaternion.LookRotation(m_Ray.direction), .5f, .1f);
            }
            Gizmos.DrawLine(m_Ray.origin, m_Ray.GetPoint(distance));
        }
Exemplo n.º 2
0
        protected static int RayDirectedTriangleIntersect(GMeshPolygon[] _polygons, Vector3[] _verticies, GRay _ray, out Vector3 hitPoint, out GDirectedTriangle hitTriangle)
        {
            collisionPoint = Vector3.zero;
            float minDistance = float.MaxValue;
            int   index       = _polygons.LastIndex(p =>
            {
                GDirectedTriangle triangle = p.GetDirectedTriangle(_verticies);
                bool intersect             = UGeometry.RayDirectedTriangleIntersect(triangle, _ray, true, true, out float distance);
                if (intersect && minDistance > distance)
                {
                    collisionTriangle = triangle;
                    collisionPoint    = _ray.GetPoint(distance);
                    minDistance       = distance;
                    return(true);
                }
                return(false);
            });

            hitPoint    = collisionPoint;
            hitTriangle = collisionTriangle;
            return(index);
        }