コード例 #1
0
        private void FindTerrainTilesUnlimited(bool sharedBoundaryTexel, bool fillOutsideTerrain)
        {
            // pixel rect bounds (in world space)
            float minX = originTerrain.transform.position.x + pixelSize.x * pixelRect.xMin;
            float minZ = originTerrain.transform.position.z + pixelSize.y * pixelRect.yMin;
            float maxX = originTerrain.transform.position.x + pixelSize.x * (pixelRect.xMax - 1);
            float maxZ = originTerrain.transform.position.z + pixelSize.y * (pixelRect.yMax - 1);

            m_HeightWorldSpaceMin = originTerrain.GetPosition().y;
            m_HeightWorldSpaceMax = m_HeightWorldSpaceMin + originTerrain.terrainData.size.y;

            // this filter limits the search to Terrains that overlap the pixel rect bounds
            Predicate <Terrain> filterOverlap =
                t =>
            {
                // terrain bounds (in world space)
                float tminX = t.transform.position.x;
                float tminZ = t.transform.position.z;
                float tmaxX = t.transform.position.x + t.terrainData.size.x;
                float tmaxZ = t.transform.position.z + t.terrainData.size.z;

                // test overlap
                return((tminX <= maxX) && (tmaxX >= minX) &&
                       (tminZ <= maxZ) && (tmaxZ >= minZ));
            };

            // gather Terrains that pass the filter
            TerrainUtils.TerrainMap terrainMap = TerrainUtils.TerrainMap.CreateFromConnectedNeighbors(originTerrain, filterOverlap, false);

            // convert those Terrains into the TerrainTile list
            m_TerrainTiles = new List <TerrainTile>();
            if (terrainMap != null)
            {
                foreach (var cur in terrainMap.terrainTiles)
                {
                    var     coord   = cur.Key;
                    Terrain terrain = cur.Value;

                    int     minPixelX        = coord.tileX * (targetTextureWidth - (sharedBoundaryTexel ? 1 : 0));
                    int     minPixelZ        = coord.tileZ * (targetTextureHeight - (sharedBoundaryTexel ? 1 : 0));
                    RectInt terrainPixelRect = new RectInt(minPixelX, minPixelZ, targetTextureWidth, targetTextureHeight);
                    if (pixelRect.Overlaps(terrainPixelRect))
                    {
                        // EdgePad fills empty regions outside terrains in PaintContext.
                        int edgePad = fillOutsideTerrain ? Mathf.Max(targetTextureWidth, targetTextureHeight) : 0;
                        m_TerrainTiles.Add(
                            TerrainTile.Make(
                                terrain,
                                minPixelX,
                                minPixelZ,
                                pixelRect,
                                targetTextureWidth,
                                targetTextureHeight,
                                edgePad));
                        m_HeightWorldSpaceMin = Mathf.Min(m_HeightWorldSpaceMin, terrain.GetPosition().y);
                        m_HeightWorldSpaceMax = Mathf.Max(m_HeightWorldSpaceMax, terrain.GetPosition().y + terrain.terrainData.size.y);
                    }
                }
            }
        }
コード例 #2
0
        private void FindTerrainTilesUnlimited()
        {
            // pixel rect bounds (in world space)
            float minX = originTerrain.transform.position.x + pixelSize.x * pixelRect.xMin;
            float minZ = originTerrain.transform.position.z + pixelSize.y * pixelRect.yMin;
            float maxX = originTerrain.transform.position.x + pixelSize.x * (pixelRect.xMax - 1);
            float maxZ = originTerrain.transform.position.z + pixelSize.y * (pixelRect.yMax - 1);

            // this filter limits the search to Terrains that overlap the pixel rect bounds
            TerrainUtility.TerrainMap.TerrainFilter filterOverlap =
                t =>
            {
                // terrain bounds (in world space)
                float tminX = t.transform.position.x;
                float tminZ = t.transform.position.z;
                float tmaxX = t.transform.position.x + t.terrainData.size.x;
                float tmaxZ = t.transform.position.z + t.terrainData.size.z;

                // test overlap
                return((tminX <= maxX) && (tmaxX >= minX) &&
                       (tminZ <= maxZ) && (tmaxZ >= minZ));
            };

            // gather Terrains that pass the filter
            TerrainUtility.TerrainMap terrainMap =
                TerrainUtility.TerrainMap.CreateFromConnectedNeighbors(originTerrain, filterOverlap, false);

            // convert those Terrains into the TerrainTile list
            m_TerrainTiles = new List <TerrainTile>();
            if (terrainMap != null)
            {
                foreach (var cur in terrainMap.m_terrainTiles)
                {
                    var     coord   = cur.Key;
                    Terrain terrain = cur.Value;

                    int     minPixelX        = coord.tileX * (targetTextureWidth - 1);
                    int     minPixelZ        = coord.tileZ * (targetTextureHeight - 1);
                    RectInt terrainPixelRect = new RectInt(minPixelX, minPixelZ, targetTextureWidth, targetTextureHeight);
                    if (pixelRect.Overlaps(terrainPixelRect))
                    {
                        m_TerrainTiles.Add(
                            TerrainTile.Make(
                                terrain,
                                minPixelX,
                                minPixelZ,
                                pixelRect,
                                targetTextureWidth,
                                targetTextureHeight));
                    }
                }
            }
        }
