예제 #1
0
        public void AddTerrainHeight()
        {
            int h = ScmapEditor.Current.Teren.terrainData.heightmapHeight;
            int w = ScmapEditor.Current.Teren.terrainData.heightmapWidth;

            ScmapEditor.GetAllHeights(ref beginHeights);
            MapLuaParser.Current.History.RegisterTerrainHeightmapChange(beginHeights);

            float Value = float.Parse(TerrainAdd.text) * 0.1f;

            Value /= ScmapEditor.TerrainHeight;

            float[,] heights = ScmapEditor.Current.Teren.terrainData.GetHeights(0, 0, ScmapEditor.Current.Teren.terrainData.heightmapWidth, ScmapEditor.Current.Teren.terrainData.heightmapHeight);

            for (int i = 0; i < ScmapEditor.Current.Teren.terrainData.heightmapWidth; i++)
            {
                for (int j = 0; j < ScmapEditor.Current.Teren.terrainData.heightmapWidth; j++)
                {
                    heights[i, j] += Value;
                }
            }
            ScmapEditor.SetAllHeights(heights);
            //ScmapEditor.Current.Teren.terrainData.SetHeights(0, 0, heights);
            RegenerateMaps();
            OnTerrainChanged();
        }
예제 #2
0
        public void AddTerrainHeight()
        {
            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 Value = float.Parse(TerrainAdd.text) * 0.1f;

            Value /= ScmapEditor.TerrainHeight;

            float[,] heights = ScmapEditor.Current.Teren.terrainData.GetHeights(0, 0, ScmapEditor.Current.Teren.terrainData.heightmapResolution, ScmapEditor.Current.Teren.terrainData.heightmapResolution);

            for (int i = 0; i < ScmapEditor.Current.Teren.terrainData.heightmapResolution; i++)
            {
                for (int j = 0; j < ScmapEditor.Current.Teren.terrainData.heightmapResolution; j++)
                {
                    heights[i, j] += Value;
                }
            }
            ScmapEditor.SetAllHeights(heights);
            //ScmapEditor.Current.Teren.terrainData.SetHeights(0, 0, heights);
            RegenerateMaps();
            OnTerrainChanged();
        }
예제 #3
0
 public override void DoUndo()
 {
     if (!RedoGenerated)
     {
         float[,] UndoData_newheights = new float[0, 0];
         ScmapEditor.GetAllHeights(ref UndoData_newheights);
         Undo.RegisterRedo(new HistoryTerrainHeight(), new TerrainHeightHistoryParameter(UndoData_newheights));
     }
     RedoGenerated = true;
     DoRedo();
 }
예제 #4
0
 public override void DoUndo()
 {
     if (!RedoGenerated)
     {
         //Undo.UndoData_newheights = ScmapEditor.Current.Teren.terrainData.GetHeights(0, 0, ScmapEditor.Current.Teren.terrainData.heightmapWidth, ScmapEditor.Current.Teren.terrainData.heightmapHeight);
         ScmapEditor.GetAllHeights(ref Undo.UndoData_newheights);
         HistoryTerrainHeight.GenerateRedo(Undo.Current.Prefabs.TerrainHeightChange).Register();
     }
     RedoGenerated = true;
     DoRedo();
 }
예제 #5
0
        void LoadNeroxisHeightmap()
        {
            ScmapEditor.GetAllHeights(ref beginHeights);

            float[,] heights          = ScmapEditor.Current.Teren.terrainData.GetHeights(0, 0, ScmapEditor.Current.Teren.terrainData.heightmapResolution, ScmapEditor.Current.Teren.terrainData.heightmapResolution);
            float[,] generatedHeights = neroxis.GenerateMapTask.GetGeneratedHeightmap();

            for (int i = 0; i < ScmapEditor.Current.Teren.terrainData.heightmapResolution; i++)
            {
                for (int j = 0; j < ScmapEditor.Current.Teren.terrainData.heightmapResolution; j++)
                {
                    heights[i, j] = generatedHeights[i, j];
                }
            }

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

            ScmapEditor.SetAllHeights(heights);
        }
