Esempio n. 1
0
        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.EncodeTerrainHeight(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.CompareTerrainTexture(DesData.Geometry.ChunkGridSize, oldHeightMapColors, heightMapColors);

            DesData.Geometry.SetRegionDirty(dirtyRects);
            DesData.SetDirty(GTerrainData.DirtyFlags.Geometry);
        }
Esempio n. 2
0
        private static void RestoreAlbedoMap(GStylizedTerrain t, string backupName)
        {
            string albedoRFileName = string.Format("{0}_{1}", t.TerrainData.Id, GBackupFile.ALBEDO_R_SUFFIX);
            string albedoGFileName = string.Format("{0}_{1}", t.TerrainData.Id, GBackupFile.ALBEDO_G_SUFFIX);
            string albedoBFileName = string.Format("{0}_{1}", t.TerrainData.Id, GBackupFile.ALBEDO_B_SUFFIX);
            string albedoAFileName = string.Format("{0}_{1}", t.TerrainData.Id, GBackupFile.ALBEDO_A_SUFFIX);

            byte[] albedoRData = GBackupFile.ReadAllBytes(backupName, albedoRFileName);
            byte[] albedoGData = GBackupFile.ReadAllBytes(backupName, albedoGFileName);
            byte[] albedoBData = GBackupFile.ReadAllBytes(backupName, albedoBFileName);
            byte[] albedoAData = GBackupFile.ReadAllBytes(backupName, albedoAFileName);
            if (albedoRData != null &&
                albedoGData != null &&
                albedoBData != null &&
                albedoAData != null)
            {
                albedoRData = GCompressor.Decompress(albedoRData);
                albedoGData = GCompressor.Decompress(albedoGData);
                albedoBData = GCompressor.Decompress(albedoBData);
                albedoAData = GCompressor.Decompress(albedoAData);
                Color32[] albedoColors = new Color32[albedoRData.Length];
                for (int i = 0; i < albedoRData.Length; ++i)
                {
                    albedoColors[i] = new Color32(albedoRData[i], albedoGData[i], albedoBData[i], albedoAData[i]);
                }

                bool willRegenerateGeometry = t.TerrainData.Geometry.AlbedoToVertexColorMode != GAlbedoToVertexColorMode.None;
                if (willRegenerateGeometry)
                {
                    Color32[]   oldAlbedoColor = t.TerrainData.Shading.AlbedoMap.GetPixels32();
                    List <Rect> dirtyRects     = new List <Rect>(GCommon.CompareTerrainTexture(t.TerrainData.Geometry.ChunkGridSize, oldAlbedoColor, albedoColors));
                    t.TerrainData.Geometry.SetRegionDirty(dirtyRects);
                    willRegenerateGeometry = true;
                }

                int resolution = Mathf.RoundToInt(Mathf.Sqrt(albedoRData.LongLength));
                t.TerrainData.Shading.AlbedoMapResolution = resolution;
                t.TerrainData.Shading.AlbedoMap.SetPixels32(albedoColors);
                t.TerrainData.Shading.AlbedoMap.Apply();
                t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading);

                if (willRegenerateGeometry)
                {
                    t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry);
                }
            }
        }
Esempio n. 3
0
        private static bool 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(false);
            }
            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.CompareTerrainTexture(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);
            return(true);
        }
Esempio n. 4
0
        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.CompareTerrainTexture(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);
        }
Esempio n. 5
0
        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.EncodeTerrainHeight(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);
                            }

                            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 || vm != null)
                    {
                        Color[]            newHeightMapColors = DesData.Geometry.HeightMap.GetPixels();
                        IEnumerable <Rect> dirtyRects         = GCommon.CompareTerrainTexture(DesData.Geometry.ChunkGridSize, oldHeightMapColors, newHeightMapColors);
                        DesData.Geometry.SetRegionDirty(dirtyRects);
                        DesData.SetDirty(GTerrainData.DirtyFlags.GeometryTimeSliced);

                        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 (hm != null)
                    {
                        GUtilities.DestroyObject(hm);
                    }
                    if (vm != null)
                    {
                        GUtilities.DestroyObject(vm);
                    }
                }

                if (MaskMap != null)
                {
                    GCommon.CopyTexture(MaskMap, DesData.Mask.MaskMap);
                }
                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
            }
        }