-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.html
57 lines (50 loc) · 2.31 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>STL Viewer</title>
<script type="module">
import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
import { STLLoader } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/STLLoader.js';
document.addEventListener('DOMContentLoaded', () => {
const container = document.getElementById('stl_cont');
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
const loader = new STLLoader();
loader.load('./lid.stl', (geometry) => {
const material = new THREE.MeshNormalMaterial();
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// Center the mesh
geometry.computeBoundingBox();
const center = new THREE.Vector3();
geometry.boundingBox.getCenter(center);
mesh.position.sub(center);
// Adjust camera position
const boundingBox = geometry.boundingBox;
const size = new THREE.Vector3();
boundingBox.getSize(size);
const maxDim = Math.max(size.x, size.y, size.z);
const fov = camera.fov * (Math.PI / 180);
let cameraZ = Math.abs(maxDim / 2 * Math.tan(fov * 2)); // Adjusted to zoom out
camera.position.z = cameraZ * 2; // Further back to zoom out
camera.lookAt(new THREE.Vector3(0, 0, 0));
const animate = () => {
requestAnimationFrame(animate);
mesh.rotation.y += 0.01;
// mesh.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
});
});
</script>
</head>
<body>
<div id="stl_cont" style="width: 600px; height: 400px;"></div>
</body>
</html>