-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds CubeCamera component (#298)
Co-authored-by: Gianmarco Simone <[email protected]>
- Loading branch information
Showing
4 changed files
with
145 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,57 @@ | ||
import * as React from 'react' | ||
import * as THREE from 'three' | ||
import { useFrame } from 'react-three-fiber' | ||
|
||
import { Setup } from '../Setup' | ||
|
||
import { Box, CubeCamera } from '../../src' | ||
|
||
export default { | ||
title: 'Camera/CubeCamera', | ||
component: CubeCamera, | ||
decorators: [(storyFn) => <Setup cameraPosition={new THREE.Vector3(0, 10, 40)}>{storyFn()}</Setup>], | ||
} | ||
|
||
declare global { | ||
namespace JSX { | ||
interface IntrinsicElements { | ||
axisHelper: object | ||
} | ||
} | ||
} | ||
|
||
function Sphere({ offset = 0, ...props }) { | ||
const ref = React.useRef<THREE.Group>() | ||
useFrame(({ clock }) => { | ||
ref.current!.position.y = Math.sin(offset + clock.elapsedTime) * 5 | ||
}) | ||
|
||
return ( | ||
<CubeCamera {...props}> | ||
{(texture) => ( | ||
<mesh ref={ref}> | ||
<sphereBufferGeometry args={[5, 64, 64]} /> | ||
<meshStandardMaterial roughness={0} metalness={1} envMap={texture} /> | ||
</mesh> | ||
)} | ||
</CubeCamera> | ||
) | ||
} | ||
|
||
function Scene() { | ||
return ( | ||
<> | ||
<fog attach="fog" args={['#f0f0f0', 100, 200]} /> | ||
|
||
<Sphere position={[-10, 10, 0]} /> | ||
<Sphere position={[10, 9, 0]} offset={2000} /> | ||
|
||
<Box material-color="hotpink" args={[5, 5, 5]} position-y={2.5} /> | ||
|
||
<gridHelper args={[100, 10]} /> | ||
</> | ||
) | ||
} | ||
|
||
export const DefaultStory = () => <Scene /> | ||
DefaultStory.storyName = 'Default' |
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,63 @@ | ||
import { | ||
Fog, | ||
FogExp2, | ||
Group, | ||
Texture, | ||
CubeCamera as CubeCameraImpl, | ||
WebGLCubeRenderTarget, | ||
LinearFilter, | ||
RGBFormat, | ||
} from 'three' | ||
import * as React from 'react' | ||
import { useFrame, useThree } from 'react-three-fiber' | ||
|
||
type Props = JSX.IntrinsicElements['group'] & { | ||
fog?: Fog | FogExp2 | ||
frames?: number | ||
resolution?: number | ||
near?: number | ||
far?: number | ||
children: (tex: Texture) => React.ReactNode | ||
} | ||
|
||
export function CubeCamera({ | ||
children, | ||
fog, | ||
frames = Infinity, | ||
resolution = 256, | ||
near = 1, | ||
far = 1000, | ||
...props | ||
}: Props) { | ||
const ref = React.useRef<Group>() | ||
const [camera, setCamera] = React.useState<CubeCameraImpl>() | ||
const { scene, gl } = useThree() | ||
const fbo = React.useMemo( | ||
() => | ||
new WebGLCubeRenderTarget(resolution, { | ||
minFilter: LinearFilter, | ||
magFilter: LinearFilter, | ||
format: RGBFormat, | ||
encoding: gl.outputEncoding, | ||
}), | ||
[resolution] | ||
) | ||
let count = 0 | ||
useFrame(() => { | ||
if (camera && ref.current && (frames === Infinity || count < frames)) { | ||
ref.current.traverse((obj) => (obj.visible = false)) | ||
const originalFog = scene.fog | ||
scene.fog = fog ?? originalFog | ||
camera.update(gl, scene) | ||
scene.fog = originalFog | ||
ref.current.traverse((obj) => (obj.visible = true)) | ||
count++ | ||
} | ||
}) | ||
return ( | ||
<group {...props}> | ||
<cubeCamera ref={setCamera} args={[near, far, fbo]} /> | ||
<group ref={ref}>{children(fbo.texture)}</group> | ||
</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
362812b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: