@react-three/postprocessing 2.11.0 → 2.11.2

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.
@@ -0,0 +1,22 @@
1
+ import * as THREE from 'three';
2
+ import React, { RefObject } from 'react';
3
+ import { Vector3 } from '@react-three/fiber';
4
+ import { DepthOfFieldEffect } from 'postprocessing';
5
+ import { DepthOfField } from './DepthOfField';
6
+ export type AutofocusProps = React.ComponentProps<typeof DepthOfField> & {
7
+ target?: Vector3;
8
+ /** should the target follow the pointer */
9
+ mouse?: boolean;
10
+ /** size of the debug green point */
11
+ debug?: number;
12
+ /** manual update */
13
+ manual?: boolean;
14
+ /** approximate time to reach the target */
15
+ smoothTime?: number;
16
+ };
17
+ export type AutofocusApi = {
18
+ dofRef: RefObject<DepthOfFieldEffect>;
19
+ hitpoint: THREE.Vector3;
20
+ update: (delta: number, updateTarget: boolean) => void;
21
+ };
22
+ export declare const Autofocus: React.ForwardRefExoticComponent<Omit<AutofocusProps, "ref"> & React.RefAttributes<AutofocusApi>>;
@@ -1,5 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { BloomEffect, BlendFunction } from 'postprocessing';
3
- export declare const Bloom: import("react").ForwardRefExoticComponent<{
4
- blendFunction: BlendFunction;
5
- } & import("react").RefAttributes<typeof BloomEffect>>;
3
+ export declare const Bloom: import("react").ForwardRefExoticComponent<Omit<import("@react-three/fiber").ExtendedColors<import("@react-three/fiber").Overwrite<Partial<BloomEffect>, import("@react-three/fiber").NodeProps<BloomEffect, typeof BloomEffect>>> & import("postprocessing").BloomEffectOptions & {
4
+ blendFunction?: BlendFunction | undefined;
5
+ opacity?: number | undefined;
6
+ }, "ref"> & import("react").RefAttributes<typeof BloomEffect>>;
@@ -1,5 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { NoiseEffect, BlendFunction } from 'postprocessing';
3
- export declare const Noise: import("react").ForwardRefExoticComponent<{
4
- blendFunction: BlendFunction;
5
- } & import("react").RefAttributes<typeof NoiseEffect>>;
3
+ export declare const Noise: import("react").ForwardRefExoticComponent<Omit<import("@react-three/fiber").ExtendedColors<import("@react-three/fiber").Overwrite<Partial<NoiseEffect>, import("@react-three/fiber").NodeProps<NoiseEffect, typeof NoiseEffect>>> & {
4
+ blendFunction?: BlendFunction | undefined;
5
+ premultiply?: boolean | undefined;
6
+ } & {
7
+ blendFunction?: BlendFunction | undefined;
8
+ opacity?: number | undefined;
9
+ }, "ref"> & import("react").RefAttributes<typeof NoiseEffect>>;
@@ -1,6 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { ScanlineEffect, BlendFunction } from 'postprocessing';
3
- export declare const Scanline: import("react").ForwardRefExoticComponent<{
4
- blendFunction: BlendFunction;
5
- density: number;
6
- } & import("react").RefAttributes<typeof ScanlineEffect>>;
3
+ export declare const Scanline: import("react").ForwardRefExoticComponent<Omit<import("@react-three/fiber").ExtendedColors<import("@react-three/fiber").Overwrite<Partial<ScanlineEffect>, import("@react-three/fiber").NodeProps<ScanlineEffect, typeof ScanlineEffect>>> & {
4
+ blendFunction?: BlendFunction | undefined;
5
+ density?: number | undefined;
6
+ } & {
7
+ blendFunction?: BlendFunction | undefined;
8
+ opacity?: number | undefined;
9
+ }, "ref"> & import("react").RefAttributes<typeof ScanlineEffect>>;
@@ -1,5 +1,17 @@
1
1
  /// <reference types="react" />
2
2
  import { TiltShiftEffect, BlendFunction } from 'postprocessing';
3
- export declare const TiltShift: import("react").ForwardRefExoticComponent<{
4
- blendFunction: BlendFunction;
5
- } & import("react").RefAttributes<typeof TiltShiftEffect>>;
3
+ export declare const TiltShift: import("react").ForwardRefExoticComponent<Omit<import("@react-three/fiber").ExtendedColors<import("@react-three/fiber").Overwrite<Partial<TiltShiftEffect>, import("@react-three/fiber").NodeProps<TiltShiftEffect, typeof TiltShiftEffect>>> & {
4
+ blendFunction?: BlendFunction | undefined;
5
+ offset?: number | undefined;
6
+ rotation?: number | undefined;
7
+ focusArea?: number | undefined;
8
+ feather?: number | undefined;
9
+ bias?: number | undefined;
10
+ kernelSize?: import("postprocessing").KernelSize | undefined;
11
+ resolutionScale?: number | undefined;
12
+ resolutionX?: number | undefined;
13
+ resolutionY?: number | undefined;
14
+ } & {
15
+ blendFunction?: BlendFunction | undefined;
16
+ opacity?: number | undefined;
17
+ }, "ref"> & import("react").RefAttributes<typeof TiltShiftEffect>>;
@@ -16,6 +16,15 @@ export declare class TiltShiftEffect extends Effect {
16
16
  direction?: number[] | undefined;
17
17
  });
18
18
  }
