From 676bbfe9a87e97c344659df88da145de08e39fac Mon Sep 17 00:00:00 2001 From: Alejandro Rosales Date: Fri, 14 Mar 2025 16:32:22 -0600 Subject: [PATCH] Make it immersive --- src/pages/Previewer.tsx | 89 ++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 51 deletions(-) diff --git a/src/pages/Previewer.tsx b/src/pages/Previewer.tsx index a75f671..49684ab 100644 --- a/src/pages/Previewer.tsx +++ b/src/pages/Previewer.tsx @@ -1,8 +1,9 @@ "use client"; -import type { OrbitControls as OrbitControlsImpl } from 'three-stdlib'; -import { Canvas } from "@react-three/fiber"; +import { OrbitControls as OrbitControlsImpl } from 'three-stdlib'; import { useGLTF, useAnimations, Environment, Loader, OrbitControls, Bounds } from "@react-three/drei"; +import { Canvas } from "@react-three/fiber"; +import { createXRStore, XR } from "@react-three/xr"; import { Suspense, useEffect, useRef, useState } from "react"; import { RotateCw, Play, Pause, Film, Shrink, Expand } from "lucide-react"; import { Button } from "@/components/ui/button"; @@ -17,10 +18,10 @@ import { useNavigate } from 'react-router'; // Model component -function Model({ url, onAnimationsLoaded, onNodesLoaded }: { +function Model({ url, onAnimationsLoaded, isAR }: { url: string; onAnimationsLoaded: (animations: string[], actions: Record) => void; - onNodesLoaded: (nodes: string[], nodePositions: Record) => void; + isAR: boolean; }) { const { scene, animations } = useGLTF(url); const { actions } = useAnimations(animations, scene); @@ -30,16 +31,17 @@ function Model({ url, onAnimationsLoaded, onNodesLoaded }: { onAnimationsLoaded(animations.map(anim => anim.name), actions); } - if (scene.children) { - const nodeList = scene.children.map(node => node.name); - const nodePositions = scene.children.reduce((acc, node) => { - acc[node.name] = [node.position.x, node.position.y, node.position.z]; - return acc; - }, {} as Record); - - onNodesLoaded(nodeList, nodePositions); + if (isAR) { + // 100 times smaller + scene.scale.set(0.01, 0.01, 0.01); + scene.position.set(0, 0, 0); } - }, [actions, animations, onAnimationsLoaded, onNodesLoaded]); + else { + scene.scale.set(1, 1, 1); + scene.position.set(0, 0, 0); + } + + }, [actions, animations, onAnimationsLoaded, isAR]); return ( @@ -50,20 +52,18 @@ function Model({ url, onAnimationsLoaded, onNodesLoaded }: { ); } +const store = createXRStore(); export default function Previewer({ modelUrl }: { modelUrl: string }) { const controlsRef = useRef(null); const containerRef = useRef(null); const [animations, setAnimations] = useState([]); const [actions, setActions] = useState>({}); const [currentAnimation, setCurrentAnimation] = useState(null); + const [isAR, setIsAR] = useState(false); - const [_, setNodes] = useState([]); - const [nodePositions, setNodePositions] = useState>({}); - const [isFullscreen, setIsFullscreen] = useState(false); const navigate = useNavigate(); - nodePositions; const playAnimation = (name: string) => { if (actions[name]) { @@ -79,24 +79,6 @@ export default function Previewer({ modelUrl }: { modelUrl: string }) { setCurrentAnimation(null); } }; - - // Handle Fullscreen Mode - const toggleFullscreen = () => { - if (!document.fullscreenElement) { - containerRef.current?.requestFullscreen().then(() => { - setIsFullscreen(true); - }).catch(err => { - console.error("Error entering fullscreen:", err); - }); - } else { - document.exitFullscreen().then(() => { - setIsFullscreen(false); - }).catch(err => { - console.error("Error exiting fullscreen:", err); - }); - } - }; - return (
@@ -144,28 +126,33 @@ export default function Previewer({ modelUrl }: { modelUrl: string }) {
}> - - - + + + { - setAnimations(animNames); - setActions(actionMap); - }} - onNodesLoaded={(nodeList, nodePos) => { - setNodes(nodeList); - setNodePositions(nodePos); - }} - /> + onAnimationsLoaded={(animNames, actionMap) => { + setAnimations(animNames); + setActions(actionMap); + }} + + isAR={isAR} + /> +