Skip to content
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

feat: ScreenSpace #1131

Merged
merged 6 commits into from Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions .storybook/stories/ScreenSpace.stories.tsx
@@ -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,
}
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -65,6 +65,7 @@ The `native` route of the library **does not** export `Html` or `Loader`. The de
<li><a href="#text3d">Text3D</a></li>
<li><a href="#positionalaudio">PositionalAudio</a></li>
<li><a href="#billboard">Billboard</a></li>
<li><a href="#screenspace">ScreenSpace</a></li>
<li><a href="#effects">Effects</a></li>
<li><a href="#gradienttexture">GradientTexture</a></li>
<li><a href="#edges">Edges</a></li>
Expand Down Expand Up @@ -927,6 +928,20 @@ Adds a `<group />` that always faces the camera.
</Billboard>
```

#### ScreenSpace

[![](https://img.shields.io/badge/-storybook-%23ff69b4)](https://drei.pmnd.rs/?path=/story/abstractions-screenspace--screen-space-story)

Adds a `<group />` that aligns objects to screen space.

```jsx
<ScreenSpace
depth={1} // Distance from camera
>
<Box>I'm in screen space</Box>
</ScreenSpace>
```
CodyJasonBennett marked this conversation as resolved.
Show resolved Hide resolved

#### GradientTexture

<p>
Expand Down
22 changes: 22 additions & 0 deletions src/core/ScreenSpace.tsx
@@ -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)
antokhio marked this conversation as resolved.
Show resolved Hide resolved

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>
)
})
1 change: 1 addition & 0 deletions src/core/index.ts
@@ -1,5 +1,6 @@
// Abstractions
export * from './Billboard'
export * from './ScreenSpace'
export * from './QuadraticBezierLine'
export * from './CubicBezierLine'
export * from './CatmullRomLine'
Expand Down