@react-three/postprocessing 2.9.2 → 2.10.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>>;
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);
@@ -549,6 +550,80 @@ const SSR = React.forwardRef(function SSR({ ENABLE_BLUR = true, USE_MRT = true,
549
550
  return jsxRuntime.jsx("primitive", { ref: ref, object: effect, ...props });
550
551
  });
551
552
 
553
+ const Autofocus = React.forwardRef(({ target = undefined, mouse: followMouse = false, debug = undefined, manual = false, smoothTime = 0, ...props }, fref) => {
554
+ const dofRef = React.useRef(null);
555
+ const hitpointRef = React.useRef(null);
556
+ const targetRef = React.useRef(null);
557
+ const scene = fiber.useThree(({ scene }) => scene);
558
+ const pointer = fiber.useThree(({ pointer }) => pointer);
559
+ const { composer, camera } = React.useContext(EffectComposerContext);
560
+ // see: https://codesandbox.io/s/depthpickingpass-x130hg
561
+ const [depthPickingPass] = React.useState(new postprocessing.DepthPickingPass());
562
+ const [copyPass] = React.useState(new postprocessing.CopyPass());
563
+ React.useEffect(() => {
564
+ composer.addPass(depthPickingPass);
565
+ composer.addPass(copyPass);
566
+ return () => {
567
+ composer.removePass(depthPickingPass);
568
+ composer.removePass(copyPass);
569
+ };
570
+ }, [composer, depthPickingPass, copyPass]);
571
+ const [hitpoint] = React.useState(new THREE__namespace.Vector3(0, 0, 0));
572
+ const [ndc] = React.useState(new THREE__namespace.Vector3(0, 0, 0));
573
+ const getHit = React.useCallback(async (x, y) => {
574
+ ndc.x = x;
575
+ ndc.y = y;
576
+ ndc.z = await depthPickingPass.readDepth(ndc);
577
+ ndc.z = ndc.z * 2.0 - 1.0;
578
+ const hit = 1 - ndc.z > 0.0000001; // it is missed if ndc.z is close to 1
579
+ return hit ? ndc.unproject(camera) : false;
580
+ }, [ndc, depthPickingPass, camera]);
581
+ const update = React.useCallback(async (delta, updateTarget = true) => {
582
+ var _a;
583
+ // Update hitpoint
584
+ if (target) {
585
+ hitpoint.set(...target);
586
+ }
587
+ else {
588
+ const { x, y } = followMouse ? pointer : { x: 0, y: 0 };
589
+ const hit = await getHit(x, y);
590
+ if (hit)
591
+ hitpoint.copy(hit);
592
+ }
593
+ // Update target
594
+ if (updateTarget && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
595
+ if (smoothTime > 0 && delta > 0) {
596
+ maath.easing.damp3(dofRef.current.target, hitpoint, smoothTime, delta);
597
+ }
598
+ else {
599
+ dofRef.current.target.copy(hitpoint);
600
+ }
601
+ }
602
+ }, [target, hitpoint, followMouse, getHit, smoothTime, pointer]);
603
+ fiber.useFrame(async (_, delta) => {
604
+ var _a;
605
+ if (!manual) {
606
+ update(delta);
607
+ }
608
+ if (hitpointRef.current) {
609
+ hitpointRef.current.position.copy(hitpoint);
610
+ }
611
+ if (targetRef.current && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
612
+ targetRef.current.position.copy(dofRef.current.target);
613
+ }
614
+ });
615
+ // Ref API
616
+ React.useImperativeHandle(fref, () => ({
617
+ dofRef,
618
+ hitpoint,
619
+ update,
620
+ }), [hitpoint, update]);
621
+ return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [debug
622
+ ? 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)
623
+ : null, jsxRuntime.jsx(DepthOfField, { ref: dofRef, ...props, target: hitpoint })] }));
624
+ });
625
+
626
+ exports.Autofocus = Autofocus;
552
627
  exports.Bloom = Bloom;
