예제 #1
0
        private IEnumerator SetTerrainHeightmap_Coroutine(float[,] heightmap, Action onComplete)
        {
            TerrainData td             = ActiveTerrain.terrainData;
            int         maxResPerFrame = TerraConfig.Instance.Generator.CoroutineRes;
            int         hmRes          = heightmap.GetLength(0) - 1;

            if (hmRes <= maxResPerFrame)
            {
                td.SetHeights(0, 0, heightmap);

                if (onComplete != null)
                {
                    onComplete();
                }

                yield break;
            }

            int resFactor     = hmRes / maxResPerFrame;
            int subResolution = hmRes / resFactor;

            //Loop through first chunk of the resolution
            for (int ix = 0; ix < resFactor; ix++)
            {
                for (int iy = 0; iy < resFactor; iy++)
                {
                    int xPlus1 = ix == resFactor - 1 ? 1 : 0;
                    int yPlus1 = iy == resFactor - 1 ? 1 : 0;

                    float[,] subheights = new float[subResolution + yPlus1, subResolution + xPlus1];

                    //Copy heights into new subdivision array
                    for (int x = 0; x < subResolution + xPlus1; x++)
                    {
                        for (int y = 0; y < subResolution + yPlus1; y++)
                        {
                            int thisHmX = ix * subResolution + x;
                            int thisHmY = iy * subResolution + y;
                            subheights[y, x] = heightmap[thisHmY, thisHmX];
                        }
                    }

                    //Set heights for this subsection
                    td.SetHeightsDelayLOD(subResolution * ix, subResolution * iy, subheights);
                    //td.SetHeights(subResolution * ix, subResolution * iy, subheights);

                    //Wait for next frame
                    yield return(null);
                }
            }

            ActiveTerrain.ApplyDelayedHeightmapModification();

            if (onComplete != null)
            {
                onComplete();
            }
        }
예제 #2
0
        /// <summary>
        /// //todo write description
        /// </summary>
        /// <param name="neighbor"></param>
        /// <param name="incrX"></param>
        /// <param name="incrStart0"></param>
        private void SetTerrainHeightmapEdges(Tile neighbor, bool incrX, bool incrStart0)
        {
            int incrStart         = incrStart0 ? 0 : HeightmapResolution - 1;
            int neighborIncrStart = 0;
            int resDifference     = 0;

            float[,] neighborHm = null;

            if (neighbor != null)
            {
                neighborIncrStart = incrStart0 ? neighbor.MeshManager.HeightmapResolution - 1 : 0;
                resDifference     = (HeightmapResolution - 1) / (neighbor.MeshManager.HeightmapResolution - 1);
                neighborHm        = neighbor.MeshManager.Heightmap;
            }

            for (int cursor = 1; cursor < HeightmapResolution; cursor++)
            {
                if (incrX)
                {
                    float[,] height = { { Heightmap[cursor, incrStart] } };
                    ActiveTerrain.terrainData.SetHeightsDelayLOD(incrStart, cursor, height);
                }
                else
                {
                    float[,] height = { { Heightmap[incrStart, cursor] } };
                    ActiveTerrain.terrainData.SetHeightsDelayLOD(incrStart, cursor, height);
                }

                if (neighbor != null && cursor % resDifference == 0)
                {
                    int neighborStartIdx = (cursor - 1) / resDifference;

                    if (incrX)
                    {
                        float[,] height = { { neighborHm[neighborStartIdx, neighborIncrStart] } };
                        ActiveTerrain.terrainData.SetHeightsDelayLOD(neighborIncrStart, neighborStartIdx, height);
                    }
                    else
                    {
                        float[,] height = { { neighborHm[neighborIncrStart, neighborStartIdx] } };
                        ActiveTerrain.terrainData.SetHeightsDelayLOD(neighborStartIdx, neighborIncrStart, height);
                    }
                }
            }

            ActiveTerrain.ApplyDelayedHeightmapModification();

            if (neighbor != null)
            {
                neighbor.MeshManager.ActiveTerrain.ApplyDelayedHeightmapModification();
            }
        }