153 lines
5.2 KiB
TypeScript
153 lines
5.2 KiB
TypeScript
"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/Mara die Kräuterfrau.mp4' },
|
|
{ targetIndex: 2, videoId: 'video2', videoSrc: '/res/tour/riddle_7/videos/Edmund der Schreiber.mp4' },
|
|
{ targetIndex: 3, videoId: 'video3', videoSrc: '/res/tour/riddle_7/videos/Sir Garrick die Torwache.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();
|
|
});
|
|
|
|
targetsRef.current.forEach((targetEl, index) => {
|
|
if (!targetEl) return;
|
|
|
|
const videoEl = videosRef.current[index];
|
|
|
|
const handleTargetFound = () => {
|
|
if (videoEl) {
|
|
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);
|
|
}
|
|
}
|
|
}}, []);
|
|
|
|
const handleLoadedMetadata = (index, videoEl) => {
|
|
if (videoEl) {
|
|
const aspect = videoEl.videoWidth / videoEl.videoHeight;
|
|
setVideoDimensions((prev) => ({
|
|
...prev,
|
|
[index]: {
|
|
width: TARGET_HEIGHT * aspect,
|
|
height: TARGET_HEIGHT,
|
|
},
|
|
}));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div
|
|
style={{
|
|
position: 'fixed',
|
|
top: 0,
|
|
left: 0,
|
|
width: '100vw',
|
|
height: '100vh',
|
|
overflow: 'hidden',
|
|
backgroundColor: '#000',
|
|
}}
|
|
>
|
|
|
|
<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>
|
|
);
|
|
}; |