-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3d.html
696 lines (695 loc) · 26.2 KB
/
3d.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<!doctype html>
<html lang="en">
<head>
<title>Quasicrystal 3D</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#000000" />
<link rel="stylesheet" type="text/css" href="icons/webfont.css" />
<link rel="stylesheet" type="text/css" href="umbra.css" />
<link rel="icon" sizes="400x400" href="thumb-3d.png" />
<style type="text/css">
html, body {
width: 100%; height: 100%; margin: 0px; padding: 0px;
overflow: hidden;
}
body { background: #000; }
canvas {
position: absolute;
top: 0px; left: 0px; bottom: 0px; right: 0px;
}
canvas#main { z-index: 0; }
canvas#emulation { z-index: -1; }
#config .box { text-align: center; }
.box {
background: rgba(15, 15, 15, 0.9);
box-shadow: 0em 0em 2em 0em rgba(255, 255, 255, 0.5);
}
.license { opacity: 0.75; }
h2 { text-align: center; }
#help h2, #config h2 { text-align: left; }
form ul {
list-style-type: none; text-align: left; padding-left: 0em;
margin-bottom: 0em;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="hammer.min.js"></script>
<script type="text/javascript" src="umbra.min.js"></script>
<script type="text/javascript" src="anima.min.js"></script>
<script type="text/javascript" src="linear.js"></script>
<script type="text/javascript" src="latticehelper.js"></script>
<script type="text/javascript">
var awaitFlags = function (target_flags, func) {
// Run func once, when all target_flags have been hit.
// Optionally, set .done to a function that will be
// passed the return value of func.
var already_run = 0;
var flags_hit = 0;
var f = function (flag) {
if (already_run) { return; }
flags_hit |= flag;
if ((flags_hit & target_flags) == target_flags) {
already_run = 1;
return f.done(func());
}
};
f.done = function (x) { return x; };
return f;
};
var fillCircle = function (context, x, y, r) {
context.beginPath();
context.arc(x, y, r, 0, Math.PI*2, true);
context.fill();
};
var setupMovable = function () {
var m = new Anima.Movable3D();
m.uFog = [2, 1];
m.isMoving = function () {
if (!(V.isZero(this.uOriginExtra))) { return true; }
return Anima.Movable3D.prototype.isMoving.call(this);
};
m.moveReset = function () {
Anima.Movable3D.prototype.moveReset.call(this);
this.radius = this.radius || 2;
this.uDotSize = this.uDotSize || 0.2;
this.uVariance = this.uVariance || 0.4;
this.uOriginExtra = [0, 0, 0];
this.uFog[1] = 1;
this.uBTMatrix = [
[1,0,0,0],
[0,1,0,0],
[0,0,1,-this.radius],
[0,0,0,1]
];
if (window.lattice_helper) { window.lattice_helper.reset(); }
$('#config_fog').prop('checked', true);
$('#config_fp').prop('checked', false);
};
m.moveFly = function (distance) {
Anima.Movable3D.prototype.moveFly.call(this, distance);
for (var i = 0; i < 3; i++) {
window.lattice_helper.offset[i] = this.origin[i];
}
};
m.moveZoom = function (amount) {
window.lattice_helper.zoom(amount);
this.scale = window.lattice_helper.scale[0];
};
m.moveAdjust = function () {
// Viewpoint yoinging
var dist_extra = Math.sqrt(V.dot(this.uOriginExtra, this.uOriginExtra));
if ((!dist_extra) || (dist_extra <= 0.01)) {
this.uOriginExtra = [0, 0, 0];
} else if (dist_extra <= 0.1) {
this.uOriginExtra = V.scale(this.uOriginExtra, 1 - 0.005/dist_extra);
} else {
this.uOriginExtra = V.scale(this.uOriginExtra, 0.95);
}
// Rezooming
window.lattice_helper.zoomAdjust();
// Recentering
window.lattice_helper.recenter();
// Synchronize lattice_helper and movable
for (var i = 0; i < 3; i++) {
this.origin[i] = window.lattice_helper.offset[i];
}
this.scale = window.lattice_helper.scale[0];
};
m.moveReset();
m.bindKeyboard(window);
m.bindTouch($('#main'));
var h = m.hammer;
h.add(new Hammer.Tap({event: 'tripletap', taps: 3, posThreshold: 20}).recognizeWith(h.get('pan')));
h.add(new Hammer.Tap({event: 'doubletap', taps: 2, posThreshold: 20}).recognizeWith(h.get('pan')).requireFailure(h.get('tripletap')));
h.on('doubletap', function (e) {
var i = window.glc.pick(
e.center.x/$(window).width(),
e.center.y/$(window).height()
);
if (!i) { return; }
// Move origin
var new_origin = Array.prototype.slice.call(glc.glData,
(i-1)*6*11, (i-1)*6*11 + 3);
window.movable.uOriginExtra = V.add(
window.movable.uOriginExtra, V.add(
window.lattice_helper.offset,
new_origin));
for (var j = 0; j < 3; j++) {
window.lattice_helper.offset[j] = window.movable.origin[j] = -new_origin[j];
}
window.movable.velocity[4] = 0;
window.frame_manager.requestFrame();
});
h.on('tripletap', function (e) {
$('#config_fp').click();
});
return m;
};
var setupForm = function () {
$('#config_fp').on('change', function (e) {
var r = 0;
if ($(this).prop('checked')) {
r = window.movable.uBTMatrix[2][3];
window.movable.uBTMatrix[2][3] = 0;
} else {
r = window.movable.uBTMatrix[2][3] + window.movable.radius;
window.movable.uBTMatrix[2][3] = -window.movable.radius;
}
// Animate transition
window.movable.uOriginExtra = V.add(
window.movable.uOriginExtra,
M.mul(M.transpose(window.movable.rotation),
[0, 0, r]));
window.frame_manager.requestFrame();
});
$('#config_fog').on('change', function (e) {
if ($(this).prop('checked')) {
window.movable.uFog[1] = 1;
} else {
window.movable.uFog[1] = -1;
}
window.frame_manager.requestFrame();
});
$('#config_reset').on('click', function (e) {
window.movable.moveReset();
window.frame_manager.requestFrame();
});
$('#config_addpoints').on('click', function (e) {
window.lattice_worker.postMessage({type: 'addVerts', iterations: 5});
});
};
$(document).ready(function () {
// MessageBox
window.message_box = Umbra.MessageBox('messages');
// Motion
window.movable = setupMovable();
setupForm();
// Animation
var glc = new Anima.GL();
// Set up
glc.glData = new Float32Array([]);
glc.mem_limit = undefined;
glc.num_verts = 0;
glc.num_verts_avail = 0;
glc.oom = function () {
var e;
while (e = this.gl.getError()) {
if (e == this.gl.OUT_OF_MEMORY) { return true; }
}
return false;
};
glc.updateData = function (glData) {
var gl = this.gl;
var t = this;
this.num_verts = this.num_verts || 1;
function saveData(glData) {
t.glData = glData.subarray(0, t.mem_limit || glData.length);
t.num_verts_avail = t.glData.length/11;
}
saveData(glData);
if (gl) {
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertex_buffer);
gl.bufferData(gl.ARRAY_BUFFER, this.glData, gl.STATIC_DRAW);
while (this.oom()) {
this.memory_full = true;
this.mem_limit = 66 * Math.ceil((this.mem_limit || glData.length)/132);
saveData(glData);
this.setupCallback();
}
}
window.frame_manager.requestFrame();
};
glc.setupCallback = function () {
var gl = this.gl;
gl.clearColor(0, 0, 0, 0);
gl.enable(gl.BLEND);
gl.disable(gl.DEPTH_TEST);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
gl.depthFunc(gl.LEQUAL);
// Upload data
this.vertex_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertex_buffer);
gl.bufferData(gl.ARRAY_BUFFER, this.glData, gl.STATIC_DRAW);
this.frame_buffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frame_buffer);
var rt = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, rt);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
var rb = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, rb);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, 1, 1);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rt, 0);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, rb);
this.last_capture = new Uint8Array(4);
// Resize
this.resize($(window).innerWidth(), $(window).innerHeight());
};
glc.resizeCallback = function (w, h) {
window.movable.setScreenCenter($('#main'));
window.movable.screen_diameter = this.screen_diameter = Math.sqrt(w*w + h*h);
var ir = window.movable.units_per_px = 3/this.screen_diameter;
window.movable.touch_map.pan_x.amount = -ir;
window.movable.touch_map.pan_y.amount = -ir;
this.setProjectionMatrix(ir*w/2, ir*h/2, 0.1, 1000);
this.uPMatrix = M.fromGL4(this.projection_matrix);
window.frame_manager.requestFrame();
};
glc.prepareDrawData = function (prog) {
var gl = glc.gl;
gl.useProgram(prog);
// Enable vertex buffers
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertex_buffer);
var pos = gl.getAttribLocation(prog, 'aVPosition');
gl.enableVertexAttribArray(pos);
gl.vertexAttribPointer(pos, 3, gl.FLOAT, false, 44, 0);
var clr = gl.getAttribLocation(prog, 'aVColor');
gl.enableVertexAttribArray(clr);
gl.vertexAttribPointer(clr, 3, gl.FLOAT, false, 44, 12);
var tex = gl.getAttribLocation(prog, 'aVTexCoord');
gl.enableVertexAttribArray(tex);
gl.vertexAttribPointer(tex, 2, gl.FLOAT, false, 44, 24);
var vid = gl.getAttribLocation(prog, 'aVID');
if (vid >= 0) {
gl.enableVertexAttribArray(vid);
gl.vertexAttribPointer(vid, 3, gl.FLOAT, false, 44, 32);
}
// Set uniforms
gl.uniformMatrix4fv(
gl.getUniformLocation(prog, 'uPMatrix'),
false, this.projection_matrix);
gl.uniformMatrix4fv(
gl.getUniformLocation(prog, 'uMVMatrix'),
false, M.toGL4(
M.mulMats(window.movable.uBTMatrix,
window.movable.rotation))
);
gl.uniform2f(gl.getUniformLocation(prog, 'uScale'),
window.lattice_helper.scale[0], window.lattice_helper.scale[3]);
gl.uniform2fv(gl.getUniformLocation(prog, 'uFog'),
new Float32Array(window.movable.uFog));
gl.uniform3f(gl.getUniformLocation(prog, 'uDotSize'),
window.movable.uDotSize, // straight-up dot size
-0.25 / window.movable.uVariance, // variance
3 / window.movable.uVariance); // color scaling
gl.uniform3fv(gl.getUniformLocation(prog, 'uOrigin'),
new Float32Array(V.add(
window.lattice_helper.offset,
window.movable.uOriginExtra)));
gl.uniform3fv(gl.getUniformLocation(prog, 'uOriginColor'),
new Float32Array(window.lattice_helper.offset.slice(3,6)));
};
glc.pick = function (x, y) {
var gl = glc.gl;
gl.disable(gl.BLEND);
gl.enable(gl.DEPTH_TEST);
gl.bindFramebuffer(gl.FRAMEBUFFER, glc.frame_buffer);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
glc.prepareDrawData(glc.shaders.picker);
gl.uniformMatrix4fv(
gl.getUniformLocation(glc.shaders.picker, 'uPMatrix'),
false, M.toGL4(M.mulMats([
[10, 0, 0, 9-10*2*x],
[0, -10, 0, 9-10*2*y],
[0, 0, 1, 0],
[0, 0, 0, 1],
], this.uPMatrix)));
gl.drawArrays(gl.TRIANGLES, 0, Math.min(glc.num_verts, (1 << 21) * 6 - 6));
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, glc.last_capture);
var clr = Array.prototype.slice.call(glc.last_capture);
return clr[0] + (clr[1] << 7) + (clr[2] << 14);
};
glc.draw = function (time_scheduled) {
var gl = glc.gl;
if (!gl) { return false; }
window.movable.update(this.dt);
if (!window.lattice_helper) { return; }
window.movable.moveAdjust();
gl.enable(gl.BLEND);
gl.disable(gl.DEPTH_TEST);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.clear(gl.COLOR_BUFFER_BIT);
glc.prepareDrawData(glc.shaders.stars);
// Draw!
this.selectLOD('realtime');
glc.num_verts = Math.min(this.lod.num_verts, glc.num_verts_avail);
gl.drawArrays(gl.TRIANGLES, 0, glc.num_verts);
if (!V.isZero(window.movable.uOriginExtra)) {
glc.prepareDrawData(glc.shaders.locker);
gl.uniform3f(gl.getUniformLocation(glc.shaders.locker, 'uDotSize'),
2*window.movable.uDotSize, 0, 0);
gl.uniform3fv(gl.getUniformLocation(glc.shaders.locker, 'uOrigin'),
new Float32Array(window.movable.uOriginExtra));
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
this.adjustLOD();
return window.movable.isMoving();
};
glc.emulatedProjection = function (f) {
return (function () {
var w = $(window).width();
var h = $(window).height();
var c2 = $('#emulation')[0].getContext('2d');
c2.globalCompositeOperation = 'lighter';
var m = M.mulMats(glc.uPMatrix,
M.mulMats(window.movable.uBTMatrix, window.movable.rotation));
var uo = V.add(window.lattice_helper.offset,
window.movable.uOriginExtra);
var usc = window.lattice_helper.scale[3];
var udv = -0.25 / window.movable.uVariance;
var udz = 3 / Math.sqrt(window.movable.uVariance);
var uds = window.movable.uDotSize * Math.sqrt(w*w + h*h) / 6;
function scaleColor(x) { return 1 / (1 + x * x * udz); }
function to255(x) { return Math.round(x * 255); }
for (var i = 0; i < glc.num_verts / 6; i++) {
var v = Array.prototype.slice.call(glc.glData, i*66, i*66 + 6);
var p = M.mul(m, V.scale(V.add(v, uo), window.lattice_helper.scale[0]).concat(1));
if (!(Math.abs(p[2]/p[3]) < 1)) { continue; }
var c = V.scale(V.add(v.slice(3,6), window.lattice_helper.offset.slice(3,6)), usc);
var clr = c.map(scaleColor);
clr = V.scale(clr, 1 / Math.max.apply(Math, clr)).map(to255);
c2.fillStyle = 'rgb(' + clr.join(',') + ')';
f(c2, p, uds * Math.exp(V.dot(c, c) * udv) / p[3], i);
}
}).bind(this);
};
glc.emulatedDraw = function (time_scheduled) {
window.movable.update(this.dt);
if (!window.lattice_helper) { return; }
window.movable.moveAdjust();
var w = $('#emulation')[0].width = $(window).width();
var h = $('#emulation')[0].height = $(window).height();
var moving = window.movable.isMoving();
var realtime = moving || this.next_id || window.movable.is_hammer_busy;
if (realtime) { this.selectLOD('realtime'); }
else { this.selectLOD('hd'); }
glc.num_verts = Math.min(this.lod.num_verts, glc.num_verts_avail);
// Draw!
glc.emulatedProjection(function (context, p, radius, i) {
var x = w * p[0]/p[3];
var y = -h * p[1]/p[3];
var diameter = 2 * radius;
if (Math.abs(x) > w + diameter || Math.abs(y) > h + diameter) {
return;
}
fillCircle(context, (w + x) / 2, (h + y) / 2, radius);
})();
this.adjustLOD();
return moving;
}
glc.emulatedPick = function (x, y) {
var last_id = -1;
var last_depth = Infinity;
var w = $(window).width();
var h = $(window).height();
glc.emulatedProjection(function (context, p, radius, i) {
var v = [
w * ((1 + p[0]/p[3]) / 2 - x),
h * ((1 - p[1]/p[3]) / 2 - y)
];
if (V.dot(v,v) < radius * radius && p[3] < last_depth) {
last_id = i;
last_depth = p[3];
}
})();
return last_id + 1;
};
window.frame_manager = new Anima.FrameManager(glc.draw);
window.frame_manager.addLOD('realtime', {
frame_min: 8, frame_max: 16, num_verts: 50,
});
window.frame_manager.addLOD('hd', {
frame_min: 250, frame_max: 500, num_verts: 60000,
});
window.frame_manager.upLOD = function () {
if (this.lod.num_verts < glc.num_verts_avail) {
this.lod.num_verts = Math.min(glc.num_verts_avail,
6 + 6 * Math.floor(this.lod.num_verts / 5));
this.requestFrame();
}
};
window.frame_manager.downLOD = function () {
this.lod.num_verts = Math.min(glc.num_verts_avail,
6 * Math.ceil(this.lod.num_verts/7));
};
var setupContinue = awaitFlags(7, function () {
try {
glc.bindCanvas($('#main')[0]);
} catch (e) {
window.message_box.post('GL: ' + e, 'error');
window.message_box.post('Falling back to emulation.', 'warning');
glc.gl = null;
glc.draw = window.frame_manager.drawCallback = glc.emulatedDraw;
glc.pick = glc.emulatedPick;
glc.resize($(window).innerWidth(), $(window).innerHeight());
}
window.movable.motionCallback = function () {
window.frame_manager.requestFrame();
};
$(window).on('resize', function (e) {
glc.resize($(window).width(), $(window).height());
});
});
glc.addProgramByURLs('stars', 'vert.c', 'frag.c', setupContinue.bind(null, 1));
glc.addProgramByURLs('picker', 'vert.c', 'fpick.c', setupContinue.bind(null, 2));
glc.addProgramByURLs('locker', 'vert.c', 'flock.c', setupContinue.bind(null, 4));
window.glc = glc;
// Init lattice
var requestMorePoints = (function () {
var i = 0;
var BASE_REQUESTS = [
{type: 'addVerts', iterations: 5},
{type: 'addVerts', iterations: 5},
{type: 'addVertsShrink'},
{type: 'addVertsGrow'},
{type: 'addVertsGrow'},
{type: 'addVertsGrow'},
];
var LIMIT_SIZE = 200;
var LIMIT_TIME = 5000;
var finalized = false;
var start_time = 0;
var progress_bar;
return function (neighbor_size) {
if (finalized) {
$('#config_addpoints').prop('disabled', false);
if (progress_bar) { progress_bar.set(1); }
return;
}
if (!progress_bar) {
progress_bar = window.message_box.makeProgress('Finding points...');
progress_bar.set(0.1);
}
if (!start_time) { start_time = (new Date()).valueOf(); }
var progress = Math.max(0,
((new Date()).valueOf() - start_time)/LIMIT_TIME,
LIMIT_SIZE/neighbor_size);
progress_bar.set(0.1 + progress * 0.8);
if (progress > 1) {
finalized = true;
window.lattice_worker.postMessage({type: 'addVertsShrink'});
window.lattice_worker.postMessage({type: 'addVertsGrow'});
window.lattice_worker.postMessage({type: 'addVerts', iterations: 5});
} else {
if (i < BASE_REQUESTS.length) {
window.lattice_worker.postMessage(BASE_REQUESTS[i]);
i++;
} else {
window.lattice_worker.postMessage({type: 'addVerts', iterations: 5});
}
}
};
})();
window.lattice_worker = new Worker('quasilattice3.js');
window.lattice_worker.onmessage = function (e) {
var data = e.data;
if (data.type == 'update') {
if (!glc.memory_full) {
glc.updateData(data.glData);
}
window.lattice_helper.translators = data.translators;
movable.uFog[0] = 0.7 * data.next_weight;
requestMorePoints((glc.screen_diameter || 512) / Math.sqrt(data.next_weight));
window.message_box.post(data.source + ' ' + data.glData.length/66 + ' ' + data.next_weight, 'debug');
} else if (data.type == 'ready') {
window.lattice_helper = new LatticeHelper(
6, 3, [], data.scale_factors);
requestMorePoints();
} else if (data.type == 'message') {
window.message_box.post(data.message, data.message_class);
} else {
window.message_box.post('Window ignored message of type ' + data.type, 'debug warning');
}
};
window.lattice_worker.postMessage({type: 'init'});
});
</script>
</head>
<body>
<canvas id="main">
<img src="thumb-3d.png" alt="Preview" />
This demo requires HTML5 Canvas support.
</canvas>
<canvas id="emulation"></canvas>
<div id="messages" class="messagebox"></div>
<div id="config" class="overlay"><div class="box">
<a class="button icon button_close button_rightmargin"
title="Cancel" href="#">×</a>
<h2>Options</h2>
<form method="post" action="#" id="form_config">
<ul>
<li><input type="checkbox" id="config_fp" /><label for="config_fp">First-person view</label></li>
<li><input type="checkbox" id="config_fog" checked="checked" /><label for="config_fog">Hide incompletely-drawn regions with fog</label></li>
<li>
<button type="button" id="config_reset"
/>Reset view</button>
<button type="button" id="config_addpoints"
disabled="disabled" />Draw more points</button>
</li>
</ul>
</form>
</div></div>
<div id="help" class="overlay">
<div class="box">
<a class="button icon button_close button_rightmargin"
title="Close" href="#">×</a>
<h2>Help</h2>
<p><a href="#help_about">What am I seeing here?</a></p>
<ul>
<li><b>Turn</b>: arrow keys and
<span class="keyboard-key"><<sub>,</sub></span>,
<span class="keyboard-key">><sub>.</sub></span>
or mouse/finger dragging.</li>
<li><b>Fly</b>: <span class="keyboard-key">spacebar</span> and
<span class="keyboard-key">Z</span> or press-and-hold.</li>
<li><b>Zoom</b>: <span class="keyboard-key">+</span> and
<span class="keyboard-key">-</span>, mouse scrolling,
or pinch zoom. (Hint: lock onto a point first.)</li>
<li><b>Lock on</b> to a point: double-tap.</li>
<li>Toggle <b>first-person</b> view: triple-tap.</li>
</ul>
<p class="license">Code written by
<a href="http://pteromys.melonisland.net/">Pteromys</a>
and released under the <a href="http://unlicense.org/">Unlicense</a>.</p>
</div>
</div>
<div id="help_about" class="overlay">
<div class="box">
<a class="button icon button_leftmargin"
title="Back" href="#help">«</a>
<a class="button icon button_close button_rightmargin"
title="Close" href="#">×</a>
<h2>About</h2>
<p>This page draws the icosahedral 3-D
<a href="http://en.wikipedia.org/wiki/Quasicrystal"
>quasicrystal</a>: a thin 3-D slice of a 6-D
<a href="http://en.wikipedia.org/wiki/Lattice_(group)"
>lattice</a> with
<a href="http://en.wikipedia.org/wiki/Icosahedral_symmetry"
>icosahedral symmetry</a>. Points are sized by
their proximity to the slice and colored
by how far they extend into the 3 unrepresented dimensions
(<span style="color: #fcc;">red</span>,
<span style="color: #cfc;">green</span>, and
<span style="color: #ccf;">blue</span>).</p>
<p>This quasicrystal has two curious properties:
<ol>
<li><a href="#help_uniqueness">It's the only 3-D lattice slice
which is truly 3-D</a>—all others with any symmetry
merely repeat a 2-D pattern.</li>
<li><a href="#help_selfsimilarity">It's self-similar</a>—a
zoomed-in view is indistinguishable from a zoomed-out view.</li>
</ol></p>
</div>
</div>
<div id="help_selfsimilarity" class="overlay">
<div class="box">
<a class="button icon button_leftmargin"
title="Back" href="#help_about">«</a>
<a class="button icon button_close button_rightmargin"
title="Close" href="#">×</a>
<h2>Self-similarity</h2>
<p>Zooming in expands space by some factor \(s\).
To compensate for the spreading out of dots,
we'll make more dots touch our slice
by scaling the unseen 3 dimensions by
\(\frac{1}{s}\) (to keep the number of dots
drawn roughly constant).</p>
<p>If \(s = 2 + \sqrt{5}\), a miracle occurs: all dots
land on other dots, and it's as if we never zoomed in.
To understand why, we need to describe the 6-D
lattice in more detail.</p>
<p>Let \(\zeta\) be the 5th
<a href="http://en.wikipedia.org/wiki/Root_of_unity"
>root of unity</a> \(e^{2\pi i/5}\).
In \(\mathbf{R} \times \mathbf{C}\), the points
\[ \pm(\sqrt{5}, 0), \pm(1, 2\zeta^k) \]
(\(k = 0\) to \(4\)) are the twelve vertices of an icosahedron,
and their sums are the dots we see.
The unseen 3 dimensions are
a second set of \(\mathbf{R} \times \mathbf{C}\)
coordinates that replace \(\zeta\) with the
<a href="http://en.wikipedia.org/wiki/Conjugate_element_(field_theory)"
>algebraically indistinguishable</a> \(\zeta^2\).
As \(\sqrt{5} = 1 + 2(\zeta + \zeta^{-1})\), this also ends up
replacing \(\sqrt{5}\) by \(-\sqrt{5}\).
You can check using
<a href="http://en.wikipedia.org/wiki/Dot_product">dot products</a>
that the twelve vertices now point in six perpendicular directions
in the 6-D combined coordinate system.</p>
<p>Scaling by \(2 + \sqrt{5}\) in the first two coordinates
amounts to replacing each vertex with the sum of
twice itself and its five neighbors—thus taking dots
to other dots.
In the last two coordinates, this becomes scaling by \(2 - \sqrt{5}\),
and the miracle stems from the fact that
\[ (2 + \sqrt{5})(2 - \sqrt{5}) = -1 . \]
That is, this scaling is what we'd obtain with a zoom factor
of \(2 + \sqrt{5}\).</p>
<p>This also occurs in
<a href="2d.html#help_zooming">the
2-D quasicrystal with 5-fold symmetry</a>,
for which a related phenomenon is illustrated by the game
<a href="http://www.math.brown.edu/~res/Java/App12/test1.html"
>Lucy and Lily</a>.</p>
</div>
</div>
<div id="help_uniqueness" class="overlay">
<div class="box">
<a class="button icon button_leftmargin"
title="Back" href="#help_about">«</a>
<a class="button icon button_close button_rightmargin"
title="Close" href="#">×</a>
<h2>Uniqueness</h2>
<p>What we're looking for is a group of symmetries \(G\)
of a higher-dimensional lattice \(\mathbf{Z}^n \subset \mathbf{R}^n\),
along with a 3-plane \(V \subset \mathbf{R}^n\) (the slice)
on which \(G\) acts irreducibly (the non-2-D condition,
which amounts to not having an axis).</p>
<p>Of the <a
href="http://en.wikipedia.org/wiki/Point_groups_in_three_dimensions#The_seven_remaining_point_groups"
>seven non-axial point groups in 3 dimensions</a>,
only the two icosahedral groups don't already
preserve an ordinary 3-D lattice. These are the
<a href="http://en.wikipedia.org/wiki/Alternating_group"
>alternating group</a> \(A_5\) and the product
\(A_5 \times \{\pm 1\}\); so it suffices to find
<a href="http://groupprops.subwiki.org/wiki/Linear_representation_theory_of_alternating_group:A5"
>a list of representations</a>
of \(A_5\) over integers and real numbers.</p>
<p>(One detail remains: \(A_5\) might only
be the quotient of \(G\) by a normal subgroup \(N\).
Fortunately, the map
\[ x \mapsto \sum_{g \in N} gx \]
is an endomorphism of \(\mathbf{Z}^n\)
on whose image \(A_5\) acts.)</p>
</div>
</div>
<a class="button icon button_root" data-key="QUESTION"
href="#help" id="button_help" title="Help">?</a>
<a class="button icon button_root"
href="#config" id="button_config" title="Settings">⚙</a>
</body>
</html>