comparison WebApplications/three.js @ 36:13698d34e059

preparing to include O3DV
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Apr 2024 07:42:06 +0200
parents Sources/viewer.js@dd0cd39e6259
children b798387b085c
comparison
equal deleted inserted replaced
35:ee3bc8f7df5b 36:13698d34e059
1 /**
2 * SPDX-FileCopyrightText: 2023-2024 Sebastien Jodogne, UCLouvain, Belgium
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 /**
7 * STL plugin for Orthanc
8 * Copyright (C) 2023-2024 Sebastien Jodogne, UCLouvain, Belgium
9 *
10 * This program is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 **/
23
24
25 import * as THREE from 'three';
26
27 import { OrbitControls } from './libs/OrbitControls.js';
28 import { STLLoader } from './libs/STLLoader.js';
29
30
31 // http://stackoverflow.com/a/21903119/881731
32 function GetUrlParameter(sParam)
33 {
34 var sPageURL = decodeURIComponent(window.location.search.substring(1));
35 var sURLVariables = sPageURL.split('&');
36
37 for (var i = 0; i < sURLVariables.length; i++) {
38 var sParameterName = sURLVariables[i].split('=');
39
40 if (sParameterName[0] === sParam) {
41 return sParameterName[1] === undefined ? '' : sParameterName[1];
42 }
43 }
44
45 return '';
46 };
47
48 var instanceId = GetUrlParameter('instance');
49
50
51 const scene = new THREE.Scene();
52
53 const renderer = new THREE.WebGLRenderer();
54 renderer.setSize(window.innerWidth, window.innerHeight);
55 document.body.appendChild(renderer.domElement);
56
57 const material = new THREE.MeshPhongMaterial();
58
59 const light = new THREE.AmbientLight(0x555555);
60 scene.add(light);
61
62 const light2 = new THREE.PointLight(0xaaaaaa, 2);
63 light2.position.set(10, 10, 10);
64 scene.add(light2);
65
66 const loader = new STLLoader()
67 loader.load(
68 //'../../instances/' + instanceId + '/content/0042-0011',
69 '../../instances/' + instanceId + '/stl',
70 function (geometry) {
71 const frustumSize = 200;
72
73 geometry.computeBoundingBox();
74 geometry.translate(-(geometry.boundingBox.min.x + geometry.boundingBox.max.x) / 2.0,
75 -(geometry.boundingBox.min.y + geometry.boundingBox.max.y) / 2.0,
76 -(geometry.boundingBox.min.z + geometry.boundingBox.max.z) / 2.0);
77
78 var maxSize = Math.max(geometry.boundingBox.max.x - geometry.boundingBox.min.x,
79 geometry.boundingBox.max.y - geometry.boundingBox.min.y,
80 geometry.boundingBox.max.z - geometry.boundingBox.min.z);
81
82 geometry.scale((frustumSize / 2.0) / maxSize,
83 (frustumSize / 2.0) / maxSize,
84 (frustumSize / 2.0) / maxSize);
85
86 const mesh = new THREE.Mesh(geometry, material);
87 scene.add(mesh);
88
89 const aspect = window.innerWidth / window.innerHeight;
90 const camera = new THREE.OrthographicCamera(
91 frustumSize * aspect / - 2, frustumSize * aspect / 2,
92 frustumSize / 2, frustumSize / - 2, 1, frustumSize);
93
94 camera.position.z = 100;
95
96 const controls = new OrbitControls(camera, renderer.domElement);
97
98 //controls.update() must be called after any manual changes to the camera's transform
99 controls.update();
100
101 function animate() {
102 requestAnimationFrame(animate);
103
104 // required if controls.enableDamping or controls.autoRotate are set to true
105 controls.update();
106 light2.position.copy(camera.position);
107
108 renderer.render(scene, camera);
109 }
110
111 animate();
112
113 function onWindowResize() {
114 const aspect = window.innerWidth / window.innerHeight;
115 camera.left = - frustumSize * aspect / 2;
116 camera.right = frustumSize * aspect / 2;
117 camera.top = frustumSize / 2;
118 camera.bottom = - frustumSize / 2;
119 camera.updateProjectionMatrix();
120 renderer.setSize(window.innerWidth, window.innerHeight);
121 }
122
123 window.addEventListener('resize', onWindowResize );
124 },
125 function (xhr) {
126 },
127 function (error) {
128 console.log(error);
129 }
130 );