コード例 #3
0
        internal void FindTerrainTiles()
        {
            m_TerrainTiles = new List <PaintContext.TerrainTile>();

            Terrain left   = originTerrain.leftNeighbor;
            Terrain right  = originTerrain.rightNeighbor;
            Terrain top    = originTerrain.topNeighbor;
            Terrain bottom = originTerrain.bottomNeighbor;

            bool wantLeft   = (pixelRect.x < 0);
            bool wantRight  = (pixelRect.xMax > (targetTextureWidth - 1));
            bool wantTop    = (pixelRect.yMax > (targetTextureHeight - 1));
            bool wantBottom = (pixelRect.y < 0);

            if (wantLeft && wantRight)
            {
                Debug.LogWarning("PaintContext pixelRect is too large!  It should touch a maximum of 2 Terrains horizontally.");
                wantRight = false;
            }

            if (wantTop && wantBottom)
            {
                Debug.LogWarning("PaintContext pixelRect is too large!  It should touch a maximum of 2 Terrains vertically.");
                wantBottom = false;
            }

            // add center tile
            m_TerrainTiles.Add(TerrainTile.Make(originTerrain, 0, 0, pixelRect, targetTextureWidth, targetTextureHeight));

            // add horizontal and vertical neighbors
            Terrain horiz         = null;
            Terrain vert          = null;
            Terrain cornerTerrain = null;

            int horizTileDelta = 0;     // how many tiles to move horizontally
            int vertTileDelta  = 0;     // how many tiles to move vertically

            if (wantLeft)
            {
                horizTileDelta = -1;
                horiz          = left;
            }
            else if (wantRight)
            {
                horizTileDelta = 1;
                horiz          = right;
            }

            if (wantTop)
            {
                vertTileDelta = 1;
                vert          = top;
            }
            else if (wantBottom)
            {
                vertTileDelta = -1;
                vert          = bottom;
            }

            if (horiz)
            {
                m_TerrainTiles.Add(TerrainTile.Make(horiz, horizTileDelta * (targetTextureWidth - 1), 0, pixelRect, targetTextureWidth, targetTextureHeight));

                // add corner, if we have a link
                if (wantTop && horiz.topNeighbor)
                {
                    cornerTerrain = horiz.topNeighbor;
                }
                else if (wantBottom && horiz.bottomNeighbor)
                {
                    cornerTerrain = horiz.bottomNeighbor;
                }
            }

            if (vert)
            {
                m_TerrainTiles.Add(TerrainTile.Make(vert, 0, vertTileDelta * (targetTextureHeight - 1), pixelRect, targetTextureWidth, targetTextureHeight));

                // add corner, if we have a link
                if (wantLeft && vert.leftNeighbor)
                {
                    cornerTerrain = vert.leftNeighbor;
                }
                else if (wantRight && vert.rightNeighbor)
                {
                    cornerTerrain = vert.rightNeighbor;
                }
            }

            if (cornerTerrain != null)
            {
                m_TerrainTiles.Add(TerrainTile.Make(cornerTerrain, horizTileDelta * (targetTextureWidth - 1), vertTileDelta * (targetTextureHeight - 1), pixelRect, targetTextureWidth, targetTextureHeight));
            }
        }