@needle-tools/engine 4.13.1 → 4.14.0-beta

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 (141) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/components.needle.json +1 -1
  3. package/dist/generateMeshBVH.worker-DiCnZlf3.js +21 -0
  4. package/dist/gltf-progressive-Bm_6aEi4.js +1528 -0
  5. package/dist/gltf-progressive-BttGBXw6.umd.cjs +10 -0
  6. package/dist/gltf-progressive-T5WKTux5.min.js +10 -0
  7. package/dist/loader.worker-BqODMeeW.js +23 -0
  8. package/dist/{materialx-B9ddsHcF.min.js → materialx-CJyQZtjt.min.js} +1 -1
  9. package/dist/{materialx-fkoFuRh3.js → materialx-DMs1E08Z.js} +2 -2
  10. package/dist/{materialx-BF23AVE8.umd.cjs → materialx-DaKKOoVk.umd.cjs} +1 -1
  11. package/dist/needle-engine.bundle-BW2VusZV.min.js +1646 -0
  12. package/dist/{needle-engine.bundle-tjI5Fq2c.umd.cjs → needle-engine.bundle-Cb5bKEqa.umd.cjs} +152 -159
  13. package/dist/{needle-engine.bundle-DauZUYl7.js → needle-engine.bundle-D9VPvp5o.js} +7798 -7497
  14. package/dist/needle-engine.d.ts +779 -42
  15. package/dist/needle-engine.js +416 -415
  16. package/dist/needle-engine.min.js +1 -1
  17. package/dist/needle-engine.umd.cjs +1 -1
  18. package/dist/{postprocessing-BVNrgYZK.min.js → postprocessing-CctM1XIO.min.js} +1 -1
  19. package/dist/{postprocessing-DdM-tz1j.js → postprocessing-DGm6qJ-I.js} +2 -2
  20. package/dist/{postprocessing-CI2TjWpu.umd.cjs → postprocessing-Dbl2PJpd.umd.cjs} +1 -1
  21. package/dist/{three-BW2s1Yl-.umd.cjs → three-BjYim-vL.umd.cjs} +4 -4
  22. package/dist/{three-VvRoMeIN.js → three-Bvk2VKbF.js} +4210 -4210
  23. package/dist/{three-I__hSXzr.min.js → three-IG2qPafA.min.js} +33 -33
  24. package/dist/{three-examples-Bpfu6ke_.umd.cjs → three-examples-BMmNgNCN.umd.cjs} +1 -1
  25. package/dist/{three-examples-BhfOE7NG.js → three-examples-CMYCd5nH.js} +1 -1
  26. package/dist/{three-examples-D8zAE_7t.min.js → three-examples-CQl1fFZp.min.js} +1 -1
  27. package/dist/{three-mesh-ui-C3QbemOV.min.js → three-mesh-ui-5HVE2RV-.min.js} +1 -1
  28. package/dist/{three-mesh-ui-CcMp-FQm.js → three-mesh-ui-BlakAItG.js} +1 -1
  29. package/dist/{three-mesh-ui-BU55xDxJ.umd.cjs → three-mesh-ui-D828VbQp.umd.cjs} +1 -1
  30. package/dist/{vendor-DW7zqjuT.min.js → vendor-BxK0WKmT.min.js} +1 -1
  31. package/dist/{vendor-COVQl0b8.umd.cjs → vendor-CIDkyBaO.umd.cjs} +1 -1
  32. package/dist/{vendor-BiyIZ61v.js → vendor-ixwD-vv2.js} +1 -1
  33. package/lib/engine/api.d.ts +1 -0
  34. package/lib/engine/api.js +1 -0
  35. package/lib/engine/api.js.map +1 -1
  36. package/lib/engine/engine_context.js +12 -2
  37. package/lib/engine/engine_context.js.map +1 -1
  38. package/lib/engine/engine_lightdata.js +8 -6
  39. package/lib/engine/engine_lightdata.js.map +1 -1
  40. package/lib/engine/engine_materialpropertyblock.d.ts +259 -0
  41. package/lib/engine/engine_materialpropertyblock.js +682 -0
  42. package/lib/engine/engine_materialpropertyblock.js.map +1 -0
  43. package/lib/engine/engine_utils.d.ts +1 -0
  44. package/lib/engine/engine_utils.js +1 -0
  45. package/lib/engine/engine_utils.js.map +1 -1
  46. package/lib/engine/engine_utils_screenshot.d.ts +171 -14
  47. package/lib/engine/engine_utils_screenshot.js +65 -0
  48. package/lib/engine/engine_utils_screenshot.js.map +1 -1
  49. package/lib/engine/engine_utils_screenshot.xr.d.ts +1 -1
  50. package/lib/engine/engine_utils_screenshot.xr.js +1 -1
  51. package/lib/engine/extensions/NEEDLE_techniques_webgl.js +3 -0
  52. package/lib/engine/extensions/NEEDLE_techniques_webgl.js.map +1 -1
  53. package/lib/engine/xr/NeedleXRSession.d.ts +5 -0
  54. package/lib/engine/xr/NeedleXRSession.js +5 -0
  55. package/lib/engine/xr/NeedleXRSession.js.map +1 -1
  56. package/lib/engine-components/Camera.js.map +1 -1
  57. package/lib/engine-components/DropListener.js.map +1 -1
  58. package/lib/engine-components/Duplicatable.js.map +1 -1
  59. package/lib/engine-components/GroundProjection.js.map +1 -1
  60. package/lib/engine-components/NeedleMenu.js.map +1 -1
  61. package/lib/engine-components/NestedGltf.js.map +1 -1
  62. package/lib/engine-components/ReflectionProbe.d.ts +32 -4
  63. package/lib/engine-components/ReflectionProbe.js +58 -88
  64. package/lib/engine-components/ReflectionProbe.js.map +1 -1
  65. package/lib/engine-components/Renderer.d.ts +2 -0
  66. package/lib/engine-components/Renderer.js +30 -6
  67. package/lib/engine-components/Renderer.js.map +1 -1
  68. package/lib/engine-components/RendererLightmap.d.ts +13 -9
  69. package/lib/engine-components/RendererLightmap.js +67 -81
  70. package/lib/engine-components/RendererLightmap.js.map +1 -1
  71. package/lib/engine-components/SeeThrough.d.ts +0 -2
  72. package/lib/engine-components/SeeThrough.js +114 -88
  73. package/lib/engine-components/SeeThrough.js.map +1 -1
  74. package/lib/engine-components/SmoothFollow.js.map +1 -1
  75. package/lib/engine-components/VideoPlayer.js +6 -0
  76. package/lib/engine-components/VideoPlayer.js.map +1 -1
  77. package/lib/engine-components/ui/Button.js.map +1 -1
  78. package/lib/engine-components/ui/Raycaster.js.map +1 -1
  79. package/lib/engine-components/utils/OpenURL.d.ts +1 -0
  80. package/lib/engine-components/utils/OpenURL.js +1 -0
  81. package/lib/engine-components/utils/OpenURL.js.map +1 -1
  82. package/lib/engine-components/web/CursorFollow.d.ts +1 -0
  83. package/lib/engine-components/web/CursorFollow.js +1 -0
  84. package/lib/engine-components/web/CursorFollow.js.map +1 -1
  85. package/lib/engine-components/web/ScrollFollow.d.ts +1 -0
  86. package/lib/engine-components/web/ScrollFollow.js +1 -0
  87. package/lib/engine-components/web/ScrollFollow.js.map +1 -1
  88. package/lib/engine-components/web/ViewBox.d.ts +2 -2
  89. package/lib/engine-components/web/ViewBox.js +2 -2
  90. package/lib/engine-components/web/ViewBox.js.map +1 -1
  91. package/lib/engine-components/webxr/WebARCameraBackground.d.ts +9 -0
  92. package/lib/engine-components/webxr/WebARCameraBackground.js +9 -0
  93. package/lib/engine-components/webxr/WebARCameraBackground.js.map +1 -1
  94. package/lib/engine-components/webxr/WebXR.d.ts +1 -0
  95. package/lib/engine-components/webxr/WebXR.js +1 -0
  96. package/lib/engine-components/webxr/WebXR.js.map +1 -1
  97. package/lib/engine-components/webxr/WebXRPlaneTracking.js.map +1 -1
  98. package/lib/engine-components/webxr/controllers/XRControllerFollow.js.map +1 -1
  99. package/lib/engine-components/webxr/controllers/XRControllerMovement.js.map +1 -1
  100. package/package.json +5 -5
  101. package/plugins/vite/build-pipeline.js +16 -2
  102. package/src/engine/api.ts +1 -0
  103. package/src/engine/engine_context.ts +17 -3
  104. package/src/engine/engine_lightdata.ts +8 -6
  105. package/src/engine/engine_materialpropertyblock.ts +866 -0
  106. package/src/engine/engine_utils.ts +1 -0
  107. package/src/engine/engine_utils_screenshot.ts +241 -17
  108. package/src/engine/engine_utils_screenshot.xr.ts +1 -1
  109. package/src/engine/extensions/NEEDLE_techniques_webgl.ts +3 -0
  110. package/src/engine/xr/NeedleXRSession.ts +5 -0
  111. package/src/engine-components/Camera.ts +2 -2
  112. package/src/engine-components/ContactShadows.ts +1 -1
  113. package/src/engine-components/DropListener.ts +1 -1
  114. package/src/engine-components/Duplicatable.ts +1 -1
  115. package/src/engine-components/GroundProjection.ts +3 -0
  116. package/src/engine-components/NeedleMenu.ts +3 -0
  117. package/src/engine-components/NestedGltf.ts +1 -1
  118. package/src/engine-components/ReflectionProbe.ts +64 -105
  119. package/src/engine-components/Renderer.ts +34 -6
  120. package/src/engine-components/RendererLightmap.ts +75 -87
  121. package/src/engine-components/SeeThrough.ts +124 -109
  122. package/src/engine-components/SmoothFollow.ts +2 -2
  123. package/src/engine-components/VideoPlayer.ts +6 -0
  124. package/src/engine-components/ui/Button.ts +1 -1
  125. package/src/engine-components/ui/Raycaster.ts +1 -1
  126. package/src/engine-components/utils/OpenURL.ts +1 -0
  127. package/src/engine-components/web/CursorFollow.ts +1 -0
  128. package/src/engine-components/web/ScrollFollow.ts +1 -0
  129. package/src/engine-components/web/ViewBox.ts +9 -2
  130. package/src/engine-components/webxr/WebARCameraBackground.ts +12 -3
  131. package/src/engine-components/webxr/WebXR.ts +1 -0
  132. package/src/engine-components/webxr/WebXRPlaneTracking.ts +3 -3
  133. package/src/engine-components/webxr/controllers/XRControllerFollow.ts +1 -1
  134. package/src/engine-components/webxr/controllers/XRControllerMovement.ts +4 -4
  135. package/dist/generateMeshBVH.worker-iyfPIK6R.js +0 -21
  136. package/dist/gltf-progressive-BURrJW0U.umd.cjs +0 -8
  137. package/dist/gltf-progressive-DHLDFNvQ.min.js +0 -8
  138. package/dist/gltf-progressive-eiJCrjLb.js +0 -1400
  139. package/dist/loader.worker-C1GG9A7C.js +0 -23
  140. package/dist/needle-engine.bundle-BNIUpreS.min.js +0 -1653
  141. package/src/include/three/DragControls.js +0 -232
