-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
175 additions
and
21 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { useSphere } from '@react-three/cannon'; | ||
import { PointerLockControls } from '@react-three/drei'; | ||
import { useFrame, useThree } from '@react-three/fiber'; | ||
import { useState, useEffect, useRef } from 'react'; | ||
import * as THREE from 'three'; | ||
|
||
export const usePlayerControls = () => { | ||
const keys = { KeyW: 'forward', KeyS: 'backward', KeyA: 'left', KeyD: 'right', Space: 'jump' }; | ||
const moveFieldByKey = (key: any) => keys[key as keyof typeof keys]; | ||
|
||
const [movement, setMovement] = useState({ | ||
forward: false, backward: false, left: false, right: false, jump: false, | ||
}); | ||
|
||
useEffect(() => { | ||
const handleKeyDown = (e) => setMovement((m) => ({ ...m, [moveFieldByKey(e.code)]: true })); | ||
const handleKeyUp = (e) => setMovement((m) => ({ ...m, [moveFieldByKey(e.code)]: false })); | ||
|
||
document.addEventListener('keydown', handleKeyDown); | ||
document.addEventListener('keyup', handleKeyUp); | ||
|
||
return () => { | ||
document.removeEventListener('keydown', handleKeyDown); | ||
document.removeEventListener('keyup', handleKeyUp); | ||
}; | ||
}, []); | ||
|
||
return movement; | ||
}; | ||
|
||
export const FPV = (props: any) => { | ||
const direction = new THREE.Vector3(); | ||
const frontVector = new THREE.Vector3(); | ||
const sideVector = new THREE.Vector3(); | ||
const speed = new THREE.Vector3(); | ||
const SPEED = 5; | ||
|
||
const { camera, gl } = useThree(); | ||
|
||
const [ref, api] = useSphere((index) => ({ | ||
mass: 1, | ||
type: 'Dynamic', | ||
position: [0, 10, 0], | ||
...props, | ||
})); | ||
|
||
const { forward, backward, left, right, jump } = usePlayerControls(); | ||
const velocity = useRef([0, 0, 0]); | ||
useEffect(() => api.velocity.subscribe((v) => (velocity.current = v)), []); | ||
|
||
useFrame((state) => { | ||
ref.current!.getWorldPosition(camera.position); | ||
frontVector.set(0, 0, Number(backward) - Number(forward)); | ||
sideVector.set(Number(left) - Number(right), 0, 0); | ||
direction | ||
.subVectors(frontVector, sideVector) | ||
.normalize() | ||
.multiplyScalar(SPEED) | ||
.applyEuler(camera.rotation); | ||
speed.fromArray(velocity.current); | ||
|
||
api.velocity.set(direction.x, velocity.current[1], direction.z); | ||
if (jump && Math.abs(Math.round(velocity.current[1] * 100) / 100) < 0.05) { | ||
api.velocity.set(velocity.current[0], 5, velocity.current[2]); | ||
} | ||
}); | ||
|
||
return ( | ||
<> | ||
<PointerLockControls args={[camera, gl.domElement]} /> | ||
<group> | ||
<mesh castShadow position={props.position} ref={ref}> | ||
<sphereGeometry args={props.args} /> | ||
<meshStandardMaterial color="#FFFF00" /> | ||
</mesh> | ||
</group> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { usePlane } from '@react-three/cannon'; | ||
|
||
export const Floor = (props) => { | ||
const [ref] = usePlane((index) => ({ type: 'Static', mass: 0, ...props })); | ||
|
||
return ( | ||
<mesh receiveShadow rotation={props.rotation} ref={ref}> | ||
<planeGeometry args={[1000, 1000]} /> | ||
<meshStandardMaterial color={props.color} /> | ||
</mesh> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { usePlane } from '@react-three/cannon'; | ||
import { extend } from '@react-three/fiber'; | ||
import { MeshStandardMaterial, PlaneGeometry } from 'three'; | ||
|
||
extend({ MeshStandardMaterial, PlaneGeometry }); | ||
|
||
export function Ground() { | ||
const [ref] = usePlane(() => ({ | ||
rotation: [-Math.PI / 2, 0, 0], | ||
position: [0, -0.01, 0], | ||
})); | ||
|
||
return ( | ||
<mesh | ||
ref={ref as any} | ||
> | ||
<planeGeometry attach="geometry" args={[100, 100]} /> | ||
<meshStandardMaterial /> | ||
</mesh> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Sparkles } from '@react-three/drei'; | ||
import { useLoader } from '@react-three/fiber'; | ||
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; | ||
|
||
export const Scene = () => { | ||
const gltf = useLoader(GLTFLoader, '/assets/3d/gallery.glb'); | ||
|
||
return ( | ||
<group> | ||
<Sparkles count={200} scale={[20, 20, 10]} size={3} speed={2} /> | ||
<primitive | ||
object={gltf.scene} | ||
position={[0, 0, 0]} | ||
children-0-castShadow | ||
/> | ||
</group> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Sky, SpotLightShadow } from '@react-three/drei'; | ||
import { Canvas } from '@react-three/fiber'; | ||
import { Physics } from '@react-three/cannon'; | ||
import { useApi } from '@/hooks/useApi'; | ||
import { UserContentFileElement } from '@/types'; | ||
import { Ground } from './components/gallery/Ground'; | ||
import { FPV } from './components/gallery/FPV'; | ||
import { Scene } from './components/gallery/Scene'; | ||
|
||
export const Gallery3D = ({ gallery }: { gallery: string }) => { | ||
const { data } = useApi<Array<UserContentFileElement>>(`gallery/${gallery}`); | ||
|
||
console.log('🐢🐢🐢🐢', data); | ||
|
||
return ( | ||
<Canvas> | ||
<ambientLight intensity={2.5} /> | ||
<spotLight | ||
penumbra={0.5} | ||
position={[10, 10, 5]} | ||
castShadow /> | ||
<Physics> | ||
<FPV controls position={[0, 1, 0]} args={[2]} color="yellow" /> | ||
<Ground /> | ||
<Scene /> | ||
</Physics> | ||
<Sky /> | ||
</Canvas> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters