@react-three/drei 9.117.3 → 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/VideoTexture.cjs.js +1 -1
- package/core/VideoTexture.d.ts +17 -6
- package/core/VideoTexture.js +30 -7
- package/index.cjs.js +1 -1
- package/package.json +1 -1
package/core/VideoTexture.cjs.js
CHANGED
|
@@ -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,
|
|
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;
|
package/core/VideoTexture.d.ts
CHANGED
|
@@ -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
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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 {};
|
package/core/VideoTexture.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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 };
|