1
1
import * as React from 'react'
2
2
import * as THREE from 'three'
3
3
import { useThree } from '@react-three/fiber'
4
+ import { forwardRef } from 'react'
5
+
6
+ type WebGLRenderTargetCtorParams = ConstructorParameters < typeof THREE . WebGLRenderTarget >
7
+ type WebGLRenderTargetOptions = WebGLRenderTargetCtorParams [ 2 ]
4
8
5
9
type FBOSettings = {
6
- /** Defines the count of MSAA samples. Can only be used with WebGL 2. Default: 0 */
7
- samples ?: number
8
- /** If set, the scene depth will be rendered into buffer.depthTexture. Default: false */
10
+ /** @deprecated use `depthBuffer` instead. If set, the scene depth will be rendered into buffer.depthTexture. Default: false */
9
11
depth ?: boolean
10
-
11
- // WebGLRenderTargetOptions => RenderTargetOptions
12
- wrapS ?: THREE . Wrapping | undefined
13
- wrapT ?: THREE . Wrapping | undefined
14
- magFilter ?: THREE . MagnificationTextureFilter | undefined
15
- minFilter ?: THREE . MinificationTextureFilter | undefined
16
- format ?: number | undefined // RGBAFormat;
17
- type ?: THREE . TextureDataType | undefined // UnsignedByteType;
18
- anisotropy ?: number | undefined // 1;
19
- depthBuffer ?: boolean | undefined // true;
20
- stencilBuffer ?: boolean | undefined // false;
21
- generateMipmaps ?: boolean | undefined // true;
22
- depthTexture ?: THREE . DepthTexture | undefined
23
- colorSpace ?: THREE . ColorSpace | undefined
24
- }
12
+ } & WebGLRenderTargetOptions
25
13
26
14
// 👇 uncomment when TS version supports function overloads
27
15
// export function useFBO(settings?: FBOSettings)
@@ -40,6 +28,8 @@ export function useFBO(
40
28
const _settings = ( typeof width === 'number' ? settings : ( width as FBOSettings ) ) || { }
41
29
const { samples = 0 , depth, ...targetSettings } = _settings
42
30
31
+ const depthBuffer = depth ?? _settings . depthBuffer // backwards compatibility for deprecated `depth` prop
32
+
43
33
const target = React . useMemo ( ( ) => {
44
34
const target = new THREE . WebGLRenderTarget ( _width , _height , {
45
35
minFilter : THREE . LinearFilter ,
@@ -48,7 +38,7 @@ export function useFBO(
48
38
...targetSettings ,
49
39
} )
50
40
51
- if ( depth ) {
41
+ if ( depthBuffer ) {
52
42
target . depthTexture = new THREE . DepthTexture ( _width , _height , THREE . FloatType )
53
43
}
54
44
@@ -68,17 +58,21 @@ export function useFBO(
68
58
return target
69
59
}
70
60
71
- export const Fbo = ( {
72
- children,
73
- width,
74
- height,
75
- ...settings
76
- } : {
77
- children ?: ( target : ReturnType < typeof useFBO > ) => React . ReactNode
78
- width : Parameters < typeof useFBO > [ 0 ]
79
- height : Parameters < typeof useFBO > [ 1 ]
80
- } & FBOSettings ) => {
61
+ //
62
+ // Fbo component
63
+ //
64
+
65
+ type UseFBOParams = Parameters < typeof useFBO >
66
+ type Fbo = ReturnType < typeof useFBO >
67
+
68
+ export type FboProps = {
69
+ children ?: ( target : Fbo ) => React . ReactNode
70
+ width : UseFBOParams [ 0 ]
71
+ height : UseFBOParams [ 1 ]
72
+ } & FBOSettings
73
+
74
+ export const Fbo = /* @__PURE__ */ forwardRef < Fbo , FboProps > ( ( { children, width, height, ...settings } , fref ) => {
81
75
const target = useFBO ( width , height , settings )
82
76
83
77
return < > { children ?.( target ) } </ >
84
- }
78
+ } )
0 commit comments