Last active
May 21, 2023 04:55
-
-
Save ektogamat/18b84f5ad652091fe5a89555a5d30000 to your computer and use it in GitHub Desktop.
Utility file for Ultimate LensFlare
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
// File copy pasted from https://github.com/pmndrs/react-postprocessing/blob/master/src/util.tsx | |
import React, { forwardRef, useMemo, useLayoutEffect, MutableRefObject } from 'react' | |
import { Vector2, Object3D } from 'three' | |
import { ReactThreeFiber, useThree } from '@react-three/fiber' | |
import { Effect, BlendFunction } from 'postprocessing' | |
type ObjectRef = MutableRefObject<Object3D> | |
type DefaultProps = Partial<{ blendFunction: BlendFunction; opacity: number }> | |
const isRef = (ref: any): ref is ObjectRef => !!ref.current | |
export const resolveRef = (ref: Object3D | ObjectRef) => (isRef(ref) ? ref.current : ref) | |
export const wrapEffect = <T extends new (...args: any[]) => Effect>( | |
effectImpl: T, | |
defaultBlendMode: BlendFunction = BlendFunction.NORMAL | |
) => | |
forwardRef<T, ConstructorParameters<typeof effectImpl>[0] & DefaultProps>(function Wrap( | |
{ blendFunction, opacity, ...props }: React.PropsWithChildren<DefaultProps & ConstructorParameters<T>[0]>, | |
ref | |
) { | |
const invalidate = useThree((state) => state.invalidate) | |
const effect: Effect = useMemo(() => new effectImpl(props), [props]) | |
useLayoutEffect(() => { | |
effect.blendMode.blendFunction = !blendFunction && blendFunction !== 0 ? defaultBlendMode : blendFunction | |
if (opacity !== undefined) effect.blendMode.opacity.value = opacity | |
invalidate() | |
}, [blendFunction, effect.blendMode, opacity]) | |
return <primitive ref={ref} object={effect} dispose={null} /> | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment