Esempio n. 1
0
        public void ImportHeightmap()
        {
            var extensions = new[]
            {
                new ExtensionFilter("Heightmap", new string[] { "raw", "r16", "bmp" })
            };

            var paths = StandaloneFileBrowser.OpenFilePanel("Import heightmap", DefaultPath, extensions, false);


            if (paths == null || paths.Length == 0 || string.IsNullOrEmpty(paths[0]))
            {
                return;
            }

            int h = ScmapEditor.Current.Teren.terrainData.heightmapResolution;
            int w = ScmapEditor.Current.Teren.terrainData.heightmapResolution;

            ScmapEditor.GetAllHeights(ref beginHeights);
            Undo.RegisterUndo(new UndoHistory.HistoryTerrainHeight(), new UndoHistory.HistoryTerrainHeight.TerrainHeightHistoryParameter(beginHeights));


            float[,] data = new float[h, w];
            //float[,] old = ScmapEditor.Current.Teren.terrainData.GetHeights(0, 0, w, h);

            if (paths[0].ToLower().EndsWith("bmp"))
            {
                BMPLoader loader = new BMPLoader();
                BMPImage  img    = loader.LoadBMP(paths[0]);
                Debug.Log(img.info.compressionMethod + ", " + img.info.nBitsPerPixel + ", " + img.rMask + ", " + img.imageData[0].r);
                Texture2D ImportedImage = img.ToTexture2D();


                if (ImportedImage.width != h || ImportedImage.height != w)
                {
                    Debug.Log("Wrong size");
                    TextureScale.Bilinear(ImportedImage, h, w);
                    ImportedImage.Apply(false);
                }

                Color[] ImportedColors = ImportedImage.GetPixels();

                for (int y = 0; y < h; y++)
                {
                    for (int x = 0; x < w; x++)
                    {
                        data[y, x] = (float)ImportedColors[x + y * w].r / 0.567f;                         // 0.58
                    }
                }
            }
            else
            {
                using (var file = System.IO.File.OpenRead(paths[0]))
                    using (var reader = new System.IO.BinaryReader(file))
                    {
                        long CheckValue = 2;
                        CheckValue *= (long)(w);
                        CheckValue *= (long)(h);
                        long FileLength = file.Length;

                        if (FileLength != CheckValue)
                        {
                            reader.Dispose();
                            file.Dispose();
                            GenericPopup.ShowPopup(GenericPopup.PopupTypes.Error, "Error", "Selected heightmap is in wrong size.\nIs: " + FileLength + "B, should be: " + CheckValue + "B", "OK", null);
                            return;
                        }

                        for (int y = 0; y < h; y++)
                        {
                            for (int x = 0; x < w; x++)
                            {
                                float v = (float)(reader.ReadUInt16() / ScmapEditor.HeightResize);
                                data[h - (y + 1), x] = v;
                            }
                        }
                    }
            }

            //ScmapEditor.Current.Teren.terrainData.SetHeights(0, 0, data);
            ScmapEditor.SetAllHeights(data);
            RegenerateMaps();
            OnTerrainChanged();
            EnvPaths.SetLastPath(ExportPathKey, Path.GetDirectoryName(paths[0]));
            GenericInfoPopup.ShowInfo("Heightmap import success!\n" + Path.GetFileName(paths[0]));


            if (ScmapEditor.IsOverMinMaxDistance())
            {
                GenericPopup.ShowPopup(GenericPopup.PopupTypes.TriButton, "Importing heightmap", "Distance between lowest and highest point is higher than 50.\nClamp it?", "Clamp Top", ClampTop, "Clamp Bottom", ClampBottom, "Ignore", null);
            }
        }