@react-three/drei 9.117.2 → 9.118.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.
package/core/Text3D.d.ts CHANGED
@@ -2,7 +2,7 @@ import * as React from 'react';
2
2
  import * as THREE from 'three';
3
3
  import { MeshProps, Node } from '@react-three/fiber';
4
4
  import { TextGeometryParameters } from 'three-stdlib';
5
- import { useFont } from './useFont';
5
+ import { FontData } from './useFont';
6
6
  import { ForwardRefComponent } from '../helpers/ts-utils';
7
7
  declare global {
8
8
  namespace JSX {
@@ -12,7 +12,7 @@ declare global {
12
12
  }
13
13
  }
14
14
  type Text3DProps = {
15
- font: Parameters<typeof useFont>[0];
15
+ font: FontData | string;
16
16
  bevelSegments?: number;
17
17
  smooth?: number;
18
18
  } & Omit<TextGeometryParameters, 'font'> & MeshProps;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("three"),r=require("@react-three/fiber"),n=require("suspend-react"),o=require("hls.js");function u(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var s,c,i=u(e),l=u(t);const a="undefined"!=typeof window&&"function"==typeof(null==(s=window.document)?void 0:s.createElement)&&"string"==typeof(null==(c=window.navigator)?void 0:c.userAgent);let d=null;function f(t,{unsuspend:s="loadedmetadata",start:c=!0,hls:i={},crossOrigin:f="anonymous",muted:p=!0,loop:g=!0,playsInline:m=!0,...v}={}){const y=r.useThree((e=>e.gl)),w=e.useRef(null),h=n.suspend((()=>new Promise((async e=>{let r,n;"string"==typeof t?r=t:n=t;const c=Object.assign(document.createElement("video"),{src:r,srcObject:n,crossOrigin:f,loop:g,muted:p,playsInline:m,...v});if(r&&a&&r.endsWith(".m3u8")){const e=w.current=await async function(...e){var t;null!==(t=d)&&void 0!==t||(d=await Promise.resolve().then((function(){return u(require("hls.js"))})));const r=d.default;return r.isSupported()?new r(...e):null}(i);e&&(e.on(o.Events.MEDIA_ATTACHED,(()=>{e.loadSource(r)})),e.attachMedia(c))}const h=new l.VideoTexture(c);"colorSpace"in h?h.colorSpace=y.outputColorSpace:h.encoding=y.outputEncoding,c.addEventListener(s,(()=>e(h)))}))),[t]);return e.useEffect((()=>(c&&h.image.play(),()=>{w.current&&(w.current.destroy(),w.current=null)})),[h,c]),h}exports.VideoTexture=({children:t,src:r,...n})=>{const o=f(r,n);return e.useEffect((()=>()=>{o.dispose()}),[o]),i.createElement(i.Fragment,null,null==t?void 0:t(o))},exports.useVideoTexture=f;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("three"),r=require("@react-three/fiber"),n=require("suspend-react"),o=require("hls.js");function u(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var s,c,a=u(e),i=u(t);const l="undefined"!=typeof window&&"function"==typeof(null==(s=window.document)?void 0:s.createElement)&&"string"==typeof(null==(c=window.navigator)?void 0:c.userAgent);let d=null;function f(t,{unsuspend:s="loadedmetadata",start:c=!0,hls:a={},crossOrigin:f="anonymous",muted:p=!0,loop:v=!0,playsInline:g=!0,onVideoFrame:y,...b}={}){const w=r.useThree((e=>e.gl)),E=e.useRef(null),h=n.suspend((()=>new Promise((async e=>{let r,n;"string"==typeof t?r=t:n=t;const c=Object.assign(document.createElement("video"),{src:r,srcObject:n,crossOrigin:f,loop:v,muted:p,playsInline:g,...b});if(r&&l&&r.endsWith(".m3u8")){const e=E.current=await async function(...e){var t;null!==(t=d)&&void 0!==t||(d=await Promise.resolve().then((function(){return u(require("hls.js"))})));const r=d.default;return r.isSupported()?new r(...e):null}(a);e&&(e.on(o.Events.MEDIA_ATTACHED,(()=>{e.loadSource(r)})),e.attachMedia(c))}const m=new i.VideoTexture(c);"colorSpace"in m?m.colorSpace=w.outputColorSpace:m.encoding=w.outputEncoding,c.addEventListener(s,(()=>e(m)))}))),[t]),O=h.source.data;return m(O,y),e.useEffect((()=>(c&&h.image.play(),()=>{E.current&&(E.current.destroy(),E.current=null)})),[h,c]),h}const p=e.forwardRef((({children:t,src:r,...n},o)=>{const u=f(r,n);return e.useEffect((()=>()=>{u.dispose()}),[u]),e.useImperativeHandle(o,(()=>u),[u]),a.createElement(a.Fragment,null,null==t?void 0:t(u))})),m=(t,r)=>{e.useEffect((()=>{if(!r)return;if(!t.requestVideoFrameCallback)return;let e;const n=(...o)=>{r(...o),e=t.requestVideoFrameCallback(n)};return t.requestVideoFrameCallback(n),()=>t.cancelVideoFrameCallback(e)}),[t,r])};exports.VideoTexture=p,exports.useVideoTexture=f;
@@ -2,14 +2,25 @@ import * as React from 'react';
2
2
  import * as THREE from 'three';
3
3
  import { type default as Hls } from 'hls.js';
4
4
  declare function getHls(...args: ConstructorParameters<typeof Hls>): Promise<Hls | null>;
5
- export declare function useVideoTexture(srcOrSrcObject: HTMLVideoElement['src' | 'srcObject'], { unsuspend, start, hls: hlsConfig, crossOrigin, muted, loop, playsInline, ...videoProps }?: {
5
+ export declare function useVideoTexture(srcOrSrcObject: HTMLVideoElement['src' | 'srcObject'], { unsuspend, start, hls: hlsConfig, crossOrigin, muted, loop, playsInline, onVideoFrame, ...videoProps }?: {
6
6
  unsuspend?: keyof HTMLVideoElementEventMap;
7
7
  start?: boolean;
8
8
  hls?: Parameters<typeof getHls>[0];
9
+ onVideoFrame?: VideoFrameRequestCallback;
9
10
  } & Partial<Omit<HTMLVideoElement, 'children' | 'src' | 'srcObject'>>): THREE.VideoTexture;
10
- type UseVideoTexture = Parameters<typeof useVideoTexture>;
11
- export declare const VideoTexture: ({ children, src, ...config }: {
12
- children?: (texture: ReturnType<typeof useVideoTexture>) => React.ReactNode;
13
- src: UseVideoTexture[0];
14
- } & UseVideoTexture[1]) => React.JSX.Element;
11
+ type UseVideoTextureParams = Parameters<typeof useVideoTexture>;
12
+ type VideoTexture = ReturnType<typeof useVideoTexture>;
13
+ export type VideoTextureProps = {
14
+ children?: (texture: VideoTexture) => React.ReactNode;
15
+ src: UseVideoTextureParams[0];
16
+ } & UseVideoTextureParams[1];
17
+ export declare const VideoTexture: React.ForwardRefExoticComponent<{
18
+ children?: (texture: VideoTexture) => React.ReactNode;
19
+ src: UseVideoTextureParams[0];
20
+ } & {
21
+ unsuspend?: keyof HTMLVideoElementEventMap;
22
+ start?: boolean;
23
+ hls?: Parameters<typeof getHls>[0];
24
+ onVideoFrame?: VideoFrameRequestCallback;
25
+ } & Partial<Omit<HTMLVideoElement, "children" | "src" | "srcObject">> & React.RefAttributes<THREE.VideoTexture>>;
15
26
  export {};
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { useRef, useEffect } from 'react';
2
+ import { useRef, useEffect, forwardRef, useImperativeHandle } from 'react';
3
3
  import * as THREE from 'three';
4
4
  import { useThree } from '@react-three/fiber';
5
5
  import { suspend } from 'suspend-react';
@@ -25,6 +25,7 @@ function useVideoTexture(srcOrSrcObject, {
25
25
  muted = true,
26
26
  loop = true,
27
27
  playsInline = true,
28
+ onVideoFrame,
28
29
  ...videoProps
29
30
  } = {}) {
30
31
  const gl = useThree(state => state.gl);
@@ -59,6 +60,8 @@ function useVideoTexture(srcOrSrcObject, {
59
60
  if ('colorSpace' in texture) texture.colorSpace = gl.outputColorSpace;else texture.encoding = gl.outputEncoding;
60
61
  video.addEventListener(unsuspend, () => res(texture));
61
62
  }), [srcOrSrcObject]);
63
+ const video = texture.source.data;
64
+ useVideoFrame(video, onVideoFrame);
62
65
  useEffect(() => {
63
66
  start && texture.image.play();
64
67
  return () => {
@@ -71,18 +74,38 @@ function useVideoTexture(srcOrSrcObject, {
71
74
  return texture;
72
75
  }
73
76
 
77
+ //
78
+ // VideoTexture
74
79
  //
75
80
 
76
- const VideoTexture = ({
81
+ const VideoTexture = /* @__PURE__ */forwardRef(({
77
82
  children,
78
83
  src,
79
84
  ...config
80
- }) => {
81
- const ret = useVideoTexture(src, config);
85
+ }, fref) => {
86
+ const texture = useVideoTexture(src, config);
87
+ useEffect(() => {
88
+ return () => void texture.dispose();
89
+ }, [texture]);
90
+ useImperativeHandle(fref, () => texture, [texture]); // expose texture through ref
91
+
92
+ return /*#__PURE__*/React.createElement(React.Fragment, null, children == null ? void 0 : children(texture));
93
+ });
94
+
95
+ // rVFC hook
96
+
97
+ const useVideoFrame = (video, f) => {
82
98
  useEffect(() => {
83
- return () => void ret.dispose();
84
- }, [ret]);
85
- return /*#__PURE__*/React.createElement(React.Fragment, null, children == null ? void 0 : children(ret));
99
+ if (!f) return;
100
+ if (!video.requestVideoFrameCallback) return;
101
+ let handle;
102
+ const callback = (...args) => {
103
+ f(...args);
104
+ handle = video.requestVideoFrameCallback(callback);
105
+ };
106
+ video.requestVideoFrameCallback(callback);
107
+ return () => video.cancelVideoFrameCallback(handle);
108
+ }, [video, f]);
86
109
  };
87
110
 
88
111
  export { VideoTexture, useVideoTexture };
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("three-stdlib"),r=require("suspend-react");let n=null,t=null;async function a(r){if("string"==typeof r){var n;const u=await fetch(r);if(null!=(n=u.headers.get("Content-Type"))&&n.includes("application/json"))return await u.json();{const r=await u.arrayBuffer();return a=r,t||(t=new e.TTFLoader),t.parse(a)}}return r;var a}async function u(r){return function(r){return n||(n=new e.FontLoader),n.parse(r)}(await a(r))}function s(e){return r.suspend(u,[e])}s.preload=e=>r.preload(u,[e]),s.clear=e=>r.clear([e]),exports.useFont=s;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("three-stdlib"),r=require("suspend-react");let t=null;async function n(r){const n=await async function(e){return"string"==typeof e?await(await fetch(e)).json():e}(r);return a=n,t||(t=new e.FontLoader),t.parse(a);var a}function a(e){return r.suspend(n,[e])}a.preload=e=>r.preload(n,[e]),a.clear=e=>r.clear([e]),exports.useFont=a;
package/core/useFont.js CHANGED
@@ -1,37 +1,19 @@
1
- import { FontLoader, TTFLoader } from 'three-stdlib';
1
+ import { FontLoader } from 'three-stdlib';
2
2
  import { suspend, preload, clear } from 'suspend-react';
3
3
 
4
4
  let fontLoader = null;
5
- let ttfLoader = null;
5
+ async function loadFontData(font) {
6
+ return typeof font === 'string' ? await (await fetch(font)).json() : font;
7
+ }
6
8
  function parseFontData(fontData) {
7
9
  if (!fontLoader) {
8
10
  fontLoader = new FontLoader();
9
11
  }
10
12
  return fontLoader.parse(fontData);
11
13
  }
12
- function parseTtfArrayBuffer(ttfData) {
13
- if (!ttfLoader) {
14
- ttfLoader = new TTFLoader();
15
- }
16
- return ttfLoader.parse(ttfData);
17
- }
18
- async function loadFontData(font) {
19
- if (typeof font === 'string') {
20
- var _res$headers$get;
21
- const res = await fetch(font);
22
- if ((_res$headers$get = res.headers.get('Content-Type')) != null && _res$headers$get.includes('application/json')) {
23
- return await res.json();
24
- } else {
25
- const arrayBuffer = await res.arrayBuffer();
26
- return parseTtfArrayBuffer(arrayBuffer);
27
- }
28
- } else {
29
- return font;
30
- }
31
- }
32
14
  async function loader(font) {
33
- const fontData = await loadFontData(font);
34
- return parseFontData(fontData);
15
+ const data = await loadFontData(font);
16
+ return parseFontData(data);
35
17
  }
36
18
  function useFont(font) {
37
19
  return suspend(loader, [font]);