19
- export declare const TiltShift2: import("react").ForwardRefExoticComponent<{
20
- blendFunction: BlendFunction;
21
- } & import("react").RefAttributes<typeof TiltShiftEffect>>;
19
+ export declare const TiltShift2: import("react").ForwardRefExoticComponent<Omit<import("@react-three/fiber").ExtendedColors<import("@react-three/fiber").Overwrite<Partial<TiltShiftEffect>, import("@react-three/fiber").NodeProps<TiltShiftEffect, typeof TiltShiftEffect>>> & {
20
+ blendFunction?: BlendFunction | undefined;
21
+ blur?: number | undefined;
22
+ taper?: number | undefined;
23
+ start?: number[] | undefined;
24
+ end?: number[] | undefined;
25
+ samples?: number | undefined;
26
+ direction?: number[] | undefined;
27
+ } & {
28
+ blendFunction?: BlendFunction | undefined;
29
+ opacity?: number | undefined;
30
+ }, "ref"> & import("react").RefAttributes<typeof TiltShiftEffect>>;
package/dist/index.cjs CHANGED
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var postprocessing = require('postprocessing');
4
3
  var jsxRuntime = require('react/jsx-runtime');
5
- var React = require('react');
6
4
  var THREE = require('three');
5
+ var React = require('react');
7
6
  var fiber = require('@react-three/fiber');
7
+ var postprocessing = require('postprocessing');
8
+ var maath = require('maath');
8
9
  var threeStdlib = require('three-stdlib');
9
10
  var screenSpaceReflections = require('screen-space-reflections');
10
- var maath = require('maath');
11
11
 
