-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
426 lines (354 loc) · 14.1 KB
/
main.js
File metadata and controls
426 lines (354 loc) · 14.1 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// main.js completo e corretto con audio per 5 image target e gestione pausa/resume
import * as THREE from 'three';
import { MindARThree } from 'mindar-image-three';
// ═══════════════════════════════════════════════════════════════
// 🔧 CONFIGURAZIONE
// ═══════════════════════════════════════════════════════════════
const NUM_TARGETS = 5;
const AUDIO_PATH = './assets/audio/';
const MIND_PATH = './assets/targets/targets-pepo.mind';
const q = (sel) => document.querySelector(sel);
const startBtn = q('#start-btn');
const stopBtn = q('#stop-btn');
const msgBox = q('#msg');
const permissionMsg = q('#permission-msg');
let hideMsgId = null;
let mindarThree = null;
// ═══════════════════════════════════════════════════════════════
// 🔊 GESTORE AUDIO CON PAUSA/RESUME
// ═══════════════════════════════════════════════════════════════
class AudioManager {
constructor() {
this.currentAudio = null;
this.wasPlayingBeforePause = false;
this.audioContext = null;
this.isAppPaused = false;
this.setupVisibilityHandlers();
}
setupVisibilityHandlers() {
// Rileva quando l'app va in background/foreground
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
this.handleAppPause();
} else {
this.handleAppResume();
}
});
// Rileva quando la finestra perde/guadagna focus
window.addEventListener('blur', () => this.handleAppPause());
window.addEventListener('focus', () => this.handleAppResume());
// Per dispositivi mobili: rileva quando lo schermo si spegne
window.addEventListener('pagehide', () => this.handleAppPause());
window.addEventListener('pageshow', () => this.handleAppResume());
}
handleAppPause() {
if (this.isAppPaused) return; // Evita chiamate multiple
console.log('🔇 App in pausa - fermo audio');
this.isAppPaused = true;
if (this.currentAudio && this.currentAudio.isPlaying) {
this.wasPlayingBeforePause = true;
this.currentAudio.pause();
showMsg('⏸️ Audio in pausa', null); // Messaggio persistente
stopBtn.style.display = 'none';
} else {
this.wasPlayingBeforePause = false;
}
// Sospendi audio context per risparmiare batteria
if (this.audioContext && this.audioContext.state === 'running') {
this.audioContext.suspend();
}
}
handleAppResume() {
if (!this.isAppPaused) return; // Evita chiamate multiple
console.log('▶️ App ripresa');
this.isAppPaused = false;
// Riattiva audio context
if (this.audioContext && this.audioContext.state === 'suspended') {
this.audioContext.resume();
}
if (this.wasPlayingBeforePause && this.currentAudio) {
// Chiedi all'utente se vuole riprendere
showMsg('🔄 Tocca per riprendere l\'audio', null);
// Aggiungi listener temporaneo per riprendere
const resumeHandler = () => {
if (this.currentAudio && this.wasPlayingBeforePause) {
this.currentAudio.play();
showMsg('▶️ Audio ripreso', 3000);
stopBtn.style.display = 'block';
}
// Rimuovi listener dopo l'uso
document.removeEventListener('click', resumeHandler);
document.removeEventListener('touchstart', resumeHandler);
};
document.addEventListener('click', resumeHandler, { once: true });
document.addEventListener('touchstart', resumeHandler, { once: true });
} else {
showMsg('✅ AR attivo - inquadra un target', 4000);
}
this.wasPlayingBeforePause = false;
}
setCurrentAudio(audio, audioContext) {
this.currentAudio = audio;
this.audioContext = audioContext;
}
stopCurrentAudio() {
if (this.currentAudio && this.currentAudio.isPlaying) {
this.currentAudio.stop();
}
this.currentAudio = null;
this.wasPlayingBeforePause = false;
stopBtn.style.display = 'none';
}
isPlaying() {
return this.currentAudio && this.currentAudio.isPlaying;
}
}
// ═══════════════════════════════════════════════════════════════
// 🛠️ UTILITY FUNCTIONS
// ═══════════════════════════════════════════════════════════════
function showMsg(text, timeout = null) {
msgBox.textContent = text;
msgBox.style.display = 'block';
clearTimeout(hideMsgId);
if (timeout !== null) {
hideMsgId = setTimeout(() => {
msgBox.style.display = 'none';
}, timeout);
}
}
function debug(message) {
console.log('[DEBUG]', message);
}
function isMobileDevice() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
// ═══════════════════════════════════════════════════════════════
// 🎯 FUNZIONE PRINCIPALE AR
// ═══════════════════════════════════════════════════════════════
// Inizializza il gestore audio globale
const audioManager = new AudioManager();
async function startAR() {
try {
debug('=== INIZIO startAR() ===');
startBtn.style.display = 'none';
if (permissionMsg) permissionMsg.style.display = 'none';
// Inizializza AudioContext per iOS Safari
try {
// Crea AudioContext e riprendi se sospeso
if (typeof AudioContext !== 'undefined' || typeof webkitAudioContext !== 'undefined') {
window.audioContext = new (window.AudioContext || window.webkitAudioContext)();
if (window.audioContext.state === 'suspended') {
await window.audioContext.resume();
}
debug('✅ AudioContext inizializzato per iOS Safari');
}
} catch (error) {
debug('⚠️ Inizializzazione AudioContext fallita: ' + error.message);
}
// Inizializza MindAR con configurazione ottimizzata
mindarThree = new MindARThree({
container: q("#container"),
imageTargetSrc: MIND_PATH,
uiScanning: "#scanning",
uiLoading: "no",
filterMinCF: 0.0005,
filterBeta: 0.005,
rendererSettings: {
alpha: true,
antialias: true,
powerPreference: "high-performance"
},
maxTrack: NUM_TARGETS,
warmupTolerance: 5,
missTolerance: 5
});
const { renderer, scene, camera } = mindarThree;
const container = q("#container");
const scanningElement = q("#scanning");
// Setup audio
const listener = new THREE.AudioListener();
camera.add(listener);
const loader = new THREE.AudioLoader();
const audios = [];
const audioPromises = [];
// Luci per la scena
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.6);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
debug('Creazione target e caricamento audio...');
// Setup per ogni target
for (let i = 0; i < NUM_TARGETS; i++) {
const anchor = mindarThree.addAnchor(i);
const sound = new THREE.PositionalAudio(listener);
// Promessa per il caricamento audio
const audioPromise = new Promise((resolve, reject) => {
loader.load(
`${AUDIO_PATH}audio_${i}.mp3`,
(buffer) => {
debug(`Audio ${i} caricato: ${buffer.duration.toFixed(2)}s`);
sound.setBuffer(buffer);
sound.setRefDistance(1); // ↙ distanza base piccola
sound.setRolloffFactor(0); // ↙ disattiva attenuazione
// sound.panner.distanceModel = 'linear'; // opzionale
sound.setVolume(1.0);
resolve(sound);
},
(progress) => {
if (progress.total > 0) {
const percent = (progress.loaded / progress.total * 100).toFixed(0);
debug(`Audio ${i} caricamento: ${percent}%`);
}
},
(error) => {
console.error(`Errore audio ${i}:`, error);
reject(error);
}
);
});
audioPromises.push(audioPromise);
audios[i] = sound;
anchor.group.add(sound);
// Mesh di debug
// const debugMesh = new THREE.Mesh(
// new THREE.PlaneGeometry(1, 1),
// new THREE.MeshBasicMaterial({
// color: 0x00ff00,
// transparent: true,
// opacity: 0.2,
// side: THREE.DoubleSide
// })
// );
// anchor.group.add(debugMesh);
// Timer per gestione perdita target
let lostTimer = null;
// Event handlers
anchor.onTargetFound = () => {
debug(`🎯 TARGET ${i} TROVATO!`);
clearTimeout(lostTimer);
// Ferma audio precedente se presente
if (audioManager.isPlaying()) {
audioManager.stopCurrentAudio();
showMsg('🔄 Cambio audio...', 2000);
}
// Verifica che l'audio context sia attivo
if (listener.context.state !== 'running') {
showMsg('⚠️ Tocca "Inizia AR" per sbloccare audio');
return;
}
if (sound.buffer) {
debug(`▶️ Riproduzione audio ${i}`);
sound.play();
audioManager.setCurrentAudio(sound, listener.context);
stopBtn.style.display = 'block';
showMsg(`🎵 Audio ${i + 1} in riproduzione`);
// Gestione fine audio
if (sound.source) {
sound.source.onended = () => {
if (audioManager.currentAudio === sound) {
audioManager.stopCurrentAudio();
showMsg('✅ Audio terminato', 4000);
}
};
}
} else {
showMsg(`⚠️ Audio ${i + 1} non ancora caricato`);
}
// Nascondi UI di scanning
setTimeout(() => {
if (scanningElement) {
scanningElement.classList.add('hidden');
scanningElement.style.display = 'none';
}
}, 50);
};
anchor.onTargetLost = () => {
debug(`🎯 Target ${i} perso... attendo conferma`);
clearTimeout(lostTimer);
lostTimer = setTimeout(() => {
debug('Perdita confermata, mostro UI');
// Mostra UI di scanning
if (scanningElement) {
scanningElement.classList.remove('hidden');
scanningElement.style.display = 'flex';
}
// L'audio continua a suonare anche se il target è perso
// (come da tua richiesta)
}, 300); // debounce: 300 ms
};
}
// Attendiamo che tutti gli audio siano caricati
await Promise.allSettled(audioPromises);
debug('✅ Tutti gli audio processati');
// Setup stop button
stopBtn.addEventListener('click', () => {
debug('🔇 STOP premuto');
audioManager.stopCurrentAudio();
showMsg('🔇 Audio fermato', 3000);
});
// Avvio MindAR
await mindarThree.start();
debug('✅ MindAR avviato');
// Resume audio context se necessario
if (listener.context.state === 'suspended') {
await listener.context.resume();
}
// Riproduci guida iniziale
const guida = new Audio('./assets/audio/guida.mp3');
guida.play().then(() => {
debug('▶️ Voce guida iniziale riprodotta');
}).catch((e) => {
debug('⚠️ Errore audio guida:', e);
});
// Setup renderer
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
// Avvia render loop
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
showMsg('✅ AR avviato! Inquadra un target', 4000);
// Debug finale
setTimeout(() => {
const video = mindarThree.video;
if (video) {
debug(`📹 Video: ${video.videoWidth}x${video.videoHeight}, playing: ${!video.paused}`);
}
debug(`📱 Scene children: ${scene.children.length}`);
}, 2000);
} catch (error) {
console.error('💥 ERRORE startAR:', error);
showMsg(`❌ Errore: ${error.message}`, 6000);
startBtn.style.display = 'block';
}
}
// ═══════════════════════════════════════════════════════════════
// 🚀 INIZIALIZZAZIONE
// ═══════════════════════════════════════════════════════════════
document.addEventListener('DOMContentLoaded', () => {
debug('📱 main.js caricato');
startBtn.addEventListener('click', startAR);
// Gestione orientamento per mobile
if (isMobileDevice()) {
window.addEventListener('orientationchange', () => {
setTimeout(() => {
if (mindarThree) {
const container = q("#container");
const { renderer } = mindarThree;
renderer.setSize(container.clientWidth, container.clientHeight);
debug('📱 Orientamento cambiato, renderer riaggiustato');
}
}, 100);
});
}
// Gestione resize
window.addEventListener('resize', () => {
if (mindarThree) {
const container = q("#container");
const { renderer } = mindarThree;
renderer.setSize(container.clientWidth, container.clientHeight);
debug(`🔄 Resize: ${container.clientWidth}x${container.clientHeight}`);
}
});
});