553
628
  exports.BrightnessContrast = BrightnessContrast;
554
629
  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;
@@ -529,4 +530,77 @@ const SSR = forwardRef(function SSR({ ENABLE_BLUR = true, USE_MRT = true, ...pro
529
530
  return jsx("primitive", { ref: ref, object: effect, ...props });
530
531
  });
531
532
 
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 };
533
+ const Autofocus = forwardRef(({ target = undefined, mouse: followMouse = false, debug = undefined, manual = false, smoothTime = 0, ...props }, fref) => {
534
+ const dofRef = useRef(null);
535
+ const hitpointRef = useRef(null);
536
+ const targetRef = useRef(null);
537
+ const scene = useThree(({ scene }) => scene);
538
+ const pointer = useThree(({ pointer }) => pointer);
539
+ const { composer, camera } = useContext(EffectComposerContext);
540
+ // see: https://codesandbox.io/s/depthpickingpass-x130hg
541
+ const [depthPickingPass] = useState(new DepthPickingPass());
542
+ const [copyPass] = useState(new CopyPass());
543
+ useEffect(() => {
544
+ composer.addPass(depthPickingPass);
545
+ composer.addPass(copyPass);
546
+ return () => {
547
+ composer.removePass(depthPickingPass);
548
+ composer.removePass(copyPass);
549
+ };
550
+ }, [composer, depthPickingPass, copyPass]);
551
+ const [hitpoint] = useState(new THREE.Vector3(0, 0, 0));
552
+ const [ndc] = useState(new THREE.Vector3(0, 0, 0));
553
+ const getHit = useCallback(async (x, y) => {
554
+ ndc.x = x;
555
+ ndc.y = y;
556
+ ndc.z = await depthPickingPass.readDepth(ndc);
557
+ ndc.z = ndc.z * 2.0 - 1.0;
558
+ const hit = 1 - ndc.z > 0.0000001; // it is missed if ndc.z is close to 1
559
+ return hit ? ndc.unproject(camera) : false;
560
+ }, [ndc, depthPickingPass, camera]);
561
+ const update = useCallback(async (delta, updateTarget = true) => {
562
+ var _a;
563
+ // Update hitpoint
564
+ if (target) {
565
+ hitpoint.set(...target);
566
+ }
567
+ else {
568
+ const { x, y } = followMouse ? pointer : { x: 0, y: 0 };
569
+ const hit = await getHit(x, y);
570
+ if (hit)
571
+ hitpoint.copy(hit);
572
+ }
573
+ // Update target
574
+ if (updateTarget && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
575
+ if (smoothTime > 0 && delta > 0) {
576
+ easing.damp3(dofRef.current.target, hitpoint, smoothTime, delta);
577
+ }
578
+ else {
579
+ dofRef.current.target.copy(hitpoint);
580
+ }
581
+ }
582
+ }, [target, hitpoint, followMouse, getHit, smoothTime, pointer]);
583
+ useFrame(async (_, delta) => {
584
+ var _a;
585
+ if (!manual) {
586
+ update(delta);
587
+ }
588
+ if (hitpointRef.current) {
589
+ hitpointRef.current.position.copy(hitpoint);
590
+ }
591
+ if (targetRef.current && ((_a = dofRef.current) === null || _a === void 0 ? void 0 : _a.target)) {
592
+ targetRef.current.position.copy(dofRef.current.target);
593
+ }
594
+ });
595
+ // Ref API
596
+ useImperativeHandle(fref, () => ({
597
+ dofRef,
598
+ hitpoint,
599
+ update,
600
+ }), [hitpoint, update]);
601
+ return (jsxs(Fragment, { children: [debug
602
+ ? 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)
603
+ : null, jsx(DepthOfField, { ref: dofRef, ...props, target: hitpoint })] }));
604
+ });
605
+
606
+ 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.10.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"