-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathortho.html
124 lines (108 loc) · 5.35 KB
/
ortho.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Model 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';
import { FBXLoader } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/FBXLoader.js';
document.addEventListener('DOMContentLoaded', () => {
const container = document.getElementById('model_cont');
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeeeeee); // Set background color
// Define the orthographic camera
const aspect = container.clientWidth / container.clientHeight;
const frustumSize = 10;
const camera = new THREE.OrthographicCamera(
frustumSize * aspect / -2,
frustumSize * aspect / 2,
frustumSize / 2,
frustumSize / -2,
0.1,
1000
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
// Add lights to the scene
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 10, 7.5).normalize();
scene.add(directionalLight);
const loadModel = (url, type) => {
if (type === 'stl') {
const loader = new STLLoader();
loader.load(url, (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 and zoom
const boundingBox = geometry.boundingBox;
const size = new THREE.Vector3();
boundingBox.getSize(size);
const maxDim = Math.max(size.x, size.y, size.z);
camera.position.set(0, 0, maxDim * 2); // Set to front view
camera.zoom = 0.5; // Zoom out further
camera.updateProjectionMatrix();
camera.lookAt(new THREE.Vector3(0, 0, 0));
const animate = () => {
requestAnimationFrame(animate);
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
});
} else if (type === 'fbx') {
const loader = new FBXLoader();
loader.load(url, (object) => {
scene.add(object);
// Traverse the model and adjust materials
object.traverse((child) => {
if (child.isMesh) {
child.material = new THREE.MeshStandardMaterial({
color: 0xaaaaaa,
metalness: 0.5,
roughness: 0.5
});
}
});
// Center the object
const box = new THREE.Box3().setFromObject(object);
const center = box.getCenter(new THREE.Vector3());
object.position.sub(center);
// Adjust camera position and zoom
const size = box.getSize(new THREE.Vector3());
const maxDim = Math.max(size.x, size.y, size.z);
camera.position.set(90, 0, maxDim * 2); // Set to front view
camera.zoom = 0.15; // Zoom out further
camera.updateProjectionMatrix();
camera.lookAt(new THREE.Vector3(0, 0, 0));
const animate = () => {
requestAnimationFrame(animate);
object.rotation.z += 0.01;
//object.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
});
}
};
// Example usage
//loadModel('path/to/your/model.stl', 'stl'); // For STL model
loadModel('./trackerpus.fbx', 'fbx'); // For FBX model
});
</script>
</head>
<body>
<div id="model_cont" style="width: 600px; height: 400px;"></div>
</body>
</html>