@react-three/fiber 9.4.2 → 10.0.0-alpha.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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.cjs +14832 -0
  3. package/dist/index.d.cts +3350 -0
  4. package/dist/index.d.mts +3350 -0
  5. package/dist/index.d.ts +3350 -0
  6. package/dist/index.mjs +14747 -0
  7. package/dist/legacy.cjs +14820 -0
  8. package/dist/legacy.d.cts +3347 -0
  9. package/dist/legacy.d.mts +3347 -0
  10. package/dist/legacy.d.ts +3347 -0
  11. package/dist/legacy.mjs +14735 -0
  12. package/dist/webgpu/index.cjs +15274 -0
  13. package/dist/webgpu/index.d.cts +3470 -0
  14. package/dist/webgpu/index.d.mts +3470 -0
  15. package/dist/webgpu/index.d.ts +3470 -0
  16. package/dist/webgpu/index.mjs +15177 -0
  17. package/package.json +44 -44
  18. package/react-reconciler/constants.d.ts +7 -0
  19. package/react-reconciler/constants.js +9 -0
  20. package/react-reconciler/index.d.ts +1044 -0
  21. package/react-reconciler/index.js +224 -0
  22. package/readme.md +98 -33
  23. package/CHANGELOG.md +0 -1186
  24. package/dist/declarations/src/core/events.d.ts +0 -92
  25. package/dist/declarations/src/core/hooks.d.ts +0 -53
  26. package/dist/declarations/src/core/index.d.ts +0 -13
  27. package/dist/declarations/src/core/loop.d.ts +0 -31
  28. package/dist/declarations/src/core/reconciler.d.ts +0 -50
  29. package/dist/declarations/src/core/renderer.d.ts +0 -89
  30. package/dist/declarations/src/core/store.d.ts +0 -130
  31. package/dist/declarations/src/core/utils.d.ts +0 -191
  32. package/dist/declarations/src/index.d.ts +0 -6
  33. package/dist/declarations/src/native/Canvas.d.ts +0 -13
  34. package/dist/declarations/src/native/events.d.ts +0 -4
  35. package/dist/declarations/src/native.d.ts +0 -6
  36. package/dist/declarations/src/three-types.d.ts +0 -68
  37. package/dist/declarations/src/web/Canvas.d.ts +0 -23
  38. package/dist/declarations/src/web/events.d.ts +0 -4
  39. package/dist/events-1eccaf1c.esm.js +0 -2510
  40. package/dist/events-5c8d1731.cjs.prod.js +0 -2569
  41. package/dist/events-c80effae.cjs.dev.js +0 -2569
  42. package/dist/react-three-fiber.cjs.d.ts +0 -2
  43. package/dist/react-three-fiber.cjs.dev.js +0 -222
  44. package/dist/react-three-fiber.cjs.js +0 -7
  45. package/dist/react-three-fiber.cjs.prod.js +0 -222
  46. package/dist/react-three-fiber.esm.js +0 -167
  47. package/native/dist/react-three-fiber-native.cjs.d.ts +0 -2
  48. package/native/dist/react-three-fiber-native.cjs.dev.js +0 -569
  49. package/native/dist/react-three-fiber-native.cjs.js +0 -7
  50. package/native/dist/react-three-fiber-native.cjs.prod.js +0 -569
  51. package/native/dist/react-three-fiber-native.esm.js +0 -517
  52. package/native/package.json +0 -5
