-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathEffectComposer.tsx
More file actions
201 lines (178 loc) · 5.94 KB
/
EffectComposer.tsx
File metadata and controls
201 lines (178 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import type { Camera, Scene, TextureDataType } from 'three'
import { HalfFloatType, NoToneMapping } from 'three'
import React, {
forwardRef,
useMemo,
useEffect,
useLayoutEffect,
createContext,
useRef,
useImperativeHandle,
} from 'react'
import { useThree, useFrame } from '@react-three/fiber'
import {
EffectComposer as EffectComposerImpl,
RenderPass,
EffectPass,
NormalPass,
// @ts-ignore
DepthDownsamplingPass,
Effect,
Pass,
EffectAttribute,
} from 'postprocessing'
import { isWebGL2Available } from 'three-stdlib'
export const EffectComposerContext = createContext<{
composer: EffectComposerImpl
normalPass: NormalPass | null
downSamplingPass: DepthDownsamplingPass | null
camera: Camera
scene: Scene
resolutionScale?: number
}>(null!)
export type EffectComposerProps = {
enabled?: boolean
children: JSX.Element | JSX.Element[]
depthBuffer?: boolean
/** Only used for SSGI currently, leave it disabled for everything else unless it's needed */
enableNormalPass?: boolean
stencilBuffer?: boolean
autoClear?: boolean
resolutionScale?: number
multisampling?: number
frameBufferType?: TextureDataType
renderPriority?: number
camera?: Camera
scene?: Scene
}
const isConvolution = (effect: Effect): boolean =>
(effect.getAttributes() & EffectAttribute.CONVOLUTION) === EffectAttribute.CONVOLUTION
export const EffectComposer = React.memo(
forwardRef<EffectComposerImpl, EffectComposerProps>(
(
{
children,
camera: _camera,
scene: _scene,
resolutionScale,
enabled = true,
renderPriority = 1,
autoClear = true,
depthBuffer,
enableNormalPass,
stencilBuffer,
multisampling = 8,
frameBufferType = HalfFloatType,
},
ref
) => {
const { gl, scene: defaultScene, camera: defaultCamera, size } = useThree()
const scene = _scene || defaultScene
const camera = _camera || defaultCamera
const [composer, normalPass, downSamplingPass] = useMemo(() => {
const webGL2Available = isWebGL2Available()
// Initialize composer
const effectComposer = new EffectComposerImpl(gl, {
depthBuffer,
stencilBuffer,
multisampling: multisampling > 0 && webGL2Available ? multisampling : 0,
frameBufferType,
})
// Add render pass
effectComposer.addPass(new RenderPass(scene, camera))
// Create normal pass
let downSamplingPass = null
let normalPass = null
if (enableNormalPass) {
normalPass = new NormalPass(scene, camera)
normalPass.enabled = false
effectComposer.addPass(normalPass)
if (resolutionScale !== undefined && webGL2Available) {
downSamplingPass = new DepthDownsamplingPass({ normalBuffer: normalPass.texture, resolutionScale })
downSamplingPass.enabled = false
effectComposer.addPass(downSamplingPass)
}
}
return [effectComposer, normalPass, downSamplingPass]
}, [
camera,
gl,
depthBuffer,
stencilBuffer,
multisampling,
frameBufferType,
scene,
enableNormalPass,
resolutionScale,
])
useEffect(() => composer?.setSize(size.width, size.height), [composer, size])
useFrame(
(_, delta) => {
if (enabled) {
const currentAutoClear = gl.autoClear
gl.autoClear = autoClear
if (stencilBuffer && !autoClear) gl.clearStencil()
composer.render(delta)
gl.autoClear = currentAutoClear
}
},
enabled ? renderPriority : 0
)
const group = useRef(null)
useLayoutEffect(() => {
const passes: Pass[] = []
// TODO: rewrite all of this with R3F v9
const groupInstance = (group.current as any)?.__r3f as { objects: unknown[] }
if (groupInstance && composer) {
const children = groupInstance.objects
for (let i = 0; i < children.length; i++) {
const child = children[i]
if (child instanceof Effect) {
const effects: Effect[] = [child]
if (!isConvolution(child)) {
let next: unknown = null
while ((next = children[i + 1]) instanceof Effect) {
if (isConvolution(next)) break
effects.push(next)
i++
}
}
const pass = new EffectPass(camera, ...effects)
passes.push(pass)
} else if (child instanceof Pass) {
passes.push(child)
}
}
for (const pass of passes) composer?.addPass(pass)
if (normalPass) normalPass.enabled = true
if (downSamplingPass) downSamplingPass.enabled = true
}
return () => {
for (const pass of passes) composer?.removePass(pass)
if (normalPass) normalPass.enabled = false
if (downSamplingPass) downSamplingPass.enabled = false
}
}, [composer, children, camera, normalPass, downSamplingPass])
// Disable tone mapping because threejs disallows tonemapping on render targets
useEffect(() => {
const currentTonemapping = gl.toneMapping
gl.toneMapping = NoToneMapping
return () => {
gl.toneMapping = currentTonemapping
}
}, [gl])
// Memoize state, otherwise it would trigger all consumers on every render
const state = useMemo(
() => ({ composer, normalPass, downSamplingPass, resolutionScale, camera, scene }),
[composer, normalPass, downSamplingPass, resolutionScale, camera, scene]
)
// Expose the composer
useImperativeHandle(ref, () => composer, [composer])
return (
<EffectComposerContext.Provider value={state}>
<group ref={group}>{children}</group>
</EffectComposerContext.Provider>
)
}
)
)