Пример #1
0
        //https://github.com/Sage-of-Mirrors/WindEditor/blob/0da4cc95cb5a013593c6c8f25baf150f46704c66/WCommon/Math/Math.cs#L89
        public static bool RayIntersectsAABB(CameraRay ray, Vector3 aabbMin, Vector3 aabbMax, out float intersectionDistance)
        {
            Vector3 t_1 = new Vector3(), t_2 = new Vector3();

            float tNear = float.MinValue;
            float tFar  = float.MaxValue;

            // Test infinite planes in each directin.
            for (int i = 0; i < 3; i++)
            {
                // Ray is parallel to planes in this direction.
                if (ray.Direction[i] == 0)
                {
                    if ((ray.Origin[i] < aabbMin[i]) || (ray.Origin[i] > aabbMax[i]))
                    {
                        // Parallel and outside of the box, thus no intersection is possible.
                        intersectionDistance = float.MinValue;
                        return(false);
                    }
                }
                else
                {
                    t_1[i] = (aabbMin[i] - ray.Origin[i]) / ray.Direction[i];
                    t_2[i] = (aabbMax[i] - ray.Origin[i]) / ray.Direction[i];

                    // Ensure T_1 holds values for intersection with near plane.
                    if (t_1[i] > t_2[i])
                    {
                        Vector3 temp = t_2;
                        t_2 = t_1;
                        t_1 = temp;
                    }

                    if (t_1[i] > tNear)
                    {
                        tNear = t_1[i];
                    }

                    if (t_2[i] < tFar)
                    {
                        tFar = t_2[i];
                    }

                    if ((tNear > tFar) || (tFar < 0))
                    {
                        intersectionDistance = float.MinValue;
                        return(false);
                    }
                }
            }

            intersectionDistance = tNear;
            return(true);
        }
Пример #2
0
        public bool RayIntersectsPlane(CameraRay ray, out float intersectDist)
        {
            float a   = Vector3.Dot(ray.Direction, Normal);
            float num = -Vector3.Dot(ray.Origin.Xyz, Normal) - Distance;

            if (Math.Abs(a) < float.Epsilon)
            {
                intersectDist = 0f;
                return(false);
            }
            intersectDist = num / a;
            return(intersectDist > 0f);
        }
Пример #3
0
        private bool CheckSelectedAxes(GLContext context, Vector2 point)
        {
            if (!UseGizmo)
            {
                return(true);
            }

            var ray = context.PointScreenRay((int)point.X, (int)point.Y);

            var position = Transform.Position;
            var rotation = Transform.Rotation;

            CameraRay localRay = new CameraRay();

            localRay.Direction = Vector3.Transform(ray.Direction, rotation.Inverted());
            localRay.Origin    = new Vector4(Vector3.Transform(ray.Origin.Xyz - position, rotation.Inverted()), localRay.Origin.W);

            List <float> results = new List <float>();

            for (int i = 0; i < AxisOjects.Count; i++)
            {
                isSelected[i] = false;

                if (GLMath.RayIntersectsAABB(localRay, AxisOjects[i].Min, AxisOjects[i].Max, out float d))
                {
                    results.Add(d);
                    isSelected[i] = true;
                }
            }

            results.Sort((x, y) => x.CompareTo(y));

            if (results.Count == 0)
            {
                return(false);
            }

            Vector3 localHitPoint = localRay.Origin.Xyz + (localRay.Direction * results[0]);
            var     m_hitPoint    = Vector3.Transform(localHitPoint, Quaternion.Identity) + Transform.Position;

            return(true);
        }
Пример #4
0
 public CameraRay PointScreenRay(int x, int y) => CameraRay.PointScreenRay(x, y, Camera);
Пример #5
0
 public CameraRay PointScreenRay() => CameraRay.PointScreenRay((int)CurrentMousePoint.X, (int)CurrentMousePoint.Y, Camera);