Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Класс для генерации Шума Перлина в двумерном пространстве
- /// </summary>
- public class PerlinNoiseGenerator
- {
- public double GetNoise(int x, int y)
- {
- int n = x + y * 57;
- n = (n << 13) ^ n;
- return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
- }
- private double SmoothNoise(double X, double Y)
- {
- int x = (int)X, y = (int)Y;
- double corners = (GetNoise(x - 1, y - 1) + GetNoise(x + 1, y - 1) + GetNoise(x - 1, y + 1) + GetNoise(x + 1, y + 1)) / 16;
- double sides = (GetNoise(x - 1, y) + GetNoise(x + 1, y) + GetNoise(x, y - 1) + GetNoise(x, y + 1)) / 8;
- double center = GetNoise(x, y) / 4;
- return corners + sides + center;
- }
- private double CompileNoise(double x, double y)
- {
- int integer_X = (int)x;
- double fractional_X = x - integer_X;
- int integer_Y = (int)y;
- double fractional_Y = y - integer_Y;
- double v1 = SmoothNoise(integer_X, integer_Y);
- double v2 = SmoothNoise(integer_X + 1, integer_Y);
- double v3 = SmoothNoise(integer_X, integer_Y + 1);
- double v4 = SmoothNoise(integer_X + 1, integer_Y + 1);
- double i1 = CosineInterpolation(v1, v2, fractional_X);
- double i2 = CosineInterpolation(v3, v4, fractional_X);
- return CosineInterpolation(i1, i2, fractional_Y);
- }
- public double GenerateNoise(double x, double y, double factor)
- {
- double total = 0;
- // это число может иметь и другие значения хоть cosf(sqrtf(2))*3.14f
- // главное чтобы было красиво и результат вас устраивал
- double persistence = 0.5f;
- // экспериментируйте с этими значениями, попробуйте ставить
- // например sqrtf(3.14f)*0.25f или что-то потяжелее для понимания J)
- double frequency = 0.1f;
- double amplitude = 0.5;//амплитуда, в прямой зависимости от значения настойчивости
- // рандомизация
- x += (factor);
- y += (factor);
- // NUM_OCTAVES - переменная, которая обозначает число октав,
- // чем больше октав, тем лучше получается шум
- int NUM_OCTAVES = 3;
- for (int i = 0; i < NUM_OCTAVES; i++)
- {
- total += CompileNoise(x * frequency, y * frequency) * amplitude;
- amplitude *= persistence;
- frequency *= 2;
- }
- total = Math.Abs(total);
- return total;
- }
- public double GenerateNoise(double x, double y, double factor, double persistence, double frequency, double amplitude)
- {
- double total = 0;
- // рандомизация
- x += (factor);
- y += (factor);
- // NUM_OCTAVES - переменная, которая обозначает число октав,
- // чем больше октав, тем лучше получается шум
- int NUM_OCTAVES = 3;
- for (int i = 0; i < NUM_OCTAVES; i++)
- {
- total += CompileNoise(x * frequency, y * frequency) * amplitude;
- amplitude *= persistence;
- frequency *= 2;
- }
- total = Math.Abs(total);
- return total;
- }
- private double SimpleInterpolation(double a, double b, double x)
- {
- return a * (1 - x) + b * x;
- }
- private double CosineInterpolation(double a, double b, double x)
- {
- double ft = x * 3.1415927;
- double f = (1 - Math.Cos(ft)) * 0.5;
- return a * (1 - f) + b * f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement