-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
1 parent
07cab9a
commit 583e97c
Showing
7 changed files
with
199 additions
and
45 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
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
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,142 @@ | ||
import { effect } from '@preact/signals-core' | ||
import { | ||
ComponentPropsWithoutRef, | ||
ReactNode, | ||
RefObject, | ||
forwardRef, | ||
useEffect, | ||
useImperativeHandle, | ||
useMemo, | ||
useRef, | ||
} from 'react' | ||
import { HalfFloatType, LinearFilter, Scene, WebGLRenderTarget } from 'three' | ||
import { Image } from './image.js' | ||
import { InjectState, RootState, createPortal, useFrame, useStore } from '@react-three/fiber' | ||
import type { DomEvent } from '@react-three/fiber/dist/declarations/src/core/events.js' | ||
import type { ComponentInternals } from './utils.js' | ||
|
||
export const Portal = forwardRef< | ||
ComponentInternals, | ||
{ | ||
frames?: number | ||
renderPriority?: number | ||
eventPriority?: number | ||
resolution?: number | ||
children?: ReactNode | ||
} & Omit<ComponentPropsWithoutRef<typeof Image>, 'src' | 'fit'> | ||
>(({ children, resolution = 1, frames = Infinity, renderPriority = 0, eventPriority = 0, ...props }, ref) => { | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const fbo = useMemo( | ||
() => | ||
new WebGLRenderTarget(1, 1, { | ||
minFilter: LinearFilter, | ||
magFilter: LinearFilter, | ||
type: HalfFloatType, | ||
}), | ||
[], | ||
) | ||
const imageRef = useRef<ComponentInternals>(null) | ||
const injectState = useMemo<InjectState>( | ||
() => ({ | ||
events: { compute: uvCompute.bind(null, imageRef), priority: eventPriority }, | ||
size: { width: 1, height: 1, left: 0, top: 0 }, | ||
}), | ||
[eventPriority], | ||
) | ||
useEffect(() => { | ||
if (imageRef.current == null) { | ||
return | ||
} | ||
const { size } = imageRef.current | ||
const unsubscribeSetSize = effect(() => { | ||
const [width, height] = size.value | ||
fbo.setSize(width, height) | ||
injectState.size!.width = width | ||
injectState.size!.height = height | ||
}) | ||
return () => { | ||
unsubscribeSetSize() | ||
fbo.dispose() | ||
} | ||
}, [fbo, injectState]) | ||
useImperativeHandle(ref, () => imageRef.current!, []) | ||
const vScene = useMemo(() => new Scene(), []) | ||
return ( | ||
<> | ||
{createPortal( | ||
<ChildrenToFBO imageRef={imageRef} renderPriority={renderPriority} frames={frames} fbo={fbo}> | ||
{children} | ||
{/* Without an element that receives pointer events state.pointer will always be 0/0 */} | ||
<group onPointerOver={() => null} /> | ||
</ChildrenToFBO>, | ||
vScene, | ||
injectState, | ||
)} | ||
<Image {...props} src={fbo.texture} fit="fill" keepAspectRatio={false} ref={imageRef} /> | ||
</> | ||
) | ||
}) | ||
|
||
function uvCompute( | ||
{ current }: RefObject<ComponentInternals>, | ||
event: DomEvent, | ||
state: RootState, | ||
previous?: RootState, | ||
) { | ||
if (current == null || previous == null) { | ||
return false | ||
} | ||
// First we call the previous state-onion-layers compute, this is what makes it possible to nest portals | ||
if (!previous.raycaster.camera) previous.events.compute?.(event, previous, previous.previousRoot?.getState()) | ||
// We run a quick check against the parent, if it isn't hit there's no need to raycast at all | ||
const [intersection] = previous.raycaster.intersectObject(current.interactionPanel) | ||
if (!intersection) return false | ||
// We take that hits uv coords, set up this layers raycaster, et voilà, we have raycasting on arbitrary surfaces | ||
const uv = intersection.uv | ||
if (!uv) return false | ||
state.raycaster.setFromCamera(state.pointer.set(uv.x * 2 - 1, uv.y * 2 - 1), state.camera) | ||
} | ||
|
||
function ChildrenToFBO({ | ||
frames, | ||
renderPriority, | ||
children, | ||
fbo, | ||
imageRef, | ||
}: { | ||
frames: number | ||
renderPriority: number | ||
children: ReactNode | ||
fbo: WebGLRenderTarget | ||
imageRef: RefObject<ComponentInternals> | ||
}) { | ||
const store = useStore() | ||
useEffect(() => { | ||
if (imageRef.current == null) { | ||
return | ||
} | ||
const { size } = imageRef.current | ||
return effect(() => { | ||
const [width, height] = size.value | ||
store.setState({ size: { width, height, top: 0, left: 0 } }) | ||
}) | ||
}) | ||
let count = 0 | ||
let oldAutoClear | ||
let oldXrEnabled | ||
useFrame((state) => { | ||
if (frames === Infinity || count < frames) { | ||
oldAutoClear = state.gl.autoClear | ||
oldXrEnabled = state.gl.xr.enabled | ||
state.gl.autoClear = true | ||
state.gl.xr.enabled = false | ||
state.gl.setRenderTarget(fbo) | ||
state.gl.render(state.scene, state.camera) | ||
state.gl.setRenderTarget(null) | ||
state.gl.autoClear = oldAutoClear | ||
state.gl.xr.enabled = oldXrEnabled | ||
count++ | ||
} | ||
}, renderPriority) | ||
return <>{children}</> | ||
} |
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,12 @@ | ||
import { ComponentPropsWithoutRef } from 'react' | ||
import { Image } from './image.js' | ||
import { useLoader } from '@react-three/fiber' | ||
import { TextureLoader } from 'three' | ||
|
||
export function SuspendingImage({ | ||
src, | ||
...props | ||
}: Omit<ComponentPropsWithoutRef<typeof Image>, 'src'> & { src: string }) { | ||
const texture = useLoader(TextureLoader, src) | ||
return <Image src={texture} {...props} /> | ||
} |