@needle-tools/engine 2.35.5-pre → 2.37.0-pre

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 (168) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/needle-engine.d.ts +312 -232
  3. package/dist/needle-engine.js +456 -437
  4. package/dist/needle-engine.js.map +4 -4
  5. package/dist/needle-engine.min.js +81 -76
  6. package/dist/needle-engine.min.js.map +4 -4
  7. package/lib/engine/api.d.ts +1 -0
  8. package/lib/engine/api.js +1 -0
  9. package/lib/engine/api.js.map +1 -1
  10. package/lib/engine/debug/debug.d.ts +1 -0
  11. package/lib/engine/debug/debug.js +3 -0
  12. package/lib/engine/debug/debug.js.map +1 -1
  13. package/lib/engine/debug/debug_overlay.js +12 -1
  14. package/lib/engine/debug/debug_overlay.js.map +1 -1
  15. package/lib/engine/engine_element_loading.js +1 -1
  16. package/lib/engine/engine_element_loading.js.map +1 -1
  17. package/lib/engine/engine_gameobject.d.ts +1 -0
  18. package/lib/engine/engine_gameobject.js +15 -2
  19. package/lib/engine/engine_gameobject.js.map +1 -1
  20. package/lib/engine/engine_gltf_builtin_components.js +4 -0
  21. package/lib/engine/engine_gltf_builtin_components.js.map +1 -1
  22. package/lib/engine/engine_mainloop_utils.d.ts +1 -1
  23. package/lib/engine/engine_mainloop_utils.js +7 -3
  24. package/lib/engine/engine_mainloop_utils.js.map +1 -1
  25. package/lib/engine/engine_physics.d.ts +49 -57
  26. package/lib/engine/engine_physics.js +464 -417
  27. package/lib/engine/engine_physics.js.map +1 -1
  28. package/lib/engine/engine_physics.types.d.ts +16 -0
  29. package/lib/engine/engine_physics.types.js +19 -0
  30. package/lib/engine/engine_physics.types.js.map +1 -0
  31. package/lib/engine/engine_serialization_core.d.ts +3 -0
  32. package/lib/engine/engine_serialization_core.js +19 -6
  33. package/lib/engine/engine_serialization_core.js.map +1 -1
  34. package/lib/engine/engine_setup.js +4 -2
  35. package/lib/engine/engine_setup.js.map +1 -1
  36. package/lib/engine/engine_time.d.ts +1 -0
  37. package/lib/engine/engine_time.js +1 -0
  38. package/lib/engine/engine_time.js.map +1 -1
  39. package/lib/engine/engine_types.d.ts +46 -26
  40. package/lib/engine/engine_types.js +24 -37
  41. package/lib/engine/engine_types.js.map +1 -1
  42. package/lib/engine/engine_typestore.d.ts +1 -0
  43. package/lib/engine/engine_typestore.js +1 -0
  44. package/lib/engine/engine_typestore.js.map +1 -1
  45. package/lib/engine/engine_util_decorator.d.ts +6 -0
  46. package/lib/engine/engine_util_decorator.js +54 -0
  47. package/lib/engine/engine_util_decorator.js.map +1 -0
  48. package/lib/engine/engine_utils.d.ts +1 -1
  49. package/lib/engine/engine_utils.js +2 -2
  50. package/lib/engine/engine_utils.js.map +1 -1
  51. package/lib/engine/extensions/NEEDLE_animator_controller_model.js.map +1 -1
  52. package/lib/engine/extensions/NEEDLE_gameobject_data.js +2 -0
  53. package/lib/engine/extensions/NEEDLE_gameobject_data.js.map +1 -1
  54. package/lib/engine/extensions/NEEDLE_techniques_webgl.js +5 -0
  55. package/lib/engine/extensions/NEEDLE_techniques_webgl.js.map +1 -1
  56. package/lib/engine-components/Animation.d.ts +5 -0
  57. package/lib/engine-components/Animation.js +14 -0
  58. package/lib/engine-components/Animation.js.map +1 -1
  59. package/lib/engine-components/AnimatorController.d.ts +1 -0
  60. package/lib/engine-components/AnimatorController.js +14 -7
  61. package/lib/engine-components/AnimatorController.js.map +1 -1
  62. package/lib/engine-components/BoxHelperComponent.d.ts +2 -2
  63. package/lib/engine-components/BoxHelperComponent.js +28 -9
  64. package/lib/engine-components/BoxHelperComponent.js.map +1 -1
  65. package/lib/engine-components/Collider.d.ts +7 -2
  66. package/lib/engine-components/Collider.js +27 -15
  67. package/lib/engine-components/Collider.js.map +1 -1
  68. package/lib/engine-components/Component.d.ts +8 -16
  69. package/lib/engine-components/Component.js +18 -117
  70. package/lib/engine-components/Component.js.map +1 -1
  71. package/lib/engine-components/DragControls.js +9 -6
  72. package/lib/engine-components/DragControls.js.map +1 -1
  73. package/lib/engine-components/GroundProjection.d.ts +2 -0
  74. package/lib/engine-components/GroundProjection.js +18 -6
  75. package/lib/engine-components/GroundProjection.js.map +1 -1
  76. package/lib/engine-components/NavMesh.d.ts +0 -5
  77. package/lib/engine-components/NavMesh.js +100 -10
  78. package/lib/engine-components/NavMesh.js.map +1 -1
  79. package/lib/engine-components/NestedGltf.js +2 -0
  80. package/lib/engine-components/NestedGltf.js.map +1 -1
  81. package/lib/engine-components/ReflectionProbe.d.ts +22 -0
  82. package/lib/engine-components/ReflectionProbe.js +134 -0
  83. package/lib/engine-components/ReflectionProbe.js.map +1 -0
  84. package/lib/engine-components/Renderer.d.ts +13 -2
  85. package/lib/engine-components/Renderer.js +96 -45
  86. package/lib/engine-components/Renderer.js.map +1 -1
  87. package/lib/engine-components/RigidBody.d.ts +40 -25
  88. package/lib/engine-components/RigidBody.js +253 -142
  89. package/lib/engine-components/RigidBody.js.map +1 -1
  90. package/lib/engine-components/SpatialTrigger.js +1 -1
  91. package/lib/engine-components/SpatialTrigger.js.map +1 -1
  92. package/lib/engine-components/SpectatorCamera.d.ts +1 -0
  93. package/lib/engine-components/SpectatorCamera.js +9 -2
  94. package/lib/engine-components/SpectatorCamera.js.map +1 -1
  95. package/lib/engine-components/SpringJoint.d.ts +0 -13
  96. package/lib/engine-components/SpringJoint.js +42 -41
  97. package/lib/engine-components/SpringJoint.js.map +1 -1
  98. package/lib/engine-components/VideoPlayer.js.map +1 -1
  99. package/lib/engine-components/WebARSessionRoot.d.ts +7 -7
  100. package/lib/engine-components/WebARSessionRoot.js +7 -7
  101. package/lib/engine-components/WebARSessionRoot.js.map +1 -1
  102. package/lib/engine-components/WebXR.d.ts +10 -8
  103. package/lib/engine-components/WebXR.js +50 -26
  104. package/lib/engine-components/WebXR.js.map +1 -1
  105. package/lib/engine-components/WebXRAvatar.d.ts +4 -5
  106. package/lib/engine-components/WebXRAvatar.js +9 -8
  107. package/lib/engine-components/WebXRAvatar.js.map +1 -1
  108. package/lib/engine-components/WebXRController.d.ts +21 -21
  109. package/lib/engine-components/WebXRController.js +90 -68
  110. package/lib/engine-components/WebXRController.js.map +1 -1
  111. package/lib/engine-components/WebXRGrabRendering.d.ts +3 -3
  112. package/lib/engine-components/WebXRGrabRendering.js +2 -2
  113. package/lib/engine-components/WebXRGrabRendering.js.map +1 -1
  114. package/lib/engine-components/WebXRSync.d.ts +8 -8
  115. package/lib/engine-components/WebXRSync.js +15 -15
  116. package/lib/engine-components/WebXRSync.js.map +1 -1
  117. package/lib/engine-components/codegen/components.d.ts +2 -3
  118. package/lib/engine-components/codegen/components.js +2 -3
  119. package/lib/engine-components/codegen/components.js.map +1 -1
  120. package/lib/engine-components/ui/EventSystem.d.ts +1 -0
  121. package/lib/engine-components/ui/EventSystem.js +21 -1
  122. package/lib/engine-components/ui/EventSystem.js.map +1 -1
  123. package/package.json +3 -4
  124. package/src/engine/api.ts +2 -1
  125. package/src/engine/codegen/register_types.js +291 -6
  126. package/src/engine/debug/debug.ts +4 -0
  127. package/src/engine/debug/debug_overlay.ts +9 -2
  128. package/src/engine/engine_element_loading.ts +1 -1
  129. package/src/engine/engine_gameobject.ts +19 -6
  130. package/src/engine/engine_gltf_builtin_components.ts +5 -1
  131. package/src/engine/engine_mainloop_utils.ts +7 -3
  132. package/src/engine/engine_physics.ts +508 -469
  133. package/src/engine/engine_physics.types.ts +19 -0
  134. package/src/engine/engine_serialization_core.ts +22 -8
  135. package/src/engine/engine_setup.ts +6 -2
  136. package/src/engine/engine_time.ts +2 -0
  137. package/src/engine/engine_types.ts +82 -55
  138. package/src/engine/engine_typestore.ts +2 -0
  139. package/src/engine/engine_util_decorator.ts +69 -0
  140. package/src/engine/engine_utils.ts +6 -5
  141. package/src/engine/extensions/EXT_texture_exr.js +1 -1
  142. package/src/engine/extensions/NEEDLE_animator_controller_model.ts +2 -1
  143. package/src/engine/extensions/NEEDLE_gameobject_data.ts +2 -0
  144. package/src/engine/extensions/NEEDLE_techniques_webgl.ts +7 -0
  145. package/src/engine-components/Animation.ts +14 -1
  146. package/src/engine-components/AnimatorController.ts +19 -9
  147. package/src/engine-components/BoxHelperComponent.ts +30 -9
  148. package/src/engine-components/Collider.ts +29 -29
  149. package/src/engine-components/Component.ts +26 -135
  150. package/src/engine-components/DragControls.ts +9 -5
  151. package/src/engine-components/GroundProjection.ts +22 -7
  152. package/src/engine-components/NavMesh.ts +114 -115
  153. package/src/engine-components/NestedGltf.ts +2 -0
  154. package/src/engine-components/ReflectionProbe.ts +141 -0
  155. package/src/engine-components/Renderer.ts +796 -737
  156. package/src/engine-components/RigidBody.ts +258 -149
  157. package/src/engine-components/SpatialTrigger.ts +1 -1
  158. package/src/engine-components/SpectatorCamera.ts +10 -2
  159. package/src/engine-components/SpringJoint.ts +41 -41
  160. package/src/engine-components/VideoPlayer.ts +1 -2
  161. package/src/engine-components/WebARSessionRoot.ts +16 -16
  162. package/src/engine-components/WebXR.ts +65 -50
  163. package/src/engine-components/WebXRAvatar.ts +16 -16
  164. package/src/engine-components/WebXRController.ts +143 -112
  165. package/src/engine-components/WebXRGrabRendering.ts +6 -6
  166. package/src/engine-components/WebXRSync.ts +20 -20
  167. package/src/engine-components/codegen/components.ts +2 -3
  168. package/src/engine-components/ui/EventSystem.ts +26 -3
