@react-three/postprocessing 3.0.1 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +81 -81
- package/dist/index.d.ts +1 -0
- package/dist/index.js +555 -0
- package/dist/index.js.map +1 -0
- package/package.json +66 -68
- package/src/EffectComposer.test.tsx +126 -126
- package/src/EffectComposer.tsx +200 -200
- package/src/Selection.tsx +46 -46
- package/src/effects/ASCII.tsx +135 -135
- package/src/effects/Autofocus.tsx +153 -153
- package/src/effects/Bloom.tsx +6 -6
- package/src/effects/BrightnessContrast.tsx +4 -4
- package/src/effects/ChromaticAberration.tsx +5 -5
- package/src/effects/ColorAverage.tsx +15 -15
- package/src/effects/ColorDepth.tsx +4 -4
- package/src/effects/Depth.tsx +4 -4
- package/src/effects/DepthOfField.tsx +89 -89
- package/src/effects/DotScreen.tsx +4 -4
- package/src/effects/FXAA.tsx +4 -4
- package/src/effects/Glitch.tsx +40 -40
- package/src/effects/GodRays.tsx +16 -16
- package/src/effects/Grid.tsx +21 -21
- package/src/effects/HueSaturation.tsx +4 -4
- package/src/effects/LUT.tsx +26 -26
- package/src/effects/LensFlare.tsx +611 -611
- package/src/effects/N8AO.tsx +81 -81
- package/src/effects/Noise.tsx +4 -4
- package/src/effects/Outline.tsx +117 -117
- package/src/effects/Pixelation.tsx +15 -15
- package/src/effects/Ramp.tsx +150 -150
- package/src/effects/SMAA.tsx +4 -4
- package/src/effects/SSAO.tsx +41 -41
- package/src/effects/ScanlineEffect.tsx +7 -7
- package/src/effects/SelectiveBloom.tsx +126 -126
- package/src/effects/Sepia.tsx +4 -4
- package/src/effects/ShockWave.tsx +4 -4
- package/src/effects/Texture.tsx +23 -23
- package/src/effects/TiltShift.tsx +4 -4
- package/src/effects/TiltShift2.tsx +90 -90
- package/src/effects/ToneMapping.tsx +6 -6
- package/src/effects/Vignette.tsx +4 -4
- package/src/effects/Water.tsx +35 -35
- package/src/index.ts +40 -40
- package/src/util.tsx +55 -55
package/src/effects/ASCII.tsx
CHANGED
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
// From: https://github.com/emilwidlund/ASCII
|
|
2
|
-
// https://twitter.com/emilwidlund/status/1652386482420609024
|
|
3
|
-
|
|
4
|
-
import { forwardRef, useMemo } from 'react'
|
|
5
|
-
import { CanvasTexture, Color, NearestFilter, RepeatWrapping, Texture, Uniform } from 'three'
|
|
6
|
-
import { Effect } from 'postprocessing'
|
|
7
|
-
|
|
8
|
-
const fragment = `
|
|
9
|
-
uniform sampler2D uCharacters;
|
|
10
|
-
uniform float uCharactersCount;
|
|
11
|
-
uniform float uCellSize;
|
|
12
|
-
uniform bool uInvert;
|
|
13
|
-
uniform vec3 uColor;
|
|
14
|
-
|
|
15
|
-
const vec2 SIZE = vec2(16.);
|
|
16
|
-
|
|
17
|
-
vec3 greyscale(vec3 color, float strength) {
|
|
18
|
-
float g = dot(color, vec3(0.299, 0.587, 0.114));
|
|
19
|
-
return mix(color, vec3(g), strength);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
vec3 greyscale(vec3 color) {
|
|
23
|
-
return greyscale(color, 1.0);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
|
|
27
|
-
vec2 cell = resolution / uCellSize;
|
|
28
|
-
vec2 grid = 1.0 / cell;
|
|
29
|
-
vec2 pixelizedUV = grid * (0.5 + floor(uv / grid));
|
|
30
|
-
vec4 pixelized = texture2D(inputBuffer, pixelizedUV);
|
|
31
|
-
float greyscaled = greyscale(pixelized.rgb).r;
|
|
32
|
-
|
|
33
|
-
if (uInvert) {
|
|
34
|
-
greyscaled = 1.0 - greyscaled;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
float characterIndex = floor((uCharactersCount - 1.0) * greyscaled);
|
|
38
|
-
vec2 characterPosition = vec2(mod(characterIndex, SIZE.x), floor(characterIndex / SIZE.y));
|
|
39
|
-
vec2 offset = vec2(characterPosition.x, -characterPosition.y) / SIZE;
|
|
40
|
-
vec2 charUV = mod(uv * (cell / SIZE), 1.0 / SIZE) - vec2(0., 1.0 / SIZE) + offset;
|
|
41
|
-
vec4 asciiCharacter = texture2D(uCharacters, charUV);
|
|
42
|
-
|
|
43
|
-
asciiCharacter.rgb = uColor * asciiCharacter.r;
|
|
44
|
-
asciiCharacter.a = pixelized.a;
|
|
45
|
-
outputColor = asciiCharacter;
|
|
46
|
-
}
|
|
47
|
-
`
|
|
48
|
-
|
|
49
|
-
interface IASCIIEffectProps {
|
|
50
|
-
font?: string
|
|
51
|
-
characters?: string
|
|
52
|
-
fontSize?: number
|
|
53
|
-
cellSize?: number
|
|
54
|
-
color?: string
|
|
55
|
-
invert?: boolean
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
class ASCIIEffect extends Effect {
|
|
59
|
-
constructor({
|
|
60
|
-
font = 'arial',
|
|
61
|
-
characters = ` .:,'-^=*+?!|0#X%WM@`,
|
|
62
|
-
fontSize = 54,
|
|
63
|
-
cellSize = 16,
|
|
64
|
-
color = '#ffffff',
|
|
65
|
-
invert = false,
|
|
66
|
-
}: IASCIIEffectProps = {}) {
|
|
67
|
-
const uniforms = new Map<string, Uniform>([
|
|
68
|
-
['uCharacters', new Uniform(new Texture())],
|
|
69
|
-
['uCellSize', new Uniform(cellSize)],
|
|
70
|
-
['uCharactersCount', new Uniform(characters.length)],
|
|
71
|
-
['uColor', new Uniform(new Color(color))],
|
|
72
|
-
['uInvert', new Uniform(invert)],
|
|
73
|
-
])
|
|
74
|
-
|
|
75
|
-
super('ASCIIEffect', fragment, { uniforms })
|
|
76
|
-
|
|
77
|
-
const charactersTextureUniform = this.uniforms.get('uCharacters')
|
|
78
|
-
|
|
79
|
-
if (charactersTextureUniform) {
|
|
80
|
-
charactersTextureUniform.value = this.createCharactersTexture(characters, font, fontSize)
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** Draws the characters on a Canvas and returns a texture */
|
|
85
|
-
public createCharactersTexture(characters: string, font: string, fontSize: number): Texture {
|
|
86
|
-
const canvas = document.createElement('canvas')
|
|
87
|
-
const SIZE = 1024
|
|
88
|
-
const MAX_PER_ROW = 16
|
|
89
|
-
const CELL = SIZE / MAX_PER_ROW
|
|
90
|
-
|
|
91
|
-
canvas.width = canvas.height = SIZE
|
|
92
|
-
const texture = new CanvasTexture(canvas, undefined, RepeatWrapping, RepeatWrapping, NearestFilter, NearestFilter)
|
|
93
|
-
const context = canvas.getContext('2d')
|
|
94
|
-
|
|
95
|
-
if (!context) {
|
|
96
|
-
throw new Error('Context not available')
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
context.clearRect(0, 0, SIZE, SIZE)
|
|
100
|
-
context.font = `${fontSize}px ${font}`
|
|
101
|
-
context.textAlign = 'center'
|
|
102
|
-
context.textBaseline = 'middle'
|
|
103
|
-
context.fillStyle = '#fff'
|
|
104
|
-
|
|
105
|
-
for (let i = 0; i < characters.length; i++) {
|
|
106
|
-
const char = characters[i]
|
|
107
|
-
const x = i % MAX_PER_ROW
|
|
108
|
-
const y = Math.floor(i / MAX_PER_ROW)
|
|
109
|
-
context.fillText(char, x * CELL + CELL / 2, y * CELL + CELL / 2)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
texture.needsUpdate = true
|
|
113
|
-
return texture
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export const ASCII = /* @__PURE__ */ forwardRef<ASCIIEffect, IASCIIEffectProps>(
|
|
118
|
-
(
|
|
119
|
-
{
|
|
120
|
-
font = 'arial',
|
|
121
|
-
characters = ` .:,'-^=*+?!|0#X%WM@`,
|
|
122
|
-
fontSize = 54,
|
|
123
|
-
cellSize = 16,
|
|
124
|
-
color = '#ffffff',
|
|
125
|
-
invert = false,
|
|
126
|
-
},
|
|
127
|
-
fref
|
|
128
|
-
) => {
|
|
129
|
-
const effect = useMemo(
|
|
130
|
-
() => new ASCIIEffect({ characters, font, fontSize, cellSize, color, invert }),
|
|
131
|
-
[characters, fontSize, cellSize, color, invert, font]
|
|
132
|
-
)
|
|
133
|
-
return <primitive ref={fref} object={effect} />
|
|
134
|
-
}
|
|
135
|
-
)
|
|
1
|
+
// From: https://github.com/emilwidlund/ASCII
|
|
2
|
+
// https://twitter.com/emilwidlund/status/1652386482420609024
|
|
3
|
+
|
|
4
|
+
import { forwardRef, useMemo } from 'react'
|
|
5
|
+
import { CanvasTexture, Color, NearestFilter, RepeatWrapping, Texture, Uniform } from 'three'
|
|
6
|
+
import { Effect } from 'postprocessing'
|
|
7
|
+
|
|
8
|
+
const fragment = `
|
|
9
|
+
uniform sampler2D uCharacters;
|
|
10
|
+
uniform float uCharactersCount;
|
|
11
|
+
uniform float uCellSize;
|
|
12
|
+
uniform bool uInvert;
|
|
13
|
+
uniform vec3 uColor;
|
|
14
|
+
|
|
15
|
+
const vec2 SIZE = vec2(16.);
|
|
16
|
+
|
|
17
|
+
vec3 greyscale(vec3 color, float strength) {
|
|
18
|
+
float g = dot(color, vec3(0.299, 0.587, 0.114));
|
|
19
|
+
return mix(color, vec3(g), strength);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
vec3 greyscale(vec3 color) {
|
|
23
|
+
return greyscale(color, 1.0);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
|
|
27
|
+
vec2 cell = resolution / uCellSize;
|
|
28
|
+
vec2 grid = 1.0 / cell;
|
|
29
|
+
vec2 pixelizedUV = grid * (0.5 + floor(uv / grid));
|
|
30
|
+
vec4 pixelized = texture2D(inputBuffer, pixelizedUV);
|
|
31
|
+
float greyscaled = greyscale(pixelized.rgb).r;
|
|
32
|
+
|
|
33
|
+
if (uInvert) {
|
|
34
|
+
greyscaled = 1.0 - greyscaled;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
float characterIndex = floor((uCharactersCount - 1.0) * greyscaled);
|
|
38
|
+
vec2 characterPosition = vec2(mod(characterIndex, SIZE.x), floor(characterIndex / SIZE.y));
|
|
39
|
+
vec2 offset = vec2(characterPosition.x, -characterPosition.y) / SIZE;
|
|
40
|
+
vec2 charUV = mod(uv * (cell / SIZE), 1.0 / SIZE) - vec2(0., 1.0 / SIZE) + offset;
|
|
41
|
+
vec4 asciiCharacter = texture2D(uCharacters, charUV);
|
|
42
|
+
|
|
43
|
+
asciiCharacter.rgb = uColor * asciiCharacter.r;
|
|
44
|
+
asciiCharacter.a = pixelized.a;
|
|
45
|
+
outputColor = asciiCharacter;
|
|
46
|
+
}
|
|
47
|
+
`
|
|
48
|
+
|
|
49
|
+
interface IASCIIEffectProps {
|
|
50
|
+
font?: string
|
|
51
|
+
characters?: string
|
|
52
|
+
fontSize?: number
|
|
53
|
+
cellSize?: number
|
|
54
|
+
color?: string
|
|
55
|
+
invert?: boolean
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
class ASCIIEffect extends Effect {
|
|
59
|
+
constructor({
|
|
60
|
+
font = 'arial',
|
|
61
|
+
characters = ` .:,'-^=*+?!|0#X%WM@`,
|
|
62
|
+
fontSize = 54,
|
|
63
|
+
cellSize = 16,
|
|
64
|
+
color = '#ffffff',
|
|
65
|
+
invert = false,
|
|
66
|
+
}: IASCIIEffectProps = {}) {
|
|
67
|
+
const uniforms = new Map<string, Uniform>([
|
|
68
|
+
['uCharacters', new Uniform(new Texture())],
|
|
69
|
+
['uCellSize', new Uniform(cellSize)],
|
|
70
|
+
['uCharactersCount', new Uniform(characters.length)],
|
|
71
|
+
['uColor', new Uniform(new Color(color))],
|
|
72
|
+
['uInvert', new Uniform(invert)],
|
|
73
|
+
])
|
|
74
|
+
|
|
75
|
+
super('ASCIIEffect', fragment, { uniforms })
|
|
76
|
+
|
|
77
|
+
const charactersTextureUniform = this.uniforms.get('uCharacters')
|
|
78
|
+
|
|
79
|
+
if (charactersTextureUniform) {
|
|
80
|
+
charactersTextureUniform.value = this.createCharactersTexture(characters, font, fontSize)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Draws the characters on a Canvas and returns a texture */
|
|
85
|
+
public createCharactersTexture(characters: string, font: string, fontSize: number): Texture {
|
|
86
|
+
const canvas = document.createElement('canvas')
|
|
87
|
+
const SIZE = 1024
|
|
88
|
+
const MAX_PER_ROW = 16
|
|
89
|
+
const CELL = SIZE / MAX_PER_ROW
|
|
90
|
+
|
|
91
|
+
canvas.width = canvas.height = SIZE
|
|
92
|
+
const texture = new CanvasTexture(canvas, undefined, RepeatWrapping, RepeatWrapping, NearestFilter, NearestFilter)
|
|
93
|
+
const context = canvas.getContext('2d')
|
|
94
|
+
|
|
95
|
+
if (!context) {
|
|
96
|
+
throw new Error('Context not available')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
context.clearRect(0, 0, SIZE, SIZE)
|
|
100
|
+
context.font = `${fontSize}px ${font}`
|
|
101
|
+
context.textAlign = 'center'
|
|
102
|
+
context.textBaseline = 'middle'
|
|
103
|
+
context.fillStyle = '#fff'
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < characters.length; i++) {
|
|
106
|
+
const char = characters[i]
|
|
107
|
+
const x = i % MAX_PER_ROW
|
|
108
|
+
const y = Math.floor(i / MAX_PER_ROW)
|
|
109
|
+
context.fillText(char, x * CELL + CELL / 2, y * CELL + CELL / 2)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
texture.needsUpdate = true
|
|
113
|
+
return texture
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const ASCII = /* @__PURE__ */ forwardRef<ASCIIEffect, IASCIIEffectProps>(
|
|
118
|
+
(
|
|
119
|
+
{
|
|
120
|
+
font = 'arial',
|
|
121
|
+
characters = ` .:,'-^=*+?!|0#X%WM@`,
|
|
122
|
+
fontSize = 54,
|
|
123
|
+
cellSize = 16,
|
|
124
|
+
color = '#ffffff',
|
|
125
|
+
invert = false,
|
|
126
|
+
},
|
|
127
|
+
fref
|
|
128
|
+
) => {
|
|
129
|
+
const effect = useMemo(
|
|
130
|
+
() => new ASCIIEffect({ characters, font, fontSize, cellSize, color, invert }),
|
|
131
|
+
[characters, fontSize, cellSize, color, invert, font]
|
|
132
|
+
)
|
|
133
|
+
return <primitive ref={fref} object={effect} />
|
|
134
|
+
}
|
|
135
|
+
)
|
|
@@ -1,153 +1,153 @@
|
|
|
1
|
-
import * as THREE from 'three'
|
|
2
|
-
import React, {
|
|
3
|
-
useRef,
|
|
4
|
-
useContext,
|
|
5
|
-
useState,
|
|
6
|
-
useEffect,
|
|
7
|
-
useCallback,
|
|
8
|
-
forwardRef,
|
|
9
|
-
useImperativeHandle,
|
|
10
|
-
RefObject,
|
|
11
|
-
useMemo,
|
|
12
|
-
} from 'react'
|
|
13
|
-
import { useThree, useFrame, createPortal, type Vector3 } from '@react-three/fiber'
|
|
14
|
-
import { CopyPass, DepthPickingPass, DepthOfFieldEffect } from 'postprocessing'
|
|
15
|
-
import { easing } from 'maath'
|
|
16
|
-
|
|
17
|
-
import { DepthOfField } from './DepthOfField'
|
|
18
|
-
import { EffectComposerContext } from '../EffectComposer'
|
|
19
|
-
|
|
20
|
-
export type AutofocusProps = React.ComponentProps<typeof DepthOfField> & {
|
|
21
|
-
target?: Vector3
|
|
22
|
-
/** should the target follow the pointer */
|
|
23
|
-
mouse?: boolean
|
|
24
|
-
/** size of the debug green point */
|
|
25
|
-
debug?: number
|
|
26
|
-
/** manual update */
|
|
27
|
-
manual?: boolean
|
|
28
|
-
/** approximate time to reach the target */
|
|
29
|
-
smoothTime?: number
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type AutofocusApi = {
|
|
33
|
-
dofRef: RefObject<DepthOfFieldEffect | null>
|
|
34
|
-
hitpoint: THREE.Vector3
|
|
35
|
-
update: (delta: number, updateTarget: boolean) => void
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export const Autofocus = /* @__PURE__ */ forwardRef<AutofocusApi, AutofocusProps>(
|
|
39
|
-
(
|
|
40
|
-
{ target = undefined, mouse: followMouse = false, debug = undefined, manual = false, smoothTime = 0.25, ...props },
|
|
41
|
-
fref
|
|
42
|
-
) => {
|
|
43
|
-
const dofRef = useRef<DepthOfFieldEffect>(null)
|
|
44
|
-
const hitpointRef = useRef<THREE.Mesh>(null)
|
|
45
|
-
const targetRef = useRef<THREE.Mesh>(null)
|
|
46
|
-
|
|
47
|
-
const scene = useThree(({ scene }) => scene)
|
|
48
|
-
const pointer = useThree(({ pointer }) => pointer)
|
|
49
|
-
const { composer, camera } = useContext(EffectComposerContext)
|
|
50
|
-
|
|
51
|
-
// see: https://codesandbox.io/s/depthpickingpass-x130hg
|
|
52
|
-
const [depthPickingPass] = useState(() => new DepthPickingPass())
|
|
53
|
-
const [copyPass] = useState(() => new CopyPass())
|
|
54
|
-
useEffect(() => {
|
|
55
|
-
composer.addPass(depthPickingPass)
|
|
56
|
-
composer.addPass(copyPass)
|
|
57
|
-
return () => {
|
|
58
|
-
composer.removePass(depthPickingPass)
|
|
59
|
-
composer.removePass(copyPass)
|
|
60
|
-
}
|
|
61
|
-
}, [composer, depthPickingPass, copyPass])
|
|
62
|
-
|
|
63
|
-
useEffect(() => {
|
|
64
|
-
return () => {
|
|
65
|
-
depthPickingPass.dispose()
|
|
66
|
-
copyPass.dispose()
|
|
67
|
-
}
|
|
68
|
-
}, [depthPickingPass, copyPass])
|
|
69
|
-
|
|
70
|
-
const [hitpoint] = useState(() => new THREE.Vector3(0, 0, 0))
|
|
71
|
-
|
|
72
|
-
const [ndc] = useState(() => new THREE.Vector3(0, 0, 0))
|
|
73
|
-
const getHit = useCallback(
|
|
74
|
-
async (x: number, y: number) => {
|
|
75
|
-
ndc.x = x
|
|
76
|
-
ndc.y = y
|
|
77
|
-
ndc.z = await depthPickingPass.readDepth(ndc)
|
|
78
|
-
ndc.z = ndc.z * 2.0 - 1.0
|
|
79
|
-
const hit = 1 - ndc.z > 0.0000001 // it is missed if ndc.z is close to 1
|
|
80
|
-
return hit ? ndc.unproject(camera) : false
|
|
81
|
-
},
|
|
82
|
-
[ndc, depthPickingPass, camera]
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
const update = useCallback(
|
|
86
|
-
async (delta: number, updateTarget = true) => {
|
|
87
|
-
// Update hitpoint
|
|
88
|
-
if (target) {
|
|
89
|
-
hitpoint.set(...(target as [number, number, number]))
|
|
90
|
-
} else {
|
|
91
|
-
const { x, y } = followMouse ? pointer : { x: 0, y: 0 }
|
|
92
|
-
const hit = await getHit(x, y)
|
|
93
|
-
if (hit) hitpoint.copy(hit)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Update target
|
|
97
|
-
if (updateTarget && dofRef.current?.target) {
|
|
98
|
-
if (smoothTime > 0 && delta > 0) {
|
|
99
|
-
easing.damp3(dofRef.current.target, hitpoint, smoothTime, delta)
|
|
100
|
-
} else {
|
|
101
|
-
dofRef.current.target.copy(hitpoint)
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
[target, hitpoint, followMouse, getHit, smoothTime, pointer]
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
useFrame(async (_, delta) => {
|
|
109
|
-
if (!manual) {
|
|
110
|
-
update(delta)
|
|
111
|
-
}
|
|
112
|
-
if (hitpointRef.current) {
|
|
113
|
-
hitpointRef.current.position.copy(hitpoint)
|
|
114
|
-
}
|
|
115
|
-
if (targetRef.current && dofRef.current?.target) {
|
|
116
|
-
targetRef.current.position.copy(dofRef.current.target)
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
// Ref API
|
|
121
|
-
const api = useMemo<AutofocusApi>(
|
|
122
|
-
() => ({
|
|
123
|
-
dofRef,
|
|
124
|
-
hitpoint,
|
|
125
|
-
update,
|
|
126
|
-
}),
|
|
127
|
-
[hitpoint, update]
|
|
128
|
-
)
|
|
129
|
-
useImperativeHandle(fref, () => api, [api])
|
|
130
|
-
|
|
131
|
-
return (
|
|
132
|
-
<>
|
|
133
|
-
{debug
|
|
134
|
-
? createPortal(
|
|
135
|
-
<>
|
|
136
|
-
<mesh ref={hitpointRef}>
|
|
137
|
-
<sphereGeometry args={[debug, 16, 16]} />
|
|
138
|
-
<meshBasicMaterial color="#00ff00" opacity={1} transparent depthWrite={false} />
|
|
139
|
-
</mesh>
|
|
140
|
-
<mesh ref={targetRef}>
|
|
141
|
-
<sphereGeometry args={[debug / 2, 16, 16]} />
|
|
142
|
-
<meshBasicMaterial color="#00ff00" opacity={0.5} transparent depthWrite={false} />
|
|
143
|
-
</mesh>
|
|
144
|
-
</>,
|
|
145
|
-
scene
|
|
146
|
-
)
|
|
147
|
-
: null}
|
|
148
|
-
|
|
149
|
-
<DepthOfField ref={dofRef} {...props} target={hitpoint} />
|
|
150
|
-
</>
|
|
151
|
-
)
|
|
152
|
-
}
|
|
153
|
-
)
|
|
1
|
+
import * as THREE from 'three'
|
|
2
|
+
import React, {
|
|
3
|
+
useRef,
|
|
4
|
+
useContext,
|
|
5
|
+
useState,
|
|
6
|
+
useEffect,
|
|
7
|
+
useCallback,
|
|
8
|
+
forwardRef,
|
|
9
|
+
useImperativeHandle,
|
|
10
|
+
RefObject,
|
|
11
|
+
useMemo,
|
|
12
|
+
} from 'react'
|
|
13
|
+
import { useThree, useFrame, createPortal, type Vector3 } from '@react-three/fiber'
|
|
14
|
+
import { CopyPass, DepthPickingPass, DepthOfFieldEffect } from 'postprocessing'
|
|
15
|
+
import { easing } from 'maath'
|
|
16
|
+
|
|
17
|
+
import { DepthOfField } from './DepthOfField'
|
|
18
|
+
import { EffectComposerContext } from '../EffectComposer'
|
|
19
|
+
|
|
20
|
+
export type AutofocusProps = React.ComponentProps<typeof DepthOfField> & {
|
|
21
|
+
target?: Vector3
|
|
22
|
+
/** should the target follow the pointer */
|
|
23
|
+
mouse?: boolean
|
|
24
|
+
/** size of the debug green point */
|
|
25
|
+
debug?: number
|
|
26
|
+
/** manual update */
|
|
27
|
+
manual?: boolean
|
|
28
|
+
/** approximate time to reach the target */
|
|
29
|
+
smoothTime?: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type AutofocusApi = {
|
|
33
|
+
dofRef: RefObject<DepthOfFieldEffect | null>
|
|
34
|
+
hitpoint: THREE.Vector3
|
|
35
|
+
update: (delta: number, updateTarget: boolean) => void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const Autofocus = /* @__PURE__ */ forwardRef<AutofocusApi, AutofocusProps>(
|
|
39
|
+
(
|
|
40
|
+
{ target = undefined, mouse: followMouse = false, debug = undefined, manual = false, smoothTime = 0.25, ...props },
|
|
41
|
+
fref
|
|
42
|
+
) => {
|
|
43
|
+
const dofRef = useRef<DepthOfFieldEffect>(null)
|
|
44
|
+
const hitpointRef = useRef<THREE.Mesh>(null)
|
|
45
|
+
const targetRef = useRef<THREE.Mesh>(null)
|
|
46
|
+
|
|
47
|
+
const scene = useThree(({ scene }) => scene)
|
|
48
|
+
const pointer = useThree(({ pointer }) => pointer)
|
|
49
|
+
const { composer, camera } = useContext(EffectComposerContext)
|
|
50
|
+
|
|
51
|
+
// see: https://codesandbox.io/s/depthpickingpass-x130hg
|
|
52
|
+
const [depthPickingPass] = useState(() => new DepthPickingPass())
|
|
53
|
+
const [copyPass] = useState(() => new CopyPass())
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
composer.addPass(depthPickingPass)
|
|
56
|
+
composer.addPass(copyPass)
|
|
57
|
+
return () => {
|
|
58
|
+
composer.removePass(depthPickingPass)
|
|
59
|
+
composer.removePass(copyPass)
|
|
60
|
+
}
|
|
61
|
+
}, [composer, depthPickingPass, copyPass])
|
|
62
|
+
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
return () => {
|
|
65
|
+
depthPickingPass.dispose()
|
|
66
|
+
copyPass.dispose()
|
|
67
|
+
}
|
|
68
|
+
}, [depthPickingPass, copyPass])
|
|
69
|
+
|
|
70
|
+
const [hitpoint] = useState(() => new THREE.Vector3(0, 0, 0))
|
|
71
|
+
|
|
72
|
+
const [ndc] = useState(() => new THREE.Vector3(0, 0, 0))
|
|
73
|
+
const getHit = useCallback(
|
|
74
|
+
async (x: number, y: number) => {
|
|
75
|
+
ndc.x = x
|
|
76
|
+
ndc.y = y
|
|
77
|
+
ndc.z = await depthPickingPass.readDepth(ndc)
|
|
78
|
+
ndc.z = ndc.z * 2.0 - 1.0
|
|
79
|
+
const hit = 1 - ndc.z > 0.0000001 // it is missed if ndc.z is close to 1
|
|
80
|
+
return hit ? ndc.unproject(camera) : false
|
|
81
|
+
},
|
|
82
|
+
[ndc, depthPickingPass, camera]
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
const update = useCallback(
|
|
86
|
+
async (delta: number, updateTarget = true) => {
|
|
87
|
+
// Update hitpoint
|
|
88
|
+
if (target) {
|
|
89
|
+
hitpoint.set(...(target as [number, number, number]))
|
|
90
|
+
} else {
|
|
91
|
+
const { x, y } = followMouse ? pointer : { x: 0, y: 0 }
|
|
92
|
+
const hit = await getHit(x, y)
|
|
93
|
+
if (hit) hitpoint.copy(hit)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Update target
|
|
97
|
+
if (updateTarget && dofRef.current?.target) {
|
|
98
|
+
if (smoothTime > 0 && delta > 0) {
|
|
99
|
+
easing.damp3(dofRef.current.target, hitpoint, smoothTime, delta)
|
|
100
|
+
} else {
|
|
101
|
+
dofRef.current.target.copy(hitpoint)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
[target, hitpoint, followMouse, getHit, smoothTime, pointer]
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
useFrame(async (_, delta) => {
|
|
109
|
+
if (!manual) {
|
|
110
|
+
update(delta)
|
|
111
|
+
}
|
|
112
|
+
if (hitpointRef.current) {
|
|
113
|
+
hitpointRef.current.position.copy(hitpoint)
|
|
114
|
+
}
|
|
115
|
+
if (targetRef.current && dofRef.current?.target) {
|
|
116
|
+
targetRef.current.position.copy(dofRef.current.target)
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
// Ref API
|
|
121
|
+
const api = useMemo<AutofocusApi>(
|
|
122
|
+
() => ({
|
|
123
|
+
dofRef,
|
|
124
|
+
hitpoint,
|
|
125
|
+
update,
|
|
126
|
+
}),
|
|
127
|
+
[hitpoint, update]
|
|
128
|
+
)
|
|
129
|
+
useImperativeHandle(fref, () => api, [api])
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<>
|
|
133
|
+
{debug
|
|
134
|
+
? createPortal(
|
|
135
|
+
<>
|
|
136
|
+
<mesh ref={hitpointRef}>
|
|
137
|
+
<sphereGeometry args={[debug, 16, 16]} />
|
|
138
|
+
<meshBasicMaterial color="#00ff00" opacity={1} transparent depthWrite={false} />
|
|
139
|
+
</mesh>
|
|
140
|
+
<mesh ref={targetRef}>
|
|
141
|
+
<sphereGeometry args={[debug / 2, 16, 16]} />
|
|
142
|
+
<meshBasicMaterial color="#00ff00" opacity={0.5} transparent depthWrite={false} />
|
|
143
|
+
</mesh>
|
|
144
|
+
</>,
|
|
145
|
+
scene
|
|
146
|
+
)
|
|
147
|
+
: null}
|
|
148
|
+
|
|
149
|
+
<DepthOfField ref={dofRef} {...props} target={hitpoint} />
|
|
150
|
+
</>
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
)
|
package/src/effects/Bloom.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BloomEffect, BlendFunction } from 'postprocessing'
|
|
2
|
-
import { wrapEffect } from '../util'
|
|
3
|
-
|
|
4
|
-
export const Bloom = /* @__PURE__ */ wrapEffect(BloomEffect, {
|
|
5
|
-
blendFunction: BlendFunction.ADD,
|
|
6
|
-
})
|
|
1
|
+
import { BloomEffect, BlendFunction } from 'postprocessing'
|
|
2
|
+
import { wrapEffect } from '../util'
|
|
3
|
+
|
|
4
|
+
export const Bloom = /* @__PURE__ */ wrapEffect(BloomEffect, {
|
|
5
|
+
blendFunction: BlendFunction.ADD,
|
|
6
|
+
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BrightnessContrastEffect } from 'postprocessing'
|
|
2
|
-
import { wrapEffect } from '../util'
|
|
3
|
-
|
|
4
|
-
export const BrightnessContrast = /* @__PURE__ */ wrapEffect(BrightnessContrastEffect)
|
|
1
|
+
import { BrightnessContrastEffect } from 'postprocessing'
|
|
2
|
+
import { wrapEffect } from '../util'
|
|
3
|
+
|
|
4
|
+
export const BrightnessContrast = /* @__PURE__ */ wrapEffect(BrightnessContrastEffect)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ChromaticAberrationEffect } from 'postprocessing'
|
|
2
|
-
import { type EffectProps, wrapEffect } from '../util'
|
|
3
|
-
|
|
4
|
-
export type ChromaticAberrationProps = EffectProps<typeof ChromaticAberrationEffect>
|
|
5
|
-
export const ChromaticAberration = /* @__PURE__ */ wrapEffect(ChromaticAberrationEffect)
|
|
1
|
+
import { ChromaticAberrationEffect } from 'postprocessing'
|
|
2
|
+
import { type EffectProps, wrapEffect } from '../util'
|
|
3
|
+
|
|
4
|
+
export type ChromaticAberrationProps = EffectProps<typeof ChromaticAberrationEffect>
|
|
5
|
+
export const ChromaticAberration = /* @__PURE__ */ wrapEffect(ChromaticAberrationEffect)
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { ColorAverageEffect, BlendFunction } from 'postprocessing'
|
|
2
|
-
import React, { Ref, forwardRef, useMemo } from 'react'
|
|
3
|
-
|
|
4
|
-
export type ColorAverageProps = Partial<{
|
|
5
|
-
blendFunction: BlendFunction
|
|
6
|
-
}>
|
|
7
|
-
|
|
8
|
-
export const ColorAverage = /* @__PURE__ */ forwardRef<ColorAverageEffect, ColorAverageProps>(function ColorAverage(
|
|
9
|
-
{ blendFunction = BlendFunction.NORMAL }: ColorAverageProps,
|
|
10
|
-
ref: Ref<ColorAverageEffect>
|
|
11
|
-
) {
|
|
12
|
-
/** Because ColorAverage blendFunction is not an object but a number, we have to define a custom prop "blendFunction" */
|
|
13
|
-
const effect = useMemo(() => new ColorAverageEffect(blendFunction), [blendFunction])
|
|
14
|
-
return <primitive ref={ref} object={effect} dispose={null} />
|
|
15
|
-
})
|
|
1
|
+
import { ColorAverageEffect, BlendFunction } from 'postprocessing'
|
|
2
|
+
import React, { Ref, forwardRef, useMemo } from 'react'
|
|
3
|
+
|
|
4
|
+
export type ColorAverageProps = Partial<{
|
|
5
|
+
blendFunction: BlendFunction
|
|
6
|
+
}>
|
|
7
|
+
|
|
8
|
+
export const ColorAverage = /* @__PURE__ */ forwardRef<ColorAverageEffect, ColorAverageProps>(function ColorAverage(
|
|
9
|
+
{ blendFunction = BlendFunction.NORMAL }: ColorAverageProps,
|
|
10
|
+
ref: Ref<ColorAverageEffect>
|
|
11
|
+
) {
|
|
12
|
+
/** Because ColorAverage blendFunction is not an object but a number, we have to define a custom prop "blendFunction" */
|
|
13
|
+
const effect = useMemo(() => new ColorAverageEffect(blendFunction), [blendFunction])
|
|
14
|
+
return <primitive ref={ref} object={effect} dispose={null} />
|
|
15
|
+
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ColorDepthEffect } from 'postprocessing'
|
|
2
|
-
import { wrapEffect } from '../util'
|
|
3
|
-
|
|
4
|
-
export const ColorDepth = /* @__PURE__ */ wrapEffect(ColorDepthEffect)
|
|
1
|
+
import { ColorDepthEffect } from 'postprocessing'
|
|
2
|
+
import { wrapEffect } from '../util'
|
|
3
|
+
|
|
4
|
+
export const ColorDepth = /* @__PURE__ */ wrapEffect(ColorDepthEffect)
|
package/src/effects/Depth.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DepthEffect } from 'postprocessing'
|
|
2
|
-
import { wrapEffect } from '../util'
|
|
3
|
-
|
|
4
|
-
export const Depth = /* @__PURE__ */ wrapEffect(DepthEffect)
|
|
1
|
+
import { DepthEffect } from 'postprocessing'
|
|
2
|
+
import { wrapEffect } from '../util'
|
|
3
|
+
|
|
4
|
+
export const Depth = /* @__PURE__ */ wrapEffect(DepthEffect)
|