MCPcopy
hub / github.com/pmndrs/react-three-fiber / Viewcube

Function Viewcube

example/src/demos/Viewcube.tsx:6–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4import * as THREE from 'three'
5
6function Viewcube() {
7 const { gl, scene: defaultScene, camera: defaultCamera, size, events } = useThree()
8 const scene = useMemo(() => new THREE.Scene(), [])
9 const camera = useMemo(() => new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 1000), [])
10
11 useLayoutEffect(() => {
12 camera.left = -size.width / 2
13 camera.right = size.width / 2
14 camera.top = size.height / 2
15 camera.bottom = -size.height / 2
16 camera.position.set(0, 0, 100)
17 camera.updateProjectionMatrix()
18 }, [size])
19
20 const ref = useRef<THREE.Mesh>(null!)
21 const [hover, setHover] = useState<number | null>(null)
22 const matrix = new THREE.Matrix4()
23
24 useFrame(() => {
25 matrix.copy(defaultCamera.matrix).invert()
26 ref.current.quaternion.setFromRotationMatrix(matrix)
27 gl.autoClear = true
28 gl.render(defaultScene, defaultCamera)
29 gl.autoClear = false
30 gl.clearDepth()
31 gl.render(scene, camera)
32 }, 1)
33
34 return (
35 <>
36 {createPortal(
37 <group>
38 <mesh
39 ref={ref}
40 position={[size.width / 2 - 120, size.height / 2 - 120, 0]}
41 onPointerOut={(e) => setHover(null)}
42 onPointerMove={(e) => setHover(Math.floor((e.faceIndex || 0) / 2))}>
43 {[...Array(6)].map((_, index) => (
44 <meshLambertMaterial
45 attach={`material-${index}`}
46 key={index}
47 color={hover === index ? 'hotpink' : 'white'}
48 />
49 ))}
50 <boxGeometry args={[80, 80, 80]} />
51 </mesh>
52 <ambientLight intensity={0.5 * Math.PI} />
53 <pointLight decay={0} position={[10, 10, 10]} intensity={0.5} />
54 </group>,
55 scene,
56 { camera, events: { priority: events.priority + 1 } },
57 )}
58 </>
59 )
60}
61
62export default function App() {
63 return (

Callers

nothing calls this directly

Calls 5

useThreeFunction · 0.90
useFrameFunction · 0.90
createPortalFunction · 0.90
setMethod · 0.80
renderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…