|
@@ -1,8 +1,84 @@
|
|
1
|
+/*
|
|
2
|
+ * 3d.js
|
|
3
|
+ *
|
|
4
|
+ * Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
|
|
5
|
+ */
|
|
6
|
+
|
1
|
7
|
import * as THREE from 'three';
|
2
|
8
|
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
3
|
9
|
import { STLLoader } from 'three/addons/loaders/STLLoader.js'
|
4
|
10
|
import { VRMLLoader } from 'three/addons/loaders/VRMLLoader.js';
|
5
|
11
|
|
|
12
|
+// https://wejn.org/2020/12/cracking-the-threejs-object-fitting-nut/
|
|
13
|
+function fitCameraToCenteredObject(camera, object, offset, orbitControls ) {
|
|
14
|
+ const boundingBox = new THREE.Box3();
|
|
15
|
+ boundingBox.setFromObject( object );
|
|
16
|
+
|
|
17
|
+ var size = new THREE.Vector3();
|
|
18
|
+ boundingBox.getSize(size);
|
|
19
|
+
|
|
20
|
+ // figure out how to fit the box in the view:
|
|
21
|
+ // 1. figure out horizontal FOV (on non-1.0 aspects)
|
|
22
|
+ // 2. figure out distance from the object in X and Y planes
|
|
23
|
+ // 3. select the max distance (to fit both sides in)
|
|
24
|
+ //
|
|
25
|
+ // The reason is as follows:
|
|
26
|
+ //
|
|
27
|
+ // Imagine a bounding box (BB) is centered at (0,0,0).
|
|
28
|
+ // Camera has vertical FOV (camera.fov) and horizontal FOV
|
|
29
|
+ // (camera.fov scaled by aspect, see fovh below)
|
|
30
|
+ //
|
|
31
|
+ // Therefore if you want to put the entire object into the field of view,
|
|
32
|
+ // you have to compute the distance as: z/2 (half of Z size of the BB
|
|
33
|
+ // protruding towards us) plus for both X and Y size of BB you have to
|
|
34
|
+ // figure out the distance created by the appropriate FOV.
|
|
35
|
+ //
|
|
36
|
+ // The FOV is always a triangle:
|
|
37
|
+ //
|
|
38
|
+ // (size/2)
|
|
39
|
+ // +--------+
|
|
40
|
+ // | /
|
|
41
|
+ // | /
|
|
42
|
+ // | /
|
|
43
|
+ // | F° /
|
|
44
|
+ // | /
|
|
45
|
+ // | /
|
|
46
|
+ // | /
|
|
47
|
+ // |/
|
|
48
|
+ //
|
|
49
|
+ // F° is half of respective FOV, so to compute the distance (the length
|
|
50
|
+ // of the straight line) one has to: `size/2 / Math.tan(F)`.
|
|
51
|
+ //
|
|
52
|
+ // FTR, from https://threejs.org/docs/#api/en/cameras/PerspectiveCamera
|
|
53
|
+ // the camera.fov is the vertical FOV.
|
|
54
|
+
|
|
55
|
+ const fov = camera.fov * ( Math.PI / 180 );
|
|
56
|
+ const fovh = 2*Math.atan(Math.tan(fov/2) * camera.aspect);
|
|
57
|
+ let dx = size.z / 2 + Math.abs( size.x / 2 / Math.tan( fovh / 2 ) );
|
|
58
|
+ let dy = size.z / 2 + Math.abs( size.y / 2 / Math.tan( fov / 2 ) );
|
|
59
|
+ let cameraZ = Math.max(dx, dy);
|
|
60
|
+
|
|
61
|
+ // offset the camera, if desired (to avoid filling the whole canvas)
|
|
62
|
+ if( offset !== undefined && offset !== 0 ) cameraZ *= offset;
|
|
63
|
+
|
|
64
|
+ camera.position.set( 0, 0, cameraZ );
|
|
65
|
+
|
|
66
|
+ // set the far plane of the camera so that it easily encompasses the whole object
|
|
67
|
+ const minZ = boundingBox.min.z;
|
|
68
|
+ const cameraToFarEdge = ( minZ < 0 ) ? -minZ + cameraZ : cameraZ - minZ;
|
|
69
|
+
|
|
70
|
+ camera.far = cameraToFarEdge * 3;
|
|
71
|
+ camera.updateProjectionMatrix();
|
|
72
|
+
|
|
73
|
+ if ( orbitControls !== undefined ) {
|
|
74
|
+ // set camera to rotate around the center
|
|
75
|
+ orbitControls.target = new THREE.Vector3(0, 0, 0);
|
|
76
|
+
|
|
77
|
+ // prevent camera from zooming out far enough to create far plane cutoff
|
|
78
|
+ orbitControls.maxDistance = cameraToFarEdge * 2;
|
|
79
|
+ }
|
|
80
|
+}
|
|
81
|
+
|
6
|
82
|
export function init_3d(path, container, status, div_width, div_height) {
|
7
|
83
|
const width = div_width;
|
8
|
84
|
const height = div_height;
|
|
@@ -17,45 +93,33 @@ export function init_3d(path, container, status, div_width, div_height) {
|
17
|
93
|
|
18
|
94
|
container.appendChild( renderer.domElement );
|
19
|
95
|
|
20
|
|
- const light = new THREE.DirectionalLight( 0xffffff, 0.5 );
|
21
|
|
- light.position.set(0, 1, 0);
|
22
|
|
- scene.add(light);
|
23
|
|
-
|
24
|
|
- const light42 = new THREE.DirectionalLight( 0xffffff, 0.5 );
|
25
|
|
- light42.position.set(1, 0, 0);
|
26
|
|
- scene.add(light42);
|
27
|
|
-
|
28
|
|
- const light23 = new THREE.DirectionalLight( 0xffffff, 0.5 );
|
29
|
|
- light23.position.set(0, 0, 1);
|
30
|
|
- scene.add(light23);
|
31
|
|
-
|
32
|
|
- const lightb = new THREE.DirectionalLight( 0xffffff, 0.5 );
|
33
|
|
- lightb.position.set(0, -1, 0);
|
34
|
|
- scene.add(lightb);
|
35
|
|
-
|
36
|
|
- const light42b = new THREE.DirectionalLight( 0xffffff, 0.5 );
|
37
|
|
- light42b.position.set(-1, 0, 0);
|
38
|
|
- scene.add(light42b);
|
39
|
|
-
|
40
|
|
- const light23b = new THREE.DirectionalLight( 0xffffff, 0.5 );
|
41
|
|
- light23b.position.set(0, 0, -1);
|
42
|
|
- scene.add(light23b);
|
|
96
|
+ const controls = new OrbitControls( camera, renderer.domElement );
|
|
97
|
+ controls.enableDamping = true;
|
43
|
98
|
|
44
|
|
- const light2 = new THREE.AmbientLight(0x101010);
|
45
|
|
- light2.position.set(100, 100, 100);
|
46
|
|
- scene.add(light2);
|
|
99
|
+ if (path.endsWith(".stl")) {
|
|
100
|
+ const light_amb = new THREE.AmbientLight(0x424242);
|
|
101
|
+ scene.add(light_amb);
|
|
102
|
+
|
|
103
|
+ for (const i of [1, -1]) {
|
|
104
|
+ for (const j of [0, 1, 2]) {
|
|
105
|
+ const light = new THREE.DirectionalLight(0xffffff, 0.5);
|
|
106
|
+ light.position.set(i * (j == 0 ? 1 : 0),
|
|
107
|
+ i * (j == 1 ? 1 : 0),
|
|
108
|
+ i * (j == 2 ? 1 : 0));
|
|
109
|
+ scene.add(light);
|
|
110
|
+ }
|
|
111
|
+ }
|
47
|
112
|
|
48
|
|
- const material = new THREE.MeshStandardMaterial();
|
49
|
|
- //material.roughness = 0.42;
|
|
113
|
+ const material = new THREE.MeshStandardMaterial();
|
|
114
|
+ //material.roughness = 0.75;
|
50
|
115
|
|
51
|
|
- if (path.endsWith(".stl")) {
|
52
|
116
|
const loader = new STLLoader();
|
53
|
117
|
loader.load(
|
54
|
|
- //'plot/actuator_all.stl',
|
55
|
118
|
path,
|
56
|
119
|
function (geometry) {
|
57
|
120
|
const mesh = new THREE.Mesh(geometry, material);
|
58
|
121
|
scene.add(mesh);
|
|
122
|
+ fitCameraToCenteredObject(camera, scene, 0, controls);
|
59
|
123
|
},
|
60
|
124
|
(xhr) => {
|
61
|
125
|
const s = (xhr.loaded / xhr.total) * 100 + '% loaded';
|
|
@@ -68,96 +132,33 @@ export function init_3d(path, container, status, div_width, div_height) {
|
68
|
132
|
}
|
69
|
133
|
);
|
70
|
134
|
} else if (path.endsWith(".wrl")) {
|
|
135
|
+ const light_amb = new THREE.AmbientLight(0xffffff);
|
|
136
|
+ scene.add(light_amb);
|
|
137
|
+
|
71
|
138
|
const loader = new VRMLLoader();
|
72
|
139
|
loader.load(
|
73
|
|
- //'plot/drumkit.kicad_pcb.wrl',
|
74
|
|
- //'plot/dispensy.wrl',
|
75
|
140
|
path,
|
76
|
141
|
function (object) {
|
77
|
142
|
scene.add(object);
|
78
|
|
- });
|
|
143
|
+ fitCameraToCenteredObject(camera, scene, 0, controls);
|
|
144
|
+ },
|
|
145
|
+ (xhr) => {
|
|
146
|
+ const s = (xhr.loaded / xhr.total) * 100 + '% loaded';
|
|
147
|
+ console.log(s);
|
|
148
|
+ status.textContent = s;
|
|
149
|
+ },
|
|
150
|
+ (error) => {
|
|
151
|
+ console.log(error);
|
|
152
|
+ status.textContent = error;
|
|
153
|
+ }
|
|
154
|
+ );
|
79
|
155
|
} else {
|
80
|
156
|
const s = "error: unknown filetype for " + path;
|
81
|
157
|
console.log(s);
|
82
|
158
|
status.textContent = s;
|
83
|
159
|
}
|
84
|
160
|
|
85
|
|
- const controls = new OrbitControls( camera, renderer.domElement );
|
86
|
|
- controls.enableDamping = true;
|
87
|
|
-
|
88
|
|
- // https://wejn.org/2020/12/cracking-the-threejs-object-fitting-nut/
|
89
|
|
- const fitCameraToCenteredObject = function (camera, object, offset, orbitControls ) {
|
90
|
|
- const boundingBox = new THREE.Box3();
|
91
|
|
- boundingBox.setFromObject( object );
|
92
|
|
-
|
93
|
|
- var middle = new THREE.Vector3();
|
94
|
|
- var size = new THREE.Vector3();
|
95
|
|
- boundingBox.getSize(size);
|
96
|
|
-
|
97
|
|
- // figure out how to fit the box in the view:
|
98
|
|
- // 1. figure out horizontal FOV (on non-1.0 aspects)
|
99
|
|
- // 2. figure out distance from the object in X and Y planes
|
100
|
|
- // 3. select the max distance (to fit both sides in)
|
101
|
|
- //
|
102
|
|
- // The reason is as follows:
|
103
|
|
- //
|
104
|
|
- // Imagine a bounding box (BB) is centered at (0,0,0).
|
105
|
|
- // Camera has vertical FOV (camera.fov) and horizontal FOV
|
106
|
|
- // (camera.fov scaled by aspect, see fovh below)
|
107
|
|
- //
|
108
|
|
- // Therefore if you want to put the entire object into the field of view,
|
109
|
|
- // you have to compute the distance as: z/2 (half of Z size of the BB
|
110
|
|
- // protruding towards us) plus for both X and Y size of BB you have to
|
111
|
|
- // figure out the distance created by the appropriate FOV.
|
112
|
|
- //
|
113
|
|
- // The FOV is always a triangle:
|
114
|
|
- //
|
115
|
|
- // (size/2)
|
116
|
|
- // +--------+
|
117
|
|
- // | /
|
118
|
|
- // | /
|
119
|
|
- // | /
|
120
|
|
- // | F° /
|
121
|
|
- // | /
|
122
|
|
- // | /
|
123
|
|
- // | /
|
124
|
|
- // |/
|
125
|
|
- //
|
126
|
|
- // F° is half of respective FOV, so to compute the distance (the length
|
127
|
|
- // of the straight line) one has to: `size/2 / Math.tan(F)`.
|
128
|
|
- //
|
129
|
|
- // FTR, from https://threejs.org/docs/#api/en/cameras/PerspectiveCamera
|
130
|
|
- // the camera.fov is the vertical FOV.
|
131
|
|
-
|
132
|
|
- const fov = camera.fov * ( Math.PI / 180 );
|
133
|
|
- const fovh = 2*Math.atan(Math.tan(fov/2) * camera.aspect);
|
134
|
|
- let dx = size.z / 2 + Math.abs( size.x / 2 / Math.tan( fovh / 2 ) );
|
135
|
|
- let dy = size.z / 2 + Math.abs( size.y / 2 / Math.tan( fov / 2 ) );
|
136
|
|
- let cameraZ = Math.max(dx, dy);
|
137
|
|
-
|
138
|
|
- // offset the camera, if desired (to avoid filling the whole canvas)
|
139
|
|
- if( offset !== undefined && offset !== 0 ) cameraZ *= offset;
|
140
|
|
-
|
141
|
|
- camera.position.set( 0, 0, cameraZ );
|
142
|
|
-
|
143
|
|
- // set the far plane of the camera so that it easily encompasses the whole object
|
144
|
|
- const minZ = boundingBox.min.z;
|
145
|
|
- const cameraToFarEdge = ( minZ < 0 ) ? -minZ + cameraZ : cameraZ - minZ;
|
146
|
|
-
|
147
|
|
- camera.far = cameraToFarEdge * 3;
|
148
|
|
- camera.updateProjectionMatrix();
|
149
|
|
-
|
150
|
|
- if ( orbitControls !== undefined ) {
|
151
|
|
- // set camera to rotate around the center
|
152
|
|
- orbitControls.target = new THREE.Vector3(0, 0, 0);
|
153
|
|
-
|
154
|
|
- // prevent camera from zooming out far enough to create far plane cutoff
|
155
|
|
- orbitControls.maxDistance = cameraToFarEdge * 2;
|
156
|
|
- }
|
157
|
|
- };
|
158
|
|
-
|
159
|
|
- //camera.position.z = 50;
|
160
|
|
- fitCameraToCenteredObject(camera, scene, 0, controls)
|
|
161
|
+ camera.position.z = 50;
|
161
|
162
|
|
162
|
163
|
function render() {
|
163
|
164
|
renderer.render(scene, camera);
|