@react-three/fiber 8.0.14 → 8.0.17

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.
@@ -1,10 +1,8 @@
1
- import * as THREE from 'three';
2
- import { Asset } from 'expo-asset';
3
- import { c as createEvents, e as extend, u as useMutableCallback, a as createRoot, E as ErrorBoundary, B as Block, d as unmountComponentAtNode } from '../../dist/index-c0771a1d.esm.js';
4
- export { t as ReactThreeFiber, s as _roots, q as act, n as addAfterEffect, m as addEffect, o as addTail, l as advance, i as applyProps, f as context, g as createPortal, a as createRoot, j as dispose, e as extend, p as getRootState, k as invalidate, h as reconciler, r as render, d as unmountComponentAtNode, x as useFrame, y as useGraph, z as useLoader, v as useStore, w as useThree } from '../../dist/index-c0771a1d.esm.js';
1
+ import { c as createEvents, e as extend, u as useMutableCallback, a as createRoot, E as ErrorBoundary, B as Block, d as unmountComponentAtNode } from '../../dist/index-136ca0b0.esm.js';
2
+ export { t as ReactThreeFiber, s as _roots, q as act, n as addAfterEffect, m as addEffect, o as addTail, l as advance, i as applyProps, f as context, g as createPortal, a as createRoot, j as dispose, e as extend, p as getRootState, k as invalidate, h as reconciler, r as render, d as unmountComponentAtNode, x as useFrame, y as useGraph, z as useLoader, v as useStore, w as useThree } from '../../dist/index-136ca0b0.esm.js';
5
3
  import _extends from '@babel/runtime/helpers/esm/extends';
6
4
  import * as React from 'react';
7
- import mergeRefs from 'react-merge-refs';
5
+ import * as THREE from 'three';
8
6
  import { PixelRatio, View, StyleSheet } from 'react-native';
9
7
  import { GLView } from 'expo-gl';
10
8
  import Pressability from 'react-native/Libraries/Pressability/Pressability';
@@ -97,6 +95,114 @@ function createTouchEvents(store) {
97
95
  };
98
96
  }
99
97
 
