-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Controls): adding CameraControls
- Loading branch information
Showing
6 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
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,89 @@ | ||
import { createPortal, useFrame } from '@react-three/fiber' | ||
import React, { useRef, useState } from 'react' | ||
import { Scene } from 'three' | ||
|
||
import { Setup } from '../Setup' | ||
import { Box, CameraControls, PerspectiveCamera, Plane, useFBO } from '../../src' | ||
|
||
import type { Camera } from 'three' | ||
import type { CameraControlsProps } from '../../src' | ||
|
||
const args = {} | ||
|
||
export const CameraControlsStory = (props: CameraControlsProps) => { | ||
const cameraControlRef = useRef<CameraControls | null>(null) | ||
|
||
return ( | ||
<> | ||
<CameraControls ref={cameraControlRef} {...props} /> | ||
<Box | ||
onClick={() => { | ||
cameraControlRef.current?.rotate(Math.PI / 4, 0, true) | ||
}} | ||
> | ||
<meshBasicMaterial wireframe /> | ||
</Box> | ||
</> | ||
) | ||
} | ||
|
||
CameraControlsStory.args = args | ||
CameraControlsStory.storyName = 'Default' | ||
|
||
export default { | ||
title: 'Controls/CameraControls', | ||
component: CameraControls, | ||
decorators: [(storyFn) => <Setup controls={false}>{storyFn()}</Setup>], | ||
} | ||
|
||
const CustomCamera = (props: CameraControlsProps) => { | ||
/** | ||
* we will render our scene in a render target and use it as a map. | ||
*/ | ||
const fbo = useFBO(400, 400) | ||
const virtualCamera = useRef<CameraControls['camera']>() | ||
const [virtualScene] = useState(() => new Scene()) | ||
const cameraControlRef = useRef<CameraControls | null>(null) | ||
|
||
useFrame(({ gl }) => { | ||
if (virtualCamera.current) { | ||
gl.setRenderTarget(fbo) | ||
gl.render(virtualScene, virtualCamera.current) | ||
|
||
gl.setRenderTarget(null) | ||
} | ||
}) | ||
|
||
return ( | ||
<> | ||
<Plane | ||
args={[4, 4, 4]} | ||
onClick={() => { | ||
cameraControlRef.current?.rotate(Math.PI / 4, 0, true) | ||
}} | ||
> | ||
<meshBasicMaterial map={fbo.texture} /> | ||
</Plane> | ||
|
||
{createPortal( | ||
<> | ||
<Box> | ||
<meshBasicMaterial wireframe /> | ||
</Box> | ||
|
||
<PerspectiveCamera name="FBO Camera" ref={virtualCamera} position={[0, 0, 5]} /> | ||
<CameraControls ref={cameraControlRef} camera={virtualCamera.current} {...props} /> | ||
|
||
{/* @ts-ignore */} | ||
<color attach="background" args={['hotpink']} /> | ||
</>, | ||
virtualScene | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export const CustomCameraStory = (props: CameraControlsProps) => <CustomCamera {...props} /> | ||
|
||
CustomCameraStory.args = args | ||
CustomCameraStory.storyName = 'Custom Camera' |
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,50 @@ | ||
import * as THREE from 'three' | ||
import type { PerspectiveCamera, OrthographicCamera } from 'three' | ||
|
||
import * as React from 'react' | ||
import { forwardRef, useMemo, useEffect } from 'react' | ||
import { extend, useFrame, useThree, ReactThreeFiber, EventManager } from '@react-three/fiber' | ||
|
||
import CameraControlsImpl from 'camera-controls' | ||
|
||
export type CameraControlsProps = Omit< | ||
ReactThreeFiber.Overwrite< | ||
ReactThreeFiber.Node<CameraControlsImpl, typeof CameraControlsImpl>, | ||
{ | ||
camera?: PerspectiveCamera | OrthographicCamera | ||
domElement?: HTMLElement | ||
} | ||
>, | ||
'ref' | ||
> | ||
|
||
export const CameraControls = forwardRef<CameraControlsImpl, CameraControlsProps>((props, ref) => { | ||
useMemo(() => { | ||
CameraControlsImpl.install({ THREE }) | ||
extend({ CameraControlsImpl }) | ||
}, []) | ||
|
||
const { camera, domElement, ...restProps } = props | ||
|
||
const defaultCamera = useThree((state) => state.camera) | ||
const gl = useThree((state) => state.gl) | ||
const invalidate = useThree((state) => state.invalidate) | ||
const events = useThree((state) => state.events) as EventManager<HTMLElement> | ||
|
||
const explCamera = camera || defaultCamera | ||
const explDomElement = (domElement || events.connected || gl.domElement) as HTMLElement | ||
|
||
const cameraControls = useMemo(() => new CameraControlsImpl(explCamera, explDomElement), [explCamera, explDomElement]) | ||
|
||
useFrame((state, delta) => { | ||
if (cameraControls.enabled) cameraControls.update(delta) | ||
}, -1) | ||
|
||
useEffect(() => { | ||
return () => void cameraControls.dispose() | ||
}, [explDomElement, cameraControls, invalidate]) | ||
|
||
return <primitive ref={ref} object={cameraControls} {...restProps} /> | ||
}) | ||
|
||
export type CameraControls = CameraControlsImpl |
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