12
12
  function _interopNamespaceDefault(e) {
13
13
  var n = Object.create(null);
@@ -28,57 +28,6 @@ function _interopNamespaceDefault(e) {
28
28
 
29
29
  var THREE__namespace = /*#__PURE__*/_interopNamespaceDefault(THREE);
30
30
 
31
- const resolveRef = (ref) => typeof ref === 'object' && ref != null && 'current' in ref ? ref.current : ref;
32
- let i = 0;
33
- const components = new WeakMap();
34
- const wrapEffect = (effect, defaults) =>
35
- /* @__PURE__ */ React.forwardRef(function Effect({ blendFunction = defaults === null || defaults === void 0 ? void 0 : defaults.blendFunction, opacity = defaults === null || defaults === void 0 ? void 0 : defaults.opacity, ...props }, ref) {
36
- let Component = components.get(effect);
37
- if (!Component) {
38
- const key = `@react-three/postprocessing/${effect.name}-${i++}`;
39
- fiber.extend({ [key]: effect });
40
- components.set(effect, (Component = key));
41
- }
42
- const camera = fiber.useThree((state) => state.camera);
43
- const args = React.useMemo(() => { var _a, _b; return [...((_a = defaults === null || defaults === void 0 ? void 0 : defaults.args) !== null && _a !== void 0 ? _a : []), ...((_b = props.args) !== null && _b !== void 0 ? _b : [{ ...defaults, ...props }])]; },
44
- // eslint-disable-next-line react-hooks/exhaustive-deps
45
- [JSON.stringify(props)]);
46
- return (jsxRuntime.jsx(Component, { camera: camera, "blendMode-blendFunction": blendFunction, "blendMode-opacity": opacity, ...props, ref: ref, args: args }));
47
- });
48
- const useVector2 = (props, key) => {
49
- const value = props[key];
50
- return React.useMemo(() => {
51
- if (typeof value === 'number')
52
- return new THREE__namespace.Vector2(value, value);
53
- else if (value)
54
- return new THREE__namespace.Vector2(...value);
55
- else
56
- return new THREE__namespace.Vector2();
57
- }, [value]);
58
- };
59
-
60
- const Bloom = wrapEffect(postprocessing.BloomEffect, {
61
- blendFunction: postprocessing.BlendFunction.ADD,
62
- });
63
-
64
- const BrightnessContrast = wrapEffect(postprocessing.BrightnessContrastEffect);
65
-
66
- const ChromaticAberration = React.forwardRef(function ChromaticAberration(props, ref) {
67
- const offset = useVector2(props, 'offset');
68
- const effect = React.useMemo(() => new postprocessing.ChromaticAberrationEffect({ ...props, offset }), [offset, props]);
69
- return jsxRuntime.jsx("primitive", { ref: ref, object: effect, dispose: null });
70
- });
71
-
72
- const ColorAverage = React.forwardRef(function ColorAverage({ blendFunction = postprocessing.BlendFunction.NORMAL }, ref) {
73
- /** Because ColorAverage blendFunction is not an object but a number, we have to define a custom prop "blendFunction" */
74
- const effect = React.useMemo(() => new postprocessing.ColorAverageEffect(blendFunction), [blendFunction]);
75
- return jsxRuntime.jsx("primitive", { ref: ref, object: effect, dispose: null });
76
- });
77
-
78
- const ColorDepth = wrapEffect(postprocessing.ColorDepthEffect);
79
-
80
- const Depth = wrapEffect(postprocessing.DepthEffect);
81
-
82
31
  const EffectComposerContext = React.createContext(null);
83
32
  const EffectComposer = React.memo(React.forwardRef(({ children, camera: _camera, scene: _scene, resolutionScale, enabled = true, renderPriority = 1, autoClear = true, depthBuffer, disableNormalPass, stencilBuffer, multisampling = 8, frameBufferType = THREE.HalfFloatType, }, ref) => {
84
33
  const { gl, scene: defaultScene, camera: defaultCamera, size } = fiber.useThree();
@@ -180,6 +129,137 @@ const DepthOfField = React.forwardRef(function DepthOfField({ target, depthTextu
180
129
  return jsxRuntime.jsx("primitive", { ref: ref, object: effect, dispose: null });
181
130
  });
182
131
 
132
+ const Autofocus = React.forwardRef(({ target = undefined, mouse: followMouse = false, debug = undefined, manual = false, smoothTime = 0.25, ...props }, fref) => {
133
+ const dofRef = React.useRef(null);
134
+ const hitpointRef = React.useRef(null);
135
+ const targetRef = React.useRef(null);
136
+ const scene = fiber.useThree(({ scene }) => scene);
137
+ const pointer = fiber.useThree(({ pointer }) => pointer);
138
+ const { composer, camera } = React.useContext(EffectComposerContext);
139
+ // see: https://codesandbox.io/s/depthpickingpass-x130hg
140
+ const [depthPickingPass] = React.useState(() => new postprocessing.DepthPickingPass());
141
+ const [copyPass] = React.useState(() => new postprocessing.CopyPass());
142
+ React.useEffect(() => {
143
+ composer.addPass(depthPickingPass);
144
+ composer.addPass(copyPass);
145
+ return () => {
146
+ composer.removePass(depthPickingPass);
147
+ composer.removePass(copyPass);
148
+ };
149
+ }, [composer, depthPickingPass, copyPass]);
150
+ React.useEffect(() => {
151
+ return () => {
152
+ depthPickingPass.dispose();
153
+ copyPass.dispose();
154
+ };
155
+ }, [depthPickingPass, copyPass]);
156
+ const [hitpoint] = React.useState(() => new THREE__namespace.Vector3(0, 0, 0));
157
+ const [ndc] = React.useState(() => new THREE__namespace.Vector3(0, 0, 0));
158
+ const getHit = React.useCallback(async (x, y) => {
159
+ ndc.x = x;
160
+ ndc.y = y;
161
+ ndc.z = await depthPickingPass.readDepth(ndc);
162
+ ndc.z = ndc.z * 2.0 - 1.0;
163
+ const hit = 1 - ndc.z > 0.0000001; // it is missed if ndc.z is close to 1
164
+ return hit ? ndc.unproject(camera) : false;
165
+ }, [ndc, depthPickingPass, camera]);
166
+ const update = React.useCallback(async (delta, updateTarget = true) => {
167
+ var _a;
168
+ // Update hitpoint
169
+ if (target) {
170
+ hitpoint.set(...target);
171
+ }
172
+ else {
173
+ const { x, y } = followMouse ? pointer : { x: 0, y: 0 };
174
+ const hit = await getHit(x, y);
175
+ if (hit)
176
+ hitpoint.copy(hit);
177
+ }
178
+ // Update target
179
+ if (updateTarget && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
180
+ if (smoothTime > 0 && delta > 0) {
181
+ maath.easing.damp3(dofRef.current.target, hitpoint, smoothTime, delta);
182
+ }
183
+ else {
184
+ dofRef.current.target.copy(hitpoint);
185
+ }
186
+ }
187
+ }, [target, hitpoint, followMouse, getHit, smoothTime, pointer]);
188
+ fiber.useFrame(async (_, delta) => {
189
+ var _a;
190
+ if (!manual) {
191
+ update(delta);
192
+ }
193
+ if (hitpointRef.current) {
194
+ hitpointRef.current.position.copy(hitpoint);
195
+ }
196
+ if (targetRef.current && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
197
+ targetRef.current.position.copy(dofRef.current.target);
198
+ }
199
+ });
200
+ // Ref API
201
+ const api = React.useMemo(() => ({
202
+ dofRef,
203
+ hitpoint,
204
+ update,
205
+ }), [hitpoint, update]);
206
+ React.useImperativeHandle(fref, () => api, [api]);
207
+ return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [debug
208
+ ? fiber.createPortal(jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("mesh", { ref: hitpointRef, children: [jsxRuntime.jsx("sphereGeometry", { args: [debug, 16, 16] }), jsxRuntime.jsx("meshBasicMaterial", { color: "#00ff00", opacity: 1, transparent: true, depthWrite: false })] }), jsxRuntime.jsxs("mesh", { ref: targetRef, children: [jsxRuntime.jsx("sphereGeometry", { args: [debug / 2, 16, 16] }), jsxRuntime.jsx("meshBasicMaterial", { color: "#00ff00", opacity: 0.5, transparent: true, depthWrite: false })] })] }), scene)
209
+ : null, jsxRuntime.jsx(DepthOfField, { ref: dofRef, ...props, target: hitpoint })] }));
210
+ });
211
+
212
+ const resolveRef = (ref) => typeof ref === 'object' && ref != null && 'current' in ref ? ref.current : ref;
213
+ let i = 0;
214
+ const components = new WeakMap();
215
+ const wrapEffect = (effect, defaults) =>
216
+ /* @__PURE__ */ React.forwardRef(function Effect({ blendFunction = defaults === null || defaults === void 0 ? void 0 : defaults.blendFunction, opacity = defaults === null || defaults === void 0 ? void 0 : defaults.opacity, ...props }, ref) {
217
+ let Component = components.get(effect);
218
+ if (!Component) {
219
+ const key = `@react-three/postprocessing/${effect.name}-${i++}`;
220
+ fiber.extend({ [key]: effect });
221
+ components.set(effect, (Component = key));
222
+ }
223
+ const camera = fiber.useThree((state) => state.camera);
224
+ const args = React.useMemo(() => { var _a, _b; return [...((_a = defaults === null || defaults === void 0 ? void 0 : defaults.args) !== null && _a !== void 0 ? _a : []), ...((_b = props.args) !== null && _b !== void 0 ? _b : [{ ...defaults, ...props }])]; },
225
+ // eslint-disable-next-line react-hooks/exhaustive-deps
226
+ [JSON.stringify(props)]);
227
+ return (jsxRuntime.jsx(Component, { camera: camera, "blendMode-blendFunction": blendFunction, "blendMode-opacity": opacity, ...props, ref: ref, args: args }));
228
+ });
229
+ const useVector2 = (props, key) => {
230
+ const value = props[key];
231
+ return React.useMemo(() => {
232
+ if (typeof value === 'number')
233
+ return new THREE__namespace.Vector2(value, value);
234
+ else if (value)
235
+ return new THREE__namespace.Vector2(...value);
236
+ else
237
+ return new THREE__namespace.Vector2();
238
+ }, [value]);
239
+ };
240
+
241
+ const Bloom = wrapEffect(postprocessing.BloomEffect, {
242
+ blendFunction: postprocessing.BlendFunction.ADD,
243
+ });
244
+
245
+ const BrightnessContrast = wrapEffect(postprocessing.BrightnessContrastEffect);
246
+
247
+ const ChromaticAberration = React.forwardRef(function ChromaticAberration(props, ref) {
248
+ const offset = useVector2(props, 'offset');
249
+ const effect = React.useMemo(() => new postprocessing.ChromaticAberrationEffect({ ...props, offset }), [offset, props]);
250
+ return jsxRuntime.jsx("primitive", { ref: ref, object: effect, dispose: null });
251
+ });
252
+
253
+ const ColorAverage = React.forwardRef(function ColorAverage({ blendFunction = postprocessing.BlendFunction.NORMAL }, ref) {
254
+ /** Because ColorAverage blendFunction is not an object but a number, we have to define a custom prop "blendFunction" */
255
+ const effect = React.useMemo(() => new postprocessing.ColorAverageEffect(blendFunction), [blendFunction]);
256
+ return jsxRuntime.jsx("primitive", { ref: ref, object: effect, dispose: null });
257
+ });
258
+
259
+ const ColorDepth = wrapEffect(postprocessing.ColorDepthEffect);
260
+
261
+ const Depth = wrapEffect(postprocessing.DepthEffect);
262
+
183
263
  const DotScreen = wrapEffect(postprocessing.DotScreenEffect);