예제 #6
0
        void Paint(Vector3 AtPosition, int id = 0)
        {
            int BrushPaintType = SelectedBrush;


            if (Smooth)
            {
                BrushPaintType = 2;                 // Smooth
            }
            int hmWidth  = ScmapEditor.Current.Teren.terrainData.heightmapWidth;
            int hmHeight = ScmapEditor.Current.Teren.terrainData.heightmapHeight;

            Vector3 tempCoord = ScmapEditor.Current.Teren.gameObject.transform.InverseTransformPoint(AtPosition);
            Vector3 coord     = Vector3.zero;

            coord.x = tempCoord.x / ScmapEditor.Current.Teren.terrainData.size.x;
            //coord.y = tempCoord.y / ScmapEditor.Current.Teren.terrainData.size.y;
            coord.z = tempCoord.z / ScmapEditor.Current.Teren.terrainData.size.z;

            if (coord.x > 1)
            {
                return;
            }
            if (coord.x < 0)
            {
                return;
            }
            if (coord.z > 1)
            {
                return;
            }
            if (coord.z < 0)
            {
                return;
            }

            // get the position of the terrain heightmap where this game object is
            int posXInTerrain = (int)(coord.x * hmWidth);
            int posYInTerrain = (int)(coord.z * hmHeight);
            // we set an offset so that all the raising terrain is under this game object
            int size = BrushSize.intValue;

            if (BrushPaintType == 2 && size < 8)
            {
                size = 8;
            }


            int offset = size / 2;
            // get the heights of the terrain under this game object

            // Horizontal Brush Offsets
            int OffsetLeft = 0;

            if (posXInTerrain - offset < 0)
            {
                OffsetLeft = Mathf.Abs(posXInTerrain - offset);
            }
            int OffsetRight = 0;

            if (posXInTerrain - offset + size > ScmapEditor.Current.Teren.terrainData.heightmapWidth)
            {
                OffsetRight = posXInTerrain - offset + size - ScmapEditor.Current.Teren.terrainData.heightmapWidth;
            }

            // Vertical Brush Offsets
            int OffsetDown = 0;

            if (posYInTerrain - offset < 0)
            {
                OffsetDown = Mathf.Abs(posYInTerrain - offset);
            }
            int OffsetTop = 0;

            if (posYInTerrain - offset + size > ScmapEditor.Current.Teren.terrainData.heightmapWidth)
            {
                OffsetTop = posYInTerrain - offset + size - ScmapEditor.Current.Teren.terrainData.heightmapWidth;
            }

            //float[,] heights = ScmapEditor.Current.Teren.terrainData.GetHeights(posXInTerrain - offset + OffsetLeft, posYInTerrain - offset + OffsetDown, (size - OffsetLeft) - OffsetRight, (size - OffsetDown) - OffsetTop);

            int GetX = posXInTerrain - offset + OffsetLeft;
            int GetY = posYInTerrain - offset + OffsetDown;


            int SizeDown = (size - OffsetDown) - OffsetTop;
            int SizeLeft = (size - OffsetLeft) - OffsetRight;

            ScmapEditor.GetValues(GetX, GetY, SizeDown, SizeLeft);


            float CenterHeight = 0;

            int   i           = 0;
            int   j           = 0;
            int   x           = 0;
            int   y           = 0;
            float SampleBrush = 0;
            float PixelPower  = 0;

            if (SelectedBrush == 1)
            {
                float Count = 0;
                for (i = 0; i < ScmapEditor.LastGetWidth; i++)
                {
                    for (j = 0; j < ScmapEditor.LastGetHeight; j++)
                    {
                        // Brush strength
                        x = (int)(((i + OffsetDown) / (float)size) * BrushGenerator.Current.PaintImageWidths[id]);
                        y = (int)(((j + OffsetLeft) / (float)size) * BrushGenerator.Current.PaintImageHeights[id]);

                        if (x < 0 || y < 0 || x >= BrushGenerator.Current.PaintImageWidths[id] || y >= BrushGenerator.Current.PaintImageHeights[id])
                        {
                            continue;
                        }
                        SampleBrush   = Mathf.Clamp01(BrushGenerator.Current.Values[id][y + BrushGenerator.Current.PaintImageHeights[id] * x] - 0.0255f);
                        CenterHeight += ScmapEditor.ReturnValues[i, j] * SampleBrush;
                        Count        += SampleBrush;
                    }
                }
                CenterHeight /= Count;
            }
            else if (SelectedBrush == 3)
            {
                float Count = 0;
                for (i = 0; i < ScmapEditor.LastGetWidth; i++)
                {
                    for (j = 0; j < ScmapEditor.LastGetHeight; j++)
                    {
                        // Brush strength
                        x = (int)(((i + OffsetDown) / (float)size) * BrushGenerator.Current.PaintImageWidths[id]);
                        y = (int)(((j + OffsetLeft) / (float)size) * BrushGenerator.Current.PaintImageHeights[id]);

                        if (x < 0 || y < 0 || x >= BrushGenerator.Current.PaintImageWidths[id] || y >= BrushGenerator.Current.PaintImageHeights[id])
                        {
                            continue;
                        }
                        SampleBrush   = Mathf.Clamp01(BrushGenerator.Current.Values[id][y + BrushGenerator.Current.PaintImageHeights[id] * x] - 0.0255f);
                        CenterHeight += ScmapEditor.ReturnValues[i, j] * SampleBrush;
                        Count        += SampleBrush;
                    }
                }
                CenterHeight /= Count;
            }


            float TargetHeight = Mathf.Clamp(((Invert?(256):(BrushTarget.value)) / ScmapEditor.Current.Data.size.y) / 10f, Min, Max);

            float BrushStrenghtValue = BrushStrength.value;
            //float PaintStrength = BrushStrenghtValue * 0.00005f * (Invert ? (-1) : 1);

            float StrengthMultiplier = 1;

            switch (BrushPaintType)
            {
            case 1:                     // Flatten
                StrengthMultiplier = BrushGenerator.Current.HeightmapBlurStrength.Evaluate(BrushStrenghtValue) * 0.00008f;
                break;

            case 2:                     // Smooth
                StrengthMultiplier = BrushGenerator.Current.HeightmapBlurStrength.Evaluate(BrushStrenghtValue) * Mathf.Clamp(size / 10f, 0, 20) * 0.01f;
                break;

            case 3:                     // Sharp
                StrengthMultiplier = BrushGenerator.Current.HeightmapBlurStrength.Evaluate(BrushStrenghtValue) * 0.00008f;
                break;

            default:
                StrengthMultiplier = BrushGenerator.Current.HeightmapPaintStrength.Evaluate(BrushStrenghtValue) * 0.00008f * (Invert ? (-1) : 1);
                break;
            }

            //float SizeSmooth = Mathf.Clamp01(size / 10f) * 15;


            for (i = 0; i < ScmapEditor.LastGetWidth; i++)
            {
                for (j = 0; j < ScmapEditor.LastGetHeight; j++)
                {
                    // Brush strength
                    x = (int)(((i + OffsetDown) / (float)size) * BrushGenerator.Current.PaintImageWidths[id]);
                    y = (int)(((j + OffsetLeft) / (float)size) * BrushGenerator.Current.PaintImageHeights[id]);

                    if (x < 0 || y < 0 || x >= BrushGenerator.Current.PaintImageWidths[id] || y >= BrushGenerator.Current.PaintImageHeights[id])
                    {
                        continue;
                    }
                    SampleBrush = Mathf.Clamp01(BrushGenerator.Current.Values[id][y + BrushGenerator.Current.PaintImageHeights[id] * x] - 0.0255f);


                    if (SampleBrush > 0)
                    {
                        //if (i < 0 || j < 0 || i >= ScmapEditor.LastGetWidth || j >= ScmapEditor.LastGetHeight)
                        //	continue;

                        switch (BrushPaintType)
                        {
                        case 1:                                 // Flatten
                            PixelPower  = Mathf.Pow(Mathf.Abs(ScmapEditor.ReturnValues[i, j] - CenterHeight), 0.454545f) + 1;
                            PixelPower /= 2f;

                            //if (PixelPower < 0.001f)
                            //	heights[i, j] = CenterHeight;
                            //else {
                            //float FlattenStrenght = PixelPower * StrengthMultiplier * Mathf.Pow(SampleBrush, 2);
                            //heights[i, j] += FlattenStrenght;
                            ScmapEditor.ReturnValues[i, j] = MoveToValue(ScmapEditor.ReturnValues[i, j], CenterHeight, StrengthMultiplier * SampleBrush * PixelPower, 0, ScmapEditor.MaxElevation);
                            //}

                            break;

                        case 2:                                 // Smooth
                            CenterHeight = GetNearValues(ref ScmapEditor.ReturnValues, i, j);

                            PixelPower  = Mathf.Pow(Mathf.Abs(ScmapEditor.ReturnValues[i, j] - CenterHeight), 0.454545f) + 1;
                            PixelPower /= 2f;
                            //PixelPower = 10;

                            //heights[i, j] = Mathf.Lerp(heights[i, j], CenterHeight, BrushStrenghtValue * Mathf.Pow(SampleBrush, 2) * PixelPower);
                            ScmapEditor.ReturnValues[i, j] = Mathf.Lerp(ScmapEditor.ReturnValues[i, j], CenterHeight, PixelPower * StrengthMultiplier * Mathf.Pow(SampleBrush, 2));

                            break;

                        case 3:                                 // Sharp
                            PixelPower  = Mathf.Pow(Mathf.Abs(ScmapEditor.ReturnValues[i, j] - CenterHeight), 0.454545f) + 1;
                            PixelPower /= 2f;
                            //heights[i, j] += Mathf.Lerp(PixelPower, 0, PixelPower * 10) * BrushStrenghtValue * 0.01f * Mathf.Pow(SampleBrush, 2);
                            ScmapEditor.ReturnValues[i, j] = MoveToValue(ScmapEditor.ReturnValues[i, j], CenterHeight, -StrengthMultiplier * SampleBrush * PixelPower, Min, Max);
                            break;

                        default:
                            //heights[i, j] += SampleBrush * StrengthMultiplier;
                            ScmapEditor.ReturnValues[i, j] = MoveToValue(ScmapEditor.ReturnValues[i, j], TargetHeight, SampleBrush * StrengthMultiplier, Min, Max);
                            break;
                        }
                    }
                }
            }

            // set the new height
            if (!TerainChanged)
            {
                ScmapEditor.GetAllHeights(ref beginHeights);

                TerainChanged = true;
            }

            //ScmapEditor.Current.Teren.terrainData.SetHeightsDelayLOD(posXInTerrain - offset + OffsetLeft, posYInTerrain - offset + OffsetDown, heights);

            ScmapEditor.ApplyChanges(GetX, GetY);

            //Markers.MarkersControler.UpdateMarkersHeights();

            OnTerrainChanged();
        }
