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
3 changed files
with
89 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
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,49 @@ | ||
import * as THREE from 'three' | ||
import * as React from 'react' | ||
import { useFrame, useThree, createPortal } from '@react-three/fiber' | ||
|
||
type RenderHudProps = { | ||
defaultScene: THREE.Scene | ||
defaultCamera: THREE.Camera | ||
renderPriority?: number | ||
} | ||
|
||
type HudProps = { | ||
/** Any React node */ | ||
children: React.ReactNode | ||
/** Render priority, default: 1 */ | ||
renderPriority?: number | ||
} | ||
|
||
function RenderHud({ defaultScene, defaultCamera, renderPriority = 1 }: RenderHudProps) { | ||
const { gl, scene, camera } = useThree() | ||
useFrame(() => { | ||
if (renderPriority === 1) { | ||
// Clear scene and render the default scene | ||
gl.autoClear = true | ||
gl.render(defaultScene, defaultCamera) | ||
} | ||
// Disable cleaning and render the portal with its own camera | ||
gl.autoClear = false | ||
gl.clearDepth() | ||
gl.render(scene, camera) | ||
}, renderPriority) | ||
return <> </> | ||
} | ||
|
||
export function Hud({ children, renderPriority = 1 }: HudProps) { | ||
const { scene: defaultScene, camera: defaultCamera } = useThree() | ||
const [hudScene] = React.useState(() => new THREE.Scene()) | ||
return ( | ||
<> | ||
{createPortal( | ||
<> | ||
{children} | ||
<RenderHud defaultScene={defaultScene} defaultCamera={defaultCamera} renderPriority={renderPriority} /> | ||
</>, | ||
hudScene, | ||
{ events: { priority: renderPriority + 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
b941939
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.vercel.app
drei-git-master-pmndrs.vercel.app
drei-pmndrs.vercel.app
drei.pmnd.rs
drei.react-spring.io