@@ -1,118 +1,117 @@
1
1
 
2
- import { Behaviour, GameObject } from "./Component";
3
- /*
4
- import { Pathfinding, PathfindingHelper } from 'three-pathfinding';
5
- import * as THREE from 'three';
6
- import { mergeBufferGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils'
7
- */
8
- export class NavMesh extends Behaviour {
9
- /*
10
- public meshes: THREE.Object3D[] = [];
11
-
12
- private pathfinding: Pathfinding | null = null;
13
- private zones: string[] = [];
14
-
15
- onEnable(): void {
16
-
17
- this.pathfinding = new Pathfinding();
18
- const ZONE = 'zone';
19
- const geometries : THREE.BufferGeometry[] = [];
20
- if (this.meshes && this.meshes.length > 0) {
21
- for (let i = 0; i < this.meshes.length; i++) {
22
- const obj = this.meshes[i] as THREE.Mesh;
23
- if (!obj) continue;
24
- const geo = obj.geometry;
25
- if (!geo) continue;
26
- const copy : THREE.BufferGeometry = {...geo};
27
- // const copy = geo;
28
- const attributesCopy = {...copy.attributes};
29
- copy.attributes = attributesCopy;
30
- if(attributesCopy.uv2) delete attributesCopy.uv2;
31
- // copy.applyMatrix4(obj.matrixWorld);
32
- geometries.push(copy);
33
- // obj.updateMatrixWorld();
34
- // singleGeometry.merge(geo);
35
- // console.log(singleGeometry, geo);
36
- }
37
- }
38
- const mesh = mergeBufferGeometries(geometries);
39
- const zone = Pathfinding.createZone(mesh);
40
- const name = ZONE;
41
- this.zones.push(name);
42
- this.pathfinding.setZoneData(name, zone);
43
-
44
- // const target = new THREE.Vector3(2, 0, 2);
45
- // const groupID = this.pathfinding.getGroup(ZONE, target);
46
- // const path = this.pathfinding.findPath(this.worldPosition, target, ZONE, groupID);
47
- // console.log(path, this.pathfinding);
48
- }
49
-
50
- public tryFindPath(from: THREE.Vector3, to: THREE.Vector3): THREE.Vector3[] | null {
51
- const zone = this.zones[0];
52
- const groupId = this.pathfinding.getGroup(zone, to);
53
- const path = this.pathfinding.findPath(from, to, zone, groupId);
54
- return path;
55
- }
56
- */
57
- }
58
-
59
- // import { input } from "../engine/engine_input";
60
- import { Physics, RaycastOptions } from "../engine/engine_physics"
61
- // import { time } from "../engine/engine_time";
62
- // import { scene } from "../engine/engine_setup";
63
- import { Quaternion } from "math/Quaternion";
64
- import {SyncedTransform} from "./SyncedTransform";
65
-
66
- export class NavMeshAgent extends Behaviour {
67
- /*
68
- public navMesh: NavMesh | null = null;
69
-
70
- private currentPath: THREE.Vector3[] | null = null;
71
- private helper : PathfindingHelper | null = null;
72
-
73
- onEnable(): void {
74
- this.helper = new PathfindingHelper();
75
- scene.add(this.helper);
76
- }
77
-
78
- update(): void {
79
- if (input.mouseClick) {
80
- if (this.navMesh) {
81
- const from = this.worldPosition;
82
- const opts = new RaycastOptions();
83
- opts.setMask(RaycastOptions.AllLayers);
84
- const hits = Physics.raycast(opts);
85
- if (hits.length > 0) {
86
- const to = hits[0].point;
87
- this.helper.setPlayerPosition(from);
88
- this.helper.setTargetPosition(to);
89
- this.currentPath = this.navMesh.tryFindPath(from, to);
90
- this.helper.setPath(this.currentPath);
91
- // console.log(...this.currentPath);
92
-
93
- const sync = GameObject.getComponent(this.gameObject, SyncedTransform);
94
- if(sync) sync.requestOwnership();
95
- }
96
- }
97
- }
98
-
99
- if (this.currentPath && this.currentPath.length > 0) {
100
-
101
- const wp = this.worldPosition;
102
- const target = this.currentPath[0];
103
- this.worldPosition = wp.lerp(target, time.deltaTime*2);
104
-
105
- this.gameObject.lookAt(target);
106
- // const vec = target.clone().sub(wp);
107
- // const wr = this.worldQuaternion;
108
- // wr.slerp(rot, time.deltaTime*5);
109
- // this.worldQuaternion = wr;
110
-
111
- if (wp.distanceTo(this.currentPath[0]) < 0.1)
112
- this.currentPath.shift();
2
+ // import { Behaviour, GameObject } from "./Component";
3
+ // /*
4
+ // import { Pathfinding, PathfindingHelper } from 'three-pathfinding';
5
+ // import * as THREE from 'three';
6
+ // import { mergeBufferGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils'
7
+ // */
8
+ // export class NavMesh extends Behaviour {
9
+ // /*
10
+ // public meshes: THREE.Object3D[] = [];
11
+
12
+ // private pathfinding: Pathfinding | null = null;
13
+ // private zones: string[] = [];
14
+
15
+ // onEnable(): void {
16
+
17
+ // this.pathfinding = new Pathfinding();
18
+ // const ZONE = 'zone';
19
+ // const geometries : THREE.BufferGeometry[] = [];
20
+ // if (this.meshes && this.meshes.length > 0) {
21
+ // for (let i = 0; i < this.meshes.length; i++) {
22
+ // const obj = this.meshes[i] as THREE.Mesh;
23
+ // if (!obj) continue;
24
+ // const geo = obj.geometry;
25
+ // if (!geo) continue;
26
+ // const copy : THREE.BufferGeometry = {...geo};
27
+ // // const copy = geo;
28
+ // const attributesCopy = {...copy.attributes};
29
+ // copy.attributes = attributesCopy;
30
+ // if(attributesCopy.uv2) delete attributesCopy.uv2;
31
+ // // copy.applyMatrix4(obj.matrixWorld);
32
+ // geometries.push(copy);
33
+ // // obj.updateMatrixWorld();
34
+ // // singleGeometry.merge(geo);
35
+ // // console.log(singleGeometry, geo);
36
+ // }
37
+ // }
38
+ // const mesh = mergeBufferGeometries(geometries);
39
+ // const zone = Pathfinding.createZone(mesh);
40
+ // const name = ZONE;
41
+ // this.zones.push(name);
42
+ // this.pathfinding.setZoneData(name, zone);
43
+
44
+ // // const target = new THREE.Vector3(2, 0, 2);
45
+ // // const groupID = this.pathfinding.getGroup(ZONE, target);
46
+ // // const path = this.pathfinding.findPath(this.worldPosition, target, ZONE, groupID);
47
+ // // console.log(path, this.pathfinding);
48
+ // }
49
+
50
+ // public tryFindPath(from: THREE.Vector3, to: THREE.Vector3): THREE.Vector3[] | null {
51
+ // const zone = this.zones[0];
52
+ // const groupId = this.pathfinding.getGroup(zone, to);
53
+ // const path = this.pathfinding.findPath(from, to, zone, groupId);
54
+ // return path;
55
+ // }
56
+ // */
57
+ // }
58
+
59
+ // // import { input } from "../engine/engine_input";
60
+ // import { Physics, RaycastOptions } from "../engine/engine_physics"
61
+ // // import { time } from "../engine/engine_time";
62
+ // // import { scene } from "../engine/engine_setup";
63
+ // import {SyncedTransform} from "./SyncedTransform";
64
+
65
+ // export class NavMeshAgent extends Behaviour {
66
+ // /*
67
+ // public navMesh: NavMesh | null = null;
68
+
69
+ // private currentPath: THREE.Vector3[] | null = null;
70
+ // private helper : PathfindingHelper | null = null;
71
+
72
+ // onEnable(): void {
73
+ // this.helper = new PathfindingHelper();
74
+ // scene.add(this.helper);
75
+ // }
76
+
77
+ // update(): void {
78
+ // if (input.mouseClick) {
79
+ // if (this.navMesh) {
80
+ // const from = this.worldPosition;
81
+ // const opts = new RaycastOptions();
82
+ // opts.setMask(RaycastOptions.AllLayers);
83
+ // const hits = Physics.raycast(opts);
84
+ // if (hits.length > 0) {
85
+ // const to = hits[0].point;
86
+ // this.helper.setPlayerPosition(from);
87
+ // this.helper.setTargetPosition(to);
88
+ // this.currentPath = this.navMesh.tryFindPath(from, to);
89
+ // this.helper.setPath(this.currentPath);
90
+ // // console.log(...this.currentPath);
91
+
92
+ // const sync = GameObject.getComponent(this.gameObject, SyncedTransform);
93
+ // if(sync) sync.requestOwnership();
94
+ // }
95
+ // }
96
+ // }
97
+
98
+ // if (this.currentPath && this.currentPath.length > 0) {
99
+
100
+ // const wp = this.worldPosition;
101
+ // const target = this.currentPath[0];
102
+ // this.worldPosition = wp.lerp(target, time.deltaTime*2);
103
+
104
+ // this.gameObject.lookAt(target);
105
+ // // const vec = target.clone().sub(wp);
106
+ // // const wr = this.worldQuaternion;
107
+ // // wr.slerp(rot, time.deltaTime*5);
108
+ // // this.worldQuaternion = wr;
109
+
110
+ // if (wp.distanceTo(this.currentPath[0]) < 0.1)
111
+ // this.currentPath.shift();
113
112
 
114
113
 
115
- }
116
- }
117
- */
118
- }
114
+ // }
115
+ // }
116
+ // */
117
+ // }
@@ -41,6 +41,8 @@ export class NestedGltf extends Behaviour {
41
41
  res.matrix.identity();
42
42
  res.applyMatrix4(matrix);
43
43
  res.matrixAutoUpdate = true;
44
+ res.layers.disableAll();
45
+ res.layers.set(this.layer);
44
46
  }
