コード例 #1
0
        public unsafe void GetRuleBounds(ref BoundingBox request, out BoundingBox ruleBounds)
        {
            Vector3 *vertices = stackalloc Vector3[8];

            ruleBounds.Min = new Vector3(float.PositiveInfinity);
            ruleBounds.Max = new Vector3(float.NegativeInfinity);

            request.GetCornersUnsafe(vertices);

            float latitude, longitude;

            if (Vector3.Zero.IsInsideInclusive(ref request.Min, ref request.Max))
            {
                ruleBounds.Min.X = 0;
            }
            else
            {
                var clamp = Vector3.Clamp(Vector3.Zero, request.Min, request.Max);
                ruleBounds.Min.X = m_planetShape.DistanceToRatio(clamp.Length());
            }

            { // Calculate furthest point in BB
                Vector3 end;
                Vector3 c = request.Center;

                if (c.X < 0)
                {
                    end.X = request.Min.X;
                }
                else
                {
                    end.X = request.Max.X;
                }

                if (c.Y < 0)
                {
                    end.Y = request.Min.Y;
                }
                else
                {
                    end.Y = request.Max.Y;
                }

                if (c.Z < 0)
                {
                    end.Z = request.Min.Z;
                }
                else
                {
                    end.Z = request.Max.Z;
                }

                ruleBounds.Max.X = m_planetShape.DistanceToRatio(end.Length());
            }

            // If box intercepts Y axis (north south axis).
            if (request.Min.X < 0 && request.Min.Z < 0 && request.Max.X > 0 && request.Max.Z > 0)
            {
                ruleBounds.Min.Z = -1;
                ruleBounds.Max.Z = 3;

                for (int i = 0; i < 8; i++)
                {
                    float len = vertices[i].Length();
                    latitude = vertices[i].Y / len;
                    if (ruleBounds.Min.Y > latitude)
                    {
                        ruleBounds.Min.Y = latitude;
                    }
                    if (ruleBounds.Max.Y < latitude)
                    {
                        ruleBounds.Max.Y = latitude;
                    }
                }
            }
            else
            {
                for (int i = 0; i < 8; i++)
                {
                    float len = vertices[i].Length();

                    vertices[i] /= len;
                    latitude     = vertices[i].Y;

                    Vector2 lon = new Vector2(-vertices[i].X, -vertices[i].Z);
                    lon.Normalize();

                    longitude = lon.Y;
                    if (lon.X > 0)
                    {
                        longitude = 2 - longitude;
                    }

                    if (ruleBounds.Min.Y > latitude)
                    {
                        ruleBounds.Min.Y = latitude;
                    }
                    if (ruleBounds.Max.Y < latitude)
                    {
                        ruleBounds.Max.Y = latitude;
                    }

                    if (ruleBounds.Min.Z > longitude)
                    {
                        ruleBounds.Min.Z = longitude;
                    }
                    if (ruleBounds.Max.Z < longitude)
                    {
                        ruleBounds.Max.Z = longitude;
                    }
                }
            }
        }