-
Notifications
You must be signed in to change notification settings - Fork 2
/
viewer.js
79 lines (70 loc) · 2.02 KB
/
viewer.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
'use strict';
let container, scene, camera, renderer;
let meshes = {};
function setupViewer() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera();
renderer = new THREE.WebGLRenderer({antialias: true});
camera.fov = 50;
camera.position.set(-24.1, 18.2, -31.9);
camera.rotation.set(-2.6, -0.54, -2.43);
camera.up = new THREE.Vector3(0, 0, 1);
camera.lookAt(new THREE.Vector3(0, -2, 2));
camera.zoom = 1.8;
renderer.setClearColor(0xffffff, 1);
container = document.getElementById('viewer');
container.appendChild(renderer.domElement);
window.addEventListener('resize', resizeViewer);
let controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render);
resizeViewer();
updateMeshes();
}
function resizeViewer() {
let w = container.offsetWidth;
let h = container.offsetHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
render();
}
function render() {
renderer.render(scene, camera);
}
function getMesh(structure) {
return meshes[structure.ID];
}
function updateMeshes() {
// `ontology_parsed` is defined in data.js
ontology_parsed.forEach((group) => {
group.structures.forEach((structure) => {
let mesh = getMesh(structure);
if (mesh) {
mesh.visible = structure.visible;
}
else if (structure.visible && !structure.loading) {
loadMesh(structure);
}
});
})
render();
}
function loadMesh(structure) {
let loader = new THREE.STLLoader();
let onLoad = (geometry) => {
addMesh(structure, geometry);
}
structure.loading = true;
loader.load(structure.filename, onLoad);
}
function addMesh(structure, geometry) {
let material = new THREE.MeshNormalMaterial();
let mesh = new THREE.Mesh(geometry, material);
// We do not store the mesh in the structure object,
// as Vue would then crash trying to see if this
// object has changed.
meshes[structure.ID] = mesh;
scene.add(mesh);
render();
structure.loading = false;
}