-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
141 lines (116 loc) · 4.07 KB
/
model.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
let autoRotate = true;
const rotationSpeed = 0.005;
let scene, camera, renderer, controls, model;
let hasClicked = false;
let container;
// Add inversion animation styles
const style = document.createElement('style');
style.textContent = `
@keyframes invertIn {
from {
filter: invert(0);
background-color: white;
}
to {
filter: invert(1);
background-color: black;
}
}
@keyframes invertOut {
from {
filter: invert(1);
background-color: black;
}
to {
filter: invert(0);
background-color: white;
}
}
`;
document.head.appendChild(style);
function initModelViewer() {
container = document.getElementById('head-model');
scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
camera = new THREE.PerspectiveCamera(
75,
container.clientWidth / container.clientHeight,
0.1,
1000
);
camera.position.z = 1;
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(0, 1, 1);
scene.add(directionalLight);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.minDistance = 0;
controls.maxDistance = 85;
const loader = new THREE.GLTFLoader();
loader.load(
'/model/3dscan.glb',
function (gltf) {
model = gltf.scene;
const box = new THREE.Box3().setFromObject(model);
const center = box.getCenter(new THREE.Vector3());
center.y -= -0.9;
model.position.sub(center);
camera.position.y = 0.1;
camera.lookAt(0, 0.5, 0);
model.scale.set(3, 3, 3);
scene.add(model);
// New click event listener for color inversion
container.addEventListener('click', function() {
if (!hasClicked) {
// Invert the whole site
document.body.style.animation = 'invertIn 0.3s forwards';
scene.background = new THREE.Color(0x000000); // Change to black
// Revert back after a delay
setTimeout(() => {
document.body.style.animation = 'invertOut 0.3s forwards';
scene.background = new THREE.Color(0xffffff); // Change back to white
}, 2000);
hasClicked = true;
setTimeout(() => hasClicked = false, 5000);
}
});
controls.addEventListener('start', function() {
autoRotate = false;
});
controls.addEventListener('end', function() {
setTimeout(() => {
autoRotate = true;
}, 3000);
});
},
function (xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
function (error) {
console.error('An error occurred loading the model:', error);
}
);
animate();
}
function animate() {
requestAnimationFrame(animate);
if (model && autoRotate) {
model.rotation.y += rotationSpeed;
}
controls.update();
renderer.render(scene, camera);
}
function onWindowResize() {
const container = document.getElementById('head-model');
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
window.addEventListener('resize', onWindowResize);
document.addEventListener('DOMContentLoaded', initModelViewer);