@@ -638,12 +638,16 @@ export class Renderer extends Behaviour implements IRenderer {
638
638
 
639
639
  this.updateReflectionProbe();
640
640
 
641
- // this.testIfLODLevelsAreAvailable();
641
+ ReflectionProbe.onEnabled.addEventListener(this.onReflectionProbeEnabled);
642
+ ReflectionProbe.onDisabled.addEventListener(this.onReflectionProbeDisabled);
642
643
  }
643
644
 
644
645
  onDisable() {
645
646
  this.setVisibility(false);
646
647
 
648
+ ReflectionProbe.onEnabled.removeEventListener(this.onReflectionProbeEnabled);
649
+ ReflectionProbe.onDisabled.removeEventListener(this.onReflectionProbeDisabled);
650
+
647
651
  if (this._handles && this._handles.length > 0) {
648
652
  this.setInstancingEnabled(false);
649
653
  }
@@ -662,14 +666,24 @@ export class Renderer extends Behaviour implements IRenderer {
662
666
  }
663
667
  }
664
668
 
669
+ private readonly onReflectionProbeEnabled = () => {
670
+ this.updateReflectionProbe();
671
+ }
672
+ private onReflectionProbeDisabled = (probe: ReflectionProbe) => {
673
+ if (this._reflectionProbe === probe) {
674
+ this._reflectionProbe.unapply(this.gameObject);
675
+ this._reflectionProbe = null;
676
+ }
677
+ }
678
+
665
679
 
