@react-three/postprocessing 2.9.2 → 2.11.0

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,43 @@
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>>;
@@ -17,5 +17,5 @@ export declare const GodRays: React.ForwardRefExoticComponent<{
17
17
  kernelSize?: import("postprocessing").KernelSize | undefined;
18
18
  blur?: boolean | undefined;
19
19
  } & {
20
- sun: Mesh | Points;
20
+ sun: Mesh | Points | React.MutableRefObject<Mesh | Points>;
21
21
  } & React.RefAttributes<GodRaysEffect>>;
package/dist/index.cjs CHANGED
@@ -7,6 +7,7 @@ var THREE = require('three');
7
7
  var fiber = require('@react-three/fiber');
8
8
  var threeStdlib = require('three-stdlib');
9
9
  var screenSpaceReflections = require('screen-space-reflections');
10
+ var maath = require('maath');
10
11
 
11
12
  function _interopNamespaceDefault(e) {
12
13
  var n = Object.create(null);
@@ -196,7 +197,9 @@ const Glitch = React.forwardRef(function Glitch({ active = true, ...props }, ref
196
197
 
197
198
  const GodRays = React.forwardRef(function GodRays(props, ref) {
198
199
  const { camera } = React.useContext(EffectComposerContext);
199
- const effect = React.useMemo(() => new postprocessing.GodRaysEffect(camera, props.sun, props), [camera, props]);
200
+ const effect = React.useMemo(() => new postprocessing.GodRaysEffect(camera, resolveRef(props.sun), props), [camera, props]);
201
+ // @ts-ignore v6.30.2 https://github.com/pmndrs/postprocessing/pull/470/commits/091ef6f9516ca02efa7576305afbecf1ce8323ae
202
+ React.useLayoutEffect(() => void (effect.lightSource = resolveRef(props.sun)), [effect, props.sun]);
200
203
  return jsxRuntime.jsx("primitive", { ref: ref, object: effect, dispose: null });
201
204
  });
202
205
 
@@ -549,6 +552,80 @@ const SSR = React.forwardRef(function SSR({ ENABLE_BLUR = true, USE_MRT = true,
549
552
  return jsxRuntime.jsx("primitive", { ref: ref, object: effect, ...props });
550
553
  });
551
554
 
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
+ exports.Autofocus = Autofocus;
552
629
  exports.Bloom = Bloom;
553
630
  exports.BrightnessContrast = BrightnessContrast;
554
631
  exports.ChromaticAberration = ChromaticAberration;
package/dist/index.d.ts CHANGED
@@ -27,5 +27,6 @@ export * from './effects/TiltShift';
27
27
  export * from './effects/TiltShift2';
28
28
  export * from './effects/SSR';
29
29
  export * from './Selection';
30
+ export * from './Autofocus';
30
31
  export * from './EffectComposer';
31
32
  export * from './util';
package/dist/index.js CHANGED
@@ -1,11 +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 } from 'postprocessing';
2
- import { jsx } from 'react/jsx-runtime';
3
- import React, { forwardRef, useMemo, createContext, useEffect, useRef, useLayoutEffect, useImperativeHandle, useContext, useState } from 'react';
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
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
+ import React, { forwardRef, useMemo, createContext, useEffect, useRef, useLayoutEffect, useImperativeHandle, useContext, useState, useCallback } from 'react';
4
4
  import * as THREE from 'three';
5
5
  import { HalfFloatType, Vector3, TextureLoader, sRGBEncoding, RepeatWrapping, Uniform } from 'three';
6
- import { extend, useThree, useFrame, useInstanceHandle, useLoader } from '@react-three/fiber';
6
+ import { extend, useThree, useFrame, useInstanceHandle, useLoader, createPortal } from '@react-three/fiber';
7
7
  import { isWebGL2Available } from 'three-stdlib';
8
8
  import { SSREffect } from 'screen-space-reflections';
9
+ import { easing } from 'maath';
9
10
 
10
11
  const resolveRef = (ref) => typeof ref === 'object' && ref != null && 'current' in ref ? ref.current : ref;
11
12
  let i = 0;
@@ -176,7 +177,9 @@ const Glitch = forwardRef(function Glitch({ active = true, ...props }, ref) {
176
177
 
177
178
  const GodRays = forwardRef(function GodRays(props, ref) {
178
179
  const { camera } = useContext(EffectComposerContext);
179
- const effect = useMemo(() => new GodRaysEffect(camera, props.sun, props), [camera, props]);
180
+ const effect = useMemo(() => new GodRaysEffect(camera, resolveRef(props.sun), props), [camera, props]);
181
+ // @ts-ignore v6.30.2 https://github.com/pmndrs/postprocessing/pull/470/commits/091ef6f9516ca02efa7576305afbecf1ce8323ae
182
+ useLayoutEffect(() => void (effect.lightSource = resolveRef(props.sun)), [effect, props.sun]);
180
183
  return jsx("primitive", { ref: ref, object: effect, dispose: null });
181
184
  });
182
185
 
@@ -529,4 +532,77 @@ const SSR = forwardRef(function SSR({ ENABLE_BLUR = true, USE_MRT = true, ...pro
529
532
  return jsx("primitive", { ref: ref, object: effect, ...props });
530
533
  });
531
534
 
532
- export { 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 };
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
+ 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/postprocessing",
3
- "version": "2.9.2",
3
+ "version": "2.11.0",
4
4
  "description": "postprocessing wrapper for React and @react-three/fiber",
5
5
  "keywords": [
6
6
  "postprocessing",
@@ -45,6 +45,7 @@
45
45
  "release": "semantic-release"
46
46
  },
47
47
  "dependencies": {
48
+ "maath": "^0.5.3",
48
49
  "postprocessing": "^6.30.2",
49
50
  "screen-space-reflections": "2.5.0",
50
51
  "three-stdlib": "^2.21.10"