Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/Autofocus.tsx → src/effects/Autofocus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import React, {
forwardRef,
useImperativeHandle,
RefObject,
useMemo,
} from 'react'
import { useThree, useFrame, createPortal } from '@react-three/fiber'
import { CopyPass, DepthPickingPass, DepthOfFieldEffect } from 'postprocessing'
import { DepthOfField, EffectComposerContext } from './index'
import { DepthOfField, EffectComposerContext } from '..'
import { easing } from 'maath'

export type AutofocusProps = typeof DepthOfField & {
Expand Down Expand Up @@ -42,8 +43,8 @@ export const Autofocus = forwardRef<AutofocusApi, AutofocusProps>(
const { composer, camera } = useContext(EffectComposerContext)

// see: https://codesandbox.io/s/depthpickingpass-x130hg
const [depthPickingPass] = useState(new DepthPickingPass())
const [copyPass] = useState(new CopyPass())
const [depthPickingPass] = useState(() => new DepthPickingPass())
const [copyPass] = useState(() => new CopyPass())
Comment on lines +46 to +47
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy setter

useEffect(() => {
composer.addPass(depthPickingPass)
composer.addPass(copyPass)
Expand All @@ -53,9 +54,16 @@ export const Autofocus = forwardRef<AutofocusApi, AutofocusProps>(
}
}, [composer, depthPickingPass, copyPass])

const [hitpoint] = useState(new THREE.Vector3(0, 0, 0))
useEffect(() => {
return () => {
depthPickingPass.dispose()
copyPass.dispose()
Comment on lines +59 to +60
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not missing .dispose() this time

}
}, [depthPickingPass, copyPass])

const [hitpoint] = useState(() => new THREE.Vector3(0, 0, 0))

const [ndc] = useState(new THREE.Vector3(0, 0, 0))
const [ndc] = useState(() => new THREE.Vector3(0, 0, 0))
const getHit = useCallback(
async (x: number, y: number) => {
ndc.x = x
Expand Down Expand Up @@ -104,15 +112,15 @@ export const Autofocus = forwardRef<AutofocusApi, AutofocusProps>(
})

// Ref API
useImperativeHandle(
fref,
const api = useMemo<AutofocusApi>(
() => ({
dofRef,
hitpoint,
update,
}),
[hitpoint, update]
)
useImperativeHandle(fref, () => api, [api])

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './effects/Autofocus'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved Autofocus to the effects/ folder

export * from './effects/Bloom'
export * from './effects/BrightnessContrast'
export * from './effects/ChromaticAberration'
Expand Down Expand Up @@ -30,6 +31,5 @@ export * from './effects/TiltShift2'
export * from './effects/SSR'

export * from './Selection'
export * from './Autofocus'
export * from './EffectComposer'
export * from './util'