예제 #1
0
        private void generateBU_Click(object sender, EventArgs e)
        {
            int f = (int)frequencyNUPDO.Value;
            int w = (int)widthNUPDO.Value,
                h = (int)heightNUPDO.Value;

            //NoiseMap = Make_PerlinNoise(f, w, h);
            //HasNoise = true;

            //displayPIBO.BackgroundImage = RE.Array_To_Bitmap(NoiseMap);
            //displayPIBO.Refresh();
            //return;

            NoiseMap = new float[w, h];

            int blockSize_x = NoiseMap.GetLength(0) / f,
                blockSize_y = NoiseMap.GetLength(1) / f;

            Vector3[,] vectors_net = new Vector3[f + 1, f + 1];
            Random rnd = new Random();

            Vector3[,] random_points = new Vector3[f, f];;
            float[,] cornerValues    = new float[f + 1, f + 1];
            Vector3[] tempDots = new Vector3[4];


            for (int y = 0; y < f + 1; y++)
            {
                for (int x = 0; x < f + 1; x++)
                {
                    vectors_net[x, y] = new Vector3(potential_vectors[rnd.Next(potential_vectors.Length)]);
                    //Vector3.Normalized(vectors_net[x, y]);
                }
            }

            float min = 0, max = 0;

            for (int y = 0; y < f; y++)
            {
                for (int x = 0; x < f; x++)
                {
                    random_points[x, y] = new Vector3(rnd.Next(0, blockSize_x), rnd.Next(0, blockSize_y), 0);

                    tempDots[0] = new Vector3(random_points[x, y]);
                    tempDots[1] = new Vector3(1 - random_points[x, y].X, random_points[x, y].Y, 0);
                    tempDots[2] = new Vector3(random_points[x, y].X, 1 - random_points[x, y].Y, 0);
                    tempDots[3] = new Vector3(1 - random_points[x, y].X, 1 - random_points[x, y].Y, 0);

                    //tempDots[0] = Vector3.Normalized(tempDots[0]);
                    //tempDots[1] = Vector3.Normalized(tempDots[1]);
                    //tempDots[2] = Vector3.Normalized(tempDots[2]);
                    //tempDots[3] = Vector3.Normalized(tempDots[3]);


                    cornerValues[x, y]         = Vector3.Dot(tempDots[0], vectors_net[x, y]);
                    cornerValues[x + 1, y]     = Vector3.Dot(tempDots[1], vectors_net[x + 1, y]);
                    cornerValues[x, y + 1]     = Vector3.Dot(tempDots[2], vectors_net[x, y + 1]);
                    cornerValues[x + 1, y + 1] = Vector3.Dot(tempDots[3], vectors_net[x + 1, y + 1]);
                }
            }

            foreach (float tempVal in cornerValues)
            {
                if (tempVal > max)
                {
                    max = tempVal;
                }
                else if (tempVal < min)
                {
                    min = tempVal;
                }
            }

            float range = max - min;

            for (int y = 0; y < f + 1; y++)
            {
                for (int x = 0; x < f + 1; x++)
                {
                    cornerValues[x, y] = (cornerValues[x, y] - min) / range;
                    //cornerValues[x, y] = RE.EaseCurve_Perlin(cornerValues[x, y]);
                }
            }


            for (int y = 0; y < f; y++)
            {
                for (int x = 0; x < f; x++)
                {
                    int real_x = x * blockSize_x,
                        real_y = y * blockSize_y;

                    //for (int yy = 0; yy < blockSize_y; yy++)
                    //{
                    //    float intrepolation_factor = (float)yy / (float)blockSize_y;

                    //    NoiseMap[real_x, real_y + yy] = RE.Interpolate_Linear(cornerValues[x, y], cornerValues[x, y + 1], intrepolation_factor);
                    //    NoiseMap[real_x, real_y + yy] = RE.EaseCurve_Perlin(NoiseMap[real_x, real_y + yy]);

                    //    NoiseMap[real_x + blockSize_x - 1, real_y + yy] = RE.Interpolate_Linear(cornerValues[x + 1, y], cornerValues[x + 1, y + 1], intrepolation_factor);
                    //    NoiseMap[real_x + blockSize_x - 1, real_y + yy] = RE.EaseCurve_Perlin(NoiseMap[real_x + blockSize_x - 1, real_y + yy]);

                    //    for (int xx = 0; xx < blockSize_x; xx++)
                    //    {
                    //        intrepolation_factor = (float)xx / (float)blockSize_x;

                    //        NoiseMap[real_x + xx, real_y + yy] = RE.Interpolate_Linear(NoiseMap[real_x, real_y + yy], NoiseMap[real_x + blockSize_x - 1, real_y + yy], intrepolation_factor);
                    //        NoiseMap[real_x + xx, real_y + yy] = RE.EaseCurve_Perlin(NoiseMap[real_x + xx, real_y + yy]);
                    //    }

                    //}

                    for (int xx = 0; xx < blockSize_x; xx++)
                    {
                        float intrepolation_factor = 1 - (float)xx / (float)blockSize_x;
                        //intrepolation_factor = RE.EaseCurve_Perlin(intrepolation_factor);

                        NoiseMap[real_x + xx, real_y] = RE.Lerp(cornerValues[x, y], cornerValues[x + 1, y], intrepolation_factor);
                        //NoiseMap[real_x + xx, real_y] = RE.EaseCurve_Perlin(NoiseMap[real_x + xx, real_y]);

                        NoiseMap[real_x + xx, real_y + blockSize_y - 1] = RE.Lerp(cornerValues[x, y + 1], cornerValues[x + 1, y + 1], intrepolation_factor);
                        //NoiseMap[real_x + xx, real_y + blockSize_y - 1] = RE.EaseCurve_Perlin(NoiseMap[real_x + xx, real_y + blockSize_y - 1]);

                        for (int yy = 0; yy < blockSize_y; yy++)
                        {
                            intrepolation_factor = 1 - (float)yy / (float)blockSize_y;
                            //intrepolation_factor = RE.EaseCurve_Perlin(intrepolation_factor);

                            NoiseMap[real_x + xx, real_y + yy] = RE.Lerp(NoiseMap[real_x + xx, real_y], NoiseMap[real_x + xx, real_y + blockSize_y - 1], intrepolation_factor);
                            //NoiseMap[real_x + xx, real_y + yy] = RE.EaseCurve_Perlin(NoiseMap[real_x + xx, real_y + yy]);
                        }
                    }
                }
            }

            for (int y = 0; y < NoiseMap.GetLength(1); y++)
            {
                for (int x = 0; x < NoiseMap.GetLength(0); x++)
                {
                    NoiseMap[x, y] = RE.EaseCurve_Perlin(NoiseMap[x, y]);
                }
            }


            HasNoise = true;

            displayPIBO.BackgroundImage = RE.Array_To_Bitmap(NoiseMap);
            displayPIBO.Refresh();
        }