|
@@ -10,10 +10,13 @@ import { STLLoader } from 'three/addons/loaders/STLLoader.js'
|
10
|
10
|
import { VRMLLoader } from 'three/addons/loaders/VRMLLoader.js';
|
11
|
11
|
|
12
|
12
|
// https://wejn.org/2020/12/cracking-the-threejs-object-fitting-nut/
|
13
|
|
-function fitCameraToCenteredObject(camera, object, offset, orbitControls, yOffset) {
|
|
13
|
+function fitCameraToObject(camera, object, offset, orbitControls, yOffset) {
|
14
|
14
|
const boundingBox = new THREE.Box3();
|
15
|
15
|
boundingBox.setFromObject( object );
|
16
|
16
|
|
|
17
|
+ var middle = new THREE.Vector3();
|
|
18
|
+ boundingBox.getCenter(middle);
|
|
19
|
+
|
17
|
20
|
var size = new THREE.Vector3();
|
18
|
21
|
boundingBox.getSize(size);
|
19
|
22
|
|
|
@@ -61,7 +64,8 @@ function fitCameraToCenteredObject(camera, object, offset, orbitControls, yOffse
|
61
|
64
|
// offset the camera, if desired (to avoid filling the whole canvas)
|
62
|
65
|
if( offset !== undefined && offset !== 0 ) cameraZ *= offset;
|
63
|
66
|
|
64
|
|
- camera.position.set( 0, yOffset * cameraZ, cameraZ );
|
|
67
|
+ camera.target = middle;
|
|
68
|
+ camera.position.set( middle.x, middle.y + yOffset * cameraZ, middle.z + cameraZ );
|
65
|
69
|
|
66
|
70
|
// set the far plane of the camera so that it easily encompasses the whole object
|
67
|
71
|
const minZ = boundingBox.min.z;
|
|
@@ -72,7 +76,7 @@ function fitCameraToCenteredObject(camera, object, offset, orbitControls, yOffse
|
72
|
76
|
|
73
|
77
|
if ( orbitControls !== undefined ) {
|
74
|
78
|
// set camera to rotate around the center
|
75
|
|
- orbitControls.target = new THREE.Vector3(0, 0, 0);
|
|
79
|
+ orbitControls.target = middle;
|
76
|
80
|
|
77
|
81
|
// prevent camera from zooming out far enough to create far plane cutoff
|
78
|
82
|
orbitControls.maxDistance = cameraToFarEdge * 2;
|
|
@@ -84,7 +88,7 @@ export function init_3d(path, container, status, div_width, div_height) {
|
84
|
88
|
const height = div_height;
|
85
|
89
|
|
86
|
90
|
const scene = new THREE.Scene();
|
87
|
|
- scene.add(new THREE.AxesHelper(1));
|
|
91
|
+ //scene.add(new THREE.AxesHelper(1));
|
88
|
92
|
|
89
|
93
|
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
|
90
|
94
|
|
|
@@ -117,8 +121,10 @@ export function init_3d(path, container, status, div_width, div_height) {
|
117
|
121
|
path,
|
118
|
122
|
function (geometry) {
|
119
|
123
|
const mesh = new THREE.Mesh(geometry, material);
|
|
124
|
+ mesh.rotation.setFromVector3(new THREE.Vector3(-Math.PI / 2, 0, 0));
|
120
|
125
|
scene.add(mesh);
|
121
|
|
- fitCameraToCenteredObject(camera, scene, 0, controls, 0);
|
|
126
|
+ fitCameraToObject(camera, scene, 0, controls, 0);
|
|
127
|
+ controls.update();
|
122
|
128
|
},
|
123
|
129
|
(xhr) => {
|
124
|
130
|
const s = (xhr.loaded / xhr.total) * 100 + '% loaded';
|
|
@@ -139,7 +145,8 @@ export function init_3d(path, container, status, div_width, div_height) {
|
139
|
145
|
path,
|
140
|
146
|
function (object) {
|
141
|
147
|
scene.add(object);
|
142
|
|
- fitCameraToCenteredObject(camera, scene, 0, controls, 0);
|
|
148
|
+ fitCameraToObject(camera, scene, 0, controls, 0);
|
|
149
|
+ controls.update();
|
143
|
150
|
},
|
144
|
151
|
(xhr) => {
|
145
|
152
|
const s = (xhr.loaded / xhr.total) * 100 + '% loaded';
|
|
@@ -195,7 +202,8 @@ export function init_3d(path, container, status, div_width, div_height) {
|
195
|
202
|
btn_rst.type = "button";
|
196
|
203
|
btn_rst.value = "Reset Camera";
|
197
|
204
|
btn_rst.addEventListener('click', function() {
|
198
|
|
- fitCameraToCenteredObject(camera, scene, 0, controls, 0);
|
|
205
|
+ fitCameraToObject(camera, scene, 0, controls, 0);
|
|
206
|
+ controls.update();
|
199
|
207
|
});
|
200
|
208
|
|
201
|
209
|
const div_rst = document.createElement("div");
|