Custom effects
If you plan to use custom effects, make sure to expose the effect itself as a primitive!
import React, { forwardRef, useMemo } from 'react'
import { PixelationEffect } from 'postprocessing'
export const Pixelation = forwardRef(({ granularity = 5 }, ref) => {
const effect = useMemo(() => new PixelationEffect(granularity), [granularity])
return <primitive ref={ref} object={effect} dispose={null} />
})
For effects that aren't present in postprocessing
you should extend the Effect
class:
import React, { forwardRef, useMemo } from 'react'
import { Uniform } from 'three'
import { Effect } from 'postprocessing'
const fragmentShader = `some_shader_code`
let _uParam
// Effect implementation
class MyCustomEffectImpl extends Effect {
constructor({ param = 0.1 } = {}) {
super('MyCustomEffect', fragmentShader, {
uniforms: new Map([['param', new Uniform(param)]]),
})
_uParam = param
}
update(renderer, inputBuffer, deltaTime) {
this.uniforms.get('param').value = _uParam
}
}
// Effect component
export const MyCustomEffect = forwardRef(({ param }, ref) => {
const effect = useMemo(() => new MyCustomEffectImpl(param), [param])
return <primitive ref={ref} object={effect} dispose={null} />
})