コード例 #1
0
        private bool TryToRenderChunk(Vector2Int _topTile, int _chunkSize, int _chunkIndex)
        {
#if UNITY_EDITOR
            mFinishTryRenderChunkNum++;
            if (mFinishTryRenderChunkNum > mMaxMayRenderChunkNum)
            {
                UnityEditor.EditorUtility.DisplayProgressBar("Render Layer " + mLayerMeta.layerName,
                                                             string.Format("Please wait.... [{1}] {0}", mFinishTryRenderChunkNum, _chunkIndex), 1f);
            }
            else
            {
                UnityEditor.EditorUtility.DisplayProgressBar("Render Layer " + mLayerMeta.layerName,
                                                             string.Format("Please wait.... [{2}] {0}/{1}", mFinishTryRenderChunkNum, mMaxMayRenderChunkNum, _chunkIndex),
                                                             (float)mFinishTryRenderChunkNum / mMaxMayRenderChunkNum);
            }
#endif
            var renderTileList = new List <ITU_RenderTileData>();
            var lineFistTile   = _topTile;

            var lineTileNum = 1;
            for (var i = 0; i < _chunkSize; i++)
            {
                TryToPushLineRenderTileData(lineFistTile, lineTileNum, renderTileList);
                ITU_MathUtils.GetNowTileLeftBottomTileWithStep(lineFistTile.x, lineFistTile.y, 1, mMapMeta.IsIsoStaggered(), out lineFistTile);
                lineTileNum++;
            }

            //For循环最后一次时候向左下移动了一格,所以进入底部渲染时候需要先向右侧移动一格
            ITU_MathUtils.GetNowTileRightTileWithStep(lineFistTile.x, lineFistTile.y, 1, mMapMeta.IsIsoStaggered(), out lineFistTile);

            lineTileNum = _chunkSize - 1;
            for (var i = 0; i < _chunkSize - 1; i++)
            {
                TryToPushLineRenderTileData(lineFistTile, lineTileNum, renderTileList);
                ITU_MathUtils.GetNowTileRightBottomTileWithStep(lineFistTile.x, lineFistTile.y, 1, mMapMeta.IsIsoStaggered(), out lineFistTile);
                lineTileNum--;
            }

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

            var chunkGo = new GameObject("Chunk_" + _chunkIndex);
            chunkGo.transform.parent        = mLayerRootGo.transform;
            chunkGo.transform.localPosition = Vector3.zero;
            DoRenderChunk(chunkGo, renderTileList);
            return(true);
        }
コード例 #2
0
        private void DoRenderLayer()
        {
            //以最小点向左上移动两个Chunk大小区域为起点,逐行扫描每个Chunk,直到Chunk的TopTile 大于MaxTile为止
            //这样进行Chunk渲染保证了正确的渲染顺序,右压左,下压上.(每个Chunk之内的Tile渲染也需要按此顺序)

            var tileWidthInUnit  = (float)mMapMeta.tileWidthInPixel / mRenderSetting.pixelsPreUnit;
            var tileHeightInUnit = (float)mMapMeta.tileHeightInPixel / mRenderSetting.pixelsPreUnit;


            var chunkIndex  = 0;
            var renderStep  = mRenderSetting.renderChunkSize;
            var isStaggered = mMapMeta.IsIsoStaggered();

            Vector2Int startPos;

            ITU_MathUtils.World2Tile(mLeftTopCornerBound.x, mLeftTopCornerBound.y, tileWidthInUnit, tileHeightInUnit, mMapMeta.IsIsoStaggered(), out startPos);
            //IsometricMathUtils.GetNowTileLeftTopTileWithStep(startPos.x, startPos.y, renderStep * 2, isStaggered, out startPos);
            var isTurnLeft = true;

            var lineFistTile = startPos;
            var nowPointTile = lineFistTile;

            while (true)
            {
                //渲染一行
                while (true)
                {
                    //nowPointTile默认指向的是当前渲染Chunk的TopTile
                    //此Tile向左下方移动chunkSize大小则为当前Chunk的最左侧Tile
                    Vector2Int chunkMinLeftTile;
                    ITU_MathUtils.GetNowTileLeftBottomTileWithStep(nowPointTile.x, nowPointTile.y, renderStep, isStaggered, out chunkMinLeftTile);
                    Vector2 chunkCheckPos;
                    ITU_MathUtils.TileTopCorner2World(chunkMinLeftTile.x, chunkMinLeftTile.y, tileWidthInUnit, tileHeightInUnit, isStaggered, out chunkCheckPos);
                    //应该是上角点减去 tileWidthInUnit/2即可.此处只是为了容错.
                    if (chunkCheckPos.x - tileWidthInUnit > mRightBottomCornerBound.x)
                    {
                        break;
                    }

                    var isDrawChunk = TryToRenderChunk(nowPointTile, mRenderSetting.renderChunkSize, chunkIndex);
                    chunkIndex = isDrawChunk ? chunkIndex + 1 : chunkIndex;
                    ITU_MathUtils.GetNowTileRightTileWithStep(nowPointTile.x, nowPointTile.y, renderStep, isStaggered, out nowPointTile);
                }

                //移动到下一行
                if (isTurnLeft)
                {
                    ITU_MathUtils.GetNowTileLeftBottomTileWithStep(lineFistTile.x, lineFistTile.y, renderStep, isStaggered, out lineFistTile);
                }
                else
                {
                    ITU_MathUtils.GetNowTileRightBottomTileWithStep(lineFistTile.x, lineFistTile.y, renderStep, isStaggered, out lineFistTile);
                }

                nowPointTile = lineFistTile;
                isTurnLeft   = !isTurnLeft;

                Vector2 bottomCheckPos;
                ITU_MathUtils.TileTopCorner2World(nowPointTile.x, nowPointTile.y, tileWidthInUnit, tileHeightInUnit, isStaggered, out bottomCheckPos);

                //Unity坐标是反向的,所以要反向检测
                if (bottomCheckPos.y < mRightBottomCornerBound.y)
                {
                    break;
                }
            }
        }