AR WORKS, some bugs present...
This commit is contained in:
1 parent
f12fc75adc
commit
c56377406b
6 files changed
+3249
-725
No files matched your search
@@ -0,0 +1,192 @@
|
||||
"use client";
|
||||
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import 'aframe';
|
||||
import 'mind-ar/dist/mindar-image-aframe.prod.js';
|
||||
|
||||
const TARGET_CONFIG = [
|
||||
{ targetIndex: 0, videoId: 'video0', videoSrc: '/res/tour/riddle_7/videos/Oswin der Stallknecht.mp4' },
|
||||
{ targetIndex: 1, videoId: 'video1', videoSrc: '/res/tour/riddle_7/videos/Oswin der Stallknecht.mp4' },
|
||||
{ targetIndex: 2, videoId: 'video2', videoSrc: '/res/tour/riddle_7/videos/Oswin der Stallknecht.mp4' },
|
||||
{ targetIndex: 3, videoId: 'video3', videoSrc: '/res/tour/riddle_7/videos/Oswin der Stallknecht.mp4' },
|
||||
];
|
||||
|
||||
export default function Riddle_7_AR_View() {
|
||||
const sceneRef = useRef(null);
|
||||
const targetsRef = useRef([]);
|
||||
const videosRef = useRef({});
|
||||
const lostTimesRef = useRef({});
|
||||
|
||||
const TARGET_HEIGHT = 1;
|
||||
|
||||
const [videoDimensions, setVideoDimensions] = useState({});
|
||||
const [hasInteracted, setHasInteracted] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const sceneEl = sceneRef.current;
|
||||
const arSystem = sceneEl.systems['mindar-image-system'];
|
||||
|
||||
sceneEl.addEventListener('renderstart', () => {
|
||||
arSystem.start();
|
||||
});
|
||||
|
||||
// Attach target tracking listeners for each target entity
|
||||
targetsRef.current.forEach((targetEl, index) => {
|
||||
if (!targetEl) return;
|
||||
|
||||
const videoEl = videosRef.current[index];
|
||||
|
||||
const handleTargetFound = () => {
|
||||
if (videoEl) {
|
||||
// Reset video if lost for > 10 seconds
|
||||
const lostTime = lostTimesRef.current[index];
|
||||
if (lostTime && Date.now() - lostTime > 10000) {
|
||||
videoEl.currentTime = 0;
|
||||
}
|
||||
lostTimesRef.current[index] = null;
|
||||
|
||||
videoEl.play().catch((err) => {
|
||||
console.log(`Autoplay blocked on target ${index}:`, err);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handleTargetLost = () => {
|
||||
if (videoEl) {
|
||||
videoEl.pause();
|
||||
lostTimesRef.current[index] = Date.now();
|
||||
}
|
||||
};
|
||||
|
||||
targetEl.addEventListener('targetFound', handleTargetFound);
|
||||
targetEl.addEventListener('targetLost', handleTargetLost);
|
||||
});
|
||||
|
||||
return () => {
|
||||
if (arSystem) {
|
||||
try {
|
||||
arSystem.stop();
|
||||
} catch (error) {
|
||||
console.warn('MindAR stopped after DOM teardown:', error);
|
||||
}
|
||||
}
|
||||
}}, []);
|
||||
|
||||
// Set width dynamically based on video aspect ratio
|
||||
const handleLoadedMetadata = (index, videoEl) => {
|
||||
if (videoEl) {
|
||||
const aspect = videoEl.videoWidth / videoEl.videoHeight;
|
||||
setVideoDimensions((prev) => ({
|
||||
...prev,
|
||||
[index]: {
|
||||
width: TARGET_HEIGHT * aspect,
|
||||
height: TARGET_HEIGHT,
|
||||
},
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
// User interaction handler to unlock web audio context on mobile devices
|
||||
const handleUserStart = () => {
|
||||
setHasInteracted(true);
|
||||
Object.values(videosRef.current).forEach((videoEl) => {
|
||||
if (videoEl) {
|
||||
videoEl
|
||||
.play()
|
||||
.then(() => videoEl.pause())
|
||||
.catch((err) => console.log('Audio unlock error:', err));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
position: 'fixed',
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: '100vw',
|
||||
height: '100vh',
|
||||
overflow: 'hidden',
|
||||
backgroundColor: '#000',
|
||||
}}
|
||||
>
|
||||
{!hasInteracted && (
|
||||
<button
|
||||
onClick={handleUserStart}
|
||||
style={{
|
||||
position: 'absolute',
|
||||
zIndex: 9999,
|
||||
top: '50%',
|
||||
left: '50%',
|
||||
transform: 'translate(-50%, -50%)',
|
||||
padding: '14px 28px',
|
||||
fontSize: '16px',
|
||||
fontWeight: 'bold',
|
||||
backgroundColor: '#007bff',
|
||||
color: '#fff',
|
||||
border: 'none',
|
||||
borderRadius: '8px',
|
||||
boxShadow: '0 4px 12px rgba(0,0,0,0.3)',
|
||||
cursor: 'pointer',
|
||||
}}
|
||||
>
|
||||
Tap to Start AR with Audio
|
||||
</button>
|
||||
)}
|
||||
|
||||
<a-scene
|
||||
ref={sceneRef}
|
||||
mindar-image="imageTargetSrc: /res/tour/riddle_7/targets.mind; autoStart: false; uiLoading: no; uiError: no; uiScanning: no; filterMinCF: 0.0001; filterBeta: 0.001;"
|
||||
color-space="sRGB"
|
||||
embedded
|
||||
renderer="colorManagement: true, physicallyCorrectLights; antialias: true"
|
||||
vr-mode-ui="enabled: false"
|
||||
device-orientation-permission-ui="enabled: false"
|
||||
style={{ width: '100%', height: '100%' }}
|
||||
>
|
||||
<a-assets>
|
||||
{TARGET_CONFIG.map((target) => (
|
||||
<video
|
||||
key={target.videoId}
|
||||
id={target.videoId}
|
||||
ref={(el) => (videosRef.current[target.targetIndex] = el)}
|
||||
src={target.videoSrc}
|
||||
preload="auto"
|
||||
loop
|
||||
crossOrigin="anonymous"
|
||||
playsInline
|
||||
onLoadedMetadata={(e) =>
|
||||
handleLoadedMetadata(target.targetIndex, e.target)
|
||||
}
|
||||
></video>
|
||||
))}
|
||||
</a-assets>
|
||||
|
||||
<a-camera position="0 0 0" look-controls="enabled: false"></a-camera>
|
||||
|
||||
{TARGET_CONFIG.map((target) => {
|
||||
const dims = videoDimensions[target.targetIndex] || {
|
||||
width: 1,
|
||||
height: TARGET_HEIGHT,
|
||||
};
|
||||
return (
|
||||
<a-entity
|
||||
key={target.targetIndex}
|
||||
ref={(el) => (targetsRef.current[target.targetIndex] = el)}
|
||||
mindar-image-target={`targetIndex: ${target.targetIndex}`}
|
||||
>
|
||||
<a-video
|
||||
src={`#${target.videoId}`}
|
||||
width={dims.width}
|
||||
height={dims.height}
|
||||
position="0 0 0"
|
||||
rotation="0 0 0"
|
||||
></a-video>
|
||||
</a-entity>
|
||||
);
|
||||
})}
|
||||
</a-scene>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user