Ejemplo n.º 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();
            }
        }
Ejemplo n.º 2
0
 public float SampleHeight(Vector3 point)
 {
     if (ActiveTerrain == null)
     {
         return(0);
     }
     if (ActiveTerrain.terrainData == null)
     {
         return(0);
     }
     return(ActiveTerrain.SampleHeight(point) + ActiveTerrain.transform.position.y);
 }
Ejemplo n.º 3
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();
            }
        }
Ejemplo n.º 4
0
        private void Enable()
        {
            m_editor.ActiveWindowChanged        += OnActiveWindowChanged;
            m_editor.Selection.SelectionChanged += OnSelectionChanged;
            m_interpolator = new CachedBicubicInterpolator();

            TerrainData data = ActiveTerrainData;

            m_additiveHeights     = data.GetHeights(0, 0, data.heightmapWidth, data.heightmapHeight);
            m_interpolatedHeights = new float[data.heightmapHeight, data.heightmapWidth];

            const float preferredSpace = 10;

            m_state = ActiveTerrain.GetComponent <TerrainToolState>();
            if (m_state == null || m_state.HeightMap.Length != data.heightmapWidth * data.heightmapHeight)
            {
                m_state = ActiveTerrain.gameObject.GetComponent <TerrainToolState>();
                if (m_state == null)
                {
                    m_state = ActiveTerrain.gameObject.AddComponent <TerrainToolState>();
                }

                m_state.ZSize = ActiveTerrainData.size.z;
                m_state.XSize = ActiveTerrainData.size.x;

                m_zCount = Mathf.Max(2, Mathf.FloorToInt(ActiveTerrainData.size.z / preferredSpace)) + 1;
                m_xCount = Mathf.Max(2, Mathf.FloorToInt(ActiveTerrainData.size.x / preferredSpace)) + 1;

                m_state.ZSpacing = m_state.ZSize / (m_zCount - 1);
                m_state.XSpacing = m_state.XSize / (m_xCount - 1);

                m_state.Grid          = new float[m_zCount * m_xCount];
                m_state.HeightMap     = new float[data.heightmapWidth * data.heightmapHeight];
                m_state.CutoutTexture = m_cutoutMaskRenderer.CreateMask(ActiveTerrainData, null);
            }
            else
            {
                m_state.ZSize = ActiveTerrainData.size.z;
                m_state.XSize = ActiveTerrainData.size.x;

                m_state.ZSpacing = m_state.ZSize / (m_zCount - 1);
                m_state.XSpacing = m_state.XSize / (m_xCount - 1);

                TryRefreshGrid();
            }

            InitHandles();
            OnActiveWindowChanged(m_editor.ActiveWindow);
            EnableZTest = EnableZTest;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Sets the <see cref="UnityEngine.Terrain"/> component's heightmap to this instances'
        /// <see cref="Heightmap"/>. If <see cref="UnityEngine.Terrain"/> hasn't been created,
        /// it is added as a component.
        /// </summary>
        /// <remarks>Since this method creates and adds a <see cref="UnityEngine.Terrain"/>
        /// component, it is not thread safe.</remarks>
        /// <param name="heightmap">Optionally use the passed heightmap instead of
        /// <see cref="TileMesh"/>'s <see cref="Heightmap"/></param>
        public void SetTerrainHeightmap(float[,] heightmap = null)
        {
            float[,] hm = heightmap ?? Heightmap;

            if (hm == null)
            {
                return;
            }
            if (ActiveTerrain == null)
            {
                AddTerrainComponent();
            }

            //ReSharper disable once PossibleNullReferenceException
            TerrainData td     = ActiveTerrain.terrainData;
            TerraConfig conf   = TerraConfig.Instance;
            int         length = conf.Generator.Length;

            td.heightmapResolution = HeightmapResolution;
            td.SetHeights(0, 0, hm);
            td.size = new Vector3(length, conf.Generator.Amplitude, length);
            ActiveTerrain.Flush();
        }