Advertisement
RostykBT

XRworkshop

Nov 20th, 2024 (edited)
103
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 2.84 KB | Source Code | 0 0
  1. "use client";
  2. import { Canvas } from '@react-three/fiber';
  3. import { OrbitControls, Environment, useGLTF, DragControls, Box, RoundedBox, ContactShadows, AccumulativeShadows, Grid } from '@react-three/drei';
  4. import { PointerEvents, XR, createXRStore, noEvents } from '@react-three/xr';
  5. import { AmbientLight, GridHelper, Mesh, PointLight, Ray, Vector3 } from 'three';
  6. import { Suspense, useRef, useState } from 'react';
  7. import { Physics, RigidBody } from '@react-three/rapier';
  8. import DraggableRigidBody from '@/helpers/DraggableRigidBody';
  9. import { DraggableRigidBodyProps } from '@/helpers/DraggableRigidBody';
  10. import { Button } from '@/components/ui/button';
  11. import { outline } from 'three/examples/jsm/tsl/display/OutlineNode.js';
  12. import { Draggable, HandInteractions } from '@/components/draggable';
  13. const store = createXRStore({
  14.   hand: {
  15.     grabPointer: true,
  16.     rayPointer: false,
  17.     touchPointer: false,
  18.   },
  19.   secondaryInputSources: true,
  20.  
  21.   controller: {
  22.     rayPointer: true,
  23.     grabPointer: true,
  24.   },
  25. })
  26.  
  27. // Beggingin of the Project //////////////////////////////////////////////
  28. export default function XRPage() {
  29.   return (
  30.     <>
  31.       <Button className='m-12' variant={"outline"} onClick={() => store.enterAR()}>Enter AR</Button>
  32.       <XRScene />
  33.  
  34.     </>
  35.   );
  36. }
  37.  
  38.  
  39.  
  40. export function XRScene() {
  41.   return (
  42.     <>
  43.       <Canvas className='h-full' camera={{ position: [0, 3, 5] }}>
  44.         <XR store={store}>
  45.           <ambientLight />
  46.           <OrbitControls />
  47.           <Environment preset='city' />
  48.  
  49.           <Physics paused={true}>
  50.             <DominoBone position={[0, 0.64, 0]} rotation={[0, 1.5707963267948966, 0]} />
  51.             {/* <DominoBone position={[0, 0.25, 0.5]} /> */}
  52.             <Floor />
  53.             <Draggable>
  54.               <Model />
  55.             </Draggable>
  56.  
  57.           </Physics>
  58.           <HandInteractions />
  59.         </XR>
  60.       </Canvas >
  61.     </>
  62.   )
  63. }
  64.  
  65. export function DominoBone({ position = [0, 0, 0], rotation = [0, 0, 0], ...props }: { position?: [number, number, number], rotation?: [number, number, number] }) {
  66.   return (
  67.     <RigidBody position={position} rotation={rotation}>
  68.       <RoundedBox
  69.         args={[0.5, 1, 0.2]}
  70.         radius={0.02}
  71.         smoothness={4}
  72.         bevelSegments={4}
  73.         castShadow={true}
  74.       >
  75.         <meshStandardMaterial color={"red"} />
  76.       </RoundedBox>
  77.     </RigidBody>
  78.   )
  79. }
  80.  
  81. function Floor() {
  82.   return (
  83.     <RigidBody type='fixed' position={[0, -0.05, 0]}>
  84.       <mesh>
  85.         <boxGeometry args={[10, 0.1, 10]} />
  86.         <meshStandardMaterial color={"grey"} />
  87.       </mesh>
  88.     </RigidBody>
  89.   )
  90. }
  91.  
  92.  
  93. function Model(props: any) {
  94.   const { scene } = useGLTF('https://vazxmixjsiawhamofees.supabase.co/storage/v1/object/public/models/hammer/model.gltf')
  95.   return <primitive object={scene} {...props} />
  96. }
  97.  
  98.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement