|
@@ -0,0 +1,231 @@
|
|
1
|
+/*
|
|
2
|
+ * 3d.js
|
|
3
|
+ *
|
|
4
|
+ * Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
|
|
5
|
+ *
|
|
6
|
+ * For the fitCameraToObject() function also:
|
|
7
|
+ * Copyright (c) 2024 Michal Jirků
|
|
8
|
+ * https://wejn.org/2020/12/cracking-the-threejs-object-fitting-nut/
|
|
9
|
+ *
|
|
10
|
+ * For everything else:
|
|
11
|
+ *
|
|
12
|
+ * This program is free software: you can redistribute it and/or modify
|
|
13
|
+ * it under the terms of the GNU General Public License as published by
|
|
14
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
15
|
+ * (at your option) any later version.
|
|
16
|
+ *
|
|
17
|
+ * This program is distributed in the hope that it will be useful,
|
|
18
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20
|
+ * GNU General Public License for more details.
|
|
21
|
+ *
|
|
22
|
+ * See <http://www.gnu.org/licenses/>.
|
|
23
|
+ */
|
|
24
|
+
|
|
25
|
+import * as THREE from 'three';
|
|
26
|
+import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
27
|
+import { STLLoader } from 'three/addons/loaders/STLLoader.js'
|
|
28
|
+import { VRMLLoader } from 'three/addons/loaders/VRMLLoader.js';
|
|
29
|
+
|
|
30
|
+function fitCameraToObject(camera, object, offset, orbitControls, yOffset) {
|
|
31
|
+ const boundingBox = new THREE.Box3();
|
|
32
|
+ boundingBox.setFromObject( object );
|
|
33
|
+
|
|
34
|
+ var middle = new THREE.Vector3();
|
|
35
|
+ boundingBox.getCenter(middle);
|
|
36
|
+
|
|
37
|
+ var size = new THREE.Vector3();
|
|
38
|
+ boundingBox.getSize(size);
|
|
39
|
+
|
|
40
|
+ // figure out how to fit the box in the view:
|
|
41
|
+ // 1. figure out horizontal FOV (on non-1.0 aspects)
|
|
42
|
+ // 2. figure out distance from the object in X and Y planes
|
|
43
|
+ // 3. select the max distance (to fit both sides in)
|
|
44
|
+ //
|
|
45
|
+ // The reason is as follows:
|
|
46
|
+ //
|
|
47
|
+ // Imagine a bounding box (BB) is centered at (0,0,0).
|
|
48
|
+ // Camera has vertical FOV (camera.fov) and horizontal FOV
|
|
49
|
+ // (camera.fov scaled by aspect, see fovh below)
|
|
50
|
+ //
|
|
51
|
+ // Therefore if you want to put the entire object into the field of view,
|
|
52
|
+ // you have to compute the distance as: z/2 (half of Z size of the BB
|
|
53
|
+ // protruding towards us) plus for both X and Y size of BB you have to
|
|
54
|
+ // figure out the distance created by the appropriate FOV.
|
|
55
|
+ //
|
|
56
|
+ // The FOV is always a triangle:
|
|
57
|
+ //
|
|
58
|
+ // (size/2)
|
|
59
|
+ // +--------+
|
|
60
|
+ // | /
|
|
61
|
+ // | /
|
|
62
|
+ // | /
|
|
63
|
+ // | F° /
|
|
64
|
+ // | /
|
|
65
|
+ // | /
|
|
66
|
+ // | /
|
|
67
|
+ // |/
|
|
68
|
+ //
|
|
69
|
+ // F° is half of respective FOV, so to compute the distance (the length
|
|
70
|
+ // of the straight line) one has to: `size/2 / Math.tan(F)`.
|
|
71
|
+ //
|
|
72
|
+ // FTR, from https://threejs.org/docs/#api/en/cameras/PerspectiveCamera
|
|
73
|
+ // the camera.fov is the vertical FOV.
|
|
74
|
+
|
|
75
|
+ const fov = camera.fov * ( Math.PI / 180 );
|
|
76
|
+ const fovh = 2*Math.atan(Math.tan(fov/2) * camera.aspect);
|
|
77
|
+ let dx = size.z / 2 + Math.abs( size.x / 2 / Math.tan( fovh / 2 ) );
|
|
78
|
+ let dy = size.z / 2 + Math.abs( size.y / 2 / Math.tan( fov / 2 ) );
|
|
79
|
+ let cameraZ = Math.max(dx, dy);
|
|
80
|
+
|
|
81
|
+ // offset the camera, if desired (to avoid filling the whole canvas)
|
|
82
|
+ if( offset !== undefined && offset !== 0 ) cameraZ *= offset;
|
|
83
|
+
|
|
84
|
+ camera.target = middle;
|
|
85
|
+ camera.position.set( middle.x, middle.y + yOffset * cameraZ, middle.z + cameraZ );
|
|
86
|
+
|
|
87
|
+ // set the far plane of the camera so that it easily encompasses the whole object
|
|
88
|
+ const minZ = boundingBox.min.z;
|
|
89
|
+ const cameraToFarEdge = ( minZ < 0 ) ? -minZ + cameraZ : cameraZ - minZ;
|
|
90
|
+
|
|
91
|
+ camera.far = cameraToFarEdge * 3;
|
|
92
|
+ camera.updateProjectionMatrix();
|
|
93
|
+
|
|
94
|
+ if ( orbitControls !== undefined ) {
|
|
95
|
+ // set camera to rotate around the center
|
|
96
|
+ orbitControls.target = middle;
|
|
97
|
+
|
|
98
|
+ // prevent camera from zooming out far enough to create far plane cutoff
|
|
99
|
+ orbitControls.maxDistance = cameraToFarEdge * 2;
|
|
100
|
+ }
|
|
101
|
+}
|
|
102
|
+
|
|
103
|
+export function init_3d(path, container, status, div_width, div_height) {
|
|
104
|
+ const width = div_width;
|
|
105
|
+ const height = div_height;
|
|
106
|
+
|
|
107
|
+ const scene = new THREE.Scene();
|
|
108
|
+ //scene.add(new THREE.AxesHelper(1));
|
|
109
|
+
|
|
110
|
+ const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
|
|
111
|
+
|
|
112
|
+ const renderer = new THREE.WebGLRenderer({ antialias: true });
|
|
113
|
+ renderer.setSize(width, height);
|
|
114
|
+
|
|
115
|
+ const controls = new OrbitControls(camera, renderer.domElement);
|
|
116
|
+ controls.enableDamping = true;
|
|
117
|
+ controls.autoRotate = true;
|
|
118
|
+
|
|
119
|
+ for (const i of [1, -1]) {
|
|
120
|
+ for (const j of [0, 1, 2]) {
|
|
121
|
+ const light = new THREE.DirectionalLight(0xffffff, 0.5);
|
|
122
|
+ light.position.set(i * (j == 0 ? 1 : 0),
|
|
123
|
+ i * (j == 1 ? 1 : 0),
|
|
124
|
+ i * (j == 2 ? 1 : 0));
|
|
125
|
+ scene.add(light);
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ if (path.endsWith(".stl")) {
|
|
130
|
+ const light_amb = new THREE.AmbientLight(0x424242);
|
|
131
|
+ scene.add(light_amb);
|
|
132
|
+
|
|
133
|
+ const material = new THREE.MeshStandardMaterial();
|
|
134
|
+ //material.roughness = 0.75;
|
|
135
|
+
|
|
136
|
+ const loader = new STLLoader();
|
|
137
|
+ loader.load(
|
|
138
|
+ path,
|
|
139
|
+ function (geometry) {
|
|
140
|
+ const mesh = new THREE.Mesh(geometry, material);
|
|
141
|
+ mesh.rotation.setFromVector3(new THREE.Vector3(-Math.PI / 2, 0, 0));
|
|
142
|
+ scene.add(mesh);
|
|
143
|
+ fitCameraToObject(camera, scene, 0, controls, 0);
|
|
144
|
+ controls.update();
|
|
145
|
+ status.textContent = "Loaded STL 100%";
|
|
146
|
+ },
|
|
147
|
+ (xhr) => {
|
|
148
|
+ const s = Math.floor((xhr.loaded / xhr.total) * 100) + '% loaded';
|
|
149
|
+ console.log(s);
|
|
150
|
+ status.textContent = s;
|
|
151
|
+ },
|
|
152
|
+ (error) => {
|
|
153
|
+ console.log(error);
|
|
154
|
+ status.textContent = error;
|
|
155
|
+ }
|
|
156
|
+ );
|
|
157
|
+ } else if (path.endsWith(".wrl")) {
|
|
158
|
+ const light_amb = new THREE.AmbientLight(0xffffff);
|
|
159
|
+ scene.add(light_amb);
|
|
160
|
+
|
|
161
|
+ const loader = new VRMLLoader();
|
|
162
|
+ loader.load(
|
|
163
|
+ path,
|
|
164
|
+ function (object) {
|
|
165
|
+ scene.add(object);
|
|
166
|
+ fitCameraToObject(camera, scene, 0, controls, 0);
|
|
167
|
+ controls.update();
|
|
168
|
+ status.textContent = "Loaded VRML 100%";
|
|
169
|
+ },
|
|
170
|
+ (xhr) => {
|
|
171
|
+ const s = Math.floor((xhr.loaded / xhr.total) * 100) + '% loaded';
|
|
172
|
+ console.log(s);
|
|
173
|
+ status.textContent = s;
|
|
174
|
+ },
|
|
175
|
+ (error) => {
|
|
176
|
+ console.log(error);
|
|
177
|
+ status.textContent = error;
|
|
178
|
+ }
|
|
179
|
+ );
|
|
180
|
+ } else {
|
|
181
|
+ const s = "error: unknown filetype for " + path;
|
|
182
|
+ console.log(s);
|
|
183
|
+ status.textContent = s;
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ camera.position.z = 50;
|
|
187
|
+ controls.update();
|
|
188
|
+
|
|
189
|
+ function animate() {
|
|
190
|
+ requestAnimationFrame(animate);
|
|
191
|
+ controls.update();
|
|
192
|
+ renderer.render(scene, camera);
|
|
193
|
+ }
|
|
194
|
+
|
|
195
|
+ animate();
|
|
196
|
+ status.textContent = "3D model ready!";
|
|
197
|
+
|
|
198
|
+ container.appendChild(renderer.domElement);
|
|
199
|
+
|
|
200
|
+ const div = document.createElement("div");
|
|
201
|
+ div.style.position = "absolute";
|
|
202
|
+ div.style.left = "5px";
|
|
203
|
+ div.style.top = "5px";
|
|
204
|
+ div.style.background = "white";
|
|
205
|
+ div.style.color = "black";
|
|
206
|
+ container.appendChild(div);
|
|
207
|
+
|
|
208
|
+ const chk_ar = document.createElement("input");
|
|
209
|
+ chk_ar.type = "checkbox";
|
|
210
|
+ chk_ar.checked = true;
|
|
211
|
+ chk_ar.addEventListener('change', function() {
|
|
212
|
+ controls.autoRotate = this.checked;
|
|
213
|
+ });
|
|
214
|
+
|
|
215
|
+ const div_ar = document.createElement("div");
|
|
216
|
+ div_ar.appendChild(chk_ar);
|
|
217
|
+ div_ar.appendChild(document.createTextNode("Auto-Rotate"));
|
|
218
|
+ div.appendChild(div_ar);
|
|
219
|
+
|
|
220
|
+ const btn_rst = document.createElement("input");
|
|
221
|
+ btn_rst.type = "button";
|
|
222
|
+ btn_rst.value = "Reset Camera";
|
|
223
|
+ btn_rst.addEventListener('click', function() {
|
|
224
|
+ fitCameraToObject(camera, scene, 0, controls, 0);
|
|
225
|
+ controls.update();
|
|
226
|
+ });
|
|
227
|
+
|
|
228
|
+ const div_rst = document.createElement("div");
|
|
229
|
+ div_rst.appendChild(btn_rst);
|
|
230
|
+ div.appendChild(div_rst);
|
|
231
|
+}
|