AR WORKS, some bugs present...

This commit is contained in:
LionTion committed 2026-10-08 21:59:06 +02:00
1 parent f12fc75adc
commit c56377406b
6 files changed
+3249 -725

No files matched your search

+192
View File
@@ -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>
);
};
+16 -10
View File
@@ -1,16 +1,17 @@
"use client";
import { useMemo, useState } from "react";
import { useState } from "react";
import OldButton from "../OldButton";
import styles from "@/css/RiddleStyles.module.css";
import { useLocalStorage } from "@/hooks/useLocalStorage";
import { PREF } from "@/utils/const";
import { RiddleScreenControls } from "../RiddleScreen";
import { addPoints } from "@/utils/points";
import RiddleCompleteBanner from "../RiddleCompleteBanner";
import { getPlayers } from "@/utils/players";
import SelectionButton from "../SelectionButton";
import extraStyles from "@/components/riddles-css/Riddle_7.module.css";
import dynamic from 'next/dynamic';
const Riddle_7_AR_View = dynamic(
() => import('../Riddle_7_AR_View'),
{ ssr: false }
);
export default function Riddle_7({
page,
@@ -48,11 +49,13 @@ export default function Riddle_7({
<p>Öffnet zusätzlich den Umschlag mit dem Beweismaterial und untersucht dessen Inhalt sorgfältig.</p>
<p>Vergleicht Aussagen, Spuren und Beweise miteinander.</p>
<div style={{width: "100%", display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center"}}>
<OldButton red={false} onAction={() => setPage(2)} disabled={false}>WEITER</OldButton>
<OldButton red={false} onAction={() => {
setPage(2);
showBanner(false);
}} disabled={false}>WEITER</OldButton>
</div>
</div>
<div className={styles["buttons"]}>
<OldButton red={true} onAction={() => setPage(0)}>ZURÜCK</OldButton>
</div>
</div>
@@ -60,7 +63,7 @@ export default function Riddle_7({
(
<div key={2}>
<div className={styles["content"]}>
{ /* CAM */ }
<Riddle_7_AR_View />
</div>
<div className={styles["buttons"]}>
<OldButton red={false} disabled={false} onAction={() => setPage(3)}>URTEIL SPRECHEN</OldButton>
@@ -88,7 +91,10 @@ export default function Riddle_7({
</div>
</div>
<div className={styles["buttons"]}>
<OldButton red={true} disabled={false} onAction={() => setPage(1)}>ZURÜCK</OldButton>
<OldButton red={true} disabled={false} onAction={() => {
setPage(1);
showBanner(true);
}}>ZURÜCK</OldButton>
</div>
</div>
),