184
264
 
185
265
  const Glitch = React.forwardRef(function Glitch({ active = true, ...props }, ref) {
@@ -552,79 +632,6 @@ const SSR = React.forwardRef(function SSR({ ENABLE_BLUR = true, USE_MRT = true,
552
632
  return jsxRuntime.jsx("primitive", { ref: ref, object: effect, ...props });
553
633
  });
554
634
 
555
- const Autofocus = React.forwardRef(({ target = undefined, mouse: followMouse = false, debug = undefined, manual = false, smoothTime = 0, ...props }, fref) => {
556
- const dofRef = React.useRef(null);
557
- const hitpointRef = React.useRef(null);
558
- const targetRef = React.useRef(null);
559
- const scene = fiber.useThree(({ scene }) => scene);
560
- const pointer = fiber.useThree(({ pointer }) => pointer);
561
- const { composer, camera } = React.useContext(EffectComposerContext);
562
- // see: https://codesandbox.io/s/depthpickingpass-x130hg
563
- const [depthPickingPass] = React.useState(new postprocessing.DepthPickingPass());
564
- const [copyPass] = React.useState(new postprocessing.CopyPass());
565
- React.useEffect(() => {
566
- composer.addPass(depthPickingPass);
567
- composer.addPass(copyPass);
568
- return () => {
569
- composer.removePass(depthPickingPass);
570
- composer.removePass(copyPass);
571
- };
572
- }, [composer, depthPickingPass, copyPass]);
573
- const [hitpoint] = React.useState(new THREE__namespace.Vector3(0, 0, 0));
574
- const [ndc] = React.useState(new THREE__namespace.Vector3(0, 0, 0));
575
- const getHit = React.useCallback(async (x, y) => {
576
- ndc.x = x;
577
- ndc.y = y;
578
- ndc.z = await depthPickingPass.readDepth(ndc);
579
- ndc.z = ndc.z * 2.0 - 1.0;
580
- const hit = 1 - ndc.z > 0.0000001; // it is missed if ndc.z is close to 1
581
- return hit ? ndc.unproject(camera) : false;
582
- }, [ndc, depthPickingPass, camera]);
583
- const update = React.useCallback(async (delta, updateTarget = true) => {
584
- var _a;
585
- // Update hitpoint
586
- if (target) {
587
- hitpoint.set(...target);
588
- }
589
- else {
590
- const { x, y } = followMouse ? pointer : { x: 0, y: 0 };
591
- const hit = await getHit(x, y);
592
- if (hit)
593
- hitpoint.copy(hit);
594
- }
595
- // Update target
596
- if (updateTarget && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
597
- if (smoothTime > 0 && delta > 0) {
598
- maath.easing.damp3(dofRef.current.target, hitpoint, smoothTime, delta);
599
- }
600
- else {
601
- dofRef.current.target.copy(hitpoint);
602
- }
603
- }
604
- }, [target, hitpoint, followMouse, getHit, smoothTime, pointer]);
605
- fiber.useFrame(async (_, delta) => {
606
- var _a;
607
- if (!manual) {
608
- update(delta);
609
- }
610
- if (hitpointRef.current) {
611
- hitpointRef.current.position.copy(hitpoint);
612
- }
613
- if (targetRef.current && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
614
- targetRef.current.position.copy(dofRef.current.target);
615
- }
616
- });
617
- // Ref API
618
- React.useImperativeHandle(fref, () => ({
619
- dofRef,
620
- hitpoint,
621
- update,
622
- }), [hitpoint, update]);
623
- return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [debug
624
- ? fiber.createPortal(jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("mesh", { ref: hitpointRef, children: [jsxRuntime.jsx("sphereGeometry", { args: [debug, 16, 16] }), jsxRuntime.jsx("meshBasicMaterial", { color: "#00ff00", opacity: 1, transparent: true, depthWrite: false })] }), jsxRuntime.jsxs("mesh", { ref: targetRef, children: [jsxRuntime.jsx("sphereGeometry", { args: [debug / 2, 16, 16] }), jsxRuntime.jsx("meshBasicMaterial", { color: "#00ff00", opacity: 0.5, transparent: true, depthWrite: false })] })] }), scene)
625
- : null, jsxRuntime.jsx(DepthOfField, { ref: dofRef, ...props, target: hitpoint })] }));
626
- });
627
-
628
635
  exports.Autofocus = Autofocus;
629
636
  exports.Bloom = Bloom;
630
637
  exports.BrightnessContrast = BrightnessContrast;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './effects/Autofocus';
1
2
  export * from './effects/Bloom';
2
3
  export * from './effects/BrightnessContrast';
3
4
  export * from './effects/ChromaticAberration';
@@ -27,6 +28,5 @@ export * from './effects/TiltShift';
27
28
  export * from './effects/TiltShift2';
28
29
  export * from './effects/SSR';
29
30
  export * from './Selection';
30
- export * from './Autofocus';
31
31
  export * from './EffectComposer';
