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
4 changed files
with
77 additions
and
0 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,39 @@ | ||
import * as React from 'react' | ||
import { Box, OrbitControls, Html, ScreenSpace } from '../../src' | ||
import { Vector3 } from 'three' | ||
|
||
import { Setup } from '../Setup' | ||
|
||
export default { | ||
title: 'Abstractions/ScreenSpace', | ||
component: ScreenSpace, | ||
decorators: [ | ||
(storyFn) => ( | ||
<Setup controls={false} cameraPosition={new Vector3(0, 0, 10)}> | ||
{storyFn()} | ||
</Setup> | ||
), | ||
], | ||
} | ||
|
||
export const ScreenSpaceStory = ({ depth }) => ( | ||
<> | ||
<Box args={[1, 1, 1]}> | ||
<meshPhysicalMaterial /> | ||
</Box> | ||
<ScreenSpace depth={depth}> | ||
<Box args={[0.1, 0.1, 0.1]} position={[0.5, 0.1, 0]}> | ||
<meshPhysicalMaterial color={'blue'} /> | ||
<Html center sprite> | ||
<div style={{ color: 'hotpink' }}>Hi i'm in screen space</div> | ||
</Html> | ||
</Box> | ||
</ScreenSpace> | ||
|
||
<OrbitControls enablePan={true} zoomSpeed={0.5} /> | ||
</> | ||
) | ||
|
||
ScreenSpaceStory.args = { | ||
depth: 1, | ||
} |
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,22 @@ | ||
import * as React from 'react' | ||
import { Group } from 'three' | ||
import mergeRefs from 'react-merge-refs' | ||
import { useFrame } from '@react-three/fiber' | ||
|
||
export type ScreenSpaceProps = { | ||
depth?: number | ||
} & JSX.IntrinsicElements['group'] | ||
|
||
export const ScreenSpace = React.forwardRef<Group, ScreenSpaceProps>(({ children, depth = -1, ...rest }, ref) => { | ||
const localRef = React.useRef<Group>(null!) | ||
|
||
useFrame(({ camera }) => { | ||
localRef.current.quaternion.copy(camera.quaternion) | ||
localRef.current.position.copy(camera.position) | ||
}) | ||
return ( | ||
<group ref={mergeRefs([ref, localRef])} {...rest}> | ||
<group position-z={-depth}>{children}</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
6b07f8e
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:
drei – ./
drei-pmndrs.vercel.app
drei.vercel.app
drei-git-master-pmndrs.vercel.app
drei.pmnd.rs
drei.react-spring.io