-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-three.js
709 lines (544 loc) · 20.9 KB
/
main-three.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
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
697
698
699
700
701
702
703
704
705
706
707
708
709
const scenes = [];
let renderer;
let meshShape;
let meshSphere;
let meshPlane;
let linesMeshShape;
let linesMeshSphere;
let linesMeshPlane;
const allDatas = [
data1,
data2,
data3
];
const selector = document.getElementById('select-db');
selector.onchange = (opt) => {
optionSelected = Number(opt.target.value);
resetDB();
};
const finished = [false, false, false];
let optionSelected = Number(selector.value);
if (optionSelected > 0) {
document.getElementById('subject-legend').hidden = false;
document.getElementById('document-legend').hidden = false;
} else {
document.getElementById('subject-legend').hidden = true;
document.getElementById('document-legend').hidden = true;
}
let checkbox = document.getElementById('checkbox');
checkbox.disabled = true;
let myDataShape = copyData3D({...allDatas[optionSelected]});
let myDataSphere = copyData3D({...allDatas[optionSelected]});
let myDataPlane = copyData3D({...allDatas[optionSelected]});
let particles = myDataShape.nodes.length;
let numLines = myDataShape.edges.length;
let radius = Math.min(1000, Math.max(100, Math.sqrt(particles * Math.PI * 20) * 2));
const myColors = [
[255, 255, 0],
[0, 0, 255],
[0, 120, 0],
[0, 120, 120],
[0, 120, 255],
[120, 0, 0],
[120, 120, 0],
[120, 120, 255],
[120, 255, 0],
[120, 255, 120],
[120, 255, 255],
];
const canvas = document.getElementById( "c" );
const template = document.getElementById( "template" ).text;
const content = document.getElementById( "content" );
const pxRatio = window.devicePixelRatio;
const h = canvas.clientHeight;
const w = canvas.clientWidth / 3;
const W = w * pxRatio;
const H = h * pxRatio;
const Rmax = pxRatio;
const Z = Math.max(W / 2 + Rmax, H / 2 + Rmax);
const width = canvas.clientWidth / 2;
const height = canvas.clientHeight;
const vertexShader = [
'precision highp float;\n' +
'\n' +
'uniform mat4 modelViewMatrix;\n' +
'uniform mat4 projectionMatrix;\n' +
'\n' +
'attribute vec3 position;\n' +
'\n' +
'void main() {\n' +
'\n' +
' gl_Position = projectionMatrix * modelViewMatrix * vec4(position.xyz , 1.0);\n' +
'\n' +
'}'
].join('\n');
const fragmentShader = [
'precision highp float;',
'',
'uniform float opacity;',
'uniform vec3 color;',
'',
'void main() {',
'',
' gl_FragColor = vec4(color, opacity);',
'',
'}'
].join('\n');
const uniforms = {
opacity: { value: 0.25 },
color: {value: new THREE.Vector3(0.18, 0.18, 0.18)}
};
const linesMaterial = new THREE.RawShaderMaterial( {
uniforms: uniforms,
vertexShader: vertexShader,
fragmentShader: fragmentShader,
transparent: true,
});
const vertexShaderNode = [
' precision highp float;',
'',
' uniform mat4 modelViewMatrix;',
' uniform mat4 projectionMatrix;',
'',
' attribute vec3 position;',
' attribute vec3 color;',
'',
' varying vec3 vColor;',
'',
' void main() {',
'',
' vColor = color;',
' gl_Position = projectionMatrix * modelViewMatrix * vec4(position.xyz , 1.0);',
'',
' gl_PointSize = (' + Rmax.toString() + '.0 * 30.0) * (300.0 / length(gl_Position.xyz));',
'',
' }'
].join('\n');
const fragmentShaderNode = [
' precision highp float;',
'',
' uniform sampler2D map;',
'',
' varying vec3 vColor;',
'',
' void main() {',
'',
' vec2 uv = vec2(gl_PointCoord.x, 1.0 - gl_PointCoord.y);',
' vec4 textureColor = vec4(texture2D(map, uv));',
' gl_FragColor = vec4(textureColor * vec4(vColor, 1.0));',
'',
' if (gl_FragColor.a >= 0.6) gl_FragColor.a = 1.0;',
'',
' if (gl_FragColor.a < 0.6) discard;',
'',
' }'
].join('\n');
const uniformsNode = {
map: { type: 't', value: new THREE.TextureLoader().load('https://i.imgur.com/JXC2TVB.png')},
};
const nodeMaterial = new THREE.RawShaderMaterial( {
uniforms: uniformsNode,
vertexShader: vertexShaderNode,
fragmentShader: fragmentShaderNode,
transparent: true
});
init();
animate();
function resetDB() {
if (optionSelected > 0) {
document.getElementById('subject-legend').hidden = false;
document.getElementById('document-legend').hidden = false;
} else {
document.getElementById('subject-legend').hidden = true;
document.getElementById('document-legend').hidden = true;
}
myDataSphere = copyData3D({...allDatas[optionSelected]});
myDataShape = copyData3D({...allDatas[optionSelected]});
myDataPlane = copyData3D({...allDatas[optionSelected]});
particles = myDataShape.nodes.length;
numLines = myDataShape.edges.length;
radius = Math.min(1000, Math.max(100, Math.sqrt(particles * Math.PI * 20) * 2));
selector.disabled = true;
checkbox.disabled = true;
startShape();
startPlane();
startSphere();
}
function init() {
selector.disabled = true;
// SHAPE (LEFT)
initShape();
// PLANE (center)
initPlane();
// SPHERE (RIGHT)
initSphere();
renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
renderer.setClearColor( 0xffffff, 1 );
renderer.setPixelRatio( window.devicePixelRatio );
}
function initShape() {
const scene = new THREE.Scene();
const element = document.createElement( "div" );
element.className = "list-item";
element.innerHTML = template.replace( '$', 'SHAPE');
scene.userData.element = element.querySelector( ".scene" );
content.appendChild( element );
const camera = new THREE.PerspectiveCamera(60, width / height, 1, 10000);
camera.position.z = Z + 400;
camera.lookAt(scene.position);
scene.userData.camera = camera;
const controls = new THREE.OrbitControls(scene.userData.camera, scene.userData.element);
controls.enablePan = true;
controls.zoomSpeed = 1.2;
controls.enableZoom = true;
scene.userData.controls = controls;
scenes.push(scene);
// ADD SHAPE
startShape();
}
function initSphere() {
const scene = new THREE.Scene();
const element = document.createElement( "div" );
element.className = "list-item";
element.innerHTML = template.replace( '$', 'SPHERE');
scene.userData.element = element.querySelector( ".scene" );
content.appendChild( element );
const camera = new THREE.PerspectiveCamera(60, width / height, 1, 10000);
camera.position.z = radius + 400;
camera.lookAt(scene.position);
camera.zoomSpeed = 1.2;
camera.enableZoom = true;
scene.userData.camera = camera;
const controls = new THREE.OrbitControls(scene.userData.camera, scene.userData.element);
controls.zoomSpeed = 1.2;
controls.enableZoom = true;
scene.userData.controls = controls;
scenes.push(scene);
// ADD SHAPE
startSphere();
}
function initPlane() {
const scene = new THREE.Scene();
const element = document.createElement( "div" );
element.className = "list-item";
element.innerHTML = template.replace( '$', '2D');
scene.userData.element = element.querySelector( ".scene" );
content.appendChild( element );
const camera = new THREE.PerspectiveCamera(60, width / height, 1, 10000);
camera.position.z = 1050;
camera.lookAt(scene.position);
scene.userData.camera = camera;
const controls = new THREE.OrbitControls(scene.userData.camera, scene.userData.element);
controls.zoomSpeed = 1.2;
controls.enableZoom = true;
scene.userData.controls = controls;
scenes.push(scene);
// ADD SHAPE
startPlane();
}
function updateSize() {
var width = canvas.clientWidth;
var height = canvas.clientHeight;
if ( canvas.width !== width || canvas.height !== height ) {
renderer.setSize( width, height, false );
}
}
function animate() {
render();
requestAnimationFrame( animate );
}
function render() {
updateSize();
renderer.setClearColor( 0x000000 );
renderer.setScissorTest( false );
renderer.clear();
renderer.setClearColor( 0xe0e0e0 );
renderer.setScissorTest( true );
scenes.forEach( function ( scene ) {
if (scene.children[0] !== undefined) {
// get the element that is a place holder for where we want to
// draw the scene
const element = scene.userData.element;
// get its position relative to the page's viewport
const rect = element.getBoundingClientRect();
// check if it's offscreen. If so skip it
if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
return; // it's off screen
}
// set the viewport
const width = rect.right - rect.left;
const height = rect.bottom - rect.top;
const left = rect.left;
const top = rect.top;
renderer.setViewport( left, top, width, height );
renderer.setScissor( left, top, width, height );
const camera = scene.userData.camera;
//camera.aspect = width / height; // not changing in this example
//camera.updateProjectionMatrix();
//scene.userData.controls.update();
renderer.render( scene, camera );
}
} );
}
function startSphere() {
const bounds = [0, 0, Math.PI, Math.PI * 2];
somS.randomize3D(myDataSphere.nodes, bounds);
setTimeout(function () {
somS(myDataSphere, {
dontRandomize: true,
bounds: bounds,
onEnd: () => {
finished[0] = true;
if (finished.filter(x => !!x).length > 0) {
enableSelector(true);
} else {
enableSelector(false);
}
},
iterationsPerUpdate: checkbox.checked ? 10 : 5,
updateDelay: 0,
onUpdate: getSphere
});
}, 1000);
getSphere();
}
function getSphere() {
if (meshSphere !== undefined && scenes.length > 2) {
scenes[2].remove(meshSphere);
}
const particle_system_geometry = new THREE.BufferGeometry();
// Buffers
const positionBuffer = new Float32Array(new ArrayBuffer(particles * 4 * 3)); // For positioning
const colorBuffer = new Uint8Array(new ArrayBuffer(particles * 3)); // For giving color to nodes
for (let i = 0; i < particles; i++) {
const angle1 = myDataSphere.nodes[i].x;
const angle2 = myDataSphere.nodes[i].y;
const posX = radius * Math.sin(angle1) * Math.cos(angle2);
const posY = radius * Math.sin(angle1) * Math.sin(angle2);
const posZ = radius * Math.cos(angle1);
positionBuffer[(i * 3)] = posX;
positionBuffer[(i * 3) + 1] = posY;
positionBuffer[(i * 3) + 2] = posZ;
colorBuffer[(i * 3)] = myColors[myDataSphere.nodes[i].group][0];
colorBuffer[(i * 3) + 1] = myColors[myDataSphere.nodes[i].group][1];
colorBuffer[(i * 3) + 2] = myColors[myDataSphere.nodes[i].group][2];
}
let nodeBufferPosition = new THREE.InterleavedBuffer(positionBuffer, 3);
let nodeBufferColor = new THREE.InterleavedBuffer(colorBuffer, 3);
particle_system_geometry.addAttribute('position', new THREE.InterleavedBufferAttribute(nodeBufferPosition, 3, 0, false));
particle_system_geometry.addAttribute('color', new THREE.InterleavedBufferAttribute(nodeBufferColor, 3, 0, true));
meshSphere = new THREE.Points(particle_system_geometry, nodeMaterial);
scenes[2].add(meshSphere);
getSphereRelations();
}
function getSphereRelations() {
if (linesMeshSphere !== undefined && scenes.length > 1) {
scenes[2].remove(linesMeshSphere);
}
const linesGeometry = new THREE.BufferGeometry();
// Buffers
const positionBuffer = new Float32Array(new ArrayBuffer(numLines * 2 * 12)); // For positioning
for (let r = 0; r < myDataSphere.edges.length; r++) {
const source = myDataSphere.nodes.filter(x => x.id === myDataSphere.edges[r].source)[0];
const target = myDataSphere.nodes.filter(x => x.id === myDataSphere.edges[r].target)[0];
const source_angle1 = source.x;
const source_angle2 = source.y;
const target_angle1 = target.x;
const target_angle2 = target.y;
const source_posX = radius * Math.sin(source_angle1) * Math.cos(source_angle2);
const source_posY = radius * Math.sin(source_angle1) * Math.sin(source_angle2);
const source_posZ = radius * Math.cos(source_angle1);
const target_posX = radius * Math.sin(target_angle1) * Math.cos(target_angle2);
const target_posY = radius * Math.sin(target_angle1) * Math.sin(target_angle2);
const target_posZ = radius * Math.cos(target_angle1);
// FROM
positionBuffer[(r * 6)] = source_posX;
positionBuffer[(r * 6) + 1] = source_posY;
positionBuffer[(r * 6) + 2] = source_posZ;
// TO
positionBuffer[(r * 6) + 3] = target_posX;
positionBuffer[(r * 6) + 4] = target_posY;
positionBuffer[(r * 6) + 5] = target_posZ;
}
const nodeBufferPosition = new THREE.InterleavedBuffer(positionBuffer, 3);
linesGeometry.addAttribute('position', new THREE.InterleavedBufferAttribute(nodeBufferPosition, 3, 0, false));
linesMeshSphere = new THREE.LineSegments( linesGeometry, linesMaterial );
scenes[2].add(linesMeshSphere);
}
function startPlane() {
const bounds = [-W / 2 + Rmax, -H / 2 + Rmax, W / 2 - Rmax, H / 2 - Rmax];
som.randomize(myDataPlane.nodes, bounds);
setTimeout(() => {
this.som(myDataPlane, {
dontRandomize: true,
bounds: bounds,
onEnd: () => {
finished[1] = true;
if (finished.filter(x => !!x).length > 0) {
enableSelector(true);
} else {
enableSelector(false);
}
},
iterationsPerUpdate: checkbox.checked ? 10 : 5,
updateDelay: 0,
onUpdate: getPlane
});
}, 1000);
getPlane();
}
function getPlane() {
if (meshPlane !== undefined && scenes.length > 1) {
scenes[1].remove(meshPlane);
}
const particle_system_geometry = new THREE.BufferGeometry();
// Buffers
const positionBuffer = new Float32Array(new ArrayBuffer(particles * 4 * 3)); // For positioning
const colorBuffer = new Uint8Array(new ArrayBuffer(particles * 3)); // For giving color to nodes
for (let i = 0; i < particles; i++) {
const posX = myDataPlane.nodes[i].x;
const posY = myDataPlane.nodes[i].y;
const posZ = 0;
positionBuffer[(i * 3)] = posX;
positionBuffer[(i * 3) + 1] = posY;
positionBuffer[(i * 3) + 2] = posZ;
colorBuffer[(i * 3)] = myColors[myDataPlane.nodes[i].group][0];
colorBuffer[(i * 3) + 1] = myColors[myDataPlane.nodes[i].group][1];
colorBuffer[(i * 3) + 2] = myColors[myDataPlane.nodes[i].group][2];
}
let nodeBufferPosition = new THREE.InterleavedBuffer(positionBuffer, 3);
let nodeBufferColor = new THREE.InterleavedBuffer(colorBuffer, 3);
particle_system_geometry.addAttribute('position', new THREE.InterleavedBufferAttribute(nodeBufferPosition, 3, 0, false));
particle_system_geometry.addAttribute('color', new THREE.InterleavedBufferAttribute(nodeBufferColor, 3, 0, true));
meshPlane = new THREE.Points(particle_system_geometry, nodeMaterial);
scenes[1].add(meshPlane);
getPlaneRelations();
}
function getPlaneRelations() {
if (linesMeshPlane !== undefined && scenes.length > 1) {
scenes[1].remove(linesMeshPlane);
}
const linesGeometry = new THREE.BufferGeometry();
// Buffers
const positionBuffer = new Float32Array(new ArrayBuffer(numLines * 2 * 12)); // For positioning
for (let r = 0; r < myDataPlane.edges.length; r++) {
const source = myDataPlane.nodes.filter(x => x.id === myDataPlane.edges[r].source)[0];
const target = myDataPlane.nodes.filter(x => x.id === myDataPlane.edges[r].target)[0];
const source_posX = source.x;
const source_posY = source.y;
const source_posZ = 0;
const target_posX = target.x;
const target_posY = target.y;
const target_posZ = 0;
// FROM
positionBuffer[(r * 6)] = source_posX;
positionBuffer[(r * 6) + 1] = source_posY;
positionBuffer[(r * 6) + 2] = source_posZ;
// TO
positionBuffer[(r * 6) + 3] = target_posX;
positionBuffer[(r * 6) + 4] = target_posY;
positionBuffer[(r * 6) + 5] = target_posZ;
}
const nodeBufferPosition = new THREE.InterleavedBuffer(positionBuffer, 3);
linesGeometry.addAttribute('position', new THREE.InterleavedBufferAttribute(nodeBufferPosition, 3, 0, false));
linesMeshPlane = new THREE.LineSegments( linesGeometry, linesMaterial );
scenes[1].add(linesMeshPlane);
}
function startShape() {
const bounds = [-W / 2 + Rmax, -H / 2 + Rmax, -Z, W / 2 - Rmax, H / 2 - Rmax, Z];
som3D.randomize3D(myDataShape.nodes, bounds);
setTimeout(function () {
som3D(myDataShape, {
dontRandomize: true,
bounds: bounds,
onEnd: () => {
finished[2] = true;
if (finished.filter(x => !!x).length > 0) {
enableSelector(true);
} else {
enableSelector(false);
}
},
iterationsPerUpdate: checkbox.checked ? 10 : 5,
updateDelay: 0,
onUpdate: getShape
});
}, 1000);
getShape();
}
function getShape() {
if (meshShape !== undefined && scenes.length > 0) {
scenes[0].remove(meshShape);
}
const particle_system_geometry = new THREE.BufferGeometry();
// Buffers
const positionBuffer = new Float32Array(new ArrayBuffer(particles * 4 * 3)); // For positioning
const colorBuffer = new Uint8Array(new ArrayBuffer(particles * 3)); // For giving color to nodes
for (let i = 0; i < particles; i++) {
const posX = myDataShape.nodes[i].x;
const posY = myDataShape.nodes[i].y;
const posZ = myDataShape.nodes[i].z;
positionBuffer[(i * 3)] = posX;
positionBuffer[(i * 3) + 1] = posY;
positionBuffer[(i * 3) + 2] = posZ;
colorBuffer[(i * 3)] = myColors[myDataShape.nodes[i].group][0];
colorBuffer[(i * 3) + 1] = myColors[myDataShape.nodes[i].group][1];
colorBuffer[(i * 3) + 2] = myColors[myDataShape.nodes[i].group][2];
}
let nodeBufferPosition = new THREE.InterleavedBuffer(positionBuffer, 3);
let nodeBufferColor = new THREE.InterleavedBuffer(colorBuffer, 3);
particle_system_geometry.addAttribute('position', new THREE.InterleavedBufferAttribute(nodeBufferPosition, 3, 0, false));
particle_system_geometry.addAttribute('color', new THREE.InterleavedBufferAttribute(nodeBufferColor, 3, 0, true));
meshShape = new THREE.Points(particle_system_geometry, nodeMaterial);
scenes[0].add(meshShape);
getShapeRelations();
}
function getShapeRelations() {
if (linesMeshShape !== undefined && scenes.length > 0) {
scenes[0].remove(linesMeshShape);
}
const linesGeometry = new THREE.BufferGeometry();
// Buffers
const positionBuffer = new Float32Array(new ArrayBuffer(numLines * 2 * 12)); // For positioning
for (let r = 0; r < myDataShape.edges.length; r++) {
const source = myDataShape.nodes.filter(x => x.id === myDataShape.edges[r].source)[0];
const target = myDataShape.nodes.filter(x => x.id === myDataShape.edges[r].target)[0];
// FROM
positionBuffer[(r * 6)] = source.x;
positionBuffer[(r * 6) + 1] = source.y;
positionBuffer[(r * 6) + 2] = source.z;
// TO
positionBuffer[(r * 6) + 3] = target.x;
positionBuffer[(r * 6) + 4] = target.y;
positionBuffer[(r * 6) + 5] = target.z;
}
const nodeBufferPosition = new THREE.InterleavedBuffer(positionBuffer, 3);
linesGeometry.addAttribute('position', new THREE.InterleavedBufferAttribute(nodeBufferPosition, 3, 0, false));
linesMeshShape = new THREE.LineSegments( linesGeometry, linesMaterial );
scenes[0].add(linesMeshShape);
}
function copyData3D(dataToCopy) {
// edges: {source: string, target: string, value: number}[]
// nodes: {group: number, id: string, x: number, y: number}[]
const nodesRet = [];
const edgesRet = [];
for (const e of dataToCopy.edges) {
const source = e.source;
const target = e.target;
const value = e.value;
edgesRet.push({source: source, target: target, value: value});
}
for (const n of dataToCopy.nodes) {
const group = n.group;
const id = n.id;
const x = n.x;
const y = n.y;
nodesRet.push({group: group, id: id, x: x, y: y, z: 0});
}
return {edges: edgesRet, nodes: nodesRet};
}
function enableSelector(enable) {
selector.disabled = !enable;
checkbox.disabled = !enable;
}