-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
370 lines (309 loc) · 10.8 KB
/
Copy pathscript.js
File metadata and controls
370 lines (309 loc) · 10.8 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
// Scene setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a0a0a);
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 50);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
document.getElementById('canvas-container').appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.enableZoom = true;
controls.enablePan = true;
controls.enabled = true;
// Lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1.2);
directionalLight.position.set(10, 10, 10);
directionalLight.castShadow = true;
scene.add(directionalLight);
const fillLight = new THREE.DirectionalLight(0xffffff, 0.5);
fillLight.position.set(-10, 0, -10);
scene.add(fillLight);
const backLight = new THREE.DirectionalLight(0xffffff, 0.4);
backLight.position.set(0, 10, -10);
scene.add(backLight);
// State
let currentModel = null;
let sensorEnabled = false;
let sensorMode = 'gyro'; // 'gyro' or 'absolute'
let activeSensor = null;
// Gyroscope values
let rotationX = 0, rotationY = 0, rotationZ = 0;
let lastGyroTimestamp = null;
// Absolute orientation quaternion
const targetQuaternion = new THREE.Quaternion();
const currentQuaternion = new THREE.Quaternion();
// UI elements
const sensorBtn = document.getElementById('sensorBtn');
const sensorStatus = document.getElementById('sensor-status');
const sensorData = document.getElementById('sensor-data');
const valX = document.getElementById('val-x');
const valY = document.getElementById('val-y');
const valZ = document.getElementById('val-z');
const modeGyro = document.getElementById('modeGyro');
const modeAbs = document.getElementById('modeAbs');
function setSensorEnabled(enabled) {
sensorEnabled = enabled;
sensorBtn.classList.toggle('active', enabled);
sensorData.classList.toggle('active', enabled);
}
function handleSensorError(label, event) {
sensorStatus.textContent = `${label}: ${event.error.name}`;
if (event.currentTarget === activeSensor) {
stopSensor();
setSensorEnabled(false);
}
}
function startSensor(sensor, label) {
return new Promise((resolve) => {
let settled = false;
const finish = (success, error) => {
if (settled) return;
settled = true;
sensor.removeEventListener('activate', onActivate);
sensor.removeEventListener('error', onError);
if (error) {
sensorStatus.textContent = `${label}: ${error.name}`;
}
resolve(success);
};
const onActivate = () => finish(true);
const onError = (event) => finish(false, event.error);
sensor.addEventListener('activate', onActivate);
sensor.addEventListener('error', onError);
try {
sensor.start();
} catch (error) {
finish(false, error);
}
});
}
async function hasSensorPermissions(names) {
if (!navigator.permissions?.query) {
return true;
}
try {
const results = await Promise.all(
names.map((name) => navigator.permissions.query({ name }))
);
return results.every((result) => result.state !== 'denied');
} catch {
return true;
}
}
// Load model
const fbxLoader = new FBXLoader();
fbxLoader.load(
'bottiボーン無し.fbx',
(object) => {
document.getElementById('loading').style.display = 'none';
const box = new THREE.Box3().setFromObject(object);
const size = box.getSize(new THREE.Vector3());
const maxDim = Math.max(size.x, size.y, size.z);
const scale = 20 / maxDim;
object.scale.setScalar(scale);
const center = box.getCenter(new THREE.Vector3());
object.position.sub(center.multiplyScalar(scale));
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
if (child.material) {
child.material.needsUpdate = true;
if (child.material.map) {
child.material.map.needsUpdate = true;
child.material.map.encoding = THREE.sRGBEncoding;
}
}
}
});
scene.add(object);
currentModel = object;
},
undefined,
(error) => {
document.getElementById('loading').textContent = 'error';
}
);
// Initialize Gyroscope
async function initGyroscope() {
if (!('Gyroscope' in window)) {
sensorStatus.textContent = 'gyro: not supported';
return false;
}
try {
const sensor = new Gyroscope({ frequency: 60 });
activeSensor = sensor;
sensor.addEventListener('reading', () => {
// Integrate angular velocity over elapsed time.
const now = sensor.timestamp;
const dt = lastGyroTimestamp === null ? 0 : (now - lastGyroTimestamp) / 1000;
lastGyroTimestamp = now;
const sensitivity = 1.5;
rotationX += sensor.x * dt * sensitivity;
rotationY += sensor.y * dt * sensitivity;
rotationZ += sensor.z * dt * sensitivity;
valX.textContent = sensor.x.toFixed(3);
valY.textContent = sensor.y.toFixed(3);
valZ.textContent = sensor.z.toFixed(3);
});
sensor.addEventListener('error', (event) => handleSensorError('gyro', event));
const started = await startSensor(sensor, 'gyro');
if (!started) {
stopSensor();
return false;
}
sensorStatus.textContent = 'gyro: active';
return true;
} catch (error) {
sensorStatus.textContent = `gyro: ${error.name}`;
return false;
}
}
// Initialize AbsoluteOrientationSensor
async function initAbsoluteOrientation() {
if (!('AbsoluteOrientationSensor' in window)) {
sensorStatus.textContent = 'absolute: not supported';
return false;
}
try {
const permitted = await hasSensorPermissions(['accelerometer', 'gyroscope', 'magnetometer']);
if (!permitted) {
sensorStatus.textContent = 'absolute: permission denied';
return false;
}
const sensor = new AbsoluteOrientationSensor({ frequency: 60 });
activeSensor = sensor;
sensor.addEventListener('reading', () => {
// Quaternion: [x, y, z, w]
targetQuaternion.fromArray(sensor.quaternion);
// Display euler angles for UI
const euler = new THREE.Euler().setFromQuaternion(targetQuaternion);
valX.textContent = euler.x.toFixed(2);
valY.textContent = euler.y.toFixed(2);
valZ.textContent = euler.z.toFixed(2);
});
sensor.addEventListener('error', (event) => handleSensorError('absolute', event));
const started = await startSensor(sensor, 'absolute');
if (!started) {
stopSensor();
return false;
}
sensorStatus.textContent = 'absolute: active';
return true;
} catch (error) {
sensorStatus.textContent = `absolute: ${error.name}`;
return false;
}
}
// Stop current sensor
function stopSensor() {
if (activeSensor) {
activeSensor.stop();
activeSensor = null;
}
lastGyroTimestamp = null;
}
// Initialize based on mode
async function initSensor() {
stopSensor();
if (sensorMode === 'gyro') {
return await initGyroscope();
} else {
return await initAbsoluteOrientation();
}
}
// Toggle sensor
async function toggleSensor() {
if (sensorEnabled) {
stopSensor();
setSensorEnabled(false);
sensorStatus.textContent = `${sensorMode}: paused`;
return false;
} else {
const success = await initSensor();
if (success) {
setSensorEnabled(true);
}
return success;
}
}
// Switch mode
async function switchMode(mode) {
if (mode === sensorMode) return;
sensorMode = mode;
// Update UI
modeGyro.classList.toggle('active', mode === 'gyro');
modeAbs.classList.toggle('active', mode === 'absolute');
// Reset rotation values
rotationX = rotationY = rotationZ = 0;
lastGyroTimestamp = null;
// Restart sensor if active
if (sensorEnabled) {
const success = await initSensor();
setSensorEnabled(success);
} else {
sensorStatus.textContent = `${mode}: standby`;
}
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
if (currentModel && sensorEnabled) {
if (sensorMode === 'gyro') {
// Gyroscope: apply integrated rotation
currentModel.rotation.x = rotationX;
currentModel.rotation.y = rotationY;
currentModel.rotation.z = rotationZ;
} else {
// Absolute: smooth quaternion interpolation
currentQuaternion.slerp(targetQuaternion, 0.15);
currentModel.quaternion.copy(currentQuaternion);
}
}
controls.update();
renderer.render(scene, camera);
}
animate();
// Resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Start prompt
document.getElementById('start-btn').addEventListener('click', async () => {
const success = await toggleSensor();
if (success) {
document.getElementById('start-prompt').classList.add('hidden');
}
});
// Sensor toggle
sensorBtn.addEventListener('click', toggleSensor);
// Mode switches
modeGyro.addEventListener('click', () => switchMode('gyro'));
modeAbs.addEventListener('click', () => switchMode('absolute'));
// Reset
document.getElementById('resetBtn').addEventListener('click', () => {
rotationX = rotationY = rotationZ = 0;
lastGyroTimestamp = null;
targetQuaternion.set(0, 0, 0, 1);
currentQuaternion.set(0, 0, 0, 1);
if (currentModel) {
currentModel.rotation.set(0, 0, 0);
currentModel.quaternion.set(0, 0, 0, 1);
}
camera.position.set(0, 0, 50);
controls.reset();
});