-
Notifications
You must be signed in to change notification settings - Fork 790
feat(Controls): adding CameraControls #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.