@@ -1,517 +0,0 @@
1
- import { e as extend, u as useBridge, a as useMutableCallback, c as createRoot, b as useIsomorphicLayoutEffect, E as ErrorBoundary, B as Block, d as unmountComponentAtNode, f as createPointerEvents, g as createEvents } from '../../dist/events-1eccaf1c.esm.js';
2
- export { t as ReactThreeFiber, _ as _roots, x as act, k as addAfterEffect, j as addEffect, l as addTail, n as advance, s as applyProps, y as buildGraph, q as context, g as createEvents, o as createPortal, c as createRoot, w as dispose, e as extend, h as flushGlobalEffects, p as flushSync, v as getRootState, m as invalidate, r as reconciler, d as unmountComponentAtNode, D as useFrame, F as useGraph, z as useInstanceHandle, G as useLoader, A as useStore, C as useThree } from '../../dist/events-1eccaf1c.esm.js';
3
- import * as React from 'react';
4
- import * as THREE from 'three';
5
- import { PanResponder, PixelRatio, StyleSheet, View, Platform, Image, NativeModules } from 'react-native';
6
- import { GLView } from 'expo-gl';
7
- import { FiberProvider } from 'its-fine';
8
- import { jsx, jsxs } from 'react/jsx-runtime';
9
- import { Asset } from 'expo-asset';
10
- import { fromByteArray } from 'base64-js';
11
- import { Buffer } from 'buffer';
12
- import 'react-reconciler/constants';
13
- import 'zustand/traditional';
14
- import 'suspend-react';
15
- import 'react-reconciler';
16
- import 'scheduler';
17
-
18
- // TODO: React 19 needs support from react-native
19
- const _View = View;
20
- function CanvasImpl({
21
- children,
22
- style,
23
- gl,
24
- events = createPointerEvents,
25
- shadows,
26
- linear,
27
- flat,
28
- legacy,
29
- orthographic,
30
- frameloop,
31
- performance,
32
- raycaster,
33
- camera,
34
- scene,
35
- onPointerMissed,
36
- onCreated,
37
- ref,
38
- pointerEvents,
39
- ...props
40
- }) {
41
- // Create a known catalogue of Threejs-native elements
42
- // This will include the entire THREE namespace by default, users can extend
43
- // their own elements by using the createRoot API instead
44
- React.useMemo(() => extend(THREE), []);
45
- const Bridge = useBridge();
46
- const [{
47
- width,
48
- height,
49
- top,
50
- left
51
- }, setSize] = React.useState({
52
- width: 0,
53
- height: 0,
54
- top: 0,
55
- left: 0
56
- });
57
- const [canvas, setCanvas] = React.useState(null);
58
- const [bind, setBind] = React.useState();
59
- React.useImperativeHandle(ref, () => viewRef.current);
60
- const handlePointerMissed = useMutableCallback(onPointerMissed);
61
- const [block, setBlock] = React.useState(false);
62
- const [error, setError] = React.useState(undefined);
63
-
64
- // Suspend this component if block is a promise (2nd run)
65
- if (block) throw block;
66
- // Throw exception outwards if anything within canvas throws
67
- if (error) throw error;
68
- const viewRef = React.useRef(null);
69
- const root = React.useRef(null);
70
- const [antialias, setAntialias] = React.useState(true);
71
- const onLayout = React.useCallback(e => {
72
- const {
73
- width,
74
- height,
75
- x,
76
- y
77
- } = e.nativeEvent.layout;
78
- setSize({
79
- width,
80
- height,
81
- top: y,
82
- left: x
83
- });
84
- }, []);
85
-
86
- // Called on context create or swap
87
- // https://github.com/pmndrs/react-three-fiber/pull/2297
88
- const onContextCreate = React.useCallback(context => {
89
- const listeners = new Map();
90
- const canvas = {
91
- style: {},
92
- width: context.drawingBufferWidth,
93
- height: context.drawingBufferHeight,
94
- clientWidth: context.drawingBufferWidth,
95
- clientHeight: context.drawingBufferHeight,
96
- getContext: (_, {
97
- antialias = false
98
- }) => {
99
- setAntialias(antialias);
100
- return context;
101
- },
102
- addEventListener(type, listener) {
103
- let callbacks = listeners.get(type);
104
- if (!callbacks) {
105
- callbacks = [];
106
- listeners.set(type, callbacks);
107
- }
108
- callbacks.push(listener);
109
- },
110
- removeEventListener(type, listener) {
111
- const callbacks = listeners.get(type);
112
- if (callbacks) {
113
- const index = callbacks.indexOf(listener);
114
- if (index !== -1) callbacks.splice(index, 1);
115
- }
116
- },
117
- dispatchEvent(event) {
118
- Object.assign(event, {
119
- target: this
120
- });
121
- const callbacks = listeners.get(event.type);
122
- if (callbacks) {
123
- for (const callback of callbacks) {
124
- callback(event);
125
- }
126
- }
127
- },
128
- setPointerCapture() {
129
- // TODO
130
- },
131
- releasePointerCapture() {
132
- // TODO
133
- }
134
- };
135
-
136
- // TODO: this is wrong but necessary to trick controls
137
- // @ts-ignore
138
- canvas.ownerDocument = canvas;
139
- canvas.getRootNode = () => canvas;
140
- root.current = createRoot(canvas);
141
- setCanvas(canvas);
142
- function handleTouch(gestureEvent, type) {
143
- gestureEvent.persist();
144
- canvas.dispatchEvent(Object.assign(gestureEvent.nativeEvent, {
145
- type,
146
- offsetX: gestureEvent.nativeEvent.locationX,
147
- offsetY: gestureEvent.nativeEvent.locationY,
148
- pointerType: 'touch',
149
- pointerId: gestureEvent.nativeEvent.identifier
150
- }));
151
- return true;
152
- }
153
- const responder = PanResponder.create({
154
- onStartShouldSetPanResponder: () => true,
155
- onMoveShouldSetPanResponder: () => true,
156
- onMoveShouldSetPanResponderCapture: () => true,
157
- onPanResponderTerminationRequest: () => true,
158
- onStartShouldSetPanResponderCapture: e => handleTouch(e, 'pointercapture'),
159
- onPanResponderStart: e => handleTouch(e, 'pointerdown'),
160
- onPanResponderMove: e => handleTouch(e, 'pointermove'),
161
- onPanResponderEnd: (e, state) => {
162
- handleTouch(e, 'pointerup');
163
- if (Math.hypot(state.dx, state.dy) < 20) handleTouch(e, 'click');
164
- },
165
- onPanResponderRelease: e => handleTouch(e, 'pointerleave'),
166
- onPanResponderTerminate: e => handleTouch(e, 'lostpointercapture'),
167
- onPanResponderReject: e => handleTouch(e, 'lostpointercapture')
168
- });
169
- setBind(responder.panHandlers);
170
- }, []);
171
- useIsomorphicLayoutEffect(() => {
172
- if (root.current && width > 0 && height > 0) {
173
- async function run() {
174
- await root.current.configure({
175
- gl,
176
- events,
177
- shadows,
178
- linear,
179
- flat,
180
- legacy,
181
- orthographic,
182
- frameloop,
183
- performance,
184
- raycaster,
185
- camera,
186
- scene,
187
- // expo-gl can only render at native dpr/resolution
188
- // https://github.com/expo/expo-three/issues/39
189
- dpr: PixelRatio.get(),
190
- size: {
191
- width,
192
- height,
193
- top,
194
- left
195
- },
196
- // Pass mutable reference to onPointerMissed so it's free to update
197
- onPointerMissed: (...args) => handlePointerMissed.current == null ? void 0 : handlePointerMissed.current(...args),
198
- // Overwrite onCreated to apply RN bindings
199
- onCreated: state => {
200
- // Bind render to RN bridge
201
- const context = state.gl.getContext();
202
- const renderFrame = state.gl.render.bind(state.gl);
203
- state.gl.render = (scene, camera) => {
204
- renderFrame(scene, camera);
205
- context.endFrameEXP();
206
- };
207
- return onCreated == null ? void 0 : onCreated(state);
208
- }
209
- });
210
- root.current.render( /*#__PURE__*/jsx(Bridge, {
211
- children: /*#__PURE__*/jsx(ErrorBoundary, {
212
- set: setError,
213
- children: /*#__PURE__*/jsx(React.Suspense, {
214
- fallback: /*#__PURE__*/jsx(Block, {
215
- set: setBlock
216
- }),
217
- children: children != null ? children : null
218
- })
219
- })
220
- }));
221
- }
222
- run();
223
- }
224
- });
225
- React.useEffect(() => {
226
- if (canvas) {
227
- return () => unmountComponentAtNode(canvas);
228
- }
229
- }, [canvas]);
230
- return /*#__PURE__*/jsxs(_View, {
231
- ...props,
232
- ref: viewRef,
233
- onLayout: onLayout,
234
- style: {
235
- flex: 1,
236
- ...style
237
- },
238
- children: [width > 0 && /*#__PURE__*/jsx(GLView, {
239
- msaaSamples: antialias ? 4 : 0,
240
- onContextCreate: onContextCreate,
241
- style: StyleSheet.absoluteFill
242
- }), /*#__PURE__*/jsx(_View, {
243
- style: StyleSheet.absoluteFill,
244
- pointerEvents: pointerEvents,
245
- ...bind
246
- })]
247
- });
248
- }
249
-
250
- /**
251
- * A native canvas which accepts threejs elements as children.
252
- * @see https://docs.pmnd.rs/react-three-fiber/api/canvas
253
- */
254
- function Canvas(props) {
255
- return /*#__PURE__*/jsx(FiberProvider, {
256
- children: /*#__PURE__*/jsx(CanvasImpl, {
257
- ...props
258
- })
259
- });
260
- }
261
-
262
- /** Default R3F event manager for react-native */
263
- function createTouchEvents(store) {
264
- const {
265
- handlePointer
266
- } = createEvents(store);
267
- const handleTouch = (event, name) => {
268
- event.persist()
269
-
270
- // Apply offset
271
- ;
272
- event.nativeEvent.offsetX = event.nativeEvent.locationX;
273
- event.nativeEvent.offsetY = event.nativeEvent.locationY;
274
-
275
- // Emulate DOM event
276
- const callback = handlePointer(name);
277
- callback(event.nativeEvent);
278
- return true;
279
- };
280
- const responder = PanResponder.create({
281
- onStartShouldSetPanResponder: () => true,
282
- onMoveShouldSetPanResponder: () => true,
283
- onMoveShouldSetPanResponderCapture: () => true,
284
- onPanResponderTerminationRequest: () => true,
285
- onStartShouldSetPanResponderCapture: e => handleTouch(e, 'onPointerCapture'),
286
- onPanResponderStart: e => handleTouch(e, 'onPointerDown'),
287
- onPanResponderMove: e => handleTouch(e, 'onPointerMove'),
288
- onPanResponderEnd: (e, state) => {
289
- handleTouch(e, 'onPointerUp');
290
- if (Math.hypot(state.dx, state.dy) < 20) handleTouch(e, 'onClick');
291
- },
292
- onPanResponderRelease: e => handleTouch(e, 'onPointerLeave'),
293
- onPanResponderTerminate: e => handleTouch(e, 'onLostPointerCapture'),
294
- onPanResponderReject: e => handleTouch(e, 'onLostPointerCapture')
295
- });
296
- return {
297
- priority: 1,
298
- enabled: true,
299
- compute(event, state, previous) {
300
- // https://github.com/pmndrs/react-three-fiber/pull/782
301
- // Events trigger outside of canvas when moved, use offsetX/Y by default and allow overrides
302
- state.pointer.set(event.offsetX / state.size.width * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
303
- state.raycaster.setFromCamera(state.pointer, state.camera);
304
- },
305
- connected: undefined,
306
- handlers: responder.panHandlers,
307
- update: () => {
308
- var _internal$lastEvent;
309
- const {
310
- events,
311
- internal
312
- } = store.getState();
313
- if ((_internal$lastEvent = internal.lastEvent) != null && _internal$lastEvent.current && events.handlers) {
314
- handlePointer('onPointerMove')(internal.lastEvent.current);
315
- }
316
- },
317
- connect: () => {
318
- const {
319
- set,
320
- events
321
- } = store.getState();
322
- events.disconnect == null ? void 0 : events.disconnect();
323
- set(state => ({
324
- events: {
325
- ...state.events,
326
- connected: true
327
- }
328
- }));
329
- },
330
- disconnect: () => {
331
- const {
332
- set
333
- } = store.getState();
334
- set(state => ({
335
- events: {
336
- ...state.events,
337
- connected: false
338
- }
339
- }));
340
- }
341
- };
342
- }
343
-
344
- // Conditionally import expo-file-system/legacy to support Expo 54
345
- const getFileSystem = () => {
346
- try {
347
- return require('expo-file-system/legacy');
348
- } catch {
349
- return require('expo-file-system');
350
- }
351
- };
352
- const fs = getFileSystem();
353
-
354
- // http://stackoverflow.com/questions/105034
355
- function uuidv4() {
356
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
357
- const r = Math.random() * 16 | 0,
358
- v = c == 'x' ? r : r & 0x3 | 0x8;
359
- return v.toString(16);
360
- });
361
- }
362
- async function getAsset(input) {
363
- if (typeof input === 'string') {
364
- var _NativeModules$BlobMo;
365
- // Don't process storage
366
- if (input.startsWith('file:')) return input;
367
-
368
- // Unpack Blobs from react-native BlobManager
369
- // https://github.com/facebook/react-native/issues/22681#issuecomment-523258955
370
- if (input.startsWith('blob:') || input.startsWith((_NativeModules$BlobMo = NativeModules.BlobModule) == null ? void 0 : _NativeModules$BlobMo.BLOB_URI_SCHEME)) {
371
- const blob = await new Promise((res, rej) => {
372
- const xhr = new XMLHttpRequest();
373
- xhr.open('GET', input);
374
- xhr.responseType = 'blob';
375
- xhr.onload = () => res(xhr.response);
376
- xhr.onerror = rej;
377
- xhr.send();
378
- });
379
- const data = await new Promise((res, rej) => {
380
- const reader = new FileReader();
381
- reader.onload = () => res(reader.result);
382
- reader.onerror = rej;
383
- reader.readAsText(blob);
384
- });
385
- input = `data:${blob.type};base64,${data}`;
386
- }
387
-
388
- // Create safe URI for JSI serialization
389
- if (input.startsWith('data:')) {
390
- const [header, data] = input.split(';base64,');
391
- const [, type] = header.split('/');
392
- const uri = fs.cacheDirectory + uuidv4() + `.${type}`;
393
- await fs.writeAsStringAsync(uri, data, {
394
- encoding: fs.EncodingType.Base64
395
- });
396
- return uri;
397
- }
398
- }
399
-
400
- // Download bundler module or external URL
401
- const asset = await Asset.fromModule(input).downloadAsync();
402
- let uri = asset.localUri || asset.uri;
403
-
404
- // Unpack assets in Android Release Mode
405
- if (!uri.includes(':')) {
406
- const file = `${fs.cacheDirectory}ExponentAsset-${asset.hash}.${asset.type}`;
407
- await fs.copyAsync({
408
- from: uri,
409
- to: file
410
- });
411
- uri = file;
412
- }
413
- return uri;
414
- }
415
- function polyfills() {
416
- // Patch Blob for ArrayBuffer and URL if unsupported
417
- // https://github.com/facebook/react-native/pull/39276
418
- // https://github.com/pmndrs/react-three-fiber/issues/3058
419
- if (Platform.OS !== 'web') {
420
- try {
421
- const blob = new Blob([new ArrayBuffer(4)]);
422
- const url = URL.createObjectURL(blob);
423
- URL.revokeObjectURL(url);
424
- } catch (_) {
425
- var _BlobManagerModule$de;
426
- const BlobManagerModule = require('react-native/Libraries/Blob/BlobManager.js');
427
- const BlobManager = (_BlobManagerModule$de = BlobManagerModule.default) != null ? _BlobManagerModule$de : BlobManagerModule;
428
- const createObjectURL = URL.createObjectURL;
429
- URL.createObjectURL = function (blob) {
430
- if (blob.data._base64) {
431
- return `data:${blob.type};base64,${blob.data._base64}`;
432
- }
433
- return createObjectURL(blob);
434
- };
435
- const createFromParts = BlobManager.createFromParts;
436
- BlobManager.createFromParts = function (parts, options) {
437
- parts = parts.map(part => {
438
- if (part instanceof ArrayBuffer || ArrayBuffer.isView(part)) {
439
- part = fromByteArray(new Uint8Array(part));
440
- }
441
- return part;
442
- });
443
- const blob = createFromParts(parts, options);
444
-
445
- // Always enable slow but safe path for iOS (previously for Android unauth)
446
- // https://github.com/pmndrs/react-three-fiber/issues/3075
447
- // if (!NativeModules.BlobModule?.BLOB_URI_SCHEME) {
448
- blob.data._base64 = '';
449
- for (const part of parts) {
450
- var _data$_base, _data;
451
- blob.data._base64 += (_data$_base = (_data = part.data) == null ? void 0 : _data._base64) != null ? _data$_base : part;
452
- }
453
- // }
454
-
455
- return blob;
456
- };
457
- }
458
- }
459
-
460
- // Don't pre-process urls, let expo-asset generate an absolute URL
461
- const extractUrlBase = THREE.LoaderUtils.extractUrlBase.bind(THREE.LoaderUtils);
462
- THREE.LoaderUtils.extractUrlBase = url => typeof url === 'string' ? extractUrlBase(url) : './';
463
-
464
- // There's no Image in native, so create a data texture instead
465
- THREE.TextureLoader.prototype.load = function load(url, onLoad, onProgress, onError) {
466
- if (this.path && typeof url === 'string') url = this.path + url;
467
- const texture = new THREE.Texture();
468
- getAsset(url).then(async uri => {
469
- // https://github.com/expo/expo-three/pull/266
470
- const {
471
- width,
472
- height
473
- } = await new Promise((res, rej) => Image.getSize(uri, (width, height) => res({
474
- width,
475
- height
476
- }), rej));
477
- texture.image = {
478
- // Special case for EXGLImageUtils::loadImage
479
- data: {
480
- localUri: uri
481
- },
482
- width,
483
- height
484
- };
485
- texture.flipY = true; // Since expo-gl@12.4.0
486
- texture.needsUpdate = true;
487
-
488
- // Force non-DOM upload for EXGL texImage2D
489
- // @ts-expect-error
490
- texture.isDataTexture = true;
491
- onLoad == null ? void 0 : onLoad(texture);
492
- }).catch(onError);
493
- return texture;
494
- };
495
-
496
- // Fetches assets via FS
497
- THREE.FileLoader.prototype.load = function load(url, onLoad, onProgress, onError) {
498
- if (this.path && typeof url === 'string') url = this.path + url;
499
- this.manager.itemStart(url);
500
- getAsset(url).then(async uri => {
501
- const base64 = await fs.readAsStringAsync(uri, {
502
- encoding: fs.EncodingType.Base64
503
- });
504
- const data = Buffer.from(base64, 'base64');
505
- onLoad == null ? void 0 : onLoad(data.buffer);
506
- }).catch(error => {
507
- onError == null ? void 0 : onError(error);
508
- this.manager.itemError(url);
509
- }).finally(() => {
510
- this.manager.itemEnd(url);
511
- });
512
- };
513
- }
514
-
515
- if (Platform.OS !== 'web') polyfills();
516
-
517
- export { Canvas, createTouchEvents as events };
@@ -1,5 +0,0 @@
1
- {
2
- "main": "dist/react-three-fiber-native.cjs.js",
3
- "module": "dist/react-three-fiber-native.esm.js",
4
- "types": "dist/react-three-fiber-native.cjs.d.ts"
5
- }