@react-three/drei 9.88.7 → 9.88.9
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/README.md
CHANGED
|
@@ -2467,8 +2467,8 @@ type Props = {
|
|
|
2467
2467
|
onLoopEnd?: Function
|
|
2468
2468
|
/** Event callback when each frame changes */
|
|
2469
2469
|
onFrame?: Function
|
|
2470
|
-
/** Control when the animation runs
|
|
2471
|
-
play?: boolean
|
|
2470
|
+
/** @deprecated Control when the animation runs*/
|
|
2471
|
+
play?: boolean
|
|
2472
2472
|
/** Control when the animation pauses */
|
|
2473
2473
|
pause?: boolean
|
|
2474
2474
|
/** Whether or not the Sprite should flip sides on the x-axis */
|
|
@@ -2477,6 +2477,8 @@ type Props = {
|
|
|
2477
2477
|
alphaTest?: number
|
|
2478
2478
|
/** Displays the texture on a SpriteGeometry always facing the camera, if set to false, it renders on a PlaneGeometry */
|
|
2479
2479
|
asSprite?: boolean
|
|
2480
|
+
/** Allows the animation to be paused after it ended so it can be restarted on demand via auto */
|
|
2481
|
+
resetOnEnd?: boolean
|
|
2480
2482
|
}
|
|
2481
2483
|
```
|
|
2482
2484
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),r=require("react"),t=require("@react-three/fiber"),n=require("three");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function u(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var s=a(e),c=u(r),f=u(n);const o=c.forwardRef((({startFrame:e,endFrame:r,fps:n,frameName:a,textureDataURL:u,textureImageURL:o,loop:m,numberOfFrames:l,autoPlay:i,animationNames:p,onStart:h,onEnd:w,onLoopEnd:d,onFrame:y,play:S,pause:b,flipX:x,alphaTest:E,children:g,asSprite:z,resetOnEnd:R,...F},A)=>{const O=c.useRef(null),[j,L]=c.useState(!1),v=c.useRef(!1),M=c.useRef(),P=c.useRef(),T=c.useRef(window.performance.now()),N=c.useRef(),k=c.useRef(e||0),q=c.useRef(a||""),_=1e3/(n||30),[D,C]=c.useState(new f.Texture),I=c.useRef(0),[U,B]=c.useState([1,1,1]),G=x?-1:1,[W,X]=c.useState(null==z||z),H=c.useRef(b);const J=(e,r)=>{const t=r/e;return P.current&&P.current.scale.set(1,t,1),[1,t,1]};c.useEffect((()=>{if(u&&o)!function(e,r,t){const n=new f.TextureLoader,a=fetch(e).then((e=>e.json())),u=new Promise((e=>{n.load(r,e)}));Promise.all([a,u]).then((e=>{t(e[0],e[1])}))}(u,o,K);else if(o){const e=new f.TextureLoader;new Promise((r=>{e.load(o,r)})).then((e=>{K(null,e)}))}}),[]),c.useEffect((()=>{X(null==z||z)}),[z]),c.useLayoutEffect((()=>{V()}),[D,x]),c.useEffect((()=>{i&&(H.current=!1)}),[i]),c.useEffect((()=>{if(q.current!==a&&a&&(k.current=0,q.current=a,v.current=!1,V(),O.current)){const{w:e,h:r}=Z(O.current.frames).sourceSize,t=J(e,r);B(t)}}),[a]);const K=(e,r)=>{if(null===e){if(l){const e=r.image.width,t=r.image.height,n=e/l,a=t;if(N.current=r,I.current=l,O.current={frames:[],meta:{version:"1.0",size:{w:e,h:t},scale:"1"}},parseInt(n.toString(),10)===n)for(let e=0;e<l;e++)O.current.frames.push({frame:{x:e*n,y:0,w:n,h:a},rotated:!1,trimmed:!1,spriteSourceSize:{x:0,y:0,w:n,h:a},sourceSize:{w:n,h:t}})}}else{O.current=e,O.current.frames=Array.isArray(e.frames)?e.frames:Q(),I.current=Array.isArray(e.frames)?e.frames.length:Object.keys(e.frames).length,N.current=r;const{w:t,h:n}=Z(e.frames).sourceSize,a=J(t,n);B(a),M.current&&(M.current.map=r)}r.premultiplyAlpha=!1,C(r)},Q=()=>{const e={},r=O.current,t=p;if(t){for(let n=0;n<t.length;n++){e[t[n]]=[];for(const a in r.frames){const u=r.frames[a],s=u.frame,c=s.x,f=s.y,o=s.w,m=s.h,l=u.sourceSize.w,i=u.sourceSize.h;-1!==a.toLowerCase().indexOf(t[n].toLowerCase())&&e[t[n]].push({x:c,y:f,w:o,h:m,frame:s,sourceSize:{w:l,h:i}})}}return e}if(a){const e=[];for(const t in r.frames)e.push(r.frames[t]);return e}},V=()=>{if(!O.current)return;const{meta:{size:e},frames:r}=O.current,{w:t,h:n}=Array.isArray(r)?r[0].sourceSize:a&&r[a]?r[a][0].sourceSize:{w:0,h:0};M.current.map.wrapS=M.current.map.wrapT=f.RepeatWrapping,M.current.map.center.set(0,0),M.current.map.repeat.set(1*G/(e.w/t),1/(e.h/n));const u=1/((e.h-1)/n);M.current.map.offset.x=0,M.current.map.offset.y=1-u,L(!0),h&&h({currentFrameName:a,currentFrame:k.current})},Y=(e,r,t,n)=>{let a=0,u=0;J(e,r);const s=(t.w-1)/e,c=(t.h-1)/r,{frame:{x:f,y:o},sourceSize:{w:m,h:l}}=n[k.current],i=1/s,p=1/c;a=G>0?i*(f/m):i*(f/m)-M.current.map.repeat.x,u=Math.abs(1-p)-p*(o/l),M.current.map.offset.x=a,M.current.map.offset.y=u};t.useFrame(((t,n)=>{var u,s;null!=(u=O.current)&&u.frames&&null!=(s=M.current)&&s.map&&(H.current||v.current||!i&&!S||((()=>{const t=window.performance.now(),n=t-T.current,{meta:{size:u},frames:s}=O.current,{w:c,h:f}=Z(s).sourceSize,o=Array.isArray(s)?s:a?s[a]:[],l=r||o.length-1;k.current>l&&(k.current=m&&null!=e?e:0,m?null==d||d({currentFrameName:a,currentFrame:k.current}):(null==w||w({currentFrameName:a,currentFrame:k.current}),v.current=!R,R&&(H.current=!0)),!m)||n<=_||(T.current=t-n%_,Y(c,f,u,o),k.current+=1)})(),y&&y({currentFrameName:q.current,currentFrame:k.current})))}));const Z=e=>{if(Array.isArray(e))return e[0];if("object"==typeof e&&null!==e){const r=Object.keys(e);return a?e[a][0]:e[r[0]][0]}return{w:0,h:0}};return c.createElement("group",s.default({ref:A},F),c.createElement(c.Suspense,{fallback:null},W&&c.createElement("sprite",{ref:P,scale:U},c.createElement("spriteMaterial",{toneMapped:!1,ref:M,map:D,transparent:!0,alphaTest:null!=E?E:0})),!W&&c.createElement("mesh",{ref:P,scale:U},c.createElement("planeGeometry",{args:[1,1]}),c.createElement("meshBasicMaterial",{toneMapped:!1,side:f.DoubleSide,ref:M,map:D,transparent:!0,alphaTest:null!=E?E:0}))),g)}));exports.SpriteAnimator=o;
|
package/core/SpriteAnimator.d.ts
CHANGED
package/core/SpriteAnimator.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import _extends from '@babel/runtime/helpers/esm/extends';
|
|
1
2
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
3
|
+
import { useFrame } from '@react-three/fiber';
|
|
3
4
|
import * as THREE from 'three';
|
|
4
5
|
|
|
5
|
-
const SpriteAnimator = ({
|
|
6
|
+
const SpriteAnimator = /* @__PURE__ */React.forwardRef(({
|
|
6
7
|
startFrame,
|
|
7
8
|
endFrame,
|
|
8
9
|
fps,
|
|
@@ -23,9 +24,9 @@ const SpriteAnimator = ({
|
|
|
23
24
|
alphaTest,
|
|
24
25
|
children,
|
|
25
26
|
asSprite,
|
|
27
|
+
resetOnEnd,
|
|
26
28
|
...props
|
|
27
29
|
}, fref) => {
|
|
28
|
-
useThree(state => state.viewport);
|
|
29
30
|
const spriteData = React.useRef(null);
|
|
30
31
|
const [isJsonReady, setJsonReady] = React.useState(false);
|
|
31
32
|
const hasEnded = React.useRef(false);
|
|
@@ -41,6 +42,7 @@ const SpriteAnimator = ({
|
|
|
41
42
|
const [aspect, setAspect] = React.useState([1, 1, 1]);
|
|
42
43
|
const flipOffset = flipX ? -1 : 1;
|
|
43
44
|
const [displayAsSprite, setDisplayAsSprite] = React.useState(asSprite !== null && asSprite !== void 0 ? asSprite : true);
|
|
45
|
+
const pauseRef = React.useRef(pause);
|
|
44
46
|
function loadJsonAndTextureAndExecuteCallback(jsonUrl, textureUrl, callback) {
|
|
45
47
|
const textureLoader = new THREE.TextureLoader();
|
|
46
48
|
const jsonPromise = fetch(jsonUrl).then(response => response.json());
|
|
@@ -80,7 +82,10 @@ const SpriteAnimator = ({
|
|
|
80
82
|
modifySpritePosition();
|
|
81
83
|
}, [spriteTexture, flipX]);
|
|
82
84
|
React.useEffect(() => {
|
|
83
|
-
|
|
85
|
+
if (autoPlay) {
|
|
86
|
+
pauseRef.current = false;
|
|
87
|
+
}
|
|
88
|
+
}, [autoPlay]);
|
|
84
89
|
React.useEffect(() => {
|
|
85
90
|
if (currentFrameName.current !== frameName && frameName) {
|
|
86
91
|
currentFrame.current = 0;
|
|
@@ -100,7 +105,7 @@ const SpriteAnimator = ({
|
|
|
100
105
|
const parseSpriteData = (json, _spriteTexture) => {
|
|
101
106
|
// sprite only case
|
|
102
107
|
if (json === null) {
|
|
103
|
-
if (
|
|
108
|
+
if (numberOfFrames) {
|
|
104
109
|
//get size from texture
|
|
105
110
|
const width = _spriteTexture.image.width;
|
|
106
111
|
const height = _spriteTexture.image.height;
|
|
@@ -145,7 +150,7 @@ const SpriteAnimator = ({
|
|
|
145
150
|
}
|
|
146
151
|
}
|
|
147
152
|
}
|
|
148
|
-
} else
|
|
153
|
+
} else {
|
|
149
154
|
spriteData.current = json;
|
|
150
155
|
spriteData.current.frames = Array.isArray(json.frames) ? json.frames : parseFrames();
|
|
151
156
|
totalFrames.current = Array.isArray(json.frames) ? json.frames.length : Object.keys(json.frames).length;
|
|
@@ -172,7 +177,7 @@ const SpriteAnimator = ({
|
|
|
172
177
|
if (delimiters) {
|
|
173
178
|
for (let i = 0; i < delimiters.length; i++) {
|
|
174
179
|
sprites[delimiters[i]] = [];
|
|
175
|
-
for (
|
|
180
|
+
for (const innerKey in data['frames']) {
|
|
176
181
|
const value = data['frames'][innerKey];
|
|
177
182
|
const frameData = value['frame'];
|
|
178
183
|
const x = frameData['x'];
|
|
@@ -181,7 +186,7 @@ const SpriteAnimator = ({
|
|
|
181
186
|
const height = frameData['h'];
|
|
182
187
|
const sourceWidth = value['sourceSize']['w'];
|
|
183
188
|
const sourceHeight = value['sourceSize']['h'];
|
|
184
|
-
if (
|
|
189
|
+
if (innerKey.toLowerCase().indexOf(delimiters[i].toLowerCase()) !== -1) {
|
|
185
190
|
sprites[delimiters[i]].push({
|
|
186
191
|
x: x,
|
|
187
192
|
y: y,
|
|
@@ -196,8 +201,14 @@ const SpriteAnimator = ({
|
|
|
196
201
|
}
|
|
197
202
|
}
|
|
198
203
|
}
|
|
204
|
+
return sprites;
|
|
205
|
+
} else if (frameName) {
|
|
206
|
+
const spritesArr = [];
|
|
207
|
+
for (const key in data.frames) {
|
|
208
|
+
spritesArr.push(data.frames[key]);
|
|
209
|
+
}
|
|
210
|
+
return spritesArr;
|
|
199
211
|
}
|
|
200
|
-
return sprites;
|
|
201
212
|
};
|
|
202
213
|
|
|
203
214
|
// modify the sprite material after json is parsed and state updated
|
|
@@ -251,8 +262,6 @@ const SpriteAnimator = ({
|
|
|
251
262
|
h: frameH
|
|
252
263
|
} = getFirstItem(frames).sourceSize;
|
|
253
264
|
const spriteFrames = Array.isArray(frames) ? frames : frameName ? frames[frameName] : [];
|
|
254
|
-
let finalValX = 0;
|
|
255
|
-
let finalValY = 0;
|
|
256
265
|
const _endFrame = endFrame || spriteFrames.length - 1;
|
|
257
266
|
if (currentFrame.current > _endFrame) {
|
|
258
267
|
currentFrame.current = loop ? startFrame !== null && startFrame !== void 0 ? startFrame : 0 : 0;
|
|
@@ -266,12 +275,23 @@ const SpriteAnimator = ({
|
|
|
266
275
|
currentFrameName: frameName,
|
|
267
276
|
currentFrame: currentFrame.current
|
|
268
277
|
});
|
|
269
|
-
hasEnded.current = true;
|
|
278
|
+
hasEnded.current = resetOnEnd ? false : true;
|
|
279
|
+
if (resetOnEnd) {
|
|
280
|
+
pauseRef.current = true;
|
|
281
|
+
//calculateFinalPosition(frameW, frameH, metaInfo, spriteFrames)
|
|
282
|
+
}
|
|
270
283
|
}
|
|
284
|
+
|
|
271
285
|
if (!loop) return;
|
|
272
286
|
}
|
|
273
287
|
if (diff <= fpsInterval) return;
|
|
274
288
|
timerOffset.current = now - diff % fpsInterval;
|
|
289
|
+
calculateFinalPosition(frameW, frameH, metaInfo, spriteFrames);
|
|
290
|
+
currentFrame.current += 1;
|
|
291
|
+
};
|
|
292
|
+
const calculateFinalPosition = (frameW, frameH, metaInfo, spriteFrames) => {
|
|
293
|
+
let finalValX = 0;
|
|
294
|
+
let finalValY = 0;
|
|
275
295
|
calculateAspectRatio(frameW, frameH);
|
|
276
296
|
const framesH = (metaInfo.w - 1) / frameW;
|
|
277
297
|
const framesV = (metaInfo.h - 1) / frameH;
|
|
@@ -291,16 +311,15 @@ const SpriteAnimator = ({
|
|
|
291
311
|
finalValY = Math.abs(1 - frameOffsetY) - frameOffsetY * (frameY / originalSizeY);
|
|
292
312
|
matRef.current.map.offset.x = finalValX;
|
|
293
313
|
matRef.current.map.offset.y = finalValY;
|
|
294
|
-
currentFrame.current += 1;
|
|
295
314
|
};
|
|
296
315
|
|
|
297
316
|
// *** Warning! It runs on every frame! ***
|
|
298
|
-
useFrame((
|
|
317
|
+
useFrame((_state, _delta) => {
|
|
299
318
|
var _spriteData$current, _matRef$current;
|
|
300
319
|
if (!((_spriteData$current = spriteData.current) != null && _spriteData$current.frames) || !((_matRef$current = matRef.current) != null && _matRef$current.map)) {
|
|
301
320
|
return;
|
|
302
321
|
}
|
|
303
|
-
if (
|
|
322
|
+
if (pauseRef.current) {
|
|
304
323
|
return;
|
|
305
324
|
}
|
|
306
325
|
if (!hasEnded.current && (autoPlay || play)) {
|
|
@@ -326,7 +345,9 @@ const SpriteAnimator = ({
|
|
|
326
345
|
};
|
|
327
346
|
}
|
|
328
347
|
};
|
|
329
|
-
return /*#__PURE__*/React.createElement("group",
|
|
348
|
+
return /*#__PURE__*/React.createElement("group", _extends({
|
|
349
|
+
ref: fref
|
|
350
|
+
}, props), /*#__PURE__*/React.createElement(React.Suspense, {
|
|
330
351
|
fallback: null
|
|
331
352
|
}, displayAsSprite && /*#__PURE__*/React.createElement("sprite", {
|
|
332
353
|
ref: spriteRef,
|
|
@@ -350,6 +371,6 @@ const SpriteAnimator = ({
|
|
|
350
371
|
transparent: true,
|
|
351
372
|
alphaTest: alphaTest !== null && alphaTest !== void 0 ? alphaTest : 0.0
|
|
352
373
|
}))), children);
|
|
353
|
-
};
|
|
374
|
+
});
|
|
354
375
|
|
|
355
376
|
export { SpriteAnimator };
|