/// <summary>
        /// Create the mask and put it in the root assets directory
        /// </summary>
        public void CreateMask()
        {
            // Check to see if we have an active terrain
            if (Terrain.activeTerrain == null)
            {
                Debug.LogError("You need an active terrain!");
                return;
            }

            // Get the terrain
            UnityHeightMap hm = new UnityHeightMap(Terrain.activeTerrain);

            // Create a heightmap for the mask
            HeightMap maskHm = new HeightMap(hm.Width(), hm.Depth());

            //Iterate though our terrain, at the resolution of the heightmap and pick up the height
            float testHeight;
            float nrmSeaLevel = m_seaLevel / Terrain.activeTerrain.terrainData.size.y;

            for (int x = 0; x < hm.Width(); x++)
            {
                for (int z = 0; z < hm.Depth(); z++)
                {
                    testHeight = hm[x, z];
                    if (testHeight < nrmSeaLevel)
                    {
                        continue;
                    }
                    else if (Gaia.GaiaUtils.Math_ApproximatelyEqual(testHeight, nrmSeaLevel))
                    {
                        //We have a shoreline, lets mask it
                        maskHm[x, z] = m_aboveWaterStrength;
                        MakeMask(x, z, nrmSeaLevel, m_maskSize, hm, maskHm);
                    }
                    else
                    {
                        maskHm[x, z] = m_aboveWaterStrength;
                    }
                }
            }

            maskHm.Flip();
            //maskHm.Smooth(1);


            string path = "Assets/GaiaMasks/";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            path = Path.Combine(path, GaiaCommon1.Utils.FixFileName(string.Format("TerrainShoreLineMask-{0:yyyyMMdd-HHmmss}", DateTime.Now)));
            Gaia.GaiaUtils.CompressToSingleChannelFileImage(maskHm.Heights(), path, TextureFormat.RGBA32, false, true);

            EditorUtility.DisplayDialog("Shoreline Masker", "Your shorline mask has been created at " + path, "OK");
        }
Exemple #2
0
        /// <summary>
        /// Load the terrain provided
        /// </summary>
        /// <param name="texture">Terrain to load</param>
        public void LoadTerain(Terrain terrain)
        {
            //Check not null
            if (terrain == null)
            {
                Debug.LogError("Must supply a valid terrain! Terrain load aborted.");
                return;
            }

            //Clear out the old
            Reset();

            //Load up the new
            m_featureName = terrain.name;

            m_scanMap = new UnityHeightMap(terrain);
            if (m_scanMap.HasData() == false)
            {
                Debug.LogError("Unable to load terrain file. Terrain load aborted.");
                return;
            }

            m_scanMap.Flip(); //Undo unity terrain shenannigans

            m_scanWidth      = m_scanMap.Width();
            m_scanDepth      = m_scanMap.Depth();
            m_scanHeight     = (int)terrain.terrainData.size.y;
            m_scanResolution = 0.1f;
            m_scanBounds     = new Bounds(transform.position, new Vector3(m_scanWidth * m_scanResolution, m_scanWidth * m_scanResolution * 0.4f, m_scanDepth * m_scanResolution));
            m_baseLevel      = m_scanMap.GetBaseLevel();

            MeshFilter mf = GetComponent <MeshFilter>();

            if (mf == null)
            {
                mf           = gameObject.AddComponent <MeshFilter>();
                mf.hideFlags = HideFlags.HideInInspector;
            }
            MeshRenderer mr = GetComponent <MeshRenderer>();

            if (mr == null)
            {
                mr           = gameObject.AddComponent <MeshRenderer>();
                mr.hideFlags = HideFlags.HideInInspector;
            }
            mf.mesh = Gaia.Utils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size);
            if (m_previewMaterial != null)
            {
                m_previewMaterial.hideFlags = HideFlags.HideInInspector;
                mr.sharedMaterial           = m_previewMaterial;
            }
        }
Exemple #3
0
        /// <summary>
        /// Load the terrain provided
        /// </summary>
        /// <param name="texture">Terrain to load</param>
        public void LoadTerain(Terrain terrain)
        {
            //Check not null
            if (terrain == null)
            {
                Debug.LogError("Must supply a valid terrain! Terrain load aborted.");
                return;
            }

            //Clear out the old
            ResetData();

            m_scanMap = new UnityHeightMap(terrain);
            if (m_scanMap.HasData() == false)
            {
                Debug.LogError("Unable to load terrain file. Terrain load aborted.");
                return;
            }

            m_scanMap.Flip(); //Undo unity terrain shenannigans

            m_scanWidth      = m_scanMap.Width();
            m_scanDepth      = m_scanMap.Depth();
            m_scanHeight     = (int)terrain.terrainData.size.y;
            m_scanResolution = 0.1f;
            //m_scanBounds = new Bounds(GetPosition(gameObject), new Vector3(m_scanWidth * m_scanResolution, m_scanWidth * m_scanResolution, m_scanDepth * m_scanResolution));
            m_scanBounds = new Bounds(GetPosition(gameObject, terrain, true), new Vector3(terrain.terrainData.size.x, terrain.terrainData.size.y, terrain.terrainData.size.z));
            //m_baseLevel = m_scanMap.GetBaseLevel();

            SetOrCreateMeshComponents();
            m_meshFilter.sharedMesh = GaiaUtils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size);
            if (m_previewMaterial != null)
            {
                m_previewMaterial.hideFlags   = HideFlags.HideInInspector;
                m_meshRenderer.sharedMaterial = m_previewMaterial;
            }

            gameObject.transform.position = m_scanBounds.center;
            m_exportFileName = terrain.name;
            m_boundsSet      = true;
        }
Exemple #4
0
        /// <summary>
        /// Create a waterflow map from the terrain
        /// </summary>
        /// <param name="terrain">Terrain to create the waterflow map from</param>
        public void CreateWaterFlowMap(Terrain terrain)
        {
            m_heightMap = new UnityHeightMap(terrain);
            int width = m_heightMap.Width();
            int depth = m_heightMap.Depth();

            m_waterFlowMap = new HeightMap(width, depth);

            //Avoid edges for now
            for (int x = 1; x < (width - 1); x++)
            {
                for (int z = 1; z < (depth - 1); z++)
                {
                    TraceWaterFlow(x, z, width, depth);
                }
            }

            //Flip it so it matches the terrain
            m_waterFlowMap.Flip();

            //Smooth the water map
            m_waterFlowMap.Smooth(m_waterflowSmoothIterations);
        }