45
47
  this.destroy();
46
48
  if (debug) console.log("Nested loading done:", this.filePath?.uri ?? this.filePath, res);
@@ -0,0 +1,141 @@
1
+ import { Behaviour } from "./Component";
2
+ import { Box3, Color, EquirectangularReflectionMapping, LineBasicMaterial, Material, MeshStandardMaterial, Object3D, sRGBEncoding, Texture, Vector3, WebGLCubeRenderTarget, WebGLRenderTarget } from "three";
3
+ import { serializeable } from "../engine/engine_serialization";
4
+ import { Context } from "../engine/engine_setup";
5
+ import { getWorldPosition, getWorldScale } from "../engine/engine_three_utils";
6
+ import { IRenderer } from "../engine/engine_types";
7
+ import { BoxHelperComponent } from "./BoxHelperComponent";
8
+ import { getParam } from "../engine/engine_utils";
9
+
10
+ export const debug = getParam("debugreflectionprobe");
11
+ const disable = getParam("noreflectionprobe");
12
+
13
+ export class ReflectionProbe extends Behaviour {
14
+
15
+ private static _probes: Map<Context, ReflectionProbe[]> = new Map();
16
+
17
+ public static get(object: Object3D | null | undefined, context: Context, isAnchor:boolean): ReflectionProbe | null {
18
+ if (!object || object.isObject3D !== true) return null;
19
+ if (disable) return null;
20
+ const probes = ReflectionProbe._probes.get(context);
21
+ if (probes) {
22
+ for (const probe of probes) {
23
+ if (!probe.__didAwake) probe.__internalAwake();
24
+ if (probe.enabled && probe.isInBox(object, isAnchor ? .00000001 : undefined)) {
25
+ if (debug) console.log("Found reflection probe", object.name, probe.name);
26
+ return probe;
27
+ }
28
+ }
29
+ }
30
+ if (debug)
31
+ console.debug("Did not find reflection probe", object.name, isAnchor, object);
32
+ return null;
33
+ }
34
+
35
+
36
+
37
+ private _texture!: Texture;
38
+ set texture(tex: Texture) {
39
+ this._texture = tex;
40
+ }
41
+ get texture(): Texture {
42
+ return this._texture;
43
+ }
44
+
45
+ @serializeable(Vector3)
46
+ center?: Vector3;
47
+ @serializeable(Vector3)
48
+ size?: Vector3;
49
+
50
+ private _boxHelper?: BoxHelperComponent;
51
+
52
+ private isInBox(obj: Object3D, scaleFactor?: number) {
53
+ return this._boxHelper?.isInBox(obj, scaleFactor);
54
+ }
55
+
56
+ constructor() {
57
+ super();
58
+ if (!ReflectionProbe._probes.has(this.context)) {
59
+ ReflectionProbe._probes.set(this.context, []);
60
+ }
61
+ ReflectionProbe._probes.get(this.context)?.push(this);
62
+ }
63
+
64
+ awake() {
65
+ this._boxHelper = this.gameObject.addNewComponent(BoxHelperComponent) as BoxHelperComponent;
66
+ this._boxHelper.updateBox(true);
67
+ if (debug)
68
+ this._boxHelper.showHelper(0x555500, true);
69
+
70
+ if (this.texture) {
71
+ this.texture.mapping = EquirectangularReflectionMapping;
72
+ this.texture.encoding = sRGBEncoding;
73
+ // this.texture.rotation = Math.PI;
74
+ // this.texture.flipY = true;
75
+ this.texture.needsUpdate = true;
76
+ }
77
+ }
78
+
79
+ onDestroy() {
80
+ const probes = ReflectionProbe._probes.get(this.context);
81
+ if (probes) {
82
+ const index = probes.indexOf(this);
83
+ if (index >= 0) {
84
+ probes.splice(index, 1);
85
+ }
86
+ }
87
+ }
88
+
89
+
90
+ // when objects are rendered and they share material
91
+ // and some need reflection probe and some don't
92
+ // we need to make sure we don't override the material but use a copy
93
+
94
+ private static _rendererMaterialsCache: Map<IRenderer, Array<{ orig: Material, copy: Material }>> = new Map();
95
+
96
+ onSet(_rend: IRenderer) {
97
+ if (disable) return;
98
+ if (_rend.sharedMaterials?.length <= 0) return;
99
+ if (!this.texture) return;
100
+
101
+ let rendererCache = ReflectionProbe._rendererMaterialsCache.get(_rend);
102
+ if (!rendererCache) {
103
+ rendererCache = [];
104
+ ReflectionProbe._rendererMaterialsCache.set(_rend, rendererCache);
105
+ }
106
+
107
+ // TODO: dont clone material for every renderer that uses reflection probes, we can do it once per material when they use the same reflection texture
108
+
109
+ // need to make sure materials are not shared when using reflection probes
110
+ // otherwise some renderers outside of the probe will be affected or vice versa
111
+ for (let i = 0; i < _rend.sharedMaterials.length; i++) {
112
+ const orig = _rend.sharedMaterials[i];
113
+ if (!orig) continue;
114
+ if (orig["envMap"] === undefined) continue;
115
+ let cached = rendererCache[i];
116
+ let copy = cached?.copy;
117
+ if (!cached) {
118
+ const clone = orig.clone();
119
+ copy = clone;
120
+ rendererCache.push({ orig, copy });
121
+
122
+ copy["__reflection_probe"] = this;
123
+ copy["envMap"] = this.texture;
124
+
125
+ if (debug)
126
+ console.log("Set reflection", _rend.name, _rend.guid);
127
+ }
128
+ _rend.sharedMaterials[i] = copy;
129
+ }
130
+ }
131
+
132
+ onUnset(_rend: IRenderer) {
133
+ const rendererCache = ReflectionProbe._rendererMaterialsCache.get(_rend);
134
+ if (rendererCache) {
135
+ for (let i = 0; i < rendererCache.length; i++) {
136
+ const cached = rendererCache[i];
137
+ _rend.sharedMaterials[i] = cached.orig;
138
+ }
139
+ }
140
+ }
141
+ }