Пример #1
0
        public Serializable2DFloatArray GetArrayFromMask(WorldStampCreator parent)
        {
            GridSize = WorldStampCreator.GetMinGridSize(parent.Template.Bounds, parent.Template.Terrain);
            var bounds = parent.Template.Bounds;
            var width  = Mathf.CeilToInt(bounds.size.x / GridSize);
            var height = Mathf.CeilToInt(bounds.size.z / GridSize);
            var array  = new Serializable2DFloatArray(width, height);

            for (var u = 0; u < width; u++)
            {
                for (var v = 0; v < height; v++)
                {
                    var pos     = new Vector3((u / (float)width) * bounds.size.x, bounds.size.y / 2, (v / (float)height) * bounds.size.z);
                    var cell    = GridManager.GetCell(pos);
                    var cellMax = GridManager.GetCellMax(cell).x0z() + bounds.min;
                    var cellMin = GridManager.GetCellCenter(cell).x0z() + bounds.min;
                    if (!bounds.Contains(cellMax) || !bounds.Contains(cellMin))
                    {
                        continue;
                    }

                    var val = Mask.GetValue(cell);
                    array[u, v] = val;
                }
            }
            return(array);
        }
Пример #2
0
        private void FillMaskFromMinY(Bounds bounds, Terrain terrain, Serializable2DFloatArray heights, Vector2 minY)
        {
            Mask.Clear();
            GridSize = WorldStampCreator.GetMinGridSize(bounds, terrain);
            for (var u = GridSize / 2f; u < bounds.size.x; u += GridSize)
            {
                for (var v = GridSize / 2f; v < bounds.size.z; v += GridSize)
                {
                    var cell    = GridManager.GetCell(new Vector3(u, 0, v));
                    var cellMax = GridManager.GetCellMax(cell).x0z() + bounds.min;
                    var cellMin = GridManager.GetCellCenter(cell).x0z() + bounds.min;
                    if (!bounds.Contains(cellMax) || !bounds.Contains(cellMin))
                    {
                        continue;
                    }

                    var h = heights.BilinearSample(new Vector2(u / bounds.size.z, v / bounds.size.x)) * bounds.size.y;
                    if (h < minY.x)
                    {
                        Mask.SetValue(cell, 0);
                    }
                    else if (h <= minY.y)
                    {
                        Mask.SetValue(cell, (h - minY.x) / (minY.y - minY.x));
                    }
                    else
                    {
                        Mask.SetValue(cell, 1);
                    }
                }
            }
        }
Пример #3
0
        public Texture2D GetTextureFromMask(WorldStampCreator parent)
        {
            GridSize = WorldStampCreator.GetMinGridSize(parent.Template.Bounds, parent.Template.Terrain);
            var bounds = parent.Template.Bounds;
            var width  = Mathf.CeilToInt(bounds.size.x / GridSize);
            var height = Mathf.CeilToInt(bounds.size.z / GridSize);
            var tex    = new Texture2D(width, height);

            for (var u = 0; u < width; u++)
            {
                for (var v = 0; v < height; v++)
                {
                    var pos     = new Vector3((u / (float)width) * bounds.size.x, bounds.size.y / 2, (v / (float)height) * bounds.size.z);
                    var cell    = GridManager.GetCell(pos);
                    var cellMax = GridManager.GetCellMax(cell).x0z() + bounds.min;
                    var cellMin = GridManager.GetCellCenter(cell).x0z() + bounds.min;
                    if (!bounds.Contains(cellMax) || !bounds.Contains(cellMin))
                    {
                        continue;
                    }

                    var val = Mask.GetValue(cell);
                    tex.SetPixel(u, v, Color.Lerp(Color.black, Color.white, val));
                }
            }
            tex.Apply();
            return(tex);
        }
