@react-three/drei 8.3.0 → 8.6.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.
@@ -0,0 +1,39 @@
1
+ import { useThree, useLoader } from '@react-three/fiber';
2
+ import { useEffect } from 'react';
3
+ import { KTX2Loader } from 'three-stdlib';
4
+ import { IsObject } from './useTexture.js';
5
+
6
+ const cdn = 'https://cdn.jsdelivr.net/gh/pmndrs/drei-assets@master';
7
+ function useKTX2(input, basisPath = `${cdn}/basis/`) {
8
+ const gl = useThree(state => state.gl);
9
+ const textures = useLoader(KTX2Loader, IsObject(input) ? Object.values(input) : input, loader => {
10
+ loader.detectSupport(gl);
11
+ loader.setTranscoderPath(basisPath);
12
+ }); // https://github.com/mrdoob/three.js/issues/22696
13
+ // Upload the texture to the GPU immediately instead of waiting for the first render
14
+
15
+ useEffect(() => {
16
+ const array = Array.isArray(textures) ? textures : [textures];
17
+ array.forEach(gl.initTexture);
18
+ }, [gl, textures]);
19
+
20
+ if (IsObject(input)) {
21
+ const keys = Object.keys(input);
22
+ const keyed = {};
23
+ keys.forEach(key => Object.assign(keyed, {
24
+ [key]: textures[keys.indexOf(key)]
25
+ }));
26
+ return keyed;
27
+ } else {
28
+ return textures;
29
+ }
30
+ }
31
+
32
+ useKTX2.preload = (url, basisPath = `${cdn}/basis/`) => useLoader.preload(KTX2Loader, url, loader => {
33
+ loader.setTranscoderPath(basisPath);
34
+ }); // @ts-expect-error new in r3f 7.0.5
35
+
36
+
37
+ useKTX2.clear = input => useLoader.clear(KTX2Loader, input);
38
+
39
+ export { useKTX2 };
@@ -1 +1 @@
1
- "use strict";function e(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var o=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,o.get?o:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}Object.defineProperty(exports,"__esModule",{value:!0});var r=e(require("three"));class t extends r.Group{constructor(){super(),this.color=new r.Color("white")}}exports.Position=t;
1
+ "use strict";function e(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)}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("three"));const r=new t.Matrix4,n=new t.Matrix4,i=[],s=new t.Mesh;class o extends t.Group{constructor(){super(),this.color=new t.Color("white"),this.instance={current:void 0},this.instanceKey={current:void 0}}get geometry(){var e;return null==(e=this.instance.current)?void 0:e.geometry}raycast(e,t){const o=this.instance.current;if(!o)return;if(!o.geometry||!o.material)return;s.geometry=o.geometry;const c=o.matrixWorld;let a=o.userData.instances.indexOf(this.instanceKey);if(-1!==a){o.getMatrixAt(a,r),n.multiplyMatrices(c,r),s.matrixWorld=n,s.raycast(e,i);for(let e=0,r=i.length;e<r;e++){const r=i[e];r.instanceId=a,r.object=this,t.push(r)}i.length=0}}}exports.Position=o;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import * as THREE from 'three';
2
3
  import { ReactThreeFiber } from '@react-three/fiber';
3
4
  declare global {
@@ -9,5 +10,9 @@ declare global {
9
10
  }
10
11
  export declare class Position extends THREE.Group {
11
12
  color: THREE.Color;
13
+ instance: React.MutableRefObject<THREE.InstancedMesh | undefined>;
14
+ instanceKey: React.MutableRefObject<JSX.IntrinsicElements['position'] | undefined>;
12
15
  constructor();
16
+ get geometry(): THREE.BufferGeometry | undefined;
17
+ raycast(raycaster: any, intersects: any): void;
13
18
  }
@@ -1,9 +1,60 @@
1
1
  import * as THREE from 'three';
2
2
 
3
+ const _instanceLocalMatrix = /*@__PURE__*/new THREE.Matrix4();
4
+
5
+ const _instanceWorldMatrix = /*@__PURE__*/new THREE.Matrix4();
6
+
7
+ const _instanceIntersects = [];
8
+
9
+ const _mesh = /*@__PURE__*/new THREE.Mesh();
10
+
3
11
  class Position extends THREE.Group {
4
12
  constructor() {
5
13
  super();
6
14
  this.color = new THREE.Color('white');
15
+ this.instance = {
16
+ current: undefined
17
+ };
18
+ this.instanceKey = {
19
+ current: undefined
20
+ };
21
+ } // This will allow the virtual instance have bounds
22
+
23
+
24
+ get geometry() {
25
+ var _this$instance$curren;
26
+
27
+ return (_this$instance$curren = this.instance.current) == null ? void 0 : _this$instance$curren.geometry;
28
+ } // And this will allow the virtual instance to receive events
29
+
30
+
31
+ raycast(raycaster, intersects) {
32
+ const parent = this.instance.current;
33
+ if (!parent) return;
34
+ if (!parent.geometry || !parent.material) return;
35
+ _mesh.geometry = parent.geometry;
36
+ const matrixWorld = parent.matrixWorld;
37
+ let instanceId = parent.userData.instances.indexOf(this.instanceKey);
38
+ if (instanceId === -1) return; // calculate the world matrix for each instance
39
+
40
+ parent.getMatrixAt(instanceId, _instanceLocalMatrix);
41
+
42
+ _instanceWorldMatrix.multiplyMatrices(matrixWorld, _instanceLocalMatrix); // the mesh represents this single instance
43
+
44
+
45
+ _mesh.matrixWorld = _instanceWorldMatrix;
46
+
47
+ _mesh.raycast(raycaster, _instanceIntersects); // process the result of raycast
48
+
49
+
50
+ for (let i = 0, l = _instanceIntersects.length; i < l; i++) {
51
+ const intersect = _instanceIntersects[i];
52
+ intersect.instanceId = instanceId;
53
+ intersect.object = this;
54
+ intersects.push(intersect);
55
+ }
56
+
57
+ _instanceIntersects.length = 0;
7
58
  }
8
59
 
9
60
  }