Esempio n. 1
0
        public HashSet <uint> BuildRegionHashSet(out string errMsg)
        {
            var bezierCurve = _map.GetBezierCurve(_region.BezierCurveId);

            if (bezierCurve == null)
            {
                errMsg = "RegionBuilder.BuildRegionHashSet: bezierCurve is null! BezierCurveId: " + _region.BezierCurveId;
                return(null);
            }
            if (!bezierCurve.Closed)
            {
                errMsg = "RegionBuilder.BuildRegionHashSet: bezierCurve is not Closed! BezierCurveId: " + _region.BezierCurveId;
                return(null);
            }

            errMsg = "";

            float   fInvGridSize = 1.0f / _mapGridSize;
            Vector2 mins         = _mapRect.max;
            Vector2 maxs         = _mapRect.min;

            _polygon.Clear();
            foreach (var point in bezierCurve.BezierNodes)
            {
                Vector2 v = new Vector2(point.PositionX, point.PositionZ);
                _polygon.Add(v);

                if (v.x < mins.x)
                {
                    mins.x = v.x;
                }

                if (v.y < mins.y)
                {
                    mins.y = v.y;
                }

                if (v.x > maxs.x)
                {
                    maxs.x = v.x;
                }

                if (v.y > maxs.y)
                {
                    maxs.y = v.y;
                }
            }

            Rect rcRegion = new Rect(mins, maxs - mins);

            int            nWidth    = (int)(_mapXLen * fInvGridSize);
            int            nHeight   = (int)(_mapZLen * fInvGridSize);
            HashSet <uint> regionSet = new HashSet <uint>();
            Vector2        vOrg      = new Vector2(_mapRect.xMin + 0.5f * _mapGridSize, _mapRect.yMax - 0.5f * _mapGridSize);

            for (int h = 0; h < nHeight; ++h)
            {
                for (int w = 0; w < nWidth; ++w)
                {
                    Vector2 vPos = vOrg + new Vector2(w * _mapGridSize, -h * _mapGridSize);
                    if (!RegionUtility.IsPointInRect(vPos, rcRegion))     //快速判断点是否在region的rect内
                    {
                        continue;
                    }

                    if (PolygonFunc.IsInPolygon2(vPos.x, vPos.y, _polygon))
                    {
                        uint col  = (uint)w;
                        uint row  = (uint)h;
                        uint data = (col | row << 16);
                        regionSet.Add(data);
                    }
                }
            }

            return(regionSet);
        }
Esempio n. 2
0
        public HashSet <uint> BuildRegionHashSet(out string errMsg)
        {
            if (_template == null)
            {
                errMsg = "ObstacleBuilder.BuildRegionHashSet: _template is null! ObstacleId: " + _obstacle.EntityInfos[0].EntityId;
                return(null);
            }

            errMsg = "";

            //欧拉角到方向
            var     quaternion = Quaternion.Euler(_obstacle.RotationX, _obstacle.RotationY, _obstacle.RotationZ);
            Vector3 vDir       = quaternion * Vector3.forward;

            vDir.y = 0;
            vDir.Normalize();
            Vector3 vCenter = new Vector3(_obstacle.PositionX, _obstacle.PositionY, _obstacle.PositionZ);

            Vector3 ExtX;
            Vector3 ExtY;
            Vector3 ExtZ;
            Vector3 vExtent = new Vector3(_obstacle.Length * 0.5f, _template.Height * 0.5f, _template.Width * 0.5f);

            Matrix4x4 mat;

            RegionUtility.a3d_TransformMatrix(vDir, Vector3.up, vCenter, out mat);
            ExtX = new Vector3(mat.m00, mat.m10, mat.m20) * vExtent.x;
            ExtY = new Vector3(mat.m01, mat.m11, mat.m21) * vExtent.y;
            ExtZ = new Vector3(mat.m02, mat.m12, mat.m22) * vExtent.z;

            float   fInvGridSize = 1.0f / _mapGridSize;
            Vector2 mins         = _mapRect.max;
            Vector2 maxs         = _mapRect.min;

            Vector3[] points = new Vector3[4];
            points[0] = vCenter - ExtX + ExtY + ExtZ;
            points[1] = points[0] + 2 * ExtX;
            points[2] = points[1] - 2 * ExtZ;
            points[3] = points[2] - 2 * ExtX;

            _polygon.Clear();
            foreach (var point in points)
            {
                Vector2 v = new Vector2(point.x, point.z);
                _polygon.Add(v);

                if (v.x < mins.x)
                {
                    mins.x = v.x;
                }

                if (v.y < mins.y)
                {
                    mins.y = v.y;
                }

                if (v.x > maxs.x)
                {
                    maxs.x = v.x;
                }

                if (v.y > maxs.y)
                {
                    maxs.y = v.y;
                }
            }
            Rect rcRegion = new Rect(mins, maxs - mins);

            int            nWidth    = (int)(_mapXLen * fInvGridSize);
            int            nHeight   = (int)(_mapZLen * fInvGridSize);
            HashSet <uint> regionSet = new HashSet <uint>();
            Vector2        vOrg      = new Vector2(_mapRect.xMin + 0.5f * _mapGridSize, _mapRect.yMax - 0.5f * _mapGridSize);

            for (int h = 0; h < nHeight; ++h)
            {
                for (int w = 0; w < nWidth; ++w)
                {
                    Vector2 vPos = vOrg + new Vector2(w * _mapGridSize, -h * _mapGridSize);
                    if (!RegionUtility.IsPointInRect(vPos, rcRegion))     //快速判断点是否在region的rect内
                    {
                        continue;
                    }

                    if (PolygonFunc.IsInPolygon2(vPos.x, vPos.y, _polygon))
                    {
                        uint col  = (uint)w;
                        uint row  = (uint)h;
                        uint data = (col | row << 16);
                        regionSet.Add(data);
                    }
                }
            }

            if (regionSet.Count == 0)
            {
                errMsg = HobaText.Format("ObstacleBuilder.BuildRegionHashSet: RegionSet Count is zero! ObstacleId: {0}, EntityGeneratorId: {1} generator len: {2}, template wid: {3}, template hei: {4}",
                                         _obstacle.EntityInfos[0].EntityId, _obstacle.Id, _obstacle.Length, _template.Width, _template.Height);
            }

            return(regionSet);
        }