Пример #1
0
        public void Draw()
        {
            noiseType = (NoiseType)EditorGUILayout.EnumPopup("Fill", noiseType);

            if (noiseType == NoiseType.PERLIN)
            {
                gradient = EditorGUILayout.GradientField("Color", gradient);

                octaves                = EditorGUILayout.IntSlider("Octaves", octaves, 1, 10);
                perlinNoiseScale       = EditorGUILayout.Slider("Scale", perlinNoiseScale, 0.01f, 1000f);
                perlinNoisePersistence = EditorGUILayout.Slider("Persistence", perlinNoisePersistence, 0f, 1f);
                seed = EditorGUILayout.IntSlider("Seed", seed, 1, 100000);
                perlinNoiseOffset = EditorGUILayout.Vector2Field("Offset", perlinNoiseOffset);

                flip = EditorGUILayout.Toggle("Flip", flip);
            }
            else if (noiseType == NoiseType.RANDOM)
            {
                seed      = EditorGUILayout.IntSlider("Seed", seed, 1, 100000);
                useColors = EditorGUILayout.Toggle("Use colors", useColors);
            }
            else if (noiseType == NoiseType.VORONOI)
            {
                voronoiDstType = (VoronoiDistanceMetric)EditorGUILayout.EnumPopup("Distance type", voronoiDstType);
                sites          = EditorGUILayout.IntField("Sites", sites);
                seed           = EditorGUILayout.IntSlider("Seed", seed, 1, 100000);
                useColors      = EditorGUILayout.Toggle("Use colors", useColors);
                renderFlat     = EditorGUILayout.Toggle("Render flat", renderFlat);

                flip = EditorGUILayout.Toggle("Flip", flip);
            }
        }
Пример #2
0
        public void Reset()
        {
            gradient = new Gradient();

            GradientColorKey[] cKeys = new GradientColorKey[2];
            cKeys[0].color = Color.white;
            cKeys[0].time  = 0.0f;
            cKeys[1].color = Color.black;
            cKeys[1].time  = 1.0f;

            GradientAlphaKey[] aKeys = new GradientAlphaKey[1];
            aKeys[0].alpha = 1.0f;
            aKeys[0].time  = 0.0f;

            gradient.SetKeys(cKeys, aKeys);

            // Perlin noise.
            octaves                = 4;
            seed                   = 1;
            perlinNoiseScale       = 150f;
            perlinNoisePersistence = 0.5f;
            // perlinNoiseFrequency = 2f;
            perlinNoiseOffset = Vector2.zero;

            // Random noise.
            useColors = false;

            // Voronoi.
            sites          = 10;
            voronoiDstType = VoronoiDistanceMetric.Euclidean;
            renderFlat     = false;

            flip = false;
        }
Пример #3
0
        /// <summary>
        /// Creates a texture and fills it with voronoi diagram.
        /// <param name="width"> The width of the texture. </param>
        /// <param name="height"> The height of the texture. </param>
        /// <param name="sites"> Number of cell sites. </param>
        /// <param name="seed"> The seed values used to init the random number generator. </param>
        /// <param name="dstMetric"> Distance metric used to calculate distance to each cell. </param>
        /// <param name="renderFlat"> If true, renders the cells as flat colors if useColors is used otherwise grayscale. </param>
        /// <param name="useColors"> Appoints a random color to each cell. </param>
        /// <param name="flip"> If true, flips texture's gradient colors. </param>
        /// </summary>
        public static Texture2D FillVoronoi(int width, int height, int sites, int seed, VoronoiDistanceMetric dstMetric, bool renderFlat, bool useColors, bool flip = false)
        {
            Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, true);

            tex.name       = "tex_voronoi_.png";
            tex.filterMode = FilterMode.Bilinear;

            Color[] colors = new Color[width * height];
            Color   c      = Color.white;

            Random.InitState(seed);

            Vector2[] points     = new Vector2[sites];
            Color[]   cellColors = new Color[sites];
            for (int i = 0; i < sites; i++)
            {
                float x = Random.Range(-1.0f, 1.0f);
                float y = Random.Range(-1.0f, 1.0f);
                points[i]     = new Vector2(x, y);
                cellColors[i] = Random.ColorHSV();
            }

            Vector2 operand = Vector2.zero;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    float dist      = float.MaxValue;
                    int   cellIndex = 0;

                    for (int i = 0; i < sites; i++)
                    {
                        float xOperand = flip? 1.0f - (x / (width - 1.0f)) : (x / (width - 1.0f));
                        float yOperand = flip? 1.0f - (y / (height - 1.0f)) : (y / (height - 1.0f));
                        operand.x = xOperand * 2.0f - 1.0f;
                        operand.y = yOperand * 2.0f - 1.0f;

                        float tDist = 100;

                        switch (dstMetric)
                        {
                        case VoronoiDistanceMetric.Euclidean:
                            tDist = Vector2.Distance(operand, points[i]);
                            break;

                        case VoronoiDistanceMetric.Manhattan:
                            tDist = Mathf.Abs(operand.x - points[i].x) + Mathf.Abs(operand.y - points[i].y);
                            break;

                        default:
                            tDist = Vector2.Distance(operand, points[i]);
                            break;
                        }

                        if (tDist < dist)
                        {
                            cellIndex = i;

                            dist = tDist;
                        }
                    }

                    if (renderFlat)
                    {
                        if (useColors)
                        {
                            c = cellColors[cellIndex];
                        }
                        else
                        {
                            c.r = c.g = c.b = (float)cellIndex / sites;
                        }
                    }
                    else
                    {
                        Color dc = dist * Color.white;
                        dc.a = 1.0f;
                        c.r  = useColors? Color.Lerp(dc, cellColors[cellIndex], dist).r : dist;
                        c.g  = useColors? Color.Lerp(dc, cellColors[cellIndex], dist).g : dist;
                        c.b  = useColors? Color.Lerp(dc, cellColors[cellIndex], dist).b : dist;
                    }

                    colors[x + y * width] = c;
                }
            }

            tex.SetPixels(colors);
            tex.Apply();

            return(tex);
        }