-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathSSAO.tsx
38 lines (36 loc) · 1.55 KB
/
SSAO.tsx
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
import { Ref, forwardRef, useContext, useMemo } from 'react'
import { SSAOEffect, BlendFunction } from 'postprocessing'
import { EffectComposerContext } from '../EffectComposer'
// first two args are camera and texture
type SSAOProps = ConstructorParameters<typeof SSAOEffect>[2]
export const SSAO = forwardRef<SSAOEffect, SSAOProps>(function SSAO(props: SSAOProps, ref: Ref<SSAOEffect>) {
const { camera, normalPass, downSamplingPass, resolutionScale } = useContext(EffectComposerContext)
const effect = useMemo<SSAOEffect | {}>(() => {
if (normalPass === null && downSamplingPass === null) {
console.error('Please enable the NormalPass in the EffectComposer in order to use SSAO.')
return {}
}
return new SSAOEffect(camera, normalPass && !downSamplingPass ? (normalPass as any).texture : null, {
blendFunction: BlendFunction.MULTIPLY,
samples: 30,
rings: 4,
distanceThreshold: 1.0,
distanceFalloff: 0.0,
rangeThreshold: 0.5,
rangeFalloff: 0.1,
luminanceInfluence: 0.9,
radius: 20,
bias: 0.5,
intensity: 1.0,
color: undefined,
// @ts-ignore
normalDepthBuffer: downSamplingPass ? downSamplingPass.texture : null,
resolutionScale: resolutionScale ?? 1,
depthAwareUpsampling: true,
...props,
})
// NOTE: `props` is an unstable reference, so we can't memoize it
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [camera, downSamplingPass, normalPass, resolutionScale])
return <primitive ref={ref} object={effect} dispose={null} />
})