32
32
  export * from './util';
package/dist/index.js CHANGED
@@ -1,63 +1,12 @@
1
- import { BlendFunction, BloomEffect, BrightnessContrastEffect, ChromaticAberrationEffect, ColorAverageEffect, ColorDepthEffect, DepthEffect, EffectComposer as EffectComposer$1, RenderPass, NormalPass, DepthDownsamplingPass, EffectPass, DepthOfFieldEffect, MaskFunction, DotScreenEffect, GlitchEffect, GlitchMode, GodRaysEffect, GridEffect, HueSaturationEffect, NoiseEffect, OutlineEffect, PixelationEffect, ScanlineEffect, SelectiveBloomEffect, SepiaEffect, SSAOEffect, SMAAEffect, TextureEffect, ToneMappingEffect, VignetteEffect, ShockWaveEffect, LUT3DEffect, TiltShiftEffect as TiltShiftEffect$1, Effect, EffectAttribute, DepthPickingPass, CopyPass } from 'postprocessing';
2
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
- import React, { forwardRef, useMemo, createContext, useEffect, useRef, useLayoutEffect, useImperativeHandle, useContext, useState, useCallback } from 'react';
4
2
  import * as THREE from 'three';
5
3
  import { HalfFloatType, Vector3, TextureLoader, sRGBEncoding, RepeatWrapping, Uniform } from 'three';
6
- import { extend, useThree, useFrame, useInstanceHandle, useLoader, createPortal } from '@react-three/fiber';
4
+ import React, { createContext, forwardRef, useMemo, useEffect, useRef, useLayoutEffect, useImperativeHandle, useContext, useState, useCallback } from 'react';
5
+ import { useThree, useFrame, useInstanceHandle, createPortal, extend, useLoader } from '@react-three/fiber';
6
+ import { EffectComposer as EffectComposer$1, RenderPass, NormalPass, DepthDownsamplingPass, EffectPass, DepthOfFieldEffect, MaskFunction, DepthPickingPass, CopyPass, BlendFunction, BloomEffect, BrightnessContrastEffect, ChromaticAberrationEffect, ColorAverageEffect, ColorDepthEffect, DepthEffect, DotScreenEffect, GlitchEffect, GlitchMode, GodRaysEffect, GridEffect, HueSaturationEffect, NoiseEffect, OutlineEffect, PixelationEffect, ScanlineEffect, SelectiveBloomEffect, SepiaEffect, SSAOEffect, SMAAEffect, TextureEffect, ToneMappingEffect, VignetteEffect, ShockWaveEffect, LUT3DEffect, TiltShiftEffect as TiltShiftEffect$1, Effect, EffectAttribute } from 'postprocessing';
7
+ import { easing } from 'maath';
7
8
  import { isWebGL2Available } from 'three-stdlib';
8
9
  import { SSREffect } from 'screen-space-reflections';
9
- import { easing } from 'maath';
10
-
11
- const resolveRef = (ref) => typeof ref === 'object' && ref != null && 'current' in ref ? ref.current : ref;
12
- let i = 0;
13
- const components = new WeakMap();
14
- const wrapEffect = (effect, defaults) =>
15
- /* @__PURE__ */ React.forwardRef(function Effect({ blendFunction = defaults === null || defaults === void 0 ? void 0 : defaults.blendFunction, opacity = defaults === null || defaults === void 0 ? void 0 : defaults.opacity, ...props }, ref) {
16
- let Component = components.get(effect);
17
- if (!Component) {
18
- const key = `@react-three/postprocessing/${effect.name}-${i++}`;
19
- extend({ [key]: effect });
20
- components.set(effect, (Component = key));
21
- }
22
- const camera = useThree((state) => state.camera);
23
- const args = React.useMemo(() => { var _a, _b; return [...((_a = defaults === null || defaults === void 0 ? void 0 : defaults.args) !== null && _a !== void 0 ? _a : []), ...((_b = props.args) !== null && _b !== void 0 ? _b : [{ ...defaults, ...props }])]; },
24
- // eslint-disable-next-line react-hooks/exhaustive-deps
25
- [JSON.stringify(props)]);
26
- return (jsx(Component, { camera: camera, "blendMode-blendFunction": blendFunction, "blendMode-opacity": opacity, ...props, ref: ref, args: args }));
27
- });
28
- const useVector2 = (props, key) => {
29
- const value = props[key];
30
- return React.useMemo(() => {
31
- if (typeof value === 'number')
32
- return new THREE.Vector2(value, value);
33
- else if (value)
34
- return new THREE.Vector2(...value);
35
- else
36
- return new THREE.Vector2();
37
- }, [value]);
38
- };
39
-
40
- const Bloom = wrapEffect(BloomEffect, {
41
- blendFunction: BlendFunction.ADD,
42
- });
43
-
44
- const BrightnessContrast = wrapEffect(BrightnessContrastEffect);
45
-
46
- const ChromaticAberration = forwardRef(function ChromaticAberration(props, ref) {
47
- const offset = useVector2(props, 'offset');
48
- const effect = useMemo(() => new ChromaticAberrationEffect({ ...props, offset }), [offset, props]);
49
- return jsx("primitive", { ref: ref, object: effect, dispose: null });
50
- });
51
-
52
- const ColorAverage = forwardRef(function ColorAverage({ blendFunction = BlendFunction.NORMAL }, ref) {
53
- /** Because ColorAverage blendFunction is not an object but a number, we have to define a custom prop "blendFunction" */
54
- const effect = useMemo(() => new ColorAverageEffect(blendFunction), [blendFunction]);
55
- return jsx("primitive", { ref: ref, object: effect, dispose: null });
56
- });
57
-
58
- const ColorDepth = wrapEffect(ColorDepthEffect);
59
-
60
- const Depth = wrapEffect(DepthEffect);
61
10
 
62
11
  const EffectComposerContext = createContext(null);
