This repository has been archived by the owner on Jan 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
noisegen.js
259 lines (191 loc) · 7.34 KB
/
noisegen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var NoiseGen = {
// consts
X_NOISE_GEN: 1619,
Y_NOISE_GEN: 31337,
Z_NOISE_GEN: 6971,
SEED_NOISE_GEN: 1013,
SHIFT_NOISE_GEN: 8,
/// Generates coherent noise quickly. When a coherent-noise function with
/// NoiseGen.prototype quality setting is used to generate a bump-map image, there are
/// noticeable "creasing" artifacts in the resulting image. This is
/// because the derivative of that function is discontinuous at integer
/// boundaries.
QUALITY_FAST: 0,
/// Generates standard-quality coherent noise. When a coherent-noise
/// function with NoiseGen.prototype quality setting is used to generate a bump-map
/// image, there are some minor "creasing" artifacts in the resulting
/// image. This is because the second derivative of that function is
/// discontinuous at integer boundaries.
QUALITY_STD: 1,
/// Generates the best-quality coherent noise. When a coherent-noise
/// function with NoiseGen.prototype quality setting is used to generate a bump-map
/// image, there are no "creasing" artifacts in the resulting image. This
/// is because the first and second derivatives of that function are
/// continuous at integer boundaries.
QUALITY_BEST: 2,
intValueNoise3D: function(x, y, z, seed) {
x = parseInt(x);
y = parseInt(y);
z = parseInt(z);
seed = parseInt(seed);
// All constants are primes and must remain prime in order for this noise
// function to work correctly.
var n = parseInt((
NoiseGen.X_NOISE_GEN * x
+ NoiseGen.Y_NOISE_GEN * y
+ NoiseGen.Z_NOISE_GEN * z
+ NoiseGen.SEED_NOISE_GEN * seed)
& 0x7fffffff
);
n = (n >> 13) ^ n;
return parseFloat((n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff);
},
valueNoise3D: function(x, y, z, seed) {
return 1.0 - (NoiseGen.intValueNoise3D(parseInt(x), parseInt(y), parseInt(z), parseInt(seed)) / 1073741824.0);
},
gradientNoise3D: function(fx, fy, fz, ix, iy, iz, seed) {
if(!seed) {
seed = 1;
}
fx = parseFloat(fx);
fy = parseFloat(fy);
fz = parseFloat(fz);
ix = parseFloat(ix);
iy = parseFloat(iy);
iz = parseFloat(iz);
// Randomly generate a gradient vector given the integer coordinates of the
// input value. This implementation generates a random number and uses it
// as an index into a normalized-vector lookup table.
var vectorIndex = parseInt(
NoiseGen.X_NOISE_GEN * ix +
NoiseGen.Y_NOISE_GEN * iy +
NoiseGen.Z_NOISE_GEN * iz +
NoiseGen.SEED_NOISE_GEN * seed
) & 0xffffffff;
vectorIndex ^= (vectorIndex >> NoiseGen.SHIFT_NOISE_GEN);
vectorIndex &= 0xff;
var xvGradient = VectorTable[(vectorIndex << 2)];
var yvGradient = VectorTable[(vectorIndex << 2) + 1];
var zvGradient = VectorTable[(vectorIndex << 2) + 2];
// Set up us another vector equal to the distance between the two vectors
// passed to this function.
var xvPoint = (fx - ix);
var yvPoint = (fy - iy);
var zvPoint = (fz - iz);
// Now compute the dot product of the gradient vector with the distance
// vector. The resulting value is gradient noise. Apply a scaling value
// so that this noise value ranges from -1.0 to 1.0.
return (
(xvGradient * xvPoint) +
(yvGradient * yvPoint) +
(zvGradient * zvPoint)
) * 2.12;
},
coherentNoise3D: function(x, y, z, seed, quality, func) {
if(!func) {
throw new Error('Must provide proper interpolation function!');
}
x = parseFloat(x);
y = parseFloat(y);
z = parseFloat(z);
if(!seed) {
seed = 1;
} else {
seed = parseInt(seed);
}
if(!quality) {
quality = NoiseGen.QUALITY_STD;
} else {
quality = parseInt(quality);
}
var xi = parseInt(x);
var yi = parseInt(y);
var zi = parseInt(z);
// Create a unit-length cube aligned along an integer boundary. This cube
// surrounds the input point.
var x0 = parseFloat(x > 0.0 ? xi : x - 1);
var x1 = x0 + 1;
var y0 = parseFloat(y > 0.0 ? yi : y - 1);
var y1 = y0 + 1;
var z0 = parseFloat(z > 0.0 ? zi : z - 1);
var z1 = z0 + 1;
// Map the difference between the coordinates of the input value and the
// coordinates of the cube's outer-lower-left vertex onto an S-curve.
var xs = 0, ys = 0, zs = 0;
switch (quality) {
case NoiseGen.QUALITY_BEST:
xs = Interpolation.quinticSCurve(x - x0);
ys = Interpolation.quinticSCurve(y - y0);
zs = Interpolation.quinticSCurve(z - z0);
break;
case NoiseGen.QUALITY_STD:
xs = Interpolation.cubicSCurve(x - x0);
ys = Interpolation.cubicSCurve(y - y0);
zs = Interpolation.cubicSCurve(z - z0);
break;
default:
case NoiseGen.QUALITY_FAST:
xs = x - x0;
ys = y - y0;
zs = z - z0;
break;
}
// use provided function to interpolate
return func(x0, y0, z0, x1, y1, z1, xs, ys, zs);
},
valueCoherentNoise3D: function(x, y, z, seed, quality) {
return NoiseGen.coherentNoise3D(x, y, z, seed, quality, function(x0, y0, z0, x1, y1, z1, xs, ys, zs) {
// Now calculate the noise values at each vertex of the cube. To generate
// the coherent-noise value at the input point, interpolate these eight
// noise values using the S-curve value as the interpolant (trilinear
// interpolation.)
var n0, n1, ix0, ix1, iy0, iy1;
n0 = NoiseGen.valueNoise3D(x0, y0, z0, seed);
n1 = NoiseGen.valueNoise3D(x1, y0, z0, seed);
ix0 = Interpolation.linear(n0, n1, xs);
n0 = NoiseGen.valueNoise3D(x0, y1, z0, seed);
n1 = NoiseGen.valueNoise3D(x1, y1, z0, seed);
ix1 = Interpolation.linear(n0, n1, xs);
iy0 = Interpolation.linear(ix0, ix1, ys);
n0 = NoiseGen.valueNoise3D(x0, y0, z1, seed);
n1 = NoiseGen.valueNoise3D(x1, y0, z1, seed);
ix0 = Interpolation.linear(n0, n1, xs);
n0 = NoiseGen.valueNoise3D(x0, y1, z1, seed);
n1 = NoiseGen.valueNoise3D(x1, y1, z1, seed);
ix1 = Interpolation.linear(n0, n1, xs);
iy1 = Interpolation.linear(ix0, ix1, ys);
return Interpolation.linear(iy0, iy1, zs);
});
},
gradientCoherentNoise3D: function(x, y, z, seed, quality) {
return NoiseGen.coherentNoise3D(x, y, z, seed, quality, function(x0, y0, z0, x1, y1, z1, xs, ys, zs) {
// Now calculate the noise values at each vertex of the cube. To generate
// the coherent-noise value at the input point, interpolate these eight
// noise values using the S-curve value as the interpolant (trilinear
// interpolation.)
var n0, n1, ix0, ix1, iy0, iy1;
n0 = NoiseGen.gradientNoise3D(x, y, z, x0, y0, z0, seed);
n1 = NoiseGen.gradientNoise3D(x, y, z, x1, y0, z0, seed);
ix0 = Interpolation.linear(n0, n1, xs);
n0 = NoiseGen.gradientNoise3D(x, y, z, x0, y1, z0, seed);
n1 = NoiseGen.gradientNoise3D(x, y, z, x1, y1, z0, seed);
ix1 = Interpolation.linear(n0, n1, xs);
iy0 = Interpolation.linear(ix0, ix1, ys);
n0 = NoiseGen.gradientNoise3D(x, y, z, x0, y0, z1, seed);
n1 = NoiseGen.gradientNoise3D(x, y, z, x1, y0, z1, seed);
ix0 = Interpolation.linear(n0, n1, xs);
n0 = NoiseGen.gradientNoise3D(x, y, z, x0, y1, z1, seed);
n1 = NoiseGen.gradientNoise3D(x, y, z, x1, y1, z1, seed);
ix1 = Interpolation.linear(n0, n1, xs);
iy1 = Interpolation.linear(ix0, ix1, ys);
return Interpolation.linear(iy0, iy1, zs);
});
}
};
if(module) {
var Interpolation = require('./interpolation');
var VectorTable = require('./vectortable');
module.exports = NoiseGen;
} else {
require(['interpolation', 'vectortable']);
}