/// <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; } }
/// <summary> /// Load the raw file at the path given /// </summary> /// <param name="path">Full path of the raw file</param> public void LoadRawFile(string path, GaiaConstants.RawByteOrder byteOrder, ref GaiaConstants.RawBitDepth bitDepth, ref int resolution) { if (string.IsNullOrEmpty(path)) { Debug.LogError("Must supply a valid path. Raw load Aborted!"); } //Clear out the old Reset(); //Load up the new m_featureName = Path.GetFileNameWithoutExtension(path); m_scanMap = new HeightMap(); m_scanMap.LoadFromRawFile(path, byteOrder, ref bitDepth, ref resolution); if (m_scanMap.HasData() == false) { Debug.LogError("Unable to load raw file. Raw load aborted."); return; } m_scanWidth = m_scanMap.Width(); m_scanDepth = m_scanMap.Depth(); m_scanHeight = m_scanWidth / 2; 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; } }
/// <summary> /// Make a mask by iterating out from this location /// </summary> /// <param name="startX"></param> /// <param name="startZ"></param> /// <param name="waterHeight"></param> /// <param name="maskSize"></param> /// <param name="hm"></param> /// <param name="maskHm"></param> private void MakeMask(int startX, int startZ, float waterHeight, int maskSize, HeightMap hm, HeightMap maskHm) { int width = hm.Width(); int depth = hm.Depth(); int minX = startX - maskSize; int maxX = startX + maskSize; int minZ = startZ - maskSize; int maxZ = startZ + maskSize; float strength; if (minX < 0) { minX = 0; } if (maxX >= width) { maxX = width; } if (minZ < 0) { minZ = 0; } if (maxZ >= depth) { maxZ = depth; } //Make the mask if height is below sea level for (int x = minX; x < maxX; x++) { for (int z = minZ; z < maxZ; z++) { if (hm[x, z] <= waterHeight) { strength = Gaia.GaiaUtils.Math_Distance(startX, startZ, x, z) / (float)maskSize; if (strength <= 1f) { strength = 1f - strength; if (strength > maskHm[x, z]) { maskHm[x, z] = strength; } } } } } }
/// <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; }
/// <summary> /// Load the texture file provided /// </summary> /// <param name="texture">Texture file to load</param> public void LoadTextureFile(Texture2D texture) { //Check not null if (texture == null) { Debug.LogError("Must supply a valid texture! Texture load aborted."); return; } //Clear out the old ResetData(); m_scanMap = new UnityHeightMap(texture); if (m_scanMap.HasData() == false) { Debug.LogError("Unable to load Texture file. Texture load aborted."); return; } m_scanWidth = m_scanMap.Width(); m_scanDepth = m_scanMap.Depth(); m_scanHeight = m_scanWidth / 2; m_scanResolution = 0.1f; m_scanBounds = new Bounds(GetPosition(gameObject, null), new Vector3(texture.width / 2, m_scanWidth * m_scanResolution, texture.height / 2)); //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 = texture.name; m_boundsSet = true; }
/// <summary> /// Load the raw file at the path given /// </summary> /// <param name="path">Full path of the raw file</param> public void LoadRawFile(string path, GaiaConstants.RawByteOrder byteOrder, ref GaiaConstants.RawBitDepth bitDepth, ref int resolution) { if (string.IsNullOrEmpty(path)) { Debug.LogError("Must supply a valid path. Raw load Aborted!"); } //Clear out the old ResetData(); //Load up the new m_scanMap = new HeightMap(); m_scanMap.LoadFromRawFile(path, byteOrder, ref bitDepth, ref resolution); if (m_scanMap.HasData() == false) { Debug.LogError("Unable to load raw file. Raw load aborted."); return; } m_scanWidth = m_scanMap.Width(); m_scanDepth = m_scanMap.Depth(); m_scanHeight = m_scanWidth / 2; m_scanResolution = 0.1f; m_scanBounds = new Bounds(GetPosition(gameObject, null), new Vector3(m_scanWidth * m_scanResolution, m_scanWidth * m_scanResolution, m_scanDepth * m_scanResolution)); //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 = path.Substring(path.LastIndexOf('/')); m_boundsSet = true; }
/// <summary> /// Compress / encode a multi layer map file to an image /// </summary> /// <param name="input">Multi layer map in format x,y,layer</param> /// <param name="imageName">Output image name - image image index and extension will be added</param> /// <param name="exportPNG">True if a png is wanted</param> /// <param name="exportJPG">True if a jpg is wanted</param> public static void CompressToMultiChannelFileImage(string imageName, HeightMap r, HeightMap g, HeightMap b, HeightMap a, TextureFormat imageStorageFormat, GaiaConstants.ImageFileType imageFileType) { int width = 0; int height = 0; if (r != null) { width = r.Width(); height = r.Depth(); } else if (g != null) { width = g.Width(); height = g.Depth(); } else if (b != null) { width = b.Width(); height = b.Depth(); } else if (a != null) { width = a.Width(); height = a.Depth(); } if (string.IsNullOrEmpty(imageName)) { Debug.LogError("Cannot write image - no name supplied!"); return; } if (width == 0 || height == 0) { Debug.LogError("Cannot write image - invalid dimensions : " + width + ", " + height); return; } Texture2D exportTexture = new Texture2D(width, height, imageStorageFormat, true, false); Color pixelColor = new Color(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { pixelColor.r = r != null ? r[x, y] : 0f; pixelColor.g = g != null ? g[x, y] : 0f; pixelColor.b = b != null ? b[x, y] : 0f; pixelColor.a = a != null ? a[x, y] : 1f; exportTexture.SetPixel(x, y, pixelColor); } } exportTexture.Apply(); #if UNITY_2017_1_OR_NEWER switch (imageFileType) { case GaiaConstants.ImageFileType.Jpg: byte[] jpgBytes = ImageConversion.EncodeToJPG(exportTexture, 100); GaiaCommon1.Utils.WriteAllBytes(imageName + ".jpg", jpgBytes); break; case GaiaConstants.ImageFileType.Png: byte[] pngBytes = ImageConversion.EncodeToPNG(exportTexture); GaiaCommon1.Utils.WriteAllBytes(imageName + ".png", pngBytes); break; case GaiaConstants.ImageFileType.Exr: byte[] exrBytes = ImageConversion.EncodeToEXR(exportTexture, Texture2D.EXRFlags.CompressZIP); GaiaCommon1.Utils.WriteAllBytes(imageName + ".exr", exrBytes); break; } #else switch (imageFileType) { case GaiaConstants.ImageFileType.Jpg: byte[] jpgBytes = exportTexture.EncodeToJPG(); GaiaCommon1.Utils.WriteAllBytes(imageName + ".jpg", jpgBytes); break; case GaiaConstants.ImageFileType.Png: byte[] pngBytes = exportTexture.EncodeToPNG(); GaiaCommon1.Utils.WriteAllBytes(imageName + ".png", pngBytes); break; case GaiaConstants.ImageFileType.Exr: byte[] exrBytes = exportTexture.EncodeToEXR(Texture2D.EXRFlags.CompressZIP); GaiaCommon1.Utils.WriteAllBytes(imageName + ".exr", exrBytes); break; } #endif #if UNITY_EDITOR AssetDatabase.Refresh(); #endif //Lose the texture DestroyImmediate(exportTexture); }