-
Notifications
You must be signed in to change notification settings - Fork 0
/
exoplanet.html
348 lines (313 loc) · 9.38 KB
/
exoplanet.html
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!doctype html>
<html>
<head>
<style>
body {
background-color: white;
}
#container {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
#canvas {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
</style>
</head>
<body>
<div id="container"><canvas id="canvas"></canvas></div>
<script>
// shared
var p = [151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180];
var perm = [];
(function() {
for (var i = 0; i < 512; i++) {
perm[i] = p[i & 255];
}
})();
// function fastfloor(x) {
// return (x > 0) ? (~~x) : (~~x - 1);
// }
function fastfloor(x) {
var xi = ~~x;
return x<xi ? xi-1 : xi;
}
// perlin noise
// ported from http://staffwww.itn.liu.se/~stegu/aqsis/aqsis-newnoise/noise1234.cpp
function fade(t) {
return t * t * t * ( t * ( t * 6 - 15 ) + 10 );
}
function lerp(t, a, b) {
return a + t * ( b - a );
}
function grad(hash, x, y) {
var h = hash & 7; // Convert low 3 bits of hash code
var u = h<4 ? x : y; // into 8 simple gradient directions,
var v = h<4 ? y : x; // and compute the dot product with (x,y).
return ((h&1)? -u : u) + ((h&2)? -2.0*v : 2.0*v);
}
function perlinNoise(x, y) {
var ix0, iy0, ix1, iy1,
fx0, fy0, fx1, fy1,
s, t, nx0, nx1, n0, n1;
ix0 = fastfloor(x); // Integer part of x
iy0 = fastfloor(y); // Integer part of y
fx0 = x - ix0; // Fractional part of x
fy0 = y - iy0; // Fractional part of y
fx1 = fx0 - 1.0;
fy1 = fy0 - 1.0;
ix1 = (ix0 + 1) & 0xff; // Wrap to 0..255
iy1 = (iy0 + 1) & 0xff;
ix0 = ix0 & 0xff;
iy0 = iy0 & 0xff;
t = fade(fy0);
s = fade(fx0);
nx0 = grad(perm[ix0 + perm[iy0]], fx0, fy0);
nx1 = grad(perm[ix0 + perm[iy1]], fx0, fy1);
n0 = lerp(t, nx0, nx1);
nx0 = grad(perm[ix1 + perm[iy0]], fx1, fy0);
nx1 = grad(perm[ix1 + perm[iy1]], fx1, fy1);
n1 = lerp(t, nx0, nx1);
return 0.507 * lerp(s, n0, n1);
}
// simplex noise
// ported from http://webstaff.itn.liu.se/~stegu/simplexnoise/SimplexNoise.java
var permMod8 = [];
(function() {
for (var i = 0; i < 512; i++) {
permMod8[i] = perm[i] % 8;
}
})();
var grad2 = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [1, 0],
[-1, 1], [0, 1], [1, 1]
];
// Skewing and unskewing factors for 2 dimensions
var F2 = 0.5*(Math.sqrt(3.0)-1.0);
var G2 = (3.0-Math.sqrt(3.0))/6.0;
function dot(g, x, y) {
return g[0]*x + g[1]*y;
}
function simplexNoise(xin, yin) {
var n0, n1, n2; // Noise contributions from the three corners
// Skew the input space to determine which simplex cell we're in
var s = (xin+yin)*F2; // Hairy factor for 2D
var i = fastfloor(xin+s);
var j = fastfloor(yin+s);
var t = (i+j)*G2;
var X0 = i-t; // Unskew the cell origin back to (x,y) space
var Y0 = j-t;
var x0 = xin-X0; // The x,y distances from the cell origin
var y0 = yin-Y0;
// For the 2D case, the simplex shape is an equilateral triangle.
// Determine which simplex we are in.
var i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1)
else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1)
// A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
// a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
// c = (3-sqrt(3))/6
var x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
var y1 = y0 - j1 + G2;
var x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
var y2 = y0 - 1.0 + 2.0 * G2;
// Work out the hashed gradient indices of the three simplex corners
var ii = i & 255;
var jj = j & 255;
var gi0 = permMod8[ii+perm[jj]];
var gi1 = permMod8[ii+i1+perm[jj+j1]];
var gi2 = permMod8[ii+1+perm[jj+1]];
// Calculate the contribution from the three corners
var t0 = 0.5 - x0*x0-y0*y0;
if(t0<0) n0 = 0.0;
else {
t0 *= t0;
n0 = t0 * t0 * dot(grad2[gi0], x0, y0);
}
var t1 = 0.5 - x1*x1-y1*y1;
if(t1<0) n1 = 0.0;
else {
t1 *= t1;
n1 = t1 * t1 * dot(grad2[gi1], x1, y1);
}
var t2 = 0.5 - x2*x2-y2*y2;
if(t2<0) n2 = 0.0;
else {
t2 *= t2;
n2 = t2 * t2 * dot(grad2[gi2], x2, y2);
}
// Add contributions from each corner to get the final noise value.
// The result is scaled to return values in the interval [-1,1].
return 70.0 * (n0 + n1 + n2);
}
// adapted from https://code.google.com/p/battlestar-tux/source/browse/procedural/simplexnoise.cpp
// and https://code.google.com/p/fractalterraingeneration/wiki/Fractional_Brownian_Motion
function octaveNoise(noise, octaves, gain, frequency, lacunarity, x, y) {
var amplitude = gain;
var maxAmplitude = 0;
var total = 0;
for (var i = 0; i < octaves; i++) {
total += noise(x * frequency, y * frequency) * amplitude;
frequency *= lacunarity;
maxAmplitude += amplitude;
amplitude *= gain;
}
return total / maxAmplitude;
}
function generateNoise(minX, minY, width, height, options, callback) {
var opts = options || {};
var octaves = opts.octaves || 1;
var gain = opts.gain || 0.5;
var frequency = opts.frequency || 1/256;
var lacunarity = opts.lacunarity || 2.0;
for (var y = minY, maxY = minY + height; y < maxY; y++) {
for (var x = minX, maxX = minX + width; x < maxX; x++) {
callback(x, y, octaveNoise(simplexNoise, octaves, gain, frequency, lacunarity, x, y));
}
}
}
// ---
var container = document.getElementById('container');
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var options = {
octaves: 8,
gain: 0.55,
frequency: 1/750,
lacunarity: 1.8715
};
var seaLevel = 0-.1;
var TileSize = 256;
var tiles = {};
var viewportRect = { x: 0, y: 0, w: 0, h: 0 };
function setPixel(data, p, r, g, b, a) {
data[p ] = r;
data[p+1] = g;
data[p+2] = b;
data[p+3] = a;
}
function createTile(i, j) {
var imageData = ctx.createImageData(TileSize, TileSize);
var data = imageData.data;
var width = imageData.width;
var height = imageData.height;
var minX = i * TileSize;
var minY = j * TileSize;
generateNoise(minX, minY, width, height, options,
function (x, y, noise) {
var p = 4 * ((y-minY) * width + (x-minX));
if (noise < seaLevel) {
var depth = seaLevel - noise;
setPixel(data, p, 0, 50-depth*50, 200-depth*150, 255);
} else {
var alt = noise - seaLevel;
setPixel(data, p, alt*30, 120-alt*90, alt*30, 255);
}
});
return { imageData: imageData };
}
function getTile(i, j) {
var tileKey = 'tile_' + i + '_' + j;
var tile = tiles[tileKey];
if (!tile) {
tile = createTile(i, j);
tiles[tileKey] = tile;
}
return tile;
}
function render() {
var x = viewportRect.x;
var y = viewportRect.y;
var w = viewportRect.w;
var h = viewportRect.h;
var imin = fastfloor(x/TileSize);
var jmin = fastfloor(y/TileSize);
var imax = fastfloor((x+w)/TileSize);
var jmax = fastfloor((y+h)/TileSize);
for (var i = imin; i <= imax; i++) {
for(var j = jmin; j <= jmax; j++) {
var tile = getTile(i, j);
ctx.putImageData(tile.imageData, i * TileSize - x, j * TileSize - y);
}
}
ctx.fillStyle = 'white';
ctx.fillText(
'('+x+','+y+','+w+','+h+')'+
' f='+options.frequency+' g='+options.gain+
' l='+options.lacunarity+' o='+options.octaves+
' s='+seaLevel,
10, 20);
}
function updateCanvasSize() {
viewportRect.w = canvas.width = container.clientWidth;
viewportRect.h = canvas.height = container.clientHeight;
render();
}
updateCanvasSize();
window.onresize = updateCanvasSize;
window.onkeydown = function (event) {
switch (event.keyCode) {
case 37: // left arrow
viewportRect.x -= 100;
break;
case 38: // up arrow
viewportRect.y -= 100;
break;
case 39: // right arrow
viewportRect.x += 100;
break;
case 40: // down arrow
viewportRect.y += 100;
break;
case 70: // f
options.frequency *= event.shiftKey ? 0.99 : 1.01;
tiles = {};
break;
case 71: // g
options.gain *= event.shiftKey ? 0.99 : 1.01;
tiles = {};
break;
case 76: // l
options.lacunarity *= event.shiftKey ? 0.99 : 1.01;
tiles = {};
break;
case 79: // o
options.octaves += event.shiftKey ? -1 : 1;
if (options.octaves < 1) options.octaves = 1;
tiles = {};
break;
case 83: // s
seaLevel += event.shiftKey ? -0.05 : 0.05;
if (seaLevel < -1) seaLevel = 1;
if (seaLevel > 1) seaLevel = 1;
tiles = {};
break;
default:
console.log(event.keyCode);
}
render();
};
</script>
</body>
</html>