MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / noise

Method noise

Source/Utils/SimplexIslands.hpp:76–127  ·  view source on GitHub ↗

2D simplex noise

Source from the content-addressed store, hash-verified

74
75 // 2D simplex noise
76 double noise(double xin, double yin) {
77 double n0, n1, n2; // Noise contributions from the three corners
78 // Skew the input space to determine which simplex cell we're in
79 double s = (xin + yin)*F2; // Hairy factor for 2D
80 int i = fastfloor(xin + s);
81 int j = fastfloor(yin + s);
82 double t = (i + j)*G2;
83 double X0 = i - t; // Unskew the cell origin back to (x,y) space
84 double Y0 = j - t;
85 double x0 = xin - X0; // The x,y distances from the cell origin
86 double y0 = yin - Y0;
87 // For the 2D case, the simplex shape is an equilateral triangle.
88 // Determine which simplex we are in.
89 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
90 if (x0 > y0) { i1 = 1; j1 = 0; } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
91 else { i1 = 0; j1 = 1; } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
92 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
93 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
94 // c = (3-sqrt(3))/6
95 double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
96 double y1 = y0 - j1 + G2;
97 double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
98 double y2 = y0 - 1.0 + 2.0 * G2;
99 // Work out the hashed gradient indices of the three simplex corners
100 int ii = i & 255;
101 int jj = j & 255;
102 int gi0 = permMod12[ii + perm[jj]];
103 int gi1 = permMod12[ii + i1 + perm[jj + j1]];
104 int gi2 = permMod12[ii + 1 + perm[jj + 1]];
105 // Calculate the contribution from the three corners
106 double t0 = 0.5 - x0 * x0 - y0 * y0;
107 if (t0 < 0) n0 = 0.0;
108 else {
109 t0 *= t0;
110 n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
111 }
112 double t1 = 0.5 - x1 * x1 - y1 * y1;
113 if (t1 < 0) n1 = 0.0;
114 else {
115 t1 *= t1;
116 n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
117 }
118 double t2 = 0.5 - x2 * x2 - y2 * y2;
119 if (t2 < 0) n2 = 0.0;
120 else {
121 t2 *= t2;
122 n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
123 }
124 // Add contributions from each corner to get the final noise value.
125 // The result is scaled to return values in the interval [-1,1].
126 return 70.0 * (n0 + n1 + n2);
127 }
128
129 std::vector<std::vector<float>> generateOctavedSimplexNoise(int width, int height, int octaves, float roughness, float scale, short seed) {
130 std::vector<std::vector<float>> totalNoise;

Callers

nothing calls this directly

Calls 1

fastfloorFunction · 0.85

Tested by

no test coverage detected