-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
206 lines (178 loc) · 7.46 KB
/
index.html
File metadata and controls
206 lines (178 loc) · 7.46 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Three.js — локальная модель test Car_Neznayka1</title>
<style>
html,body{height:100%;margin:0;background:#000;font-family:Arial, sans-serif}
canvas{display:block}
#overlay{
position:absolute;inset:0;display:flex;flex-direction:column;align-items:center;justify-content:center;
color:#fff;background:rgba(0,0,0,0.45);z-index:10
}
.row{margin:8px 0}
button{padding:8px 12px;border-radius:6px;border:0;cursor:pointer}
button.secondary{background:#333;color:#fff}
button.primary{background:#0af;color:#000;font-weight:700}
.error{color:#ff6b6b;margin-top:8px;font-weight:600}
</style>
</head>
<body>
<div id="overlay">
<div id="status">Инициализация…</div>
<div class="row">
<button id="btnRetry" class="secondary">Повторить локальную загрузку</button>
<button id="btnTest" class="primary">Загрузить тестовую модель (онлайн)</button>
</div>
<div id="err" class="error" style="display:none"></div>
<div style="font-size:12px;margin-top:10px;opacity:0.8">Файл: <code>model/Car_Neznayka1.glb</code></div>
</div>
<script type="module">
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.module.js';
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/three@0.158.0/examples/jsm/loaders/GLTFLoader.js';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.158.0/examples/jsm/controls/OrbitControls.js';
const STATUS = document.getElementById('status');
const ERR = document.getElementById('err');
const OVERLAY = document.getElementById('overlay');
const BTN_RETRY = document.getElementById('btnRetry');
const BTN_TEST = document.getElementById('btnTest');
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.05, 1000);
camera.position.set(0, 1.6, 3);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.07;
const hemi = new THREE.HemisphereLight(0xffffff, 0x222222, 0.6);
scene.add(hemi);
const dir = new THREE.DirectionalLight(0xffffff, 1.0);
dir.position.set(5, 10, 7);
dir.castShadow = true;
dir.shadow.mapSize.set(2048, 2048);
dir.shadow.radius = 4;
scene.add(dir);
const ground = new THREE.Mesh(new THREE.PlaneGeometry(40, 40), new THREE.ShadowMaterial({ opacity: 0.4 }));
ground.rotation.x = -Math.PI/2;
ground.receiveShadow = true;
scene.add(ground);
const loader = new GLTFLoader();
const LOCAL_PATH = 'model/Car_Neznayka1.glb';
const TEST_MODEL = 'https://modelviewer.dev/shared-assets/models/Astronaut.glb';
// утилита: подогнать камеру под объект
function fitCameraToObject(camera, object, offset = 1.25) {
const box = new THREE.Box3().setFromObject(object);
const size = box.getSize(new THREE.Vector3());
const center = box.getCenter(new THREE.Vector3());
const maxSize = Math.max(size.x, size.y, size.z);
const fitHeightDistance = maxSize / (2 * Math.atan(Math.PI * camera.fov / 360));
const fitWidthDistance = fitHeightDistance / camera.aspect;
const distance = offset * Math.max(fitHeightDistance, fitWidthDistance);
camera.position.copy(center);
camera.position.x += distance;
camera.position.y += distance * 0.25;
camera.position.z += distance;
camera.lookAt(center);
controls.target.copy(center);
controls.update();
}
// функция загрузки локального файла через fetch -> parse (даёт явный контроль и ошибки)
async function fetchAndParseLocal(url, timeoutMs = 10000) {
STATUS.textContent = `Попытка загрузки локального файла: ${url}`;
ERR.style.display = 'none';
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
const resp = await fetch(url, { method: 'GET', signal: controller.signal });
clearTimeout(timeout);
if (!resp.ok) {
throw new Error(`HTTP ${resp.status} ${resp.statusText}`);
}
const arrayBuffer = await resp.arrayBuffer();
STATUS.textContent = 'Парсинг GLB…';
return new Promise((resolve, reject) => {
loader.parse(arrayBuffer, '', (gltf) => resolve(gltf), (err) => reject(err));
});
} catch (err) {
throw err;
}
}
async function loadLocalThenShow() {
try {
const gltf = await fetchAndParseLocal(LOCAL_PATH, 10000);
showModel(gltf.scene);
} catch (err) {
console.error('Локальная загрузка failed:', err);
ERR.style.display = 'block';
ERR.textContent = `Локальная загрузка не удалась: ${err.message || err}`;
STATUS.textContent = 'Локальная загрузка не удалась';
}
}
function showModel(model) {
// очистка сцены от предыдущих моделей (если были)
const toRemove = scene.children.filter(c => c.userData.isDynamicModel);
toRemove.forEach(c => scene.remove(c));
model.traverse(n => {
if (n.isMesh) {
n.castShadow = true;
n.receiveShadow = true;
// на всякий случай force sRGB/linear корректность
if (n.material) {
if (Array.isArray(n.material)) n.material.forEach(m => m.needsUpdate = true);
else n.material.needsUpdate = true;
}
}
});
model.userData.isDynamicModel = true;
scene.add(model);
fitCameraToObject(camera, model, 1.4);
STATUS.textContent = 'Модель загружена и отображена';
setTimeout(() => { OVERLAY.style.display = 'none'; }, 500);
}
// fallback: загрузить тестовую модель с сети
function loadTestModel() {
STATUS.textContent = 'Загрузка тестовой модели…';
ERR.style.display = 'none';
loader.load(TEST_MODEL, (gltf) => {
showModel(gltf.scene);
}, (xhr) => {
const pct = xhr.total ? Math.round(xhr.loaded / xhr.total * 100) : 0;
STATUS.textContent = `Загрузка тестовой модели… ${pct}%`;
}, (err) => {
console.error('Ошибка тестовой модели:', err);
ERR.style.display = 'block';
ERR.textContent = 'Не удалось загрузить тестовую модель (проверь интернет)';
});
}
// UI handlers
BTN_RETRY.addEventListener('click', () => {
STATUS.textContent = 'Повтор локальной загрузки…';
ERR.style.display = 'none';
loadLocalThenShow();
});
BTN_TEST.addEventListener('click', loadTestModel);
// автозапуск локальной загрузки
loadLocalThenShow();
// рендер
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>