|
@@ -0,0 +1,174 @@
|
|
1
|
+import * as THREE from 'three';
|
|
2
|
+import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
3
|
+import { STLLoader } from 'three/addons/loaders/STLLoader.js'
|
|
4
|
+import { VRMLLoader } from 'three/addons/loaders/VRMLLoader.js';
|
|
5
|
+
|
|
6
|
+export function init_3d(path, container, status, div_width, div_height) {
|
|
7
|
+ const width = div_width;
|
|
8
|
+ const height = div_height;
|
|
9
|
+
|
|
10
|
+ const scene = new THREE.Scene();
|
|
11
|
+ scene.add(new THREE.AxesHelper(1));
|
|
12
|
+
|
|
13
|
+ const camera = new THREE.PerspectiveCamera( 75, width / height, 0.1, 1000 );
|
|
14
|
+
|
|
15
|
+ const renderer = new THREE.WebGLRenderer();
|
|
16
|
+ renderer.setSize( width, height );
|
|
17
|
+
|
|
18
|
+ container.appendChild( renderer.domElement );
|
|
19
|
+
|
|
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);
|
|
43
|
+
|
|
44
|
+ const light2 = new THREE.AmbientLight(0x101010);
|
|
45
|
+ light2.position.set(100, 100, 100);
|
|
46
|
+ scene.add(light2);
|
|
47
|
+
|
|
48
|
+ const material = new THREE.MeshStandardMaterial();
|
|
49
|
+ //material.roughness = 0.42;
|
|
50
|
+
|
|
51
|
+ if (path.endsWith(".stl")) {
|
|
52
|
+ const loader = new STLLoader();
|
|
53
|
+ loader.load(
|
|
54
|
+ //'plot/actuator_all.stl',
|
|
55
|
+ path,
|
|
56
|
+ function (geometry) {
|
|
57
|
+ const mesh = new THREE.Mesh(geometry, material);
|
|
58
|
+ scene.add(mesh);
|
|
59
|
+ },
|
|
60
|
+ (xhr) => {
|
|
61
|
+ const s = (xhr.loaded / xhr.total) * 100 + '% loaded';
|
|
62
|
+ console.log(s);
|
|
63
|
+ status.textContent = s;
|
|
64
|
+ },
|
|
65
|
+ (error) => {
|
|
66
|
+ console.log(error);
|
|
67
|
+ status.textContent = error;
|
|
68
|
+ }
|
|
69
|
+ );
|
|
70
|
+ } else if (path.endsWith(".wrl")) {
|
|
71
|
+ const loader = new VRMLLoader();
|
|
72
|
+ loader.load(
|
|
73
|
+ //'plot/drumkit.kicad_pcb.wrl',
|
|
74
|
+ //'plot/dispensy.wrl',
|
|
75
|
+ path,
|
|
76
|
+ function (object) {
|
|
77
|
+ scene.add(object);
|
|
78
|
+ });
|
|
79
|
+ } else {
|
|
80
|
+ const s = "error: unknown filetype for " + path;
|
|
81
|
+ console.log(s);
|
|
82
|
+ status.textContent = s;
|
|
83
|
+ }
|
|
84
|
+
|
|
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
|
+
|
|
162
|
+ function render() {
|
|
163
|
+ renderer.render(scene, camera);
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ function animate() {
|
|
167
|
+ requestAnimationFrame(animate);
|
|
168
|
+ controls.update();
|
|
169
|
+ render();
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ animate();
|
|
173
|
+ status.textContent = "3D model ready!";
|
|
174
|
+}
|