예제 #1
0
        public ILayerMasked Do(Vector2 size)
        {
            //int maxDim = (int)JryMath.Max(size.X, size.Y);
//CONSTANT - There is a problem with values that are not 2^n wher n is element of N

            var            maxDim        = 256;
            int            gradSize      = 8;
            IInterpolation interpolation = new LinearClipped();

            Layer2DObject map   = new Layer2DObject(size);
            int           hgrid = (int)size.X;
            int           vgrid = (int)size.Y;


            float gain       = _noiseParameters.Amplitude;
            float lacunarity = _noiseParameters.Lacunarity;

            var gradients = setupGradient(gradSize);

            //set up the random numbers table
            int[] permutations = getPermutaions(maxDim);

            int maxDimMinOne   = maxDim - 1;
            int gradSizeMinOne = gradSize - 1;

            for (int i = 0; i < vgrid; i++)
            {
                for (int j = 0; j < hgrid; j++)
                {
                    float pixel_value = 0.0f;

                    float amplitude = 1.0f;
                    float frequency = 1.0f / maxDim;

                    for (int k = _noiseParameters.FromDepth; k < _noiseParameters.ToDepth; k++)
                    {
                        int x = JryMath.Floor(j * frequency);
                        int y = JryMath.Floor(i * frequency);

                        float fracX = j * frequency - x;
                        float fracY = i * frequency - y;

                        // following two lines solved the bug.
                        x += k;
                        y += k;
                        IntVector4 v              = getIndices(permutations, maxDimMinOne, gradSizeMinOne, x, y);
                        Vector2[]  grads          = getGrads(gradients, v);
                        float      interpolatedxy = biInterpolate(interpolation, grads, fracX, fracY);

                        pixel_value += interpolatedxy * amplitude;
                        amplitude   *= gain;
                        frequency   *= lacunarity;
                    }

                    //put it in the map
                    map[j, i] = pixel_value;
                }
            }
            return(map);
        }