-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
180 lines (154 loc) · 5.38 KB
/
Copy pathindex.html
File metadata and controls
180 lines (154 loc) · 5.38 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D模型主页</title>
<style>
* { margin: 0; padding: 0; }
body { overflow: hidden; background: #1a1a1a; }
canvas { display: block; }
/* 电脑默认大小 */
.model-buttons {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
z-index: 99999;
display: flex;
gap: 12px;
}
.model-btn {
padding: 12px 24px;
font-size: 16px;
background: #3498db;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
transition: background 0.2s;
}
.model-btn:hover { background: #2980b9; }
.model-btn.active { background: #2ecc71; }
/* 👇 手机端自动缩小(手机适用) */
@media (max-width: 768px) {
.model-buttons {
gap: 6px;
}
.model-btn {
padding: 6px 12px;
font-size: 12px;
}
}
.loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 99998;
color: white;
font-size: 18px;
background: rgba(0,0,0,0.7);
padding: 15px 30px;
border-radius: 8px;
opacity: 0;
transition: opacity 0.3s ease;
pointer-events: none;
}
.loading.show {
opacity: 1;
}
</style>
</head>
<body>
<div class="loading" id="loading">正在加载模型...</div>
<script src="./global-menu.js"></script>
<div class="model-buttons">
<button class="model-btn active" data-model="./主图.glb">模型 1</button>
<button class="model-btn" data-model="./分割1.glb">模型 2</button>
<button class="model-btn" data-model="./分割2.glb">模型 3</button>
<button class="model-btn" data-model="./分割3.glb">模型 4</button>
</div>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x202020);
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 10;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 1);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 1);
dirLight.position.set(5, 10, 5);
scene.add(dirLight);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.08;
let currentModel = null;
const loader = new GLTFLoader();
const loading = document.getElementById('loading');
function loadModel(url) {
loading.classList.add('show');
if (currentModel) {
scene.remove(currentModel);
currentModel.traverse(child => {
if (child.material) {
if (child.material.map) child.material.map.dispose();
if (child.material.normalMap) child.material.normalMap.dispose();
if (child.material.roughnessMap) child.material.roughnessMap.dispose();
if (child.material.metalnessMap) child.material.metalnessMap.dispose();
child.material.dispose();
}
if (child.geometry) child.geometry.dispose();
});
currentModel = null;
}
loader.load(url, (gltf) => {
const model = gltf.scene;
currentModel = model;
const box = new THREE.Box3().setFromObject(model);
const center = box.getCenter(new THREE.Vector3());
const size = box.getSize(new THREE.Vector3());
const maxDim = Math.max(size.x, size.y, size.z);
const scale = 4 / maxDim;
model.scale.setScalar(scale);
model.position.sub(center.multiplyScalar(scale));
scene.add(model);
loading.classList.remove('show');
});
}
const buttons = document.querySelectorAll('.model-btn');
buttons.forEach(btn => {
btn.addEventListener('click', () => {
buttons.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
loadModel(btn.dataset.model);
});
});
loadModel("./主图.glb");
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>