Ejemplo n.º 1
0
        public void AddIntersection(Ray ray, Vector3[] vertices, Matrix world, object obj)
        {
            var localRay = ray.Transform(Matrix.Invert(world));

            SlimDX.Vector3 v0;
            SlimDX.Vector3 v1;
            SlimDX.Vector3 v2;
            SlimDX.Vector3 normal;
            var            dist = MeshRaycaster.RaycastMeshPart(vertices.Select(v => v.dx()).ToArray(), localRay.dx(), out v0, out v1, out v2, out normal);

            if (!dist.HasValue)
            {
                return;
            }
            var point = Vector3.TransformCoordinate(localRay.GetPoint(dist.Value), world);

            cache.Set(Vector3.Distance(ray.Position, point), obj);
            if (!cache.IsCloser(closest))
            {
                return;
            }
            closest.Set(cache.Distance, cache.Object);
            closest.V1        = Vector3.TransformCoordinate(v0, world).dx();
            closest.V2        = Vector3.TransformCoordinate(v1, world).dx();
            closest.V3        = Vector3.TransformCoordinate(v2, world).dx();
            closest.HitNormal = Vector3.TransformNormal(normal, world);
        }
Ejemplo n.º 2
0
        public static RaycastResult Raycast <T>(this IEnumerable <T> e, Func <T, Bounds> getLocalBoundingbox, Func <T, Matrix4x4> getTransform, Ray ray) where T : class
        {
            var closest   = new RaycastResult();
            var newResult = new RaycastResult();

            //IWorldSelectableProvider closestProvider = null;
            //Selectable closestSelectable = null;

            foreach (var s in e)
            {
                var   transformation = getTransform(s);
                var   localRay       = ray.Transform(Matrix4x4.Inverse(transformation));
                float dist;
                if (getLocalBoundingbox(s).IntersectRay(localRay, out dist))
                {
                    var localPoint = localRay.GetPoint(dist);
                    var point      = transformation.MultiplyPoint3x4(localPoint);
                    dist = Vector3.Distance(ray.origin, point);
                }

                newResult.Set(dist, s);

                if (newResult.IsCloser(closest))
                {
                    newResult.CopyTo(closest);
                }
            }

            return(closest);
        }
        public static RaycastResult Raycast <T>(this IEnumerable <T> e, Func <T, BoundingBox> getLocalBoundingbox, Func <T, Matrix> getTransform, Ray ray) where T : class
        {
            var closest   = new RaycastResult();
            var newResult = new RaycastResult();

            //IWorldSelectableProvider closestProvider = null;
            //Selectable closestSelectable = null;

            foreach (var s in e)
            {
                var transformation = getTransform(s);
                var localRay       = ray.Transform(Matrix.Invert(transformation));
                var dist           = localRay.xna().Intersects(getLocalBoundingbox(s).xna());
                if (dist != null)
                {
                    var localPoint = localRay.GetPoint(dist.Value);
                    var point      = Vector3.TransformCoordinate(localPoint, transformation);
                    dist = Vector3.Distance(ray.Position, point);
                }

                newResult.Set(dist, s);

                if (newResult.IsCloser(closest))
                {
                    newResult.CopyTo(closest);
                }
            }

            return(closest);
        }
Ejemplo n.º 4
0
        public static RaycastResult RaycastDetail <T>(this IEnumerable <T> e, Func <T, Ray, float?> intersect, Ray ray) where T : class
        {
            var closest   = new RaycastResult();
            var newResult = new RaycastResult();

            //IWorldSelectableProvider closestProvider = null;
            //Selectable closestSelectable = null;

            foreach (var s in e)
            {
                var dist = intersect(s, ray);

                newResult.Set(dist, s);

                if (newResult.IsCloser(closest))
                {
                    newResult.CopyTo(closest);
                }
            }

            return(closest);
        }
Ejemplo n.º 5
0
        public void UpdateTarget(Ray ray)
        {
            var closest   = new RaycastResult();
            var newResult = new RaycastResult();

            IWorldSelectableProvider closestProvider = null;
            Selectable closestSelectable             = null;

            foreach (var p in providers)
            {
                foreach (var s in p.GetSelectables())
                {
                    newResult = s.Intersects(ray);
                    if (newResult.IsCloser(closest))
                    {
                        newResult.CopyTo(closest);
                        closestProvider   = p;
                        closestSelectable = s;
                    }
                }
            }
            setTargeted(closestProvider, closestSelectable);
        }