示例#1
0
        public static GTABoundingBox2 ComputeBoundingBox(Entity entity)
        {
            var m = entity.Model;

            (Vector3 gmin, Vector3 gmax) = m.Dimensions;

            var bbox = new SharpDX.BoundingBox(new SharpDX.Vector3(gmin.X, gmin.Y, gmin.Z), new SharpDX.Vector3(gmax.X, gmax.Y, gmax.Z));

            var res = new GTABoundingBox2
            {
                Min = new Vector2(float.PositiveInfinity, float.PositiveInfinity),
                Max = new Vector2(float.NegativeInfinity, float.NegativeInfinity)
            };

            foreach (var corner in bbox.GetCorners())
            {
                var cornerVector = new Vector3(corner.X, corner.Y, corner.Z);

                cornerVector = entity.GetOffsetPosition(cornerVector);
                Vector2 position = HashFunctions.Convert3dTo2d(cornerVector);

                if (position.X == .1f || position.Y == .1f || position.X == .9f || position.Y == .9f)
                {
                    return(new GTABoundingBox2
                    {
                        Min = new Vector2(float.PositiveInfinity, float.PositiveInfinity),
                        Max = new Vector2(float.NegativeInfinity, float.NegativeInfinity),
                        Quality = DataQuality.Low
                    });
                }

                res = new GTABoundingBox2
                {
                    Min     = new Vector2(Math.Min(res.Min.X, position.X), Math.Min(res.Min.Y, position.Y)),
                    Max     = new Vector2(Math.Max(res.Max.X, position.X), Math.Max(res.Max.Y, position.Y)),
                    Quality = DataQuality.High
                };
            }

            if (res.Max.X == res.Min.X || res.Max.Y == res.Min.Y)
            {
                res.Quality = DataQuality.Low;
            }

            return(res);
        }