Пример #1
0
        public static VoronoiData Create(int width, int height, int regionCount, int seed)
        {
            var data = new VoronoiData
            {
                width       = width,
                height      = height,
                centroids   = new Vector2Int[regionCount],
                regionCount = regionCount,
                regionData  = new int[width * height]
            };

            var rng = new System.Random(seed);

            for (var i = 0; i < data.regionCount; i++)
            {
                data.centroids[i] = new Vector2Int(rng.Next(0, data.width), rng.Next(0, data.height));
            }

            //Generate Region Data for each 'pixel'
            for (var y = 0; y < data.height; y++)
            {
                for (var x = 0; x < data.width; x++)
                {
                    var index = y * data.width + x;
                    var pt    = new Vector2Int(x, y);
                    data.regionData[index] = GetClosestCentroidIndex(pt, data.centroids);
                }
            }

            return(data);
        }
Пример #2
0
        public static Texture2D CreateFalloffTexture(VoronoiData data)
        {
            var distances = new float[data.width * data.height];

            for (var y = 0; y < data.height; y++)
            {
                for (var x = 0; x < data.width; x++)
                {
                    var pt          = new Vector2Int(x, y);
                    var index       = y * data.width + x;
                    var regionIndex = data.regionData[index];
                    distances[index] = Vector2.Distance(pt, data.centroids[regionIndex]);
                }
            }

            var maxDistance = distances.Max();
            var pixelColors = new Color[data.width * data.height];

            for (var i = 0; i < distances.Length; i++)
            {
                var val = Mathf.Clamp01(distances[i] / maxDistance);
                pixelColors[i] = new Color(val, val, val, 1f);
            }

            return(CreateTextureFromColorArray(pixelColors, data.width, data.height));
        }
Пример #3
0
        public static Texture2D CreateBorderTexture(VoronoiData data)
        {
            var pixelColors = new Color[data.width * data.height];

            for (var y = 0; y < data.height; y++)
            {
                for (var x = 0; x < data.width; x++)
                {
                    var pixelIndex = y * data.width + x;

                    if (x - 1 < 0 || x + 1 >= data.width)
                    {
                        pixelColors[pixelIndex] = Color.white;
                    }
                    else if (y - 1 < 0 || y + 1 >= data.height)
                    {
                        pixelColors[pixelIndex] = Color.white;
                    }
                    else
                    {
                        var up    = (y + 1) * data.width + x;
                        var down  = (y - 1) * data.width + x;
                        var left  = y * data.width + x - 1;
                        var right = y * data.width + x + 1;

                        var thisRegion  = data.regionData[pixelIndex];
                        var upRegion    = data.regionData[up];
                        var downRegion  = data.regionData[down];
                        var leftRegion  = data.regionData[left];
                        var rightRegion = data.regionData[right];

                        if ((thisRegion == upRegion) &&
                            (thisRegion == downRegion) &&
                            (thisRegion == leftRegion) &&
                            (thisRegion == rightRegion))
                        {
                            pixelColors[pixelIndex] = Color.black;
                        }
                        else
                        {
                            pixelColors[pixelIndex] = Color.white;
                        }
                    }
                }
            }

            return(CreateTextureFromColorArray(pixelColors, data.width, data.height));
        }
Пример #4
0
        public static Texture2D CreateTexture(VoronoiData data)
        {
            //Randomly select some colors
            var regionColors = new Color[data.regionCount];

            for (var i = 0; i < data.regionCount; i++)
            {
                regionColors[i] = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1f);
            }

            var pixelColors = new Color[data.width * data.height];

            for (var y = 0; y < data.height; y++)
            {
                for (var x = 0; x < data.width; x++)
                {
                    var index = y * data.width + x;
                    pixelColors[index] = regionColors[data.regionData[index]];
                }
            }

            return(CreateTextureFromColorArray(pixelColors, data.width, data.height));
        }