666
680
  onBeforeRender() {
667
681
  if (!this.gameObject) {
668
682
  return;
669
683
  }
670
684
 
671
- if (this._probeAnchorLastFrame !== this.probeAnchor) {
672
- this._reflectionProbe?.onUnset(this);
685
+ if (this._probeAnchorLastFrame !== this.probeAnchor || this._reflectionProbe?.activeAndEnabled === false) {
686
+ this._reflectionProbe?.unapply(this.gameObject);
673
687
  this.updateReflectionProbe();
674
688
  }
675
689
 
@@ -726,8 +740,21 @@ export class Renderer extends Behaviour implements IRenderer {
726
740
  }
727
741
 
728
742
  if (this.reflectionProbeUsage !== ReflectionProbeUsage.Off && this._reflectionProbe) {
729
- this._reflectionProbe.onSet(this);
743
+ // @TODO: when reflection probes are applied via bounding box we currently NEVER update it again
744
+ // if(this.probeAnchor === this._reflectionProbe.gameObject)
745
+ this._reflectionProbe.apply(this.gameObject);
746
+ // else {
747
+ // const probe = this._reflectionProbe;
748
+ // this._updateReflectionProbe();
749
+ // console.log(this.name, "Reflection probe updated on before render. Probe anchor changed:", this.probeAnchor !== this._probeAnchorLastFrame, "Probe active:", probe.activeAndEnabled, "New probe anchor:", this._reflectionProbe?.gameObject.name);
750
+ // if(!this._reflectionProbe) probe?.unapply(this.gameObject);
751
+ // else this._reflectionProbe?.apply(this.gameObject);
752
+ // }
753
+ }
754
+ else if (this.reflectionProbeUsage === ReflectionProbeUsage.Off && this._reflectionProbe) {
755
+ this._reflectionProbe.unapply(this.gameObject);
730
756
  }
757
+
731
758
  // since three 163 we need to set the envMap to the scene envMap if it is not set
732
759
  // otherwise the envmapIntensity has no effect: https://github.com/mrdoob/three.js/pull/27903
733
760
  // internal issue: https://linear.app/needle/issue/NE-6363
@@ -766,8 +793,8 @@ export class Renderer extends Behaviour implements IRenderer {
766
793
  }
767
794
  }
768
795
 
769
- if (this.reflectionProbeUsage !== ReflectionProbeUsage.Off && this._reflectionProbe) {
770
- this._reflectionProbe.onUnset(this);
796
+ if (this._reflectionProbe?.activeAndEnabled === false) {
797
+ this._reflectionProbe.unapply(this.gameObject);
771
798
  }
772
799
 
773
800
  if (this.static && this.gameObject.matrixAutoUpdate) {
@@ -798,6 +825,7 @@ export class Renderer extends Behaviour implements IRenderer {
798
825
  private updateReflectionProbe() {
799
826
  // handle reflection probe
800
827
  this._reflectionProbe = null;
828
+
801
829
  if (this.reflectionProbeUsage !== ReflectionProbeUsage.Off) {
802
830
  // update the reflection probe right before rendering
803
831
  // if we do it immediately the reflection probe might not be enabled yet
@@ -1,22 +1,19 @@
1
1
  import { NEEDLE_progressive } from "@needle-tools/gltf-progressive";
2
- import { Group, Material, Mesh, MeshPhysicalMaterial, Object3D, ShaderMaterial, Texture, Vector4, WebGLProgramParametersWithUniforms } from "three";
2
+ import { Group, Mesh, Object3D, ShaderMaterial, Texture, Vector2, Vector4 } from "three";
3
3
 
4
4
  import type { Context } from "../engine/engine_setup.js";
5
5
  import { getParam } from "../engine/engine_utils.js";
6
+ import { MaterialPropertyBlock } from "../engine/engine_materialpropertyblock.js";
6
7
  import { type Renderer } from "./Renderer.js";
7
8
 
8
9
  const debug = getParam("debuglightmaps");
9
10
 
10
- declare type MaterialWithLightmap = Material & { lightMap?: Texture | null };
11
-
12
- let cloningCounter = 0;
13
-
14
- const $lightmapVersion = Symbol("lightmap-material-version");
11
+ const $lightmapKey = Symbol("lightmapKey");
15
12
 
16
13
 
17
14
  /**
18
15
  * This component is automatically added by the {@link Renderer} component if the object has lightmap uvs AND we have a lightmap.
19
- *
16
+ *
20
17
  * @category Rendering
21
18
  * @group Components
22
19
  */
@@ -29,9 +26,13 @@ export class RendererLightmap {
29
26
  if (tex !== this.lightmapTexture) {
30
27
  this.lightmapTexture = tex;
31
28
  this.applyLightmap();
29
+ this.updatePropertyBlockTexture();
32
30
  if (this.lightmapTexture) {
33
31
  NEEDLE_progressive.assignTextureLOD(this.lightmapTexture, 0).then(res => {
34
- if ((res as Texture)?.isTexture) this.lightmapTexture = res as Texture;
32
+ if ((res as Texture)?.isTexture) {
33
+ this.lightmapTexture = res as Texture;
34
+ this.updatePropertyBlockTexture();
35
+ }
35
36
  })
36
37
  }
37
38
  }
@@ -41,13 +42,12 @@ export class RendererLightmap {
41
42
  private lightmapScaleOffset: Vector4 = new Vector4(1, 1, 0, 0);
42
43
 
43
44
  private readonly renderer: Renderer;
44
- private readonly clonedMaterials = new Array<Material>();
45
+ private _isApplied: boolean = false;
45
46
 
46
47
  private get context(): Context { return this.renderer.context; }
47
48
  private get gameObject() { return this.renderer.gameObject; }
49
+
48
50
  private lightmapTexture: Texture | null = null;
49
- private lightmapScaleOffsetUniform = { value: new Vector4(1, 1, 0, 0) };
50
- private lightmapUniform: { value: Texture | null } = { value: null };
51
51
 
52
52
  constructor(renderer: Renderer) {
53
53
  this.renderer = renderer;
@@ -60,7 +60,10 @@ export class RendererLightmap {
60
60
  this.lightmapScaleOffset = lightmapScaleOffset;
61
61
  this.lightmapTexture = lightmapTexture;
62
62
  NEEDLE_progressive.assignTextureLOD(lightmapTexture, 0).then(res => {
63
- if ((res as Texture)?.isTexture) this.lightmapTexture = res as Texture;
63
+ if ((res as Texture)?.isTexture) {
64
+ this.lightmapTexture = res as Texture;
65
+ this.updatePropertyBlockTexture();
66
+ }
64
67
  })
65
68
  if (debug == "show") {
66
69
  console.log("Lightmap:", this.gameObject.name, lightmapIndex, "\nScaleOffset:", lightmapScaleOffset, "\nTexture:", lightmapTexture)
@@ -70,18 +73,18 @@ export class RendererLightmap {
70
73
  this.applyLightmap();
71
74
  }
72
75
 
73
- updateLightmapUniforms(material: Material) {
74
- const uniforms = material["uniforms"];
75
- if (uniforms && uniforms.lightmap) {
76
- this.lightmapScaleOffsetUniform.value = this.lightmapScaleOffset;
77
- uniforms.lightmapScaleOffset = this.lightmapScaleOffsetUniform;
78
- }
76
+ updateLightmapUniforms(_material: any) {
79
77
  }
80
78
 
81
79
  /**
82
- * Apply the lightmap to the object. This will clone the material and set the lightmap texture and scale/offset
80
+ * Apply the lightmap to the object using MaterialPropertyBlock instead of cloning materials.
81
+ * The lightmap texture and its per-object UV transform are set as overrides via PropertyBlock.
82
+ * Three.js reads material.lightMap to determine shader defines and upload uniforms,
83
+ * and uses texture.offset/repeat to compute lightMapTransform in the vertex shader.
83
84
  */
84
85
  applyLightmap() {
86
+ if (this._isApplied) return;
87
+
85
88
  if (this.gameObject.type === "Object3D") {
86
89
  if (debug)
87
90
  console.warn("Can not add lightmap. Is this object missing a renderer?", this.gameObject.name);
@@ -91,23 +94,60 @@ export class RendererLightmap {
91
94
  const mesh = this.gameObject as unknown as (Mesh | Group);
92
95
  this.ensureLightmapUvs(mesh);
93
96
 
94
- for (let i = 0; i < this.renderer.sharedMaterials.length; i++) {
95
-
96
- const mat = this.renderer.sharedMaterials[i];
97
- if (!mat) continue;
98
- const newMat = this.ensureLightmapMaterial(mat, i);
99
- if (mat !== newMat) {
100
- this.renderer.sharedMaterials[i] = newMat;
97
+ if (this.lightmapIndex >= 0 && this.lightmapTexture) {
98
+ this.lightmapTexture.channel = 1;
99
+ const so = this.lightmapScaleOffset;
100
+ for (let i = 0; i < this.renderer.sharedMaterials.length; i++) {
101
+ const mat = this.renderer.sharedMaterials[i];
102
+ if (!mat) continue;
103
+
104
+ // If the material doesn't support lightmaps, skip it
105
+ if (mat["lightMap"] === undefined) continue;
106
+
107
+ if (debug) console.log("Setting lightmap on material", mat.name, "for renderer", this.renderer.name);
108
+
109
+ // Use property block to set lightMap with per-object UV transform.
110
+ // The texture transform differentiates cache keys per object and
111
+ // PropertyBlock handles save/restore of the shared texture's offset/repeat.
112
+ const propertyBlock = MaterialPropertyBlock.get(this.gameObject);
113
+ propertyBlock.setOverride("lightMap", this.lightmapTexture, {
114
+ offset: new Vector2(so.z, 1 - so.y - so.w),
115
+ repeat: new Vector2(so.x, so.y)
116
+ });
117
+
118
+ (mat as any)[$lightmapKey] = true;
101
119
  }
120
+
121
+ this._isApplied = true;
102
122
  }
123
+ }
103
124
 
104
- if (this.lightmapIndex >= 0 && this.lightmapTexture) {
105
- // always on channel 1 for now. We could optimize this by passing the correct lightmap index along
106
- this.lightmapTexture.channel = 1;
107
- for (const mat of this.renderer.sharedMaterials) {
108
- if (mat) this.assignLightmapTexture(mat);
125
+ /** Update the lightMap override on all property blocks (e.g. after LOD swap) */
126
+ private updatePropertyBlockTexture() {
127
+ if (!this._isApplied || !this.lightmapTexture) return;
128
+ this.lightmapTexture.channel = 1;
129
+ const so = this.lightmapScaleOffset;
130
+ const propertyBlock = MaterialPropertyBlock.get(this.gameObject);
131
+ propertyBlock.setOverride("lightMap", this.lightmapTexture, {
132
+ offset: new Vector2(so.z, 1 - so.y - so.w),
133
+ repeat: new Vector2(so.x, so.y)
134
+ });
135
+ }
136
+
137
+ /**
138
+ * Remove the lightmap from the object
139
+ */
140
+ onUnset() {
141
+ for (let i = 0; i < this.renderer.sharedMaterials.length; i++) {
142
+ const mat = this.renderer.sharedMaterials[i];
143
+ if (mat) {
144
+ delete (mat as any)[$lightmapKey];
109
145
  }
110
146
  }
147
+ const block = MaterialPropertyBlock.get(this.gameObject);
148
+ if (block) {
149
+ block.removeOveride("lightMap");
150
+ }
111
151
  }
112
152
 
113
153
  private ensureLightmapUvs(object: Object3D | Group | Mesh) {
@@ -123,59 +163,8 @@ export class RendererLightmap {
123
163
  }
124
164
  }
125
165
 
126
- private ensureLightmapMaterial(material: Material, index: number) {
127
- if (!material.userData) material.userData = {};
128
- // if (material instanceof MeshPhysicalMaterial) {
129
- // return material;
130
- // }
131
- // check if the material version has changed and only then clone the material
132
- if (this.clonedMaterials[index] !== material) {
133
- if (debug) {
134
- ++cloningCounter;
135
- if (cloningCounter++ < 1000) {
136
- console.warn(`Cloning material for lightmap ${this.renderer.name}: '${material.name}'`);
137
- }
138
- else if (cloningCounter === 1000) {
139
- console.warn(`Further material cloning for lightmaps suppressed to avoid flooding the console.`);
140
- }
141
- }
142
- const mat: Material = material.clone();
143
- if (!mat.name?.includes("(lightmap)")) mat.name = material.name + " (lightmap)";
144
- material = mat;
145
- material.onBeforeCompile = this.onBeforeCompile;
146
- this.clonedMaterials[index] = material;
147
-
148
- }
149
- return material;
150
- }
151
-
152
- private assignLightmapTexture(material: MaterialWithLightmap) {
153
- if (!material) return;
154
- if (material instanceof MeshPhysicalMaterial && material.transmission > 0) {
155
- return;
156
- }
157
- const hasChanged = material.lightMap !== this.lightmapTexture || material[$lightmapVersion] !== material.version;
158
- if (!hasChanged) {
159
- return;
160
- }
161
-
162
- if (debug) console.log(`Assigning lightmap texture ${this.renderer.name}: '${material.name}' (${material.version} ${material[$lightmapVersion]})`);
163
-
164
- // assign the lightmap
165
- material.lightMap = this.lightmapTexture;
166
- material.needsUpdate = true;
167
- // store the version of the material
168
- material[$lightmapVersion] = material.version;
169
- }
170
-
171
- private onBeforeCompile = (shader: WebGLProgramParametersWithUniforms, _) => {
172
- if (debug === "verbose") console.log("Lightmaps, before compile\n", shader)
173
- this.lightmapScaleOffsetUniform.value = this.lightmapScaleOffset;
174
- this.lightmapUniform.value = this.lightmapTexture;
175
- shader.uniforms.lightmapScaleOffset = this.lightmapScaleOffsetUniform;
176
- }
177
-
178
166
  private setLightmapDebugMaterial() {
167
+ const so = this.lightmapScaleOffset;
179
168
 
180
169
  // debug lightmaps
181
170
  this.gameObject["material"] = new ShaderMaterial({
@@ -190,7 +179,6 @@ export class RendererLightmap {
190
179
  fragmentShader: `
191
180
  uniform sampler2D lightMap;
192
181
  uniform float lightMapIntensity;
193
- uniform vec4 lightmapScaleOffset;
194
182
  varying vec2 vUv1;
195
183
 
196
184
  // took from threejs 05fc79cd52b79e8c3e8dec1e7dca72c5c39983a4
@@ -199,8 +187,8 @@ export class RendererLightmap {
199
187
  }
200
188
 
201
189
  void main() {
202
- vec2 lUv = vUv1.xy * lightmapScaleOffset.xy + vec2(lightmapScaleOffset.z, (1. - (lightmapScaleOffset.y + lightmapScaleOffset.w)));
203
-
190
+ vec2 lUv = vUv1.xy * vec2(${so.x.toFixed(6)}, ${so.y.toFixed(6)}) + vec2(${so.z.toFixed(6)}, ${(1 - so.y - so.w).toFixed(6)});
191
+
204
192
  vec4 lightMapTexel = texture2D( lightMap, lUv);
205
193
  gl_FragColor = lightMapTexel;
206
194
  gl_FragColor.a = 1.;
@@ -209,4 +197,4 @@ export class RendererLightmap {
209
197
  defines: { USE_LIGHTMAP: '' }
210
198
  });
211
199
  }
212
- }
200
+ }
@@ -5,30 +5,30 @@ import { Mathf } from "../engine/engine_math.js";
5
5
  import { serializable } from "../engine/engine_serialization_decorator.js";
6
6
  import { getTempVector } from "../engine/engine_three_utils.js";
7
7
  import { getParam } from "../engine/engine_utils.js";
8
- import { USDObject, USDZExporterContext } from "./api.js";
8
+ import type { Camera } from "./Camera.js";
9
9
  import { Behaviour } from "./Component.js";
10
10
  import { IUSDExporterExtension } from "./export/usdz/Extension.js";
11
11
  import { USDZExporter } from "./export/usdz/USDZExporter.js";
12
- import { Renderer } from "./Renderer.js";
13
- import type { Camera } from "./Camera.js";
14
12
  import type { OrbitControls } from "./OrbitControls.js";
13
+ import { Renderer } from "./Renderer.js";
14
+ import { MaterialPropertyBlock } from "../engine/engine_materialpropertyblock.js";
15
15
 
16
16
  const debugSeeThrough = getParam("debugseethrough");
17
17
 
18
- type MaterialState = {
19
- opacity: number,
20
- transparent: boolean,
21
- alphaHash: boolean
22
- }
23
-
24
- type MaterialWithState = Material & {
25
- /** Original values */
26
- userData: {
27
- seeThrough: {
28
- initial: MaterialState,
29
- }
30
- }
31
- };
18
+ // type MaterialState = {
19
+ // opacity: number,
20
+ // transparent: boolean,
21
+ // alphaHash: boolean
22
+ // }
23
+
24
+ // type MaterialWithState = Material & {
25
+ // /** Original values */
26
+ // userData: {
27
+ // seeThrough: {
28
+ // initial: MaterialState,
29
+ // }
30
+ // }
31
+ // };
32
32
 
33
33
  let i = 0;
34
34
 
@@ -176,26 +176,26 @@ export class SeeThrough extends Behaviour {
176
176
  onEnable() {
177
177
  this._needsUpdate = true;
178
178
  this._renderer = null;
179
- SeeThroughUsdzExporterPlugin.components.push(this);
179
+ // SeeThroughUsdzExporterPlugin.components.push(this);
180
180
  }
181
181
 
182
182
  /** @internal */
183
183
  onDisable() {
184
- this._renderer?.forEach(r => {
185
- const original = this.rendererMaterialsOriginal.get(r);
186
- for (let i = 0; i < r.sharedMaterials.length; i++) {
187
- const mat = r.sharedMaterials[i];
188
- if (!mat) continue;
189
- if (original && original[i]) {
190
- r.sharedMaterials[i] = original[i];
191
- }
192
- }
193
- this.rendererMaterials.delete(r);
194
- this.rendererMaterialsOriginal.delete(r);
195
- });
196
-
197
- const index = SeeThroughUsdzExporterPlugin.components.indexOf(this);
198
- if (index !== -1) SeeThroughUsdzExporterPlugin.components.splice(index, 1);
184
+ // this._renderer?.forEach(r => {
185
+ // const original = this.rendererMaterialsOriginal.get(r);
186
+ // for (let i = 0; i < r.sharedMaterials.length; i++) {
187
+ // const mat = r.sharedMaterials[i];
188
+ // if (!mat) continue;
189
+ // if (original && original[i]) {
190
+ // r.sharedMaterials[i] = original[i];
191
+ // }
192
+ // }
193
+ // this.rendererMaterials.delete(r);
194
+ // this.rendererMaterialsOriginal.delete(r);
195
+ // });
196
+
197
+ // const index = SeeThroughUsdzExporterPlugin.components.indexOf(this);
198
+ // if (index !== -1) SeeThroughUsdzExporterPlugin.components.splice(index, 1);
199
199
  }
200
200
 
201
201
  /**
@@ -239,8 +239,8 @@ export class SeeThrough extends Behaviour {
239
239
  }
240
240
  }
241
241
 
242
- private readonly rendererMaterials = new WeakMap<Renderer, Array<MaterialWithState>>();
243
- private readonly rendererMaterialsOriginal = new WeakMap<Renderer, Array<Material>>();
242
+ // private readonly rendererMaterials = new WeakMap<Renderer, Array<MaterialWithState>>();
243
+ // private readonly rendererMaterialsOriginal = new WeakMap<Renderer, Array<Material>>();
244
244
 
245
245
  private updateDirection() {
246
246
  this.referencePoint ??= this.context.scene;
@@ -272,66 +272,81 @@ export class SeeThrough extends Behaviour {
272
272
  renderer.gameObject.raycastAllowed = true;
273
273
  }
274
274
 
275
- if (!this.rendererMaterials.has(renderer)) {
276
- const originalMaterials = new Array<Material>();
277
- const clonedMaterials = new Array<MaterialWithState>();
278
-
279
- // We clone the materials once and store them, so we can modify the opacity without affecting other objects using the same material. This could potentially be optimized further to re-use materials between renderers if multiple renderers use the same material.
280
- for (let i = 0; i < renderer.sharedMaterials.length; i++) {
281
- const mat = renderer.sharedMaterials[i];
282
- if (!mat) continue;
283
- originalMaterials.push(mat);
284
- const matClone = mat.clone() as MaterialWithState;
285
- // @ts-ignore
286
- matClone.userData = mat.userData || {};
287
- matClone.userData.seeThrough = {
288
- initial: {
289
- opacity: matClone.opacity,
290
- transparent: matClone.transparent,
291
- alphaHash: matClone.alphaHash
292
- }
293
- }
294
- clonedMaterials.push(matClone);
295
- renderer.sharedMaterials[i] = matClone;
296
- }
297
-
298
- this.rendererMaterials.set(renderer, clonedMaterials);
299
- this.rendererMaterialsOriginal.set(renderer, originalMaterials);
300
- }
301
-
302
- const materials = renderer.hasLightmap ? renderer.sharedMaterials : this.rendererMaterials.get(renderer);
275
+ // if (!this.rendererMaterials.has(renderer)) {
276
+ // const originalMaterials = new Array<Material>();
277
+ // const clonedMaterials = new Array<MaterialWithState>();
278
+
279
+ // // We clone the materials once and store them, so we can modify the opacity without affecting other objects using the same material. This could potentially be optimized further to re-use materials between renderers if multiple renderers use the same material.
280
+ // for (let i = 0; i < renderer.sharedMaterials.length; i++) {
281
+ // const mat = renderer.sharedMaterials[i];
282
+ // if (!mat) continue;
283
+ // originalMaterials.push(mat);
284
+ // const matClone = mat.clone() as MaterialWithState;
285
+ // // @ts-ignore
286
+ // matClone.userData = mat.userData || {};
287
+ // matClone.userData.seeThrough = {
288
+ // initial: {
289
+ // opacity: matClone.opacity,
290
+ // transparent: matClone.transparent,
291
+ // alphaHash: matClone.alphaHash
292
+ // }
293
+ // }
294
+ // clonedMaterials.push(matClone);
295
+ // // renderer.sharedMaterials[i] = matClone;
296
+ // }
297
+
298
+ // this.rendererMaterials.set(renderer, clonedMaterials);
299
+ // this.rendererMaterialsOriginal.set(renderer, originalMaterials);
300
+ // }
301
+
302
+ const materials = renderer.sharedMaterials;// : this.rendererMaterials.get(renderer);
303
303
  if (!materials) return;
304
304
 
305
- for (const mat of materials) {
306
- if (!mat) continue;
305
+ const block = MaterialPropertyBlock.get(renderer.gameObject);
307
306
 
308
- let newAlpha = Mathf.lerp(mat.opacity, targetAlpha, duration <= 0 ? 1 : this.context.time.deltaTime / duration);;
309
- if (newAlpha >= 0.99) newAlpha = 1;
310
- else if (newAlpha <= 0.01) newAlpha = 0;
307
+ const currentOpacity = (block.getOverride("opacity")?.value ?? materials[0].opacity ?? 1);
311
308
 
309
+ let newAlpha = Mathf.lerp(currentOpacity, targetAlpha, duration <= 0 ? 1 : this.context.time.deltaTime / duration);;
310
+ if (newAlpha >= 0.99) newAlpha = 1;
311
+ else if (newAlpha <= 0.01) newAlpha = 0;
312
312
 
313
- const wasTransparent = mat.transparent;
314
- const wasAlphaHash = mat.alphaHash;
315
- const previousOpacity = mat.opacity;
313
+ // const currentTransparent = (block.getOverride("transparent")?.value ?? materials[0].transparent ?? false) as boolean;
316
314
 
317
- mat.alphaHash = this.useAlphaHash;
315
+ block.setOverride("alphaHash", this.useAlphaHash);
316
+ block.setOverride("opacity", newAlpha);
317
+ block.setOverride("transparent", newAlpha >= 0.99999 ? false : !this.useAlphaHash);
318
318
 
319
- if (mat.userData && "seeThrough" in mat.userData) {
320
- const initial = mat.userData.seeThrough.initial as MaterialState;
321
- mat.opacity = initial.opacity * newAlpha;
322
- mat.transparent = mat.opacity >= 1 ? initial.transparent : !this.useAlphaHash;
323
- }
324
- else {
325
- mat.transparent = mat.opacity >= 1 ? false : !this.useAlphaHash;
326
- }
327
319
 
328
- if (wasTransparent !== mat.transparent
329
- || wasAlphaHash !== mat.alphaHash
330
- || mat.opacity !== previousOpacity // MeshPhysicsMaterial needs that and maybe other materials too...
331
- ) {
332
- mat.needsUpdate = true;
333
- }
334
- }
320
+ // for (const mat of materials) {
321
+ // if (!mat) continue;
322
+
323
+ // let newAlpha = Mathf.lerp(mat.opacity, targetAlpha, duration <= 0 ? 1 : this.context.time.deltaTime / duration);;
324
+ // if (newAlpha >= 0.99) newAlpha = 1;
325
+ // else if (newAlpha <= 0.01) newAlpha = 0;
326
+
327
+
328
+ // const wasTransparent = mat.transparent;
329
+ // const wasAlphaHash = mat.alphaHash;
330
+ // const previousOpacity = mat.opacity;
331
+
332
+ // mat.alphaHash = this.useAlphaHash;
333
+
334
+ // if (mat.userData && "seeThrough" in mat.userData) {
335
+ // const initial = mat.userData.seeThrough.initial as MaterialState;
336
+ // mat.opacity = initial.opacity * newAlpha;
337
+ // mat.transparent = mat.opacity >= 1 ? initial.transparent : !this.useAlphaHash;
338
+ // }
339
+ // else {
340
+ // mat.transparent = mat.opacity >= 1 ? false : !this.useAlphaHash;
341
+ // }
342
+
343
+ // if (wasTransparent !== mat.transparent
344
+ // || wasAlphaHash !== mat.alphaHash
345
+ // || mat.opacity !== previousOpacity // MeshPhysicsMaterial needs that and maybe other materials too...
346
+ // ) {
347
+ // mat.needsUpdate = true;
348
+ // }
349
+ // }
335
350
  });
336
351
  }
337
352
 
@@ -339,33 +354,33 @@ export class SeeThrough extends Behaviour {
339
354
 
340
355
 
341
356
  ;
342
- class SeeThroughUsdzExporterPlugin implements IUSDExporterExtension {
357
+ // class SeeThroughUsdzExporterPlugin implements IUSDExporterExtension {
343
358
 
344
- static readonly components: SeeThrough[] = [];
359
+ // static readonly components: SeeThrough[] = [];
345
360
 
346
- get extensionName() {
347
- return "SeeThrough";
348
- }
361
+ // get extensionName() {
362
+ // return "SeeThrough";
363
+ // }
349
364
 
350
- // onExportObject(object: Object3D<Object3DEventMap>, model: USDObject, context: USDZExporterContext) {
351
- // const component = SeeThroughUsdzExporterPlugin.components.find(c => c.gameObject === object);
352
- // if(!component) return;
353
- // console.log("OH MY GOD SEE THROUGH USDZ EXPORTER", component, model);
365
+ // // onExportObject(object: Object3D<Object3DEventMap>, model: USDObject, context: USDZExporterContext) {
366
+ // // const component = SeeThroughUsdzExporterPlugin.components.find(c => c.gameObject === object);
367
+ // // if(!component) return;
368
+ // // console.log("OH MY GOD SEE THROUGH USDZ EXPORTER", component, model);
354
369
 
355
- // model.materialName = "AlphaHashMaterialInstance"; // we could make this unique per object if needed
370
+ // // model.materialName = "AlphaHashMaterialInstance"; // we could make this unique per object if needed
356
371
 
357
- // model.addEventListener("serialize", (writer, context) => {
358
- // writer.appendLine(`# SeeThrough component on ${object.name}`);
359
- // });
360
- // }
372
+ // // model.addEventListener("serialize", (writer, context) => {
373
+ // // writer.appendLine(`# SeeThrough component on ${object.name}`);
374
+ // // });
375
+ // // }
361
376
 
362
- }
377
+ // }
363
378
 
364
- const seeThroughUsdzExporterPlugin = new SeeThroughUsdzExporterPlugin();
379
+ // const seeThroughUsdzExporterPlugin = new SeeThroughUsdzExporterPlugin();
365
380
 
366
- USDZExporter.beforeExport.addEventListener(args => {
367
- if (SeeThroughUsdzExporterPlugin.components.length === 0) return;
368
- if (args.exporter.extensions.includes(seeThroughUsdzExporterPlugin) === false) {
369
- args.exporter.extensions.push(seeThroughUsdzExporterPlugin);
370
- }
371
- });
381
+ // USDZExporter.beforeExport.addEventListener(args => {
382
+ // if (SeeThroughUsdzExporterPlugin.components.length === 0) return;
383
+ // if (args.exporter.extensions.includes(seeThroughUsdzExporterPlugin) === false) {
384
+ // args.exporter.extensions.push(seeThroughUsdzExporterPlugin);
385
+ // }
386
+ // });
@@ -73,7 +73,7 @@ export class SmoothFollow extends Behaviour {
73
73
  * @default 0.1
74
74
  */
75
75
  @serializable()
76
- followFactor = .1;
76
+ followFactor: number = .1;
77
77
 
78
78
  /**
79
79
  * Speed factor for rotation following.
@@ -83,7 +83,7 @@ export class SmoothFollow extends Behaviour {
83
83
  * @default 0.1
84
84
  */
85
85
  @serializable()
86
- rotateFactor = .1;
86
+ rotateFactor: number = .1;
87
87
 
88
88
  /**
89
89
  * Which position axes to follow. Use bitwise OR to combine: