예제 #1
0
        public static bool Raycast(Ray ray, out float t, Vector3 boxCenter, Vector3 boxSize, Quaternion boxRotation, BoxEpsilon epsilon = new BoxEpsilon())
        {
            t        = 0.0f;
            boxSize += epsilon.SizeEps;

            const float minBoxSize         = 1e-6f;
            int         invalidSizeCounter = 0;

            if (boxSize.x < minBoxSize)
            {
                ++invalidSizeCounter;
            }
            if (boxSize.y < minBoxSize)
            {
                ++invalidSizeCounter;
            }
            if (boxSize.z < minBoxSize)
            {
                ++invalidSizeCounter;
            }
            if (invalidSizeCounter > 1)
            {
                return(false);
            }

            if (invalidSizeCounter == 1)
            {
                if (boxSize.x < minBoxSize)
                {
                    Vector3 quadRight = boxRotation * Vector3.forward;
                    Vector3 quadUp    = boxRotation * Vector3.up;
                    return(QuadMath.Raycast(ray, out t, boxCenter, boxSize.z, boxSize.y, quadRight, quadUp));
                }
                else
                if (boxSize.y < minBoxSize)
                {
                    Vector3 quadRight = boxRotation * Vector3.right;
                    Vector3 quadUp    = boxRotation * Vector3.forward;
                    return(QuadMath.Raycast(ray, out t, boxCenter, boxSize.x, boxSize.z, quadRight, quadUp));
                }
                else
                {
                    Vector3 quadRight = boxRotation * Vector3.right;
                    Vector3 quadUp    = boxRotation * Vector3.up;
                    return(QuadMath.Raycast(ray, out t, boxCenter, boxSize.x, boxSize.y, quadRight, quadUp));
                }
            }

            Matrix4x4 boxMatrix = Matrix4x4.TRS(boxCenter, boxRotation, boxSize);
            Ray       modelRay  = ray.InverseTransform(boxMatrix);

            if (modelRay.direction.sqrMagnitude == 0.0f)
            {
                return(false);
            }

            Bounds unitCube = new Bounds(Vector3.zero, Vector3.one);

            if (unitCube.IntersectRay(modelRay, out t))
            {
                Vector3 intersectPt = boxMatrix.MultiplyPoint(modelRay.GetPoint(t));
                t = (intersectPt - ray.origin).magnitude;
                return(true);
            }

            return(false);
        }
예제 #2
0
        public static bool ContainsPoint(Vector3 point, Vector3 boxCenter, Vector3 boxSize, Quaternion boxRotation, BoxEpsilon epsilon = new BoxEpsilon())
        {
            boxSize += epsilon.SizeEps;
            Vector3 extents = boxSize * 0.5f;

            Vector3 right = boxRotation * Vector3.right;
            Vector3 up    = boxRotation * Vector3.up;
            Vector3 look  = boxRotation * Vector3.forward;

            point -= boxCenter;
            float dot = Vector3.Dot(right, point);

            if (dot < -extents.x || dot > extents.x)
            {
                return(false);
            }

            dot = Vector3.Dot(up, point);
            if (dot < -extents.y || dot > extents.y)
            {
                return(false);
            }

            dot = Vector3.Dot(look, point);
            return(dot >= -extents.z && dot <= extents.z);
        }
예제 #3
0
        public static bool Raycast(Ray ray, Vector3 boxCenter, Vector3 boxSize, Quaternion boxRotation, BoxEpsilon epsilon = new BoxEpsilon())
        {
            float t;

            return(Raycast(ray, out t, boxCenter, boxSize, boxRotation, epsilon));
        }