98
+ // Check if expo-asset is installed (available with expo modules)
99
+ let expAsset;
100
+
101
+ try {
102
+ var _require;
103
+
104
+ expAsset = (_require = require('expo-asset')) == null ? void 0 : _require.Asset;
105
+ } catch (_) {}
106
+ /**
107
+ * Generates an asset based on input type.
108
+ */
109
+
110
+
111
+ function getAsset(input) {
112
+ switch (typeof input) {
113
+ case 'string':
114
+ return expAsset.fromURI(input);
115
+
116
+ case 'number':
117
+ return expAsset.fromModule(input);
118
+
119
+ default:
120
+ throw 'Invalid asset! Must be a URI or module.';
121
+ }
122
+ }
123
+
124
+ let injected = false;
125
+ function polyfills() {
126
+ if (!expAsset || injected) return;
127
+ injected = true; // Don't pre-process urls, let expo-asset generate an absolute URL
128
+
129
+ const extractUrlBase = THREE.LoaderUtils.extractUrlBase.bind(THREE.LoaderUtils);
130
+
131
+ THREE.LoaderUtils.extractUrlBase = url => typeof url === 'string' ? extractUrlBase(url) : './'; // There's no Image in native, so create a data texture instead
132
+
133
+
134
+ const prevTextureLoad = THREE.TextureLoader.prototype.load;
135
+
136
+ THREE.TextureLoader.prototype.load = function load(url, onLoad, onProgress, onError) {
137
+ const texture = new THREE.Texture(); // @ts-ignore
138
+
139
+ texture.isDataTexture = true;
140
+ getAsset(url).downloadAsync().then(asset => {
141
+ texture.image = {
142
+ data: asset,
143
+ width: asset.width,
144
+ height: asset.height
145
+ };
146
+ texture.flipY = true;
147
+ texture.unpackAlignment = 1;
148
+ texture.needsUpdate = true;
149
+ onLoad == null ? void 0 : onLoad(texture);
150
+ }).catch(onError);
151
+ return texture;
152
+ }; // Fetches assets via XMLHttpRequest
153
+
154
+
155
+ const prevFileLoad = THREE.FileLoader.prototype.load;
156
+
157
+ THREE.FileLoader.prototype.load = function (url, onLoad, onProgress, onError) {
158
+ if (this.path) url = this.path + url;
159
+ const request = new XMLHttpRequest();
160
+ getAsset(url).downloadAsync().then(asset => {
161
+ request.open('GET', asset.uri, true);
162
+ request.addEventListener('load', event => {
163
+ if (request.status === 200) {
164
+ onLoad == null ? void 0 : onLoad(request.response);
165
+ this.manager.itemEnd(url);
166
+ } else {
167
+ onError == null ? void 0 : onError(event);
168
+ this.manager.itemError(url);
169
+ this.manager.itemEnd(url);
170
+ }
171
+ }, false);
172
+ request.addEventListener('progress', event => {
173
+ onProgress == null ? void 0 : onProgress(event);
174
+ }, false);
175
+ request.addEventListener('error', event => {
176
+ onError == null ? void 0 : onError(event);
177
+ this.manager.itemError(url);
178
+ this.manager.itemEnd(url);
179
+ }, false);
180
+ request.addEventListener('abort', event => {
181
+ onError == null ? void 0 : onError(event);
182
+ this.manager.itemError(url);
183
+ this.manager.itemEnd(url);
184
+ }, false);
185
+ if (this.responseType) request.responseType = this.responseType;
186
+ if (this.withCredentials) request.withCredentials = this.withCredentials;
187
+
188
+ for (const header in this.requestHeader) {
189
+ request.setRequestHeader(header, this.requestHeader[header]);
190
+ }
191
+
192
+ request.send(null);
193
+ this.manager.itemStart(url);
194
+ });
195
+ return request;
196
+ }; // Cleanup function
197
+
198
+
199
+ return () => {
200
+ THREE.LoaderUtils.extractUrlBase = extractUrlBase;
201
+ THREE.TextureLoader.prototype.load = prevTextureLoad;
202
+ THREE.FileLoader.prototype.load = prevFileLoad;
203
+ };
204
+ }
205
+
100
206
  /**
101
207
  * A native canvas which accepts threejs elements as children.
102
208
  * @see https://docs.pmnd.rs/react-three-fiber/api/canvas
@@ -132,6 +238,7 @@ const Canvas = /*#__PURE__*/React.forwardRef(({
132
238
  });
133
239
  const [canvas, setCanvas] = React.useState(null);
134
240
  const [bind, setBind] = React.useState();
241
+ React.useImperativeHandle(forwardedRef, () => viewRef.current);
135
242
  const handlePointerMissed = useMutableCallback(onPointerMissed);
136
243
  const [block, setBlock] = React.useState(false);
137
244
  const [error, setError] = React.useState(false); // Suspend this component if block is a promise (2nd run)
@@ -140,7 +247,9 @@ const Canvas = /*#__PURE__*/React.forwardRef(({
140
247
 
141
248
  if (error) throw error;
142
249
  const viewRef = React.useRef(null);
143
- const root = React.useRef(null);
250
+ const root = React.useRef(null); // Inject and cleanup RN polyfills if able
251
+
252
+ React.useLayoutEffect(() => polyfills(), []);
144
253
  const onLayout = React.useCallback(e => {
145
254
  const {
146
255
  width,
@@ -219,7 +328,7 @@ const Canvas = /*#__PURE__*/React.forwardRef(({
219
328
  }
220
329
  }, [canvas]);
221
330
  return /*#__PURE__*/React.createElement(View, _extends({}, props, {
222
- ref: mergeRefs([viewRef, forwardedRef]),
331
+ ref: viewRef,
223
332
  onLayout: onLayout,
224
333
  style: {
225
334
  flex: 1,
@@ -231,87 +340,4 @@ const Canvas = /*#__PURE__*/React.forwardRef(({
231
340
  }));
232
341
  });
233
342
 
234
- /**
235
- * Generates an asset based on input type.
236
- */
237
-
238
- const getAsset = input => {
239
- if (input instanceof Asset) return input;
240
-
241
- switch (typeof input) {
242
- case 'string':
243
- return Asset.fromURI(input);
244
-
245
- case 'number':
246
- return Asset.fromModule(input);
247
-
248
- default:
249
- throw 'Invalid asset! Must be a URI or module.';
250
- }
251
- }; // Don't pre-process urls, let expo-asset generate an absolute URL
252
-
253
-
254
- const extractUrlBase = THREE.LoaderUtils.extractUrlBase.bind(THREE.LoaderUtils);
255
-
256
- THREE.LoaderUtils.extractUrlBase = url => typeof url === 'string' ? extractUrlBase(url) : './'; // There's no Image in native, so create a data texture instead
257
-
258
-
259
- THREE.TextureLoader.prototype.load = function load(url, onLoad, onProgress, onError) {
260
- const texture = new THREE.Texture(); // @ts-ignore
261
-
262
- texture.isDataTexture = true;
263
- getAsset(url).downloadAsync().then(asset => {
264
- texture.image = {
265
- data: asset,
266
- width: asset.width,
267
- height: asset.height
268
- };
269
- texture.needsUpdate = true;
270
- onLoad == null ? void 0 : onLoad(texture);
271
- }).catch(onError);
272
- return texture;
273
- }; // Fetches assets via XMLHttpRequest
274
-
275
-
276
- THREE.FileLoader.prototype.load = function (url, onLoad, onProgress, onError) {
277
- if (this.path) url = this.path + url;
278
- const request = new XMLHttpRequest();
279
- getAsset(url).downloadAsync().then(asset => {
280
- request.open('GET', asset.uri, true);
281
- request.addEventListener('load', event => {
282
- if (request.status === 200) {
283
- onLoad == null ? void 0 : onLoad(request.response);
284
- this.manager.itemEnd(url);
285
- } else {
286
- onError == null ? void 0 : onError(event);
287
- this.manager.itemError(url);
288
- this.manager.itemEnd(url);
289
- }
290
- }, false);
291
- request.addEventListener('progress', event => {
292
- onProgress == null ? void 0 : onProgress(event);
293
- }, false);
294
- request.addEventListener('error', event => {
295
- onError == null ? void 0 : onError(event);
296
- this.manager.itemError(url);
297
- this.manager.itemEnd(url);
298
- }, false);
299
- request.addEventListener('abort', event => {
300
- onError == null ? void 0 : onError(event);
301
- this.manager.itemError(url);
302
- this.manager.itemEnd(url);
303
- }, false);
304
- if (this.responseType) request.responseType = this.responseType;
305
- if (this.withCredentials) request.withCredentials = this.withCredentials;
306
-
307
- for (const header in this.requestHeader) {
308
- request.setRequestHeader(header, this.requestHeader[header]);
309
- }
310
-
311
- request.send(null);
312
- this.manager.itemStart(url);
313
- });
314
- return request;
315
- };
316
-
317
343
  export { Canvas, createTouchEvents as events };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/fiber",
3
- "version": "8.0.14",
3
+ "version": "8.0.17",
4
4
  "description": "A React renderer for Threejs",
5
5
  "keywords": [
6
6
  "react",
@@ -43,8 +43,7 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@babel/runtime": "^7.17.8",
46
- "@types/react-reconciler": "^0.26.6",
47
- "react-merge-refs": "^1.1.0",
46
+ "@types/react-reconciler": "^0.26.7",
48
47
  "react-reconciler": "^0.27.0",
49
48
  "react-use-measure": "^2.1.1",
50
49
  "scheduler": "^0.21.0",