63
12
  const EffectComposer = React.memo(forwardRef(({ children, camera: _camera, scene: _scene, resolutionScale, enabled = true, renderPriority = 1, autoClear = true, depthBuffer, disableNormalPass, stencilBuffer, multisampling = 8, frameBufferType = HalfFloatType, }, ref) => {
@@ -160,6 +109,137 @@ const DepthOfField = forwardRef(function DepthOfField({ target, depthTexture, ..
160
109
  return jsx("primitive", { ref: ref, object: effect, dispose: null });
161
110
  });
162
111
 
112
+ const Autofocus = forwardRef(({ target = undefined, mouse: followMouse = false, debug = undefined, manual = false, smoothTime = 0.25, ...props }, fref) => {
113
+ const dofRef = useRef(null);
114
+ const hitpointRef = useRef(null);
115
+ const targetRef = useRef(null);
116
+ const scene = useThree(({ scene }) => scene);
117
+ const pointer = useThree(({ pointer }) => pointer);
118
+ const { composer, camera } = useContext(EffectComposerContext);
119
+ // see: https://codesandbox.io/s/depthpickingpass-x130hg
120
+ const [depthPickingPass] = useState(() => new DepthPickingPass());
121
+ const [copyPass] = useState(() => new CopyPass());
122
+ useEffect(() => {
123
+ composer.addPass(depthPickingPass);
124
+ composer.addPass(copyPass);
125
+ return () => {
126
+ composer.removePass(depthPickingPass);
127
+ composer.removePass(copyPass);
128
+ };
129
+ }, [composer, depthPickingPass, copyPass]);
130
+ useEffect(() => {
131
+ return () => {
132
+ depthPickingPass.dispose();
133
+ copyPass.dispose();
134
+ };
135
+ }, [depthPickingPass, copyPass]);
136
+ const [hitpoint] = useState(() => new THREE.Vector3(0, 0, 0));
137
+ const [ndc] = useState(() => new THREE.Vector3(0, 0, 0));
138
+ const getHit = useCallback(async (x, y) => {
139
+ ndc.x = x;
140
+ ndc.y = y;
141
+ ndc.z = await depthPickingPass.readDepth(ndc);
142
+ ndc.z = ndc.z * 2.0 - 1.0;
143
+ const hit = 1 - ndc.z > 0.0000001; // it is missed if ndc.z is close to 1
144
+ return hit ? ndc.unproject(camera) : false;
145
+ }, [ndc, depthPickingPass, camera]);
146
+ const update = useCallback(async (delta, updateTarget = true) => {
147
+ var _a;
148
+ // Update hitpoint
149
+ if (target) {
150
+ hitpoint.set(...target);
151
+ }
152
+ else {
153
+ const { x, y } = followMouse ? pointer : { x: 0, y: 0 };
154
+ const hit = await getHit(x, y);
155
+ if (hit)
156
+ hitpoint.copy(hit);
157
+ }
158
+ // Update target
159
+ if (updateTarget && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
160
+ if (smoothTime > 0 && delta > 0) {
161
+ easing.damp3(dofRef.current.target, hitpoint, smoothTime, delta);
162
+ }
163
+ else {
164
+ dofRef.current.target.copy(hitpoint);
165
+ }
166
+ }
167
+ }, [target, hitpoint, followMouse, getHit, smoothTime, pointer]);
168
+ useFrame(async (_, delta) => {
169
+ var _a;
170
+ if (!manual) {
171
+ update(delta);
172
+ }
173
+ if (hitpointRef.current) {
174
+ hitpointRef.current.position.copy(hitpoint);
175
+ }
176
+ if (targetRef.current && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
177
+ targetRef.current.position.copy(dofRef.current.target);
178
+ }
179
+ });
180
+ // Ref API
181
+ const api = useMemo(() => ({
182
+ dofRef,
183
+ hitpoint,
184
+ update,
185
+ }), [hitpoint, update]);
186
+ useImperativeHandle(fref, () => api, [api]);
187
+ return (jsxs(Fragment, { children: [debug
188
+ ? createPortal(jsxs(Fragment, { children: [jsxs("mesh", { ref: hitpointRef, children: [jsx("sphereGeometry", { args: [debug, 16, 16] }), jsx("meshBasicMaterial", { color: "#00ff00", opacity: 1, transparent: true, depthWrite: false })] }), jsxs("mesh", { ref: targetRef, children: [jsx("sphereGeometry", { args: [debug / 2, 16, 16] }), jsx("meshBasicMaterial", { color: "#00ff00", opacity: 0.5, transparent: true, depthWrite: false })] })] }), scene)
189
+ : null, jsx(DepthOfField, { ref: dofRef, ...props, target: hitpoint })] }));
190
+ });
191
+
192
+ const resolveRef = (ref) => typeof ref === 'object' && ref != null && 'current' in ref ? ref.current : ref;
193
+ let i = 0;
194
+ const components = new WeakMap();
195
+ const wrapEffect = (effect, defaults) =>
196
+ /* @__PURE__ */ React.forwardRef(function Effect({ blendFunction = defaults === null || defaults === void 0 ? void 0 : defaults.blendFunction, opacity = defaults === null || defaults === void 0 ? void 0 : defaults.opacity, ...props }, ref) {
197
+ let Component = components.get(effect);
198
+ if (!Component) {
199
+ const key = `@react-three/postprocessing/${effect.name}-${i++}`;
200
+ extend({ [key]: effect });
201
+ components.set(effect, (Component = key));
202
+ }
203
+ const camera = useThree((state) => state.camera);
204
+ const args = React.useMemo(() => { var _a, _b; return [...((_a = defaults === null || defaults === void 0 ? void 0 : defaults.args) !== null && _a !== void 0 ? _a : []), ...((_b = props.args) !== null && _b !== void 0 ? _b : [{ ...defaults, ...props }])]; },
205
+ // eslint-disable-next-line react-hooks/exhaustive-deps
206
+ [JSON.stringify(props)]);
207
+ return (jsx(Component, { camera: camera, "blendMode-blendFunction": blendFunction, "blendMode-opacity": opacity, ...props, ref: ref, args: args }));
208
+ });
209
+ const useVector2 = (props, key) => {
210
+ const value = props[key];
211
+ return React.useMemo(() => {
212
+ if (typeof value === 'number')
213
+ return new THREE.Vector2(value, value);
214
+ else if (value)
215
+ return new THREE.Vector2(...value);
216
+ else
217
+ return new THREE.Vector2();
218
+ }, [value]);
219
+ };
220
+
221
+ const Bloom = wrapEffect(BloomEffect, {
222
+ blendFunction: BlendFunction.ADD,
223
+ });
224
+
225
+ const BrightnessContrast = wrapEffect(BrightnessContrastEffect);
226
+
227
+ const ChromaticAberration = forwardRef(function ChromaticAberration(props, ref) {
228
+ const offset = useVector2(props, 'offset');
229
+ const effect = useMemo(() => new ChromaticAberrationEffect({ ...props, offset }), [offset, props]);
230
+ return jsx("primitive", { ref: ref, object: effect, dispose: null });
231
+ });
232
+
233
+ const ColorAverage = forwardRef(function ColorAverage({ blendFunction = BlendFunction.NORMAL }, ref) {
234
+ /** Because ColorAverage blendFunction is not an object but a number, we have to define a custom prop "blendFunction" */
235
+ const effect = useMemo(() => new ColorAverageEffect(blendFunction), [blendFunction]);
236
+ return jsx("primitive", { ref: ref, object: effect, dispose: null });
237
+ });
238
+
239
+ const ColorDepth = wrapEffect(ColorDepthEffect);
240
+
241
+ const Depth = wrapEffect(DepthEffect);
242
+
163
243
  const DotScreen = wrapEffect(DotScreenEffect);
164
244
 
165
245
  const Glitch = forwardRef(function Glitch({ active = true, ...props }, ref) {
@@ -532,77 +612,4 @@ const SSR = forwardRef(function SSR({ ENABLE_BLUR = true, USE_MRT = true, ...pro
532
612
  return jsx("primitive", { ref: ref, object: effect, ...props });
533
613
  });
534
614
 
535
- const Autofocus = forwardRef(({ target = undefined, mouse: followMouse = false, debug = undefined, manual = false, smoothTime = 0, ...props }, fref) => {
536
- const dofRef = useRef(null);
537
- const hitpointRef = useRef(null);
538
- const targetRef = useRef(null);
539
- const scene = useThree(({ scene }) => scene);
540
- const pointer = useThree(({ pointer }) => pointer);
541
- const { composer, camera } = useContext(EffectComposerContext);
542
- // see: https://codesandbox.io/s/depthpickingpass-x130hg
543
- const [depthPickingPass] = useState(new DepthPickingPass());
544
- const [copyPass] = useState(new CopyPass());
545
- useEffect(() => {
546
- composer.addPass(depthPickingPass);
547
- composer.addPass(copyPass);
548
- return () => {
549
- composer.removePass(depthPickingPass);
550
- composer.removePass(copyPass);
551
- };
552
- }, [composer, depthPickingPass, copyPass]);
553
- const [hitpoint] = useState(new THREE.Vector3(0, 0, 0));
554
- const [ndc] = useState(new THREE.Vector3(0, 0, 0));
555
- const getHit = useCallback(async (x, y) => {
556
- ndc.x = x;
557
- ndc.y = y;
558
- ndc.z = await depthPickingPass.readDepth(ndc);
559
- ndc.z = ndc.z * 2.0 - 1.0;
560
- const hit = 1 - ndc.z > 0.0000001; // it is missed if ndc.z is close to 1
561
- return hit ? ndc.unproject(camera) : false;
562
- }, [ndc, depthPickingPass, camera]);
563
- const update = useCallback(async (delta, updateTarget = true) => {
564
- var _a;
565
- // Update hitpoint
566
- if (target) {
567
- hitpoint.set(...target);
568
- }
569
- else {
570
- const { x, y } = followMouse ? pointer : { x: 0, y: 0 };
571
- const hit = await getHit(x, y);
572
- if (hit)
573
- hitpoint.copy(hit);
574
- }
575
- // Update target
576
- if (updateTarget && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
577
- if (smoothTime > 0 && delta > 0) {
578
- easing.damp3(dofRef.current.target, hitpoint, smoothTime, delta);
579
- }
580
- else {
581
- dofRef.current.target.copy(hitpoint);
582
- }
583
- }
584
- }, [target, hitpoint, followMouse, getHit, smoothTime, pointer]);
585
- useFrame(async (_, delta) => {
586
- var _a;
587
- if (!manual) {
588
- update(delta);
589
- }
590
- if (hitpointRef.current) {
591
- hitpointRef.current.position.copy(hitpoint);
592
- }
593
- if (targetRef.current && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
594
- targetRef.current.position.copy(dofRef.current.target);
595
- }
596
- });
597
- // Ref API
598
- useImperativeHandle(fref, () => ({
599
- dofRef,
600
- hitpoint,
601
- update,
602
- }), [hitpoint, update]);
603
- return (jsxs(Fragment, { children: [debug
604
- ? createPortal(jsxs(Fragment, { children: [jsxs("mesh", { ref: hitpointRef, children: [jsx("sphereGeometry", { args: [debug, 16, 16] }), jsx("meshBasicMaterial", { color: "#00ff00", opacity: 1, transparent: true, depthWrite: false })] }), jsxs("mesh", { ref: targetRef, children: [jsx("sphereGeometry", { args: [debug / 2, 16, 16] }), jsx("meshBasicMaterial", { color: "#00ff00", opacity: 0.5, transparent: true, depthWrite: false })] })] }), scene)
605
- : null, jsx(DepthOfField, { ref: dofRef, ...props, target: hitpoint })] }));
606
- });
607
-
608
615
  export { Autofocus, Bloom, BrightnessContrast, ChromaticAberration, ColorAverage, ColorDepth, Depth, DepthOfField, DotScreen, EffectComposer, EffectComposerContext, Glitch, GodRays, Grid, HueSaturation, LUT, Noise, Outline, Pixelation, SMAA, SSAO, SSR, Scanline, Select, Selection, SelectiveBloom, Sepia, ShockWave, Texture, TiltShift, TiltShift2, TiltShiftEffect, ToneMapping, Vignette, resolveRef, selectionContext, useVector2, wrapEffect };
package/dist/util.d.ts CHANGED
@@ -9,5 +9,5 @@ export type EffectProps<T extends EffectConstructor> = ReactThreeFiber.Node<T ex
9
9
  blendFunction?: BlendFunction;
10
10
  opacity?: number;
11
11
  };
12
- export declare const wrapEffect: <T extends EffectConstructor, P extends EffectProps<T> = EffectProps<T>>(effect: T, defaults?: P | undefined) => React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<T>>;
12
+ export declare const wrapEffect: <T extends EffectConstructor>(effect: T, defaults?: EffectProps<T> | undefined) => React.ForwardRefExoticComponent<React.PropsWithoutRef<EffectProps<T>> & React.RefAttributes<T>>;
13
13
  export declare const useVector2: (props: Record<string, unknown>, key: string) => THREE.Vector2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/postprocessing",
3
- "version": "2.11.0",
3
+ "version": "2.11.2",
4
4
  "description": "postprocessing wrapper for React and @react-three/fiber",
5
5
  "keywords": [
6
6
  "postprocessing",
@@ -42,7 +42,9 @@
42
42
  "test": "echo no tests yet",
43
43
  "typecheck": "tsc --noEmit --emitDeclarationOnly false --strict --jsx react",
44
44
  "typegen": "tsc --emitDeclarationOnly || true",
45
- "release": "semantic-release"
45
+ "release": "semantic-release",
46
+ "storybook": "storybook dev -p 6006",
47
+ "build-storybook": "storybook build"
46
48
  },
47
49
  "dependencies": {
48
50
  "maath": "^0.5.3",
@@ -51,10 +53,19 @@
51
53
  "three-stdlib": "^2.21.10"
52
54
  },
53
55
  "devDependencies": {
56
+ "@react-three/drei": "^9.68.2",
54
57
  "@react-three/fiber": "^8.13.0",
58
+ "@rollup/plugin-babel": "^6.0.3",
55
59
  "@rollup/plugin-commonjs": "^24.1.0",
56
60
  "@rollup/plugin-node-resolve": "^15.0.2",
57
61
  "@rollup/plugin-typescript": "^11.1.0",
62
+ "@storybook/addon-essentials": "^7.0.10",
63
+ "@storybook/addon-interactions": "^7.0.10",
64
+ "@storybook/addon-links": "^7.0.10",
65
+ "@storybook/blocks": "^7.0.10",
66
+ "@storybook/react": "^7.0.10",
67
+ "@storybook/react-vite": "^7.0.11",
68
+ "@storybook/testing-library": "^0.0.14-next.2",
58
69
  "@types/react": "^18.2.0",
59
70
  "@types/react-dom": "^18.2.1",
60
71
  "@types/three": "^0.150.2",
@@ -67,6 +78,7 @@
67
78
  "eslint-plugin-prettier": "^4.2.1",
68
79
  "eslint-plugin-react": "^7.32.2",
69
80
  "eslint-plugin-react-hooks": "^4.6.0",
81
+ "eslint-plugin-storybook": "^0.6.12",
70
82
  "husky": "^8.0.3",
71
83
  "lint-staged": "^13.2.2",
72
84
  "prettier": "^2.8.8",
@@ -76,8 +88,10 @@
76
88
  "rollup": "^3.21.0",
77
89
  "rollup-plugin-filesize": "^10.0.0",
78
90
  "semantic-release": "^21.0.2",
91
+ "storybook": "^7.0.10",
79
92
  "three": "^0.151.3",
80
- "typescript": "^5.0.4"
93
+ "typescript": "^5.0.4",
94
+ "vite": "^4.3.5"
81
95
  },
82
96
  "peerDependencies": {
83
97
  "@react-three/fiber": ">=8.0",
@@ -1,43 +0,0 @@
1
- import * as THREE from 'three';
2
- import React, { RefObject } from 'react';
3
- import { DepthOfFieldEffect } from 'postprocessing';
4
- import { DepthOfField } from './index';
5
- export type AutofocusProps = typeof DepthOfField & {
6
- target?: [number, number, number];
7
- mouse?: boolean;
8
- debug?: number;
9
- manual?: boolean;
10
- smoothTime?: number;
11
- };
12
- export type AutofocusApi = {
13
- dofRef: RefObject<DepthOfFieldEffect>;
14
- hitpoint: THREE.Vector3;
15
- update: (delta: number, updateTarget: boolean) => void;
16
- };
17
- export declare const Autofocus: React.ForwardRefExoticComponent<React.ForwardRefExoticComponent<{
18
- blendFunction?: import("postprocessing").BlendFunction | undefined;
19
- worldFocusDistance?: number | undefined;
20
- worldFocusRange?: number | undefined;
21
- focusDistance?: number | undefined;
22
- focalLength?: number | undefined;
23
- focusRange?: number | undefined;
24
- bokehScale?: number | undefined;
25
- resolutionScale?: number | undefined;
26
- resolutionX?: number | undefined;
27
- resolutionY?: number | undefined;
28
- width?: number | undefined;
29
- height?: number | undefined;
30
- } & Partial<{
31
- target: import("@react-three/fiber").Vector3;
32
- depthTexture: {
33
- texture: THREE.Texture;
34
- packing: number;
35
- };
36
- blur: number;
37
- }> & React.RefAttributes<DepthOfFieldEffect>> & {
38
- target?: [number, number, number] | undefined;
39
- mouse?: boolean | undefined;
40
- debug?: number | undefined;
41
- manual?: boolean | undefined;
42
- smoothTime?: number | undefined;
43
- } & React.RefAttributes<AutofocusApi>>;