§ Вариант 1: 2D
float rnd(float x, float y) {
float m = 43758.5453123*sin(x*12.9898 + y*78.233);
return m - floor(m);
}
float noise(float x, float y) {
float ix = floor(x), iy = floor(y);
float fx = x - ix;
float fy = y - iy;
float a = rnd(ix, iy);
float b = rnd(ix + 1, iy);
float c = rnd(ix, iy + 1);
float d = rnd(ix + 1, iy + 1);
float ux = fx*fx*(3 - 2*fx);
float uy = fy*fy*(3 - 2*fy);
return a * (1 - ux) + b * ux + (c - a) * uy * (1 - ux) + (d - b) * ux * uy;
}
float fbm(float x, float y, int octaves) {
float value = 0;
float amp = 0.5;
for (int i = 0; i < octaves; i++) {
value += amp * noise(x, y);
x *= 2;
y *= 2;
amp = amp * .5;
}
return value;
}
§ Вариант 2: 3D
struct vec3 { double x, y, z; };
struct vec4 { double x, y, z, w; };
double mix(double x, double y, double a) {
return x*(1.0-a) + y*a;
}
double noise(vec3 p)
{
vec3 i = {floor(p.x), floor(p.y), floor(p.z)};
double d = i.x + i.y*57.0 + i.z*21.0;
vec4 a = {d, d + 57.0, d + 21.0, d + 78.0};
vec3 f = {
0.5 - cos((p.x-i.x)*M_PI)/2.0,
0.5 - cos((p.y-i.y)*M_PI)/2.0,
0.5 - cos((p.z-i.z)*M_PI)/2.0,
};
a.x = mix(sin(cos(a.x)*a.x), sin(cos(1+a.x)*(1+a.x)), f.x);
a.y = mix(sin(cos(a.y)*a.y), sin(cos(1+a.y)*(1+a.y)), f.x);
a.z = mix(sin(cos(a.z)*a.z), sin(cos(1+a.z)*(1+a.z)), f.x);
a.w = mix(sin(cos(a.w)*a.w), sin(cos(1+a.w)*(1+a.w)), f.x);
a.x = mix(a.x, a.y, f.y);
a.y = mix(a.z, a.w, f.y);
return mix(a.x, a.y, f.z);
}