private void ImportRaw16() { byte[] data = File.ReadAllBytes(FilePath); int rawResolution = Mathf.FloorToInt(Mathf.Sqrt(data.Length / sizeof(UInt16))); if (rawResolution * rawResolution != data.Length / sizeof(UInt16)) { throw new Exception("Error on populate data, RAW file doesn't have squared size or bit depth has been mis-configured!"); } if (UseRawResolution) { DesData.Geometry.HeightMapResolution = rawResolution; } float[] heightData = new float[rawResolution * rawResolution]; for (int i = 0; i < heightData.Length; ++i) { UInt16 pixelValue = BitConverter.ToUInt16(data, i * 2); heightData[i] = pixelValue * 1.0f / UInt16.MaxValue; } Vector2 uv = Vector2.zero; Vector2 enc = Vector2.zero; float h = 0; Color[] heightMapColors = DesData.Geometry.HeightMap.GetPixels(); Color[] oldHeightMapColors = new Color[heightMapColors.Length]; Array.Copy(heightMapColors, oldHeightMapColors, heightMapColors.Length); int heightMapResolution = DesData.Geometry.HeightMapResolution; for (int z = 0; z < heightMapResolution; ++z) { for (int x = 0; x < heightMapResolution; ++x) { uv.Set( Mathf.InverseLerp(0, heightMapResolution - 1, x), 1 - Mathf.InverseLerp(0, heightMapResolution - 1, z)); Color c = heightMapColors[GUtilities.To1DIndex(x, z, heightMapResolution)]; h = GUtilities.GetValueBilinear(heightData, rawResolution, rawResolution, uv); enc = GCommon.EncodeFloatRG(h); c.r = enc.x; c.g = enc.y; heightMapColors[GUtilities.To1DIndex(x, z, heightMapResolution)] = c; } } DesData.Geometry.HeightMap.SetPixels(heightMapColors); DesData.Geometry.HeightMap.Apply(); IEnumerable <Rect> dirtyRects = GCommon.CompareHeightMap(DesData.Geometry.ChunkGridSize, oldHeightMapColors, heightMapColors); DesData.Geometry.SetRegionDirty(dirtyRects); DesData.SetDirty(GTerrainData.DirtyFlags.Geometry); }
private static void RestoreHeightMap(GStylizedTerrain t, string backupName) { string rFileName = string.Format("{0}_{1}", t.TerrainData.Id, GBackupFile.HEIGHT_R_SUFFIX); string gFileName = string.Format("{0}_{1}", t.TerrainData.Id, GBackupFile.HEIGHT_G_SUFFIX); string bFileName = string.Format("{0}_{1}", t.TerrainData.Id, GBackupFile.HEIGHT_B_SUFFIX); string aFileName = string.Format("{0}_{1}", t.TerrainData.Id, GBackupFile.HEIGHT_A_SUFFIX); byte[] dataR = GBackupFile.ReadAllBytes(backupName, rFileName); byte[] dataG = GBackupFile.ReadAllBytes(backupName, gFileName); byte[] dataB = GBackupFile.ReadAllBytes(backupName, bFileName); byte[] dataA = GBackupFile.ReadAllBytes(backupName, aFileName); if (dataR == null || dataG == null || dataB == null || dataA == null) { return; } dataR = GCompressor.Decompress(dataR); dataG = GCompressor.Decompress(dataG); dataB = GCompressor.Decompress(dataB); dataA = GCompressor.Decompress(dataA); Color32[] heightMapColors = new Color32[dataR.Length]; for (int i = 0; i < heightMapColors.Length; ++i) { heightMapColors[i].r = dataR[i]; heightMapColors[i].g = dataG[i]; heightMapColors[i].b = dataB[i]; heightMapColors[i].a = dataA[i]; } Color32[] oldHeightMapColors = t.TerrainData.Geometry.HeightMap.GetPixels32(); int heightMapResolution = Mathf.RoundToInt(Mathf.Sqrt(heightMapColors.LongLength)); t.TerrainData.Geometry.HeightMapResolution = heightMapResolution; t.TerrainData.Geometry.HeightMap.SetPixels32(heightMapColors); t.TerrainData.Geometry.HeightMap.Apply(); List <Rect> dirtyRects = new List <Rect>(GCommon.CompareHeightMap(t.TerrainData.Geometry.ChunkGridSize, oldHeightMapColors, heightMapColors)); for (int i = 0; i < dirtyRects.Count; ++i) { t.TerrainData.Geometry.SetRegionDirty(dirtyRects[i]); } t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); }
private void Apply(GStylizedTerrain t) { if (t.TerrainData == null) { return; } int heightMapResolution = t.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = new RenderTexture(heightMapResolution, heightMapResolution, 0, GGeometry.HeightMapRTFormat, RenderTextureReadWrite.Linear); List <Vector4> vertices = SplineCreator.GenerateVerticesWithFalloff(); Internal_Apply(t, rt, vertices); Color[] oldHeightMapColors = t.TerrainData.Geometry.HeightMap.GetPixels(); RenderTexture.active = rt; t.TerrainData.Geometry.HeightMap.ReadPixels(new Rect(0, 0, heightMapResolution, heightMapResolution), 0, 0); t.TerrainData.Geometry.HeightMap.Apply(); RenderTexture.active = null; Color[] newHeightMapColors = t.TerrainData.Geometry.HeightMap.GetPixels(); rt.Release(); Object.DestroyImmediate(rt); List <Rect> dirtyRects = new List <Rect>(GCommon.CompareHeightMap(t.TerrainData.Geometry.ChunkGridSize, oldHeightMapColors, newHeightMapColors)); for (int i = 0; i < dirtyRects.Count; ++i) { t.TerrainData.Geometry.SetRegionDirty(dirtyRects[i]); } t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); for (int i = 0; i < dirtyRects.Count; ++i) { t.TerrainData.Foliage.SetTreeRegionDirty(dirtyRects[i]); t.TerrainData.Foliage.SetGrassRegionDirty(dirtyRects[i]); } t.UpdateTreesPosition(); t.UpdateGrassPatches(); t.TerrainData.Foliage.ClearTreeDirtyRegions(); t.TerrainData.Foliage.ClearGrassDirtyRegions(); t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Foliage); }
public void Import() { try { #if UNITY_EDITOR GCommonGUI.ProgressBar("Working", "Importing textures...", 1f); #endif if (HeightMap != null || VisibilityMap != null) { Color[] oldHeightMapColors = DesData.Geometry.HeightMap.GetPixels(); Texture2D hm = null; if (HeightMap != null) { hm = GCommon.CloneTexture(HeightMap); } Texture2D vm = null; if (VisibilityMap != null) { vm = GCommon.CloneTexture(VisibilityMap); } int desResolution = DesData.Geometry.HeightMapResolution; Color[] newColor = new Color[desResolution * desResolution]; Vector2 uv = Vector2.zero; Vector2 enc = Vector2.zero; for (int y = 0; y < desResolution; ++y) { for (int x = 0; x < desResolution; ++x) { uv.Set( Mathf.InverseLerp(0, desResolution, x), Mathf.InverseLerp(0, desResolution, y)); Color c = Color.clear; if (hm != null) { enc = GCommon.EncodeFloatRG(hm.GetPixelBilinear(uv.x, uv.y).r); c.r = enc.x; c.g = enc.y; } else { c = DesData.Geometry.HeightMap.GetPixelBilinear(uv.x, uv.y); c.b = 0; } if (vm != null) { c.a = 1 - vm.GetPixelBilinear(uv.x, uv.y).r; } else { c.a = DesData.Geometry.HeightMap.GetPixelBilinear(uv.x, uv.y).a; } newColor[GUtilities.To1DIndex(x, y, desResolution)] = c; } } DesData.Geometry.HeightMap.SetPixels(newColor); DesData.Geometry.HeightMap.Apply(); if (hm != null) { GUtilities.DestroyObject(hm); } if (vm != null) { GUtilities.DestroyObject(vm); } //Color[] hmColor = hm.GetPixels(); //for (int i = 0; i < hmColor.Length; ++i) //{ // Color c = hmColor[i]; // hmColor[i] = new Color(c.r, 0, 0, 0); //} //hm.SetPixels(hmColor); //hm.Apply(); //GCommon.CopyTexture(hm, DesData.Geometry.Internal_HeightMap); //GUtilities.DestroyObject(hm); Color[] newHeightMapColors = DesData.Geometry.HeightMap.GetPixels(); IEnumerable <Rect> dirtyRects = GCommon.CompareHeightMap(DesData.Geometry.ChunkGridSize, oldHeightMapColors, newHeightMapColors); DesData.Geometry.SetRegionDirty(dirtyRects); DesData.SetDirty(GTerrainData.DirtyFlags.Geometry); if (Terrain != null) { DesData.Foliage.SetTreeRegionDirty(GCommon.UnitRect); DesData.Foliage.SetGrassRegionDirty(GCommon.UnitRect); Terrain.UpdateTreesPosition(true); Terrain.UpdateGrassPatches(-1, true); DesData.Foliage.ClearTreeDirtyRegions(); DesData.Foliage.ClearGrassDirtyRegions(); } } if (AlbedoMap != null) { GCommon.CopyTexture(AlbedoMap, DesData.Shading.AlbedoMap); DesData.SetDirty(GTerrainData.DirtyFlags.Shading); } if (MetallicMap != null) { GCommon.CopyTexture(MetallicMap, DesData.Shading.MetallicMap); DesData.SetDirty(GTerrainData.DirtyFlags.Shading); } if (SplatControlMaps != null) { int count = Mathf.Min(SplatControlMaps.Length, DesData.Shading.SplatControlMapCount); for (int i = 0; i < count; ++i) { if (SplatControlMaps[i] != null) { GCommon.CopyTexture(SplatControlMaps[i], DesData.Shading.GetSplatControl(i)); } } DesData.SetDirty(GTerrainData.DirtyFlags.Shading); } } catch (System.Exception e) { Debug.LogError(e); } finally { #if UNITY_EDITOR GCommonGUI.ClearProgressBar(); #endif } }