@react-three/fiber 8.0.0-alpha-09 → 8.0.0-beta-03
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/CHANGELOG.md +97 -0
- package/dist/declarations/src/core/events.d.ts +63 -59
- package/dist/declarations/src/core/hooks.d.ts +21 -29
- package/dist/declarations/src/core/loop.d.ts +12 -12
- package/dist/declarations/src/core/renderer.d.ts +50 -51
- package/dist/declarations/src/core/store.d.ts +117 -106
- package/dist/declarations/src/core/utils.d.ts +50 -27
- package/dist/declarations/src/index.d.ts +8 -7
- package/dist/declarations/src/native/Canvas.d.ts +16 -0
- package/dist/declarations/src/native/events.d.ts +6 -0
- package/dist/declarations/src/native/hooks.d.ts +9 -0
- package/dist/declarations/src/native/index.d.ts +36 -0
- package/dist/declarations/src/native.d.ts +8 -0
- package/dist/declarations/src/three-types.d.ts +309 -319
- package/dist/declarations/src/web/Canvas.d.ts +13 -13
- package/dist/declarations/src/web/events.d.ts +5 -5
- package/dist/declarations/src/web/index.d.ts +33 -30
- package/dist/hooks-15c12e3e.esm.js +1539 -0
- package/dist/hooks-6526f63c.cjs.dev.js +1589 -0
- package/dist/hooks-7b7e01e6.cjs.prod.js +1589 -0
- package/dist/react-three-fiber.cjs.dev.js +261 -1596
- package/dist/react-three-fiber.cjs.prod.js +261 -1596
- package/dist/react-three-fiber.esm.js +222 -1554
- package/native/dist/react-three-fiber-native.cjs.d.ts +1 -0
- package/native/dist/react-three-fiber-native.cjs.dev.js +589 -0
- package/native/dist/react-three-fiber-native.cjs.js +7 -0
- package/native/dist/react-three-fiber-native.cjs.prod.js +589 -0
- package/native/dist/react-three-fiber-native.esm.js +537 -0
- package/native/package.json +5 -0
- package/package.json +18 -8
- package/readme.md +10 -10
- package/__mocks__/react-use-measure/index.ts +0 -22
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
import { c as createEvents, s as buildGraph, v as is, a as createLoop, b as createRenderer, e as createStore, f as context, g as dispose, i as isRenderer, h as extend, p as pick, o as omit } from '../../dist/hooks-15c12e3e.esm.js';
|
|
2
|
+
export { t as ReactThreeFiber, k as addAfterEffect, j as addEffect, l as addTail, f as context, g as dispose, h as extend, n as useFrame, q as useGraph, u as useStore, m as useThree } from '../../dist/hooks-15c12e3e.esm.js';
|
|
3
|
+
import _extends from '@babel/runtime/helpers/esm/extends';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import * as THREE from 'three';
|
|
6
|
+
import { PixelRatio, View, StyleSheet } from 'react-native';
|
|
7
|
+
import { GLView } from 'expo-gl';
|
|
8
|
+
import { DefaultEventPriority, ContinuousEventPriority, DiscreteEventPriority, ConcurrentRoot } from 'react-reconciler/constants';
|
|
9
|
+
import Pressability from 'react-native/Libraries/Pressability/Pressability';
|
|
10
|
+
import { Asset } from 'expo-asset';
|
|
11
|
+
import { readAsStringAsync } from 'expo-file-system';
|
|
12
|
+
import { decode } from 'base64-arraybuffer';
|
|
13
|
+
import { suspend, preload, clear } from 'suspend-react';
|
|
14
|
+
import 'react-reconciler';
|
|
15
|
+
import 'scheduler';
|
|
16
|
+
import 'zustand';
|
|
17
|
+
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
const EVENTS = {
|
|
20
|
+
PRESS: 'onPress',
|
|
21
|
+
PRESSIN: 'onPressIn',
|
|
22
|
+
PRESSOUT: 'onPressOut',
|
|
23
|
+
LONGPRESS: 'onLongPress',
|
|
24
|
+
HOVERIN: 'onHoverIn',
|
|
25
|
+
HOVEROUT: 'onHoverOut',
|
|
26
|
+
PRESSMOVE: 'onPressMove'
|
|
27
|
+
};
|
|
28
|
+
const DOM_EVENTS = {
|
|
29
|
+
[EVENTS.PRESS]: 'onClick',
|
|
30
|
+
[EVENTS.PRESSIN]: 'onPointerDown',
|
|
31
|
+
[EVENTS.PRESSOUT]: 'onPointerUp',
|
|
32
|
+
[EVENTS.LONGPRESS]: 'onDoubleClick',
|
|
33
|
+
[EVENTS.HOVERIN]: 'onPointerOver',
|
|
34
|
+
[EVENTS.HOVEROUT]: 'onPointerOut',
|
|
35
|
+
[EVENTS.PRESSMOVE]: 'onPointerMove'
|
|
36
|
+
}; // https://github.com/facebook/react/tree/main/packages/react-reconciler#getcurrenteventpriority
|
|
37
|
+
// Gives React a clue as to how import the current interaction is
|
|
38
|
+
|
|
39
|
+
function getEventPriority() {
|
|
40
|
+
var _window, _window$event;
|
|
41
|
+
|
|
42
|
+
let name = (_window = window) == null ? void 0 : (_window$event = _window.event) == null ? void 0 : _window$event.type;
|
|
43
|
+
|
|
44
|
+
switch (name) {
|
|
45
|
+
case EVENTS.PRESS:
|
|
46
|
+
case EVENTS.PRESSIN:
|
|
47
|
+
case EVENTS.PRESSOUT:
|
|
48
|
+
case EVENTS.LONGPRESS:
|
|
49
|
+
return DiscreteEventPriority;
|
|
50
|
+
|
|
51
|
+
case EVENTS.HOVERIN:
|
|
52
|
+
case EVENTS.HOVEROUT:
|
|
53
|
+
case EVENTS.PRESSMOVE:
|
|
54
|
+
return ContinuousEventPriority;
|
|
55
|
+
|
|
56
|
+
default:
|
|
57
|
+
return DefaultEventPriority;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function createTouchEvents(store) {
|
|
61
|
+
const {
|
|
62
|
+
handlePointer
|
|
63
|
+
} = createEvents(store);
|
|
64
|
+
|
|
65
|
+
const handleTouch = (event, name) => {
|
|
66
|
+
event.persist() // Apply offset
|
|
67
|
+
;
|
|
68
|
+
event.nativeEvent.offsetX = event.nativeEvent.pageX;
|
|
69
|
+
event.nativeEvent.offsetY = event.nativeEvent.pageY; // Emulate DOM event
|
|
70
|
+
|
|
71
|
+
const callback = handlePointer(DOM_EVENTS[name]);
|
|
72
|
+
return callback(event.nativeEvent);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
connected: false,
|
|
77
|
+
handlers: Object.values(EVENTS).reduce((acc, name) => ({ ...acc,
|
|
78
|
+
[name]: event => handleTouch(event, name)
|
|
79
|
+
}), {}),
|
|
80
|
+
connect: () => {
|
|
81
|
+
const {
|
|
82
|
+
set,
|
|
83
|
+
events
|
|
84
|
+
} = store.getState();
|
|
85
|
+
events.disconnect == null ? void 0 : events.disconnect();
|
|
86
|
+
const manager = new Pressability(events == null ? void 0 : events.handlers);
|
|
87
|
+
set(state => ({
|
|
88
|
+
events: { ...state.events,
|
|
89
|
+
connected: manager
|
|
90
|
+
}
|
|
91
|
+
}));
|
|
92
|
+
},
|
|
93
|
+
disconnect: () => {
|
|
94
|
+
const {
|
|
95
|
+
set,
|
|
96
|
+
events
|
|
97
|
+
} = store.getState();
|
|
98
|
+
|
|
99
|
+
if (events.connected) {
|
|
100
|
+
events.connected.reset();
|
|
101
|
+
set(state => ({
|
|
102
|
+
events: { ...state.events,
|
|
103
|
+
connected: false
|
|
104
|
+
}
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Generates an asset based on input type.
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
const getAsset = input => {
|
|
116
|
+
if (input instanceof Asset) return input;
|
|
117
|
+
|
|
118
|
+
switch (typeof input) {
|
|
119
|
+
case 'string':
|
|
120
|
+
return Asset.fromURI(input);
|
|
121
|
+
|
|
122
|
+
case 'number':
|
|
123
|
+
return Asset.fromModule(input);
|
|
124
|
+
|
|
125
|
+
default:
|
|
126
|
+
throw 'Invalid asset! Must be a URI or module.';
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Downloads from a local URI and decodes into an ArrayBuffer.
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
const toBuffer = async localUri => readAsStringAsync(localUri, {
|
|
135
|
+
encoding: 'base64'
|
|
136
|
+
}).then(decode);
|
|
137
|
+
|
|
138
|
+
function loadingFn(extensions, onProgress) {
|
|
139
|
+
return function (Proto, ...input) {
|
|
140
|
+
// Construct new loader and run extensions
|
|
141
|
+
const loader = new Proto();
|
|
142
|
+
if (extensions) extensions(loader); // Go through the urls and load them
|
|
143
|
+
|
|
144
|
+
return Promise.all(input.map(entry => new Promise(async (res, reject) => {
|
|
145
|
+
// Construct URL
|
|
146
|
+
const url = typeof entry === 'string' ? loader.path + entry : entry; // There's no Image in native, so we create & pass a data texture instead
|
|
147
|
+
|
|
148
|
+
if (loader instanceof THREE.TextureLoader) {
|
|
149
|
+
const asset = await getAsset(url).downloadAsync();
|
|
150
|
+
const texture = new THREE.Texture();
|
|
151
|
+
texture.isDataTexture = true;
|
|
152
|
+
texture.image = {
|
|
153
|
+
data: asset,
|
|
154
|
+
width: asset.width,
|
|
155
|
+
height: asset.height
|
|
156
|
+
};
|
|
157
|
+
texture.needsUpdate = true;
|
|
158
|
+
return res(texture);
|
|
159
|
+
} // Do similar for CubeTextures
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
if (loader instanceof THREE.CubeTextureLoader) {
|
|
163
|
+
const texture = new THREE.CubeTexture();
|
|
164
|
+
texture.isDataTexture = true;
|
|
165
|
+
texture.images = await Promise.all(url.map(async src => {
|
|
166
|
+
const asset = await getAsset(src).downloadAsync();
|
|
167
|
+
return {
|
|
168
|
+
data: asset,
|
|
169
|
+
width: asset.width,
|
|
170
|
+
height: asset.height
|
|
171
|
+
};
|
|
172
|
+
}));
|
|
173
|
+
texture.needsUpdate = true;
|
|
174
|
+
return res(texture);
|
|
175
|
+
} // If asset is external and not an Image, load it
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
if (url.startsWith != null && url.startsWith('http') && Proto.prototype.hasOwnProperty('load')) {
|
|
179
|
+
return loader.load(entry, data => {
|
|
180
|
+
if (data.scene) Object.assign(data, buildGraph(data.scene));
|
|
181
|
+
res(data);
|
|
182
|
+
}, onProgress, error => reject(`Could not load ${url}: ${error.message}`));
|
|
183
|
+
} // Otherwise, create a localUri and a file buffer
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
const {
|
|
187
|
+
localUri
|
|
188
|
+
} = await getAsset(url).downloadAsync();
|
|
189
|
+
const arrayBuffer = await toBuffer(localUri); // Parse it
|
|
190
|
+
|
|
191
|
+
const parsed = loader.parse == null ? void 0 : loader.parse(arrayBuffer, undefined, data => {
|
|
192
|
+
if (data.scene) Object.assign(data, buildGraph(data.scene));
|
|
193
|
+
res(data);
|
|
194
|
+
}, error => reject(`Could not load ${url}: ${error.message}`)); // Respect synchronous parsers which don't have callbacks
|
|
195
|
+
|
|
196
|
+
if (parsed) return res(parsed);
|
|
197
|
+
})));
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function useLoader(Proto, input, extensions, onProgress) {
|
|
202
|
+
// Use suspense to load async assets
|
|
203
|
+
const keys = Array.isArray(input) ? input : [input];
|
|
204
|
+
const results = suspend(loadingFn(extensions, onProgress), [Proto, ...keys], {
|
|
205
|
+
equal: is.equ
|
|
206
|
+
}); // Return the object/s
|
|
207
|
+
|
|
208
|
+
return Array.isArray(input) ? results : results[0];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
useLoader.preload = function (Proto, input, extensions) {
|
|
212
|
+
const keys = Array.isArray(input) ? input : [input];
|
|
213
|
+
return preload(loadingFn(extensions), [Proto, ...keys]);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
useLoader.clear = function (Proto, input) {
|
|
217
|
+
const keys = Array.isArray(input) ? input : [input];
|
|
218
|
+
return clear([Proto, ...keys]);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const roots = new Map();
|
|
222
|
+
const {
|
|
223
|
+
invalidate,
|
|
224
|
+
advance
|
|
225
|
+
} = createLoop(roots);
|
|
226
|
+
const {
|
|
227
|
+
reconciler,
|
|
228
|
+
applyProps
|
|
229
|
+
} = createRenderer(roots, getEventPriority);
|
|
230
|
+
|
|
231
|
+
const createRendererInstance = (gl, context) => {
|
|
232
|
+
// Create canvas shim
|
|
233
|
+
const canvas = {
|
|
234
|
+
width: context.drawingBufferWidth,
|
|
235
|
+
height: context.drawingBufferHeight,
|
|
236
|
+
style: {},
|
|
237
|
+
addEventListener: () => {},
|
|
238
|
+
removeEventListener: () => {},
|
|
239
|
+
clientHeight: context.drawingBufferHeight
|
|
240
|
+
}; // If a renderer is specified, return it
|
|
241
|
+
|
|
242
|
+
const customRenderer = typeof gl === 'function' ? gl(canvas, context) : gl;
|
|
243
|
+
if (isRenderer(customRenderer)) return customRenderer; // Create renderer and pass our canvas and its context
|
|
244
|
+
|
|
245
|
+
const renderer = new THREE.WebGLRenderer({
|
|
246
|
+
powerPreference: 'high-performance',
|
|
247
|
+
antialias: true,
|
|
248
|
+
alpha: true,
|
|
249
|
+
...gl,
|
|
250
|
+
canvas,
|
|
251
|
+
context
|
|
252
|
+
}); // Set color management
|
|
253
|
+
|
|
254
|
+
renderer.outputEncoding = THREE.sRGBEncoding;
|
|
255
|
+
renderer.toneMapping = THREE.ACESFilmicToneMapping; // Set GL props
|
|
256
|
+
|
|
257
|
+
if (gl) applyProps(renderer, gl); // Bind render to RN bridge
|
|
258
|
+
|
|
259
|
+
if (context.endFrameEXP) {
|
|
260
|
+
const renderFrame = renderer.render.bind(renderer);
|
|
261
|
+
|
|
262
|
+
renderer.render = (scene, camera) => {
|
|
263
|
+
renderFrame(scene, camera);
|
|
264
|
+
context.endFrameEXP();
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return renderer;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
function createRoot(context, config) {
|
|
272
|
+
return {
|
|
273
|
+
render: element => {
|
|
274
|
+
var _store;
|
|
275
|
+
|
|
276
|
+
let {
|
|
277
|
+
gl,
|
|
278
|
+
size = {
|
|
279
|
+
width: 0,
|
|
280
|
+
height: 0
|
|
281
|
+
},
|
|
282
|
+
events,
|
|
283
|
+
onCreated,
|
|
284
|
+
...props
|
|
285
|
+
} = config || {};
|
|
286
|
+
let root = roots.get(context);
|
|
287
|
+
let fiber = root == null ? void 0 : root.fiber;
|
|
288
|
+
let store = root == null ? void 0 : root.store;
|
|
289
|
+
let state = (_store = store) == null ? void 0 : _store.getState();
|
|
290
|
+
|
|
291
|
+
if (fiber && state) {
|
|
292
|
+
// When a root was found, see if any fundamental props must be changed or exchanged
|
|
293
|
+
// Check size
|
|
294
|
+
if (state.size.width !== size.width || state.size.height !== size.height) state.setSize(size.width, size.height); // Check frameloop
|
|
295
|
+
|
|
296
|
+
if (state.frameloop !== props.frameloop) state.setFrameloop(props.frameloop); // For some props we want to reset the entire root
|
|
297
|
+
// Changes to the color-space
|
|
298
|
+
|
|
299
|
+
const linearChanged = props.linear !== state.internal.lastProps.linear;
|
|
300
|
+
|
|
301
|
+
if (linearChanged) {
|
|
302
|
+
unmountComponentAtNode(context);
|
|
303
|
+
fiber = undefined;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (!fiber) {
|
|
308
|
+
// If no root has been found, make one
|
|
309
|
+
// Create gl
|
|
310
|
+
const glRenderer = createRendererInstance(gl, context); // Create store
|
|
311
|
+
|
|
312
|
+
store = createStore(applyProps, invalidate, advance, {
|
|
313
|
+
gl: glRenderer,
|
|
314
|
+
size,
|
|
315
|
+
...props,
|
|
316
|
+
// expo-gl can only render at native dpr/resolution
|
|
317
|
+
// https://github.com/expo/expo-three/issues/39
|
|
318
|
+
dpr: PixelRatio.get()
|
|
319
|
+
});
|
|
320
|
+
const state = store.getState(); // Create renderer
|
|
321
|
+
|
|
322
|
+
fiber = reconciler.createContainer(store, ConcurrentRoot, false, null); // Map it
|
|
323
|
+
|
|
324
|
+
roots.set(context, {
|
|
325
|
+
fiber,
|
|
326
|
+
store
|
|
327
|
+
}); // Store event manager internally and connect it
|
|
328
|
+
|
|
329
|
+
if (events) {
|
|
330
|
+
var _state$get$events$con, _state$get$events;
|
|
331
|
+
|
|
332
|
+
state.set({
|
|
333
|
+
events: events(store)
|
|
334
|
+
});
|
|
335
|
+
(_state$get$events$con = (_state$get$events = state.get().events).connect) == null ? void 0 : _state$get$events$con.call(_state$get$events, context);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (store && fiber) {
|
|
340
|
+
reconciler.updateContainer( /*#__PURE__*/React.createElement(Provider, {
|
|
341
|
+
store: store,
|
|
342
|
+
element: element,
|
|
343
|
+
onCreated: onCreated
|
|
344
|
+
}), fiber, null, () => undefined);
|
|
345
|
+
return store;
|
|
346
|
+
} else {
|
|
347
|
+
throw 'Error creating root!';
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
unmount: () => unmountComponentAtNode(context)
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function render(element, context, config = {}) {
|
|
355
|
+
console.warn('R3F.render is no longer supported in React 18. Use createRoot instead!');
|
|
356
|
+
return createRoot(context, config).render(element);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function Provider({
|
|
360
|
+
store,
|
|
361
|
+
element,
|
|
362
|
+
onCreated
|
|
363
|
+
}) {
|
|
364
|
+
React.useEffect(() => {
|
|
365
|
+
const state = store.getState(); // Flag the canvas active, rendering will now begin
|
|
366
|
+
|
|
367
|
+
state.set(state => ({
|
|
368
|
+
internal: { ...state.internal,
|
|
369
|
+
active: true
|
|
370
|
+
}
|
|
371
|
+
})); // Notifiy that init is completed, the scene graph exists, but nothing has yet rendered
|
|
372
|
+
|
|
373
|
+
if (onCreated) onCreated(state); // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
374
|
+
}, []);
|
|
375
|
+
return /*#__PURE__*/React.createElement(context.Provider, {
|
|
376
|
+
value: store
|
|
377
|
+
}, element);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function unmountComponentAtNode(context, callback) {
|
|
381
|
+
const root = roots.get(context);
|
|
382
|
+
const fiber = root == null ? void 0 : root.fiber;
|
|
383
|
+
|
|
384
|
+
if (fiber) {
|
|
385
|
+
const state = root == null ? void 0 : root.store.getState();
|
|
386
|
+
if (state) state.internal.active = false;
|
|
387
|
+
reconciler.updateContainer(null, fiber, null, () => {
|
|
388
|
+
if (state) {
|
|
389
|
+
setTimeout(() => {
|
|
390
|
+
try {
|
|
391
|
+
var _state$gl, _state$gl$renderLists, _state$gl2;
|
|
392
|
+
|
|
393
|
+
state.events.disconnect == null ? void 0 : state.events.disconnect();
|
|
394
|
+
(_state$gl = state.gl) == null ? void 0 : (_state$gl$renderLists = _state$gl.renderLists) == null ? void 0 : _state$gl$renderLists.dispose == null ? void 0 : _state$gl$renderLists.dispose();
|
|
395
|
+
(_state$gl2 = state.gl) == null ? void 0 : _state$gl2.forceContextLoss == null ? void 0 : _state$gl2.forceContextLoss();
|
|
396
|
+
dispose(state);
|
|
397
|
+
roots.delete(context);
|
|
398
|
+
if (callback) callback(context);
|
|
399
|
+
} catch (e) {
|
|
400
|
+
/* ... */
|
|
401
|
+
}
|
|
402
|
+
}, 500);
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const act = React.unstable_act;
|
|
409
|
+
|
|
410
|
+
function createPortal(children, container) {
|
|
411
|
+
return reconciler.createPortal(children, container, null, null);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
reconciler.injectIntoDevTools({
|
|
415
|
+
bundleType: process.env.NODE_ENV === 'production' ? 0 : 1,
|
|
416
|
+
rendererPackageName: '@react-three/fiber',
|
|
417
|
+
version: '18.0.0'
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
const CANVAS_PROPS = ['gl', 'events', 'shadows', 'linear', 'flat', 'orthographic', 'frameloop', 'performance', 'clock', 'raycaster', 'camera', 'onPointerMissed', 'onCreated'];
|
|
421
|
+
|
|
422
|
+
function Block({
|
|
423
|
+
set
|
|
424
|
+
}) {
|
|
425
|
+
React.useLayoutEffect(() => {
|
|
426
|
+
set(new Promise(() => null));
|
|
427
|
+
return () => set(false);
|
|
428
|
+
}, [set]);
|
|
429
|
+
return null;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
class ErrorBoundary extends React.Component {
|
|
433
|
+
constructor(...args) {
|
|
434
|
+
super(...args);
|
|
435
|
+
this.state = {
|
|
436
|
+
error: false
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
componentDidCatch(error) {
|
|
441
|
+
this.props.set(error);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
render() {
|
|
445
|
+
return this.state.error ? null : this.props.children;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
ErrorBoundary.getDerivedStateFromError = () => ({
|
|
451
|
+
error: true
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const Canvas = /*#__PURE__*/React.forwardRef(({
|
|
455
|
+
children,
|
|
456
|
+
fallback,
|
|
457
|
+
style,
|
|
458
|
+
events,
|
|
459
|
+
nativeRef_EXPERIMENTAL,
|
|
460
|
+
onContextCreate,
|
|
461
|
+
...props
|
|
462
|
+
}, forwardedRef) => {
|
|
463
|
+
// Create a known catalogue of Threejs-native elements
|
|
464
|
+
// This will include the entire THREE namespace by default, users can extend
|
|
465
|
+
// their own elements by using the createRoot API instead
|
|
466
|
+
React.useMemo(() => extend(THREE), []);
|
|
467
|
+
const [{
|
|
468
|
+
width,
|
|
469
|
+
height
|
|
470
|
+
}, setSize] = React.useState({
|
|
471
|
+
width: 0,
|
|
472
|
+
height: 0
|
|
473
|
+
});
|
|
474
|
+
const [context, setContext] = React.useState(null);
|
|
475
|
+
const [bind, setBind] = React.useState();
|
|
476
|
+
const canvasProps = pick(props, CANVAS_PROPS);
|
|
477
|
+
const viewProps = omit(props, CANVAS_PROPS);
|
|
478
|
+
const [block, setBlock] = React.useState(false);
|
|
479
|
+
const [error, setError] = React.useState(false); // Suspend this component if block is a promise (2nd run)
|
|
480
|
+
|
|
481
|
+
if (block) throw block; // Throw exception outwards if anything within canvas throws
|
|
482
|
+
|
|
483
|
+
if (error) throw error;
|
|
484
|
+
const onLayout = React.useCallback(e => {
|
|
485
|
+
const {
|
|
486
|
+
width,
|
|
487
|
+
height
|
|
488
|
+
} = e.nativeEvent.layout;
|
|
489
|
+
setSize({
|
|
490
|
+
width,
|
|
491
|
+
height
|
|
492
|
+
});
|
|
493
|
+
}, []);
|
|
494
|
+
|
|
495
|
+
if (width > 0 && height > 0 && context) {
|
|
496
|
+
const store = createRoot(context, { ...canvasProps,
|
|
497
|
+
size: {
|
|
498
|
+
width,
|
|
499
|
+
height
|
|
500
|
+
},
|
|
501
|
+
events: events || createTouchEvents
|
|
502
|
+
}).render( /*#__PURE__*/React.createElement(ErrorBoundary, {
|
|
503
|
+
set: setError
|
|
504
|
+
}, /*#__PURE__*/React.createElement(React.Suspense, {
|
|
505
|
+
fallback: /*#__PURE__*/React.createElement(Block, {
|
|
506
|
+
set: setBlock
|
|
507
|
+
})
|
|
508
|
+
}, children)));
|
|
509
|
+
const state = store.getState();
|
|
510
|
+
setBind(state.events.connected.getEventHandlers());
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
React.useEffect(() => {
|
|
514
|
+
return () => unmountComponentAtNode(context);
|
|
515
|
+
}, [context]);
|
|
516
|
+
return /*#__PURE__*/React.createElement(View, _extends({}, viewProps, {
|
|
517
|
+
ref: forwardedRef,
|
|
518
|
+
onLayout: onLayout,
|
|
519
|
+
style: {
|
|
520
|
+
flex: 1,
|
|
521
|
+
...style
|
|
522
|
+
}
|
|
523
|
+
}, bind), width > 0 && /*#__PURE__*/React.createElement(GLView, {
|
|
524
|
+
nativeRef_EXPERIMENTAL: ref => {
|
|
525
|
+
if (nativeRef_EXPERIMENTAL && !nativeRef_EXPERIMENTAL.current) {
|
|
526
|
+
nativeRef_EXPERIMENTAL.current = ref;
|
|
527
|
+
}
|
|
528
|
+
},
|
|
529
|
+
onContextCreate: async gl => {
|
|
530
|
+
await (onContextCreate == null ? void 0 : onContextCreate(gl));
|
|
531
|
+
setContext(gl);
|
|
532
|
+
},
|
|
533
|
+
style: StyleSheet.absoluteFill
|
|
534
|
+
}));
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
export { Canvas, roots as _roots, act, advance, applyProps, createPortal, createRoot, createTouchEvents as events, invalidate, reconciler, render, unmountComponentAtNode, useLoader };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-three/fiber",
|
|
3
|
-
"version": "8.0.0-
|
|
3
|
+
"version": "8.0.0-beta-03",
|
|
4
4
|
"description": "A React renderer for Threejs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"author": "Paul Henschel (https://github.com/drcmda)",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"maintainers": [
|
|
15
|
-
"Josh Ellis (https://github.com/joshuaellis)"
|
|
15
|
+
"Josh Ellis (https://github.com/joshuaellis)",
|
|
16
|
+
"Cody Bennett (https://github.com/codyjasonbennett)"
|
|
16
17
|
],
|
|
17
18
|
"bugs": {
|
|
18
19
|
"url": "https://github.com/pmndrs/react-three-fiber/issues"
|
|
@@ -29,11 +30,12 @@
|
|
|
29
30
|
"main": "dist/react-three-fiber.cjs.js",
|
|
30
31
|
"module": "dist/react-three-fiber.esm.js",
|
|
31
32
|
"types": "dist/react-three-fiber.cjs.d.ts",
|
|
32
|
-
"react-native": "dist/native.js",
|
|
33
|
+
"react-native": "native/dist/react-three-fiber-native.cjs.js",
|
|
33
34
|
"sideEffects": false,
|
|
34
35
|
"preconstruct": {
|
|
35
36
|
"entrypoints": [
|
|
36
|
-
"index.tsx"
|
|
37
|
+
"index.tsx",
|
|
38
|
+
"native.tsx"
|
|
37
39
|
]
|
|
38
40
|
},
|
|
39
41
|
"scripts": {
|
|
@@ -41,23 +43,31 @@
|
|
|
41
43
|
},
|
|
42
44
|
"dependencies": {
|
|
43
45
|
"@babel/runtime": "^7.15.4",
|
|
46
|
+
"base64-arraybuffer": "^1.0.1",
|
|
47
|
+
"expo-asset": "^8.4.3",
|
|
48
|
+
"expo-file-system": "^13.0.3",
|
|
49
|
+
"expo-gl": "^11.0.3",
|
|
44
50
|
"react-merge-refs": "^1.1.0",
|
|
45
|
-
"react-reconciler": "0.0.0
|
|
46
|
-
"react-use-measure": "^2.
|
|
51
|
+
"react-reconciler": "^0.27.0-rc.0",
|
|
52
|
+
"react-use-measure": "^2.1.1",
|
|
47
53
|
"resize-observer-polyfill": "^1.5.1",
|
|
48
|
-
"scheduler": "0.0.0
|
|
49
|
-
"
|
|
54
|
+
"scheduler": "0.21.0-rc.0",
|
|
55
|
+
"suspend-react": "^0.0.8",
|
|
50
56
|
"utility-types": "^3.10.0",
|
|
51
57
|
"zustand": "^3.5.10"
|
|
52
58
|
},
|
|
53
59
|
"peerDependencies": {
|
|
54
60
|
"react": ">=18.0",
|
|
55
61
|
"react-dom": ">=18.0",
|
|
62
|
+
"react-native": ">=0.64",
|
|
56
63
|
"three": ">=0.132"
|
|
57
64
|
},
|
|
58
65
|
"peerDependenciesMeta": {
|
|
59
66
|
"react-dom": {
|
|
60
67
|
"optional": true
|
|
68
|
+
},
|
|
69
|
+
"react-native": {
|
|
70
|
+
"optional": true
|
|
61
71
|
}
|
|
62
72
|
}
|
|
63
73
|
}
|
package/readme.md
CHANGED
|
@@ -156,7 +156,7 @@ ReactDOM.render(
|
|
|
156
156
|
You need to be versed in both React and Threejs before rushing into this. If you are unsure about React consult the official [React docs](https://reactjs.org/docs/getting-started.html), especially [the section about hooks](https://reactjs.org/docs/hooks-reference.html). As for Threejs, make sure you at least glance over the following links:
|
|
157
157
|
|
|
158
158
|
1. Make sure you have a [basic grasp of Threejs](https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene). Keep that site open.
|
|
159
|
-
2. When you know what a scene is, a camera, mesh, geometry, material, fork the [demo above](https://github.com/
|
|
159
|
+
2. When you know what a scene is, a camera, mesh, geometry, material, fork the [demo above](https://github.com/pmndrs/react-three-fiber#what-does-it-look-like).
|
|
160
160
|
3. [Look up](https://threejs.org/docs/index.html#api/en/objects/Mesh) the JSX elements that you see (mesh, ambientLight, etc), _all_ threejs exports are native to three-fiber.
|
|
161
161
|
4. Try changing some values, scroll though our [Api](/markdown/api.md) to see what the various settings and hooks do.
|
|
162
162
|
|
|
@@ -171,15 +171,15 @@ Some reading material:
|
|
|
171
171
|
|
|
172
172
|
# Ecosystem
|
|
173
173
|
|
|
174
|
-
- [`@react-three/gltfjsx`](https://github.com/
|
|
175
|
-
- [`@react-three/drei`](https://github.com/
|
|
176
|
-
- [`@react-three/postprocessing`](https://github.com/
|
|
177
|
-
- [`@react-three/flex`](https://github.com/
|
|
178
|
-
- [`@react-three/xr`](https://github.com/
|
|
179
|
-
- [`@react-three/cannon`](https://github.com/
|
|
180
|
-
- [`zustand`](https://github.com/
|
|
181
|
-
- [`react-spring`](https://github.com/
|
|
182
|
-
- [`react-use-gesture`](https://github.com/
|
|
174
|
+
- [`@react-three/gltfjsx`](https://github.com/pmndrs/gltfjsx) – turns GLTFs into JSX components
|
|
175
|
+
- [`@react-three/drei`](https://github.com/pmndrs/drei) – useful helpers for react-three-fiber
|
|
176
|
+
- [`@react-three/postprocessing`](https://github.com/pmndrs/react-postprocessing) – post-processing effects
|
|
177
|
+
- [`@react-three/flex`](https://github.com/pmndrs/react-three-flex) – flexbox for react-three-fiber
|
|
178
|
+
- [`@react-three/xr`](https://github.com/pmndrs/react-xr) – VR/AR controllers and events
|
|
179
|
+
- [`@react-three/cannon`](https://github.com/pmndrs/use-cannon) – physics based hooks
|
|
180
|
+
- [`zustand`](https://github.com/pmndrs/zustand) – state management
|
|
181
|
+
- [`react-spring`](https://github.com/pmndrs/react-spring) – a spring-physics-based animation library
|
|
182
|
+
- [`react-use-gesture`](https://github.com/pmndrs/react-use-gesture) – mouse/touch gestures
|
|
183
183
|
|
|
184
184
|
# How to contribute
|
|
185
185
|
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import * as React from 'react'
|
|
2
|
-
|
|
3
|
-
export default function useMeasure() {
|
|
4
|
-
const element = React.useRef<HTMLElement | null>(null)
|
|
5
|
-
const [bounds] = React.useState({
|
|
6
|
-
left: 0,
|
|
7
|
-
top: 0,
|
|
8
|
-
width: 1280,
|
|
9
|
-
height: 800,
|
|
10
|
-
bottom: 0,
|
|
11
|
-
right: 0,
|
|
12
|
-
x: 0,
|
|
13
|
-
y: 0,
|
|
14
|
-
})
|
|
15
|
-
const ref = (node: HTMLElement) => {
|
|
16
|
-
if (!node || element.current) {
|
|
17
|
-
return
|
|
18
|
-
}
|
|
19
|
-
element.current = node
|
|
20
|
-
}
|
|
21
|
-
return [ref, bounds]
|
|
22
|
-
}
|