void ExportMask(float[] data, int size, int resolution, string name, DateTime stamp, float spread, bool isClip, bool isReadable) { //Create interpolated array from data so the mask resolution does not need to match the terrain height map size. InterpolatedArray2f heightMap = new InterpolatedArray2f(data, size, size, 1, false); //Create the path name to save mask. string fileName = string.Format("{0}-{1:yyyyMMdd-HHmmss}.png", name, stamp); string path = System.IO.Path.Combine(Application.dataPath, fileName); path = path.Replace('\\', '/'); Texture2D mask = null; //Create the mask texture. Clip masks are created slightly different. if (isClip) { mask = ShoreMaskGenerator.CreateClipMask(heightMap, resolution, resolution, m_oceanLevel + spread, TextureFormat.ARGB32); } else { mask = ShoreMaskGenerator.CreateMask(heightMap, resolution, resolution, m_oceanLevel, spread, TextureFormat.ARGB32); } //Save the texture. byte[] bytes = mask.EncodeToPNG(); System.IO.File.WriteAllBytes(path, bytes); DestroyImmediate(mask); string relativePath = "Assets/" + fileName; //Update asset data base with new mask texture. AssetDatabase.ImportAsset(relativePath, ImportAssetOptions.ForceUpdate); //Create a importer from the texture so some of its settings can be changed. AssetImporter tmp = TextureImporter.GetAtPath(relativePath); TextureImporter importer = tmp as TextureImporter; if (importer != null) { //Change some of the settings of textures. //Should always be bilinear clamped with no mipmaps. //Height and clip masks should also be readable. importer.wrapMode = TextureWrapMode.Clamp; importer.filterMode = FilterMode.Bilinear; importer.textureType = TextureImporterType.Default; importer.mipmapEnabled = false; importer.isReadable = isReadable; AssetDatabase.ImportAsset(relativePath, ImportAssetOptions.ForceUpdate); } else { string msg = string.Format("{0:MM/dd/yy H:mm:ss zzz} - Failed to modify texture settings after creation. You will need to manually adjust texture settings.", DateTime.Now); Ocean.LogInfo(msg); } }
void CreateShoreMasks() { Terrain terrain; GameObject obj = Selection.activeGameObject; //If obj is null user has not selected a object. if (obj == null) { string msg = string.Format("{0:MM/dd/yy H:mm:ss zzz} - No object was selected. Please select the terrain in the scene. The shore mask will not be created.", DateTime.Now); Ocean.LogInfo(msg); return; } terrain = obj.GetComponent <Terrain>(); //If terrain is null user has not selected a object with a terrain component. if (terrain == null) { string msg = string.Format("{0:MM/dd/yy H:mm:ss zzz} - Selected object was not a terrain. The shore mask will not be created.", DateTime.Now); Ocean.LogInfo(msg); return; } //If terrain data is null user has deleted the data at some point. if (terrain.terrainData == null) { string msg = string.Format("{0:MM/dd/yy H:mm:ss zzz} - Terrains data is null. The shore mask will not be created.", DateTime.Now); Ocean.LogInfo(msg); return; } DateTime time = DateTime.Now; //Get the height map data from the terrain. float[] data = ShoreMaskGenerator.CreateHeightMap(terrain); int resolution = terrain.terrainData.heightmapResolution; //Export each of the masks if required. if (m_exportHeightMask) { ExportMask(data, resolution, m_heightMaskResolution, terrain.name + "-HeightMask", time, m_heightMaskSpread, false, true); } if (m_exportEdgeFoam) { ExportMask(data, resolution, m_edgeFoamResolution, terrain.name + "-EdgeFoam", time, m_edgeFoamSpread, false, false); } if (m_exportFoamMask) { ExportMask(data, resolution, m_foamMaskResolution, terrain.name + "-FoamMask", time, m_foamMaskSpread, false, false); } if (m_exportNormalMask) { ExportMask(data, resolution, m_normalMaskResolution, terrain.name + "-NormalMask", time, m_normalMaskSpread, false, false); } if (m_exportClipMask) { ExportMask(data, resolution, m_clipMaskResolution, terrain.name + "-ClipMask", time, m_clipMaskOffset, true, true); } }