Пример #4
0
        protected override void PreviewInSceneInternal(WorldStampCreator parent)
        {
            var bounds = parent.Template.Bounds;

            if (_maskPainter == null)
            {
                _maskPainter      = new Painter(Mask, GridManager);
                _maskPainter.Ramp = new Gradient()
                {
                    colorKeys = new[] { new GradientColorKey(Color.red, 0), new GradientColorKey(Color.black, 0.001f), new GradientColorKey(Color.black, 1), },
                    alphaKeys = new[] { new GradientAlphaKey(1, 0), new GradientAlphaKey(0, 1), }
                };
                _maskPainter.Rect = new Rect(bounds.min.xz(), bounds.size.xz());
            }
            else
            {
                GridSize = WorldStampCreator.GetMinGridSize(bounds, parent.Template.Terrain);
                _maskPainter.GridManager = GridManager;
                _maskPainter.Canvas      = Mask;
                _maskPainter.MaxValue    = 1;
                _maskPainter.MinValue    = 0;
                _maskPainter.Rect        = new Rect(Vector2.zero, bounds.size.xz());
                _maskPainter.TRS         = Matrix4x4.TRS(bounds.min, Quaternion.identity, Vector3.one);
                //_maskPainter.Repaint();
                _maskPainter.PaintingEnabled = true;
                _maskPainter.OnSceneGUI();
            }
        }
Пример #5
0
 private void ResetMask(Bounds bounds, Terrain terrain)
 {
     Mask.Clear();
     GridSize = WorldStampCreator.GetMinGridSize(bounds, terrain);
     for (var u = GridSize / 2f; u < bounds.size.x; u += GridSize)
     {
         for (var v = GridSize / 2f; v < bounds.size.z; v += GridSize)
         {
             var cell    = GridManager.GetCell(new Vector3(u, 0, v));
             var cellMax = GridManager.GetCellMax(cell).x0z() + bounds.min;
             var cellMin = GridManager.GetCellCenter(cell).x0z() + bounds.min;
             if (!bounds.Contains(cellMax) || !bounds.Contains(cellMin))
             {
                 continue;
             }
             Mask.SetValue(cell, 1);
         }
     }
     LastBounds = bounds;
 }
Пример #6
0
 public void SetMaskFromTexture(WorldStampCreator parent, Texture2D tex)
 {
     GridSize = WorldStampCreator.GetMinGridSize(parent.Template.Bounds, parent.Template.Terrain);
     Mask.Clear();
     for (var u = 0f; u < parent.Template.Bounds.size.x; u += GridSize)
     {
         for (var v = 0f; v < parent.Template.Bounds.size.z; v += GridSize)
         {
             var cell    = GridManager.GetCell(new Vector3(u, 0, v));
             var cellMax = GridManager.GetCellMax(cell).x0z() + parent.Template.Bounds.min;
             var cellMin = GridManager.GetCellCenter(cell).x0z() + parent.Template.Bounds.min;
             if (!parent.Template.Bounds.Contains(cellMax) || !parent.Template.Bounds.Contains(cellMin))
             {
                 continue;
             }
             var val = tex.GetPixelBilinear(u / parent.Template.Bounds.size.x, v / parent.Template.Bounds.size.z).grayscale;
             Mask.SetValue(cell, val);
         }
     }
 }
Пример #7
0
 public void SetMaskFromArray(WorldStampCreator parent, Serializable2DFloatArray mask)
 {
     GridSize = WorldStampCreator.GetMinGridSize(parent.Template.Bounds, parent.Template.Terrain);
     Mask.Clear();
     for (var u = GridSize / 2f; u < parent.Template.Bounds.size.x; u += GridSize)
     {
         for (var v = GridSize / 2f; v < parent.Template.Bounds.size.z; v += GridSize)
         {
             var cell    = GridManager.GetCell(new Vector3(u, 0, v));
             var cellMax = GridManager.GetCellMax(cell).x0z() + parent.Template.Bounds.min;
             var cellMin = GridManager.GetCellCenter(cell).x0z() + parent.Template.Bounds.min;
             if (!parent.Template.Bounds.Contains(cellMax) || !parent.Template.Bounds.Contains(cellMin))
             {
                 continue;
             }
             var val = mask.BilinearSample(new Vector2(u / parent.Template.Bounds.size.x, v / parent.Template.Bounds.size.z));
             Mask.SetValue(cell, val);
         }
     }
 }