예제 #7
0
        public void ImportHeightmap()
        {
            var extensions = new[]
            {
                new ExtensionFilter("Heightmap", new string[] { "raw", "r16", "bmp" })
                //new ExtensionFilter("Stratum mask", "raw, bmp")
            };

            var paths = StandaloneFileBrowser.OpenFilePanel("Import stratum mask", EnvPaths.GetMapsPath() + MapLuaParser.Current.FolderName, extensions, false);


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


            int h = ScmapEditor.Current.Teren.terrainData.heightmapHeight;
            int w = ScmapEditor.Current.Teren.terrainData.heightmapWidth;

            ScmapEditor.GetAllHeights(ref beginHeights);
            MapLuaParser.Current.History.RegisterTerrainHeightmapChange(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))
                    {
                        for (int y = 0; y < h; y++)
                        {
                            for (int x = 0; x < w; x++)
                            {
                                float v = (float)reader.ReadUInt16() / (float)HeightConversion;
                                data[h - (y + 1), x] = v;
                            }
                        }
                    }
            }
            //ScmapEditor.Current.Teren.terrainData.SetHeights(0, 0, data);
            ScmapEditor.SetAllHeights(data);
            RegenerateMaps();
            OnTerrainChanged();
        }
예제 #8
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);
            }
        }