@babylonjs/core 9.13.0 → 9.15.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.
Files changed (237) hide show
  1. package/Animations/animationGroup.d.ts +0 -1
  2. package/Animations/animationGroup.js +0 -1
  3. package/Animations/animationGroup.js.map +1 -1
  4. package/Animations/animationGroup.pure.js +2 -0
  5. package/Animations/animationGroup.pure.js.map +1 -1
  6. package/Animations/runtimeAnimation.js +11 -0
  7. package/Animations/runtimeAnimation.js.map +1 -1
  8. package/AudioV2/abstractAudio/abstractSound.d.ts +2 -1
  9. package/AudioV2/abstractAudio/abstractSound.js +3 -2
  10. package/AudioV2/abstractAudio/abstractSound.js.map +1 -1
  11. package/AudioV2/abstractAudio/abstractSoundInstance.d.ts +1 -1
  12. package/AudioV2/abstractAudio/abstractSoundInstance.js.map +1 -1
  13. package/AudioV2/abstractAudio/staticSound.d.ts +6 -0
  14. package/AudioV2/abstractAudio/staticSound.js +9 -1
  15. package/AudioV2/abstractAudio/staticSound.js.map +1 -1
  16. package/AudioV2/abstractAudio/streamingSound.d.ts +6 -0
  17. package/AudioV2/abstractAudio/streamingSound.js +9 -1
  18. package/AudioV2/abstractAudio/streamingSound.js.map +1 -1
  19. package/AudioV2/webAudio/webAudioStaticSound.d.ts +1 -1
  20. package/AudioV2/webAudio/webAudioStaticSound.js +6 -3
  21. package/AudioV2/webAudio/webAudioStaticSound.js.map +1 -1
  22. package/AudioV2/webAudio/webAudioStreamingSound.d.ts +1 -1
  23. package/AudioV2/webAudio/webAudioStreamingSound.js +7 -4
  24. package/AudioV2/webAudio/webAudioStreamingSound.js.map +1 -1
  25. package/Cameras/Inputs/BaseCameraPointersInput.d.ts +2 -1
  26. package/Cameras/Inputs/BaseCameraPointersInput.js +3 -2
  27. package/Cameras/Inputs/BaseCameraPointersInput.js.map +1 -1
  28. package/Cameras/Inputs/arcRotateCameraKeyboardMoveInput.d.ts +3 -0
  29. package/Cameras/Inputs/arcRotateCameraKeyboardMoveInput.js +37 -10
  30. package/Cameras/Inputs/arcRotateCameraKeyboardMoveInput.js.map +1 -1
  31. package/Cameras/Inputs/flyCameraKeyboardInput.js +19 -9
  32. package/Cameras/Inputs/flyCameraKeyboardInput.js.map +1 -1
  33. package/Cameras/Inputs/freeCameraKeyboardMoveInput.js +21 -18
  34. package/Cameras/Inputs/freeCameraKeyboardMoveInput.js.map +1 -1
  35. package/Cameras/Inputs/geospatialCameraKeyboardInput.d.ts +2 -0
  36. package/Cameras/Inputs/geospatialCameraKeyboardInput.js +31 -12
  37. package/Cameras/Inputs/geospatialCameraKeyboardInput.js.map +1 -1
  38. package/Cameras/Inputs/geospatialCameraPointersInput.d.ts +11 -1
  39. package/Cameras/Inputs/geospatialCameraPointersInput.js +18 -2
  40. package/Cameras/Inputs/geospatialCameraPointersInput.js.map +1 -1
  41. package/Cameras/VR/vrExperienceHelper.d.ts +0 -2
  42. package/Cameras/VR/vrExperienceHelper.js +0 -2
  43. package/Cameras/VR/vrExperienceHelper.js.map +1 -1
  44. package/Cameras/VR/vrExperienceHelper.pure.js +5 -0
  45. package/Cameras/VR/vrExperienceHelper.pure.js.map +1 -1
  46. package/Cameras/arcRotateCamera.pure.js +9 -3
  47. package/Cameras/arcRotateCamera.pure.js.map +1 -1
  48. package/Cameras/targetCamera.pure.js +18 -18
  49. package/Cameras/targetCamera.pure.js.map +1 -1
  50. package/Collisions/gpuPicker.js +23 -13
  51. package/Collisions/gpuPicker.js.map +1 -1
  52. package/Engines/Native/nativeInterfaces.d.ts +3 -2
  53. package/Engines/Native/nativeInterfaces.js.map +1 -1
  54. package/Engines/Native/nativeRenderTargetWrapper.d.ts +3 -0
  55. package/Engines/Native/nativeRenderTargetWrapper.js +24 -1
  56. package/Engines/Native/nativeRenderTargetWrapper.js.map +1 -1
  57. package/Engines/WebGPU/Extensions/engine.videoTexture.pure.js +16 -3
  58. package/Engines/WebGPU/Extensions/engine.videoTexture.pure.js.map +1 -1
  59. package/Engines/WebGPU/webgpuClearQuad.pure.js +7 -1
  60. package/Engines/WebGPU/webgpuClearQuad.pure.js.map +1 -1
  61. package/Engines/abstractEngine.pure.d.ts +8 -1
  62. package/Engines/abstractEngine.pure.js +4 -3
  63. package/Engines/abstractEngine.pure.js.map +1 -1
  64. package/Engines/engine.d.ts +113 -0
  65. package/Engines/thinNativeEngine.pure.d.ts +2 -0
  66. package/Engines/thinNativeEngine.pure.js +115 -9
  67. package/Engines/thinNativeEngine.pure.js.map +1 -1
  68. package/FlowGraph/flowGraph.js +9 -0
  69. package/FlowGraph/flowGraph.js.map +1 -1
  70. package/FlowGraph/flowGraphDataConnection.pure.d.ts +14 -0
  71. package/FlowGraph/flowGraphDataConnection.pure.js +38 -1
  72. package/FlowGraph/flowGraphDataConnection.pure.js.map +1 -1
  73. package/FlowGraph/serialization.d.ts +11 -0
  74. package/FlowGraph/serialization.js +11 -1
  75. package/FlowGraph/serialization.js.map +1 -1
  76. package/FrameGraph/Tasks/Rendering/iblShadows/iblShadowsAccumulationTask.d.ts +2 -2
  77. package/FrameGraph/Tasks/Rendering/iblShadows/iblShadowsAccumulationTask.js +43 -51
  78. package/FrameGraph/Tasks/Rendering/iblShadows/iblShadowsAccumulationTask.js.map +1 -1
  79. package/FrameGraph/Tasks/Rendering/iblShadowsRendererTask.d.ts +0 -1
  80. package/FrameGraph/Tasks/Rendering/iblShadowsRendererTask.js +0 -1
  81. package/FrameGraph/Tasks/Rendering/iblShadowsRendererTask.js.map +1 -1
  82. package/FrameGraph/Tasks/Rendering/iblShadowsRendererTask.pure.d.ts +0 -1
  83. package/FrameGraph/Tasks/Rendering/iblShadowsRendererTask.pure.js +66 -35
  84. package/FrameGraph/Tasks/Rendering/iblShadowsRendererTask.pure.js.map +1 -1
  85. package/Helpers/environmentHelper.js +2 -2
  86. package/Helpers/environmentHelper.js.map +1 -1
  87. package/Lights/Clustered/clusteredLightContainer.pure.d.ts +1 -1
  88. package/Lights/Clustered/clusteredLightContainer.pure.js +15 -2
  89. package/Lights/Clustered/clusteredLightContainer.pure.js.map +1 -1
  90. package/Lights/areaLight.pure.js +5 -0
  91. package/Lights/areaLight.pure.js.map +1 -1
  92. package/Lights/light.d.ts +4 -2
  93. package/Lights/light.js +10 -4
  94. package/Lights/light.js.map +1 -1
  95. package/Lights/rectAreaLight.pure.d.ts +17 -0
  96. package/Lights/rectAreaLight.pure.js +67 -0
  97. package/Lights/rectAreaLight.pure.js.map +1 -1
  98. package/Loading/Plugins/babylonFileLoader.pure.js +4 -4
  99. package/Loading/Plugins/babylonFileLoader.pure.js.map +1 -1
  100. package/Materials/Background/backgroundMaterial.pure.js +3 -1
  101. package/Materials/Background/backgroundMaterial.pure.js.map +1 -1
  102. package/Materials/Node/Blocks/Dual/reflectionTextureBaseBlock.pure.js +2 -2
  103. package/Materials/Node/Blocks/Dual/reflectionTextureBaseBlock.pure.js.map +1 -1
  104. package/Materials/Node/Blocks/PBR/refractionBlock.pure.js +2 -2
  105. package/Materials/Node/Blocks/PBR/refractionBlock.pure.js.map +1 -1
  106. package/Materials/Node/nodeMaterial.pure.js +3 -0
  107. package/Materials/Node/nodeMaterial.pure.js.map +1 -1
  108. package/Materials/Node/nodeMaterialBuildState.d.ts +3 -3
  109. package/Materials/PBR/openpbrMaterial.pure.js +3 -1
  110. package/Materials/PBR/openpbrMaterial.pure.js.map +1 -1
  111. package/Materials/PBR/pbrMaterial.pure.js +3 -0
  112. package/Materials/PBR/pbrMaterial.pure.js.map +1 -1
  113. package/Materials/PBR/pbrMetallicRoughnessMaterial.pure.js +3 -0
  114. package/Materials/PBR/pbrMetallicRoughnessMaterial.pure.js.map +1 -1
  115. package/Materials/PBR/pbrSpecularGlossinessMaterial.pure.js +3 -0
  116. package/Materials/PBR/pbrSpecularGlossinessMaterial.pure.js.map +1 -1
  117. package/Materials/Textures/HTML/htmlInCanvasPolyfill.d.ts +43 -0
  118. package/Materials/Textures/HTML/htmlInCanvasPolyfill.js +64 -0
  119. package/Materials/Textures/HTML/htmlInCanvasPolyfill.js.map +1 -0
  120. package/Materials/Textures/HTML/htmlInteractionManager.d.ts +69 -0
  121. package/Materials/Textures/HTML/htmlInteractionManager.js +130 -0
  122. package/Materials/Textures/HTML/htmlInteractionManager.js.map +1 -0
  123. package/Materials/Textures/HTML/htmlRaycastInteractionManager.d.ts +84 -0
  124. package/Materials/Textures/HTML/htmlRaycastInteractionManager.js +246 -0
  125. package/Materials/Textures/HTML/htmlRaycastInteractionManager.js.map +1 -0
  126. package/Materials/Textures/HTML/htmlTexture.d.ts +148 -0
  127. package/Materials/Textures/HTML/htmlTexture.js +455 -0
  128. package/Materials/Textures/HTML/htmlTexture.js.map +1 -0
  129. package/Materials/Textures/HTML/index.d.ts +4 -0
  130. package/Materials/Textures/HTML/index.js +5 -0
  131. package/Materials/Textures/HTML/index.js.map +1 -0
  132. package/Materials/Textures/HTML/pure.d.ts +5 -0
  133. package/Materials/Textures/HTML/pure.js +6 -0
  134. package/Materials/Textures/HTML/pure.js.map +1 -0
  135. package/Materials/Textures/cubeTexture.d.ts +0 -1
  136. package/Materials/Textures/cubeTexture.js +0 -1
  137. package/Materials/Textures/cubeTexture.js.map +1 -1
  138. package/Materials/Textures/cubeTexture.pure.js +2 -0
  139. package/Materials/Textures/cubeTexture.pure.js.map +1 -1
  140. package/Materials/Textures/dynamicTexture.d.ts +0 -1
  141. package/Materials/Textures/dynamicTexture.js +0 -1
  142. package/Materials/Textures/dynamicTexture.js.map +1 -1
  143. package/Materials/Textures/dynamicTexture.pure.js +2 -0
  144. package/Materials/Textures/dynamicTexture.pure.js.map +1 -1
  145. package/Materials/Textures/htmlElementTexture.d.ts +0 -2
  146. package/Materials/Textures/htmlElementTexture.js +0 -2
  147. package/Materials/Textures/htmlElementTexture.js.map +1 -1
  148. package/Materials/Textures/htmlElementTexture.pure.js +4 -0
  149. package/Materials/Textures/htmlElementTexture.pure.js.map +1 -1
  150. package/Materials/Textures/index.d.ts +1 -0
  151. package/Materials/Textures/index.js +1 -1
  152. package/Materials/Textures/index.js.map +1 -1
  153. package/Materials/Textures/multiRenderTarget.d.ts +0 -1
  154. package/Materials/Textures/multiRenderTarget.js +0 -1
  155. package/Materials/Textures/multiRenderTarget.js.map +1 -1
  156. package/Materials/Textures/multiRenderTarget.pure.js +2 -0
  157. package/Materials/Textures/multiRenderTarget.pure.js.map +1 -1
  158. package/Materials/Textures/pure.d.ts +1 -0
  159. package/Materials/Textures/pure.js +1 -0
  160. package/Materials/Textures/pure.js.map +1 -1
  161. package/Materials/Textures/videoTexture.pure.d.ts +3 -0
  162. package/Materials/Textures/videoTexture.pure.js +30 -0
  163. package/Materials/Textures/videoTexture.pure.js.map +1 -1
  164. package/Materials/standardMaterial.pure.js +3 -1
  165. package/Materials/standardMaterial.pure.js.map +1 -1
  166. package/Meshes/GaussianSplatting/gaussianSplattingMeshBase.pure.d.ts +9 -0
  167. package/Meshes/GaussianSplatting/gaussianSplattingMeshBase.pure.js +12 -0
  168. package/Meshes/GaussianSplatting/gaussianSplattingMeshBase.pure.js.map +1 -1
  169. package/Meshes/GaussianSplatting/gaussianSplattingSortWorker.d.ts +1 -1
  170. package/Meshes/GaussianSplatting/gaussianSplattingSortWorker.js +20 -6
  171. package/Meshes/GaussianSplatting/gaussianSplattingSortWorker.js.map +1 -1
  172. package/Misc/decorators.functions.d.ts +7 -0
  173. package/Misc/decorators.functions.js +86 -35
  174. package/Misc/decorators.functions.js.map +1 -1
  175. package/Misc/snapshotRenderingHelper.d.ts +4 -1
  176. package/Misc/snapshotRenderingHelper.js.map +1 -1
  177. package/Misc/symbolMetadataPolyfill.d.ts +11 -0
  178. package/Misc/symbolMetadataPolyfill.js +22 -0
  179. package/Misc/symbolMetadataPolyfill.js.map +1 -0
  180. package/Misc/tools.pure.js +1 -1
  181. package/Misc/tools.pure.js.map +1 -1
  182. package/Particles/gpuParticleSystem.d.ts +0 -1
  183. package/Particles/gpuParticleSystem.js +0 -1
  184. package/Particles/gpuParticleSystem.js.map +1 -1
  185. package/Particles/thinParticleSystem.d.ts +0 -1
  186. package/Particles/thinParticleSystem.js +0 -1
  187. package/Particles/thinParticleSystem.js.map +1 -1
  188. package/Particles/thinParticleSystem.pure.js +2 -0
  189. package/Particles/thinParticleSystem.pure.js.map +1 -1
  190. package/Particles/webgl2ParticleSystem.pure.js +2 -0
  191. package/Particles/webgl2ParticleSystem.pure.js.map +1 -1
  192. package/PostProcesses/RenderPipeline/Pipelines/lensRenderingPipeline.d.ts +0 -1
  193. package/PostProcesses/RenderPipeline/Pipelines/lensRenderingPipeline.js +0 -1
  194. package/PostProcesses/RenderPipeline/Pipelines/lensRenderingPipeline.js.map +1 -1
  195. package/PostProcesses/RenderPipeline/Pipelines/ssao2RenderingPipeline.pure.js +7 -0
  196. package/PostProcesses/RenderPipeline/Pipelines/ssao2RenderingPipeline.pure.js.map +1 -1
  197. package/PostProcesses/RenderPipeline/Pipelines/ssaoRenderingPipeline.d.ts +0 -1
  198. package/PostProcesses/RenderPipeline/Pipelines/ssaoRenderingPipeline.js +0 -1
  199. package/PostProcesses/RenderPipeline/Pipelines/ssaoRenderingPipeline.js.map +1 -1
  200. package/PostProcesses/postProcess.pure.js +5 -0
  201. package/PostProcesses/postProcess.pure.js.map +1 -1
  202. package/PostProcesses/vrMultiviewToSingleviewPostProcess.d.ts +0 -1
  203. package/PostProcesses/vrMultiviewToSingleviewPostProcess.js +0 -1
  204. package/PostProcesses/vrMultiviewToSingleviewPostProcess.js.map +1 -1
  205. package/PostProcesses/vrMultiviewToSingleviewPostProcess.pure.js +2 -0
  206. package/PostProcesses/vrMultiviewToSingleviewPostProcess.pure.js.map +1 -1
  207. package/Rendering/IBLShadows/iblShadowsRenderPipeline.d.ts +0 -2
  208. package/Rendering/IBLShadows/iblShadowsRenderPipeline.js +0 -2
  209. package/Rendering/IBLShadows/iblShadowsRenderPipeline.js.map +1 -1
  210. package/Rendering/IBLShadows/iblShadowsRenderPipeline.pure.js +5 -0
  211. package/Rendering/IBLShadows/iblShadowsRenderPipeline.pure.js.map +1 -1
  212. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.d.ts +0 -1
  213. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.js +0 -1
  214. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.js.map +1 -1
  215. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.pure.d.ts +1 -0
  216. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.pure.js +34 -0
  217. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.pure.js.map +1 -1
  218. package/Rendering/prePassRenderer.d.ts +0 -2
  219. package/Rendering/prePassRenderer.js +0 -2
  220. package/Rendering/prePassRenderer.js.map +1 -1
  221. package/Rendering/prePassRenderer.pure.js +2 -0
  222. package/Rendering/prePassRenderer.pure.js.map +1 -1
  223. package/Rendering/thinDepthPeelingRenderer.d.ts +0 -1
  224. package/Rendering/thinDepthPeelingRenderer.js +0 -1
  225. package/Rendering/thinDepthPeelingRenderer.js.map +1 -1
  226. package/Shaders/ShadersInclude/openpbrEnvironmentLighting.js +35 -5
  227. package/Shaders/ShadersInclude/openpbrEnvironmentLighting.js.map +1 -1
  228. package/ShadersWGSL/ShadersInclude/helperFunctions.js +2 -2
  229. package/ShadersWGSL/ShadersInclude/helperFunctions.js.map +1 -1
  230. package/ShadersWGSL/ShadersInclude/openpbrEnvironmentLighting.js +39 -7
  231. package/ShadersWGSL/ShadersInclude/openpbrEnvironmentLighting.js.map +1 -1
  232. package/ShadersWGSL/ShadersInclude/openpbrIblFunctions.js +4 -3
  233. package/ShadersWGSL/ShadersInclude/openpbrIblFunctions.js.map +1 -1
  234. package/index.d.ts +1 -0
  235. package/index.js +1 -0
  236. package/index.js.map +1 -1
  237. package/package.json +3 -11
@@ -0,0 +1,130 @@
1
+ import { Matrix, Vector3 } from "../../../Maths/math.vector.pure.js";
2
+ import { Viewport } from "../../../Maths/math.viewport.js";
3
+ // Attribution: the overlay-and-hit-test technique used here is prior art from `three-html-render`
4
+ // (Palash Bansal, MIT), three.js' `InteractionManager`, and Jake Archibald's `curved-markup` demo.
5
+ // The implementation below is a clean-room rewrite against Babylon.js' own projection APIs.
6
+ /**
7
+ * Builds the CSS `transform` string that positions an overlay element over the projected face of a mesh.
8
+ *
9
+ * The transform is applied with `transform-origin: 0 0`. Reading right to left it centers the element on
10
+ * its own origin, scales and rotates it to match the projected face, then translates it to the projected
11
+ * center (in CSS pixels, top-left origin).
12
+ * @param centerX projected center X in CSS pixels
13
+ * @param centerY projected center Y in CSS pixels
14
+ * @param scaleX horizontal scale to apply to the element
15
+ * @param scaleY vertical scale to apply to the element
16
+ * @param rotation in-plane rotation in radians
17
+ * @param elementWidth element width in pixels
18
+ * @param elementHeight element height in pixels
19
+ * @returns the CSS transform string
20
+ */
21
+ export function ComputeOverlayCssTransform(centerX, centerY, scaleX, scaleY, rotation, elementWidth, elementHeight) {
22
+ return `translate(${centerX}px, ${centerY}px) rotate(${rotation}rad) scale(${scaleX}, ${scaleY}) translate(${-elementWidth / 2}px, ${-elementHeight / 2}px)`;
23
+ }
24
+ /**
25
+ * Overlays the live HTML element of an {@link HtmlTexture} on top of the projected face of a planar mesh,
26
+ * so the browser hit-tests the real DOM element natively (enabling focus, text selection and form input).
27
+ *
28
+ * Each frame the mesh face is projected to screen space and the element is translated, scaled and rotated
29
+ * to match. This is best suited to planar, camera-facing surfaces. For arbitrary or curved meshes, use the
30
+ * UV-based {@link HtmlRaycastInteractionManager} instead.
31
+ *
32
+ * The overlay is screen-aligned (position, size and in-plane rotation); it does not apply a full
33
+ * perspective skew, so steeply oblique faces will not be perspective-correct.
34
+ */
35
+ export class HtmlInteractionManager {
36
+ /**
37
+ * Creates an overlay interaction manager.
38
+ * @param scene the scene whose camera drives the projection
39
+ * @param texture the HTML texture whose element should be overlaid
40
+ * @param mesh the planar mesh displaying the texture
41
+ * @param options optional configuration
42
+ */
43
+ constructor(scene, texture, mesh, options = {}) {
44
+ this._previousTransform = "";
45
+ // Reused across frames to avoid per-frame allocations.
46
+ this._viewport = new Viewport(0, 0, 0, 0);
47
+ this._center = new Vector3();
48
+ this._xAxis = new Vector3();
49
+ this._yAxis = new Vector3();
50
+ this._projectedCenter = new Vector3();
51
+ this._projectedX = new Vector3();
52
+ this._projectedY = new Vector3();
53
+ this._corner = new Vector3();
54
+ this._restoreInertOnDispose = false;
55
+ this._scene = scene;
56
+ this._mesh = mesh;
57
+ this._element = options.targetElement ?? texture.element;
58
+ this._element.style.position = "absolute";
59
+ this._element.style.left = "0px";
60
+ this._element.style.top = "0px";
61
+ this._element.style.transformOrigin = "0 0";
62
+ this._element.style.pointerEvents = (options.enablePointerEvents ?? true) ? "auto" : "none";
63
+ // This manager relies on native browser hit testing of the element. HtmlTexture marks the element
64
+ // `inert` while hosting it inside the rendering canvas (so it does not steal input from the canvas);
65
+ // clear that here so native interaction works, and restore it on dispose.
66
+ this._restoreInertOnDispose = this._element.hasAttribute("inert");
67
+ if (this._restoreInertOnDispose) {
68
+ this._element.removeAttribute("inert");
69
+ }
70
+ this._observer = scene.onAfterRenderObservable.add(() => this._update());
71
+ }
72
+ _update() {
73
+ const camera = this._scene.activeCamera;
74
+ const engine = this._scene.getEngine();
75
+ if (!camera) {
76
+ return;
77
+ }
78
+ const boundingBox = this._mesh.getBoundingInfo().boundingBox;
79
+ const world = this._mesh.getWorldMatrix();
80
+ const extend = boundingBox.extendSize;
81
+ this._center.copyFrom(boundingBox.centerWorld);
82
+ // World-space half-axis directions of the mesh face.
83
+ Vector3.TransformNormalFromFloatsToRef(extend.x, 0, 0, world, this._xAxis);
84
+ Vector3.TransformNormalFromFloatsToRef(0, extend.y, 0, world, this._yAxis);
85
+ const transform = this._scene.getTransformMatrix();
86
+ camera.viewport.toGlobalToRef(engine.getRenderWidth(), engine.getRenderHeight(), this._viewport);
87
+ Vector3.ProjectToRef(this._center, Matrix.IdentityReadOnly, transform, this._viewport, this._projectedCenter);
88
+ this._center.addToRef(this._xAxis, this._corner);
89
+ Vector3.ProjectToRef(this._corner, Matrix.IdentityReadOnly, transform, this._viewport, this._projectedX);
90
+ this._center.addToRef(this._yAxis, this._corner);
91
+ Vector3.ProjectToRef(this._corner, Matrix.IdentityReadOnly, transform, this._viewport, this._projectedY);
92
+ // Hide the overlay when the face is behind the camera.
93
+ if (this._projectedCenter.z < 0 || this._projectedCenter.z > 1) {
94
+ this._setTransform("translate(-99999px, -99999px)");
95
+ return;
96
+ }
97
+ const dpr = engine.getHardwareScalingLevel();
98
+ const centerX = this._projectedCenter.x * dpr;
99
+ const centerY = this._projectedCenter.y * dpr;
100
+ const halfWidth = Math.hypot(this._projectedX.x - this._projectedCenter.x, this._projectedX.y - this._projectedCenter.y) * dpr;
101
+ const halfHeight = Math.hypot(this._projectedY.x - this._projectedCenter.x, this._projectedY.y - this._projectedCenter.y) * dpr;
102
+ const rotation = Math.atan2(this._projectedX.y - this._projectedCenter.y, this._projectedX.x - this._projectedCenter.x);
103
+ const elementWidth = this._element.offsetWidth || 1;
104
+ const elementHeight = this._element.offsetHeight || 1;
105
+ const scaleX = (2 * halfWidth) / elementWidth;
106
+ const scaleY = (2 * halfHeight) / elementHeight;
107
+ this._setTransform(ComputeOverlayCssTransform(centerX, centerY, scaleX, scaleY, rotation, elementWidth, elementHeight));
108
+ }
109
+ _setTransform(transform) {
110
+ if (transform === this._previousTransform) {
111
+ return;
112
+ }
113
+ this._previousTransform = transform;
114
+ this._element.style.transform = transform;
115
+ }
116
+ /**
117
+ * Detaches the manager and stops updating the overlay.
118
+ */
119
+ dispose() {
120
+ if (this._restoreInertOnDispose) {
121
+ this._element.setAttribute("inert", "");
122
+ this._restoreInertOnDispose = false;
123
+ }
124
+ if (this._observer) {
125
+ this._scene.onAfterRenderObservable.remove(this._observer);
126
+ this._observer = null;
127
+ }
128
+ }
129
+ }
130
+ //# sourceMappingURL=htmlInteractionManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"htmlInteractionManager.js","sourceRoot":"","sources":["../../../../../../dev/core/src/Materials/Textures/HTML/htmlInteractionManager.ts"],"names":[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AAG,UAAU,CAAC,GAAG,aAAa,CAAC;QAEhD,IAAI,CAAC,aAAa,CAAC,0BAA0B,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,aAAa,CAAC,CAAC,CAAC;IAC5H,CAAC;IAEO,aAAa,CAAC,SAAiB;QACnC,IAAI,SAAS,KAAK,IAAI,CAAC,kBAAkB,EAAE,CAAC;YACxC,OAAO;QACX,CAAC;QACD,IAAI,CAAC,kBAAkB,GAAG,SAAS,CAAC;QACpC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,SAAS,GAAG,SAAS,CAAC;IAC9C,CAAC;IAED;;OAEG;IACI,OAAO;QACV,IAAI,IAAI,CAAC,sBAAsB,EAAE,CAAC;YAC9B,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;YACxC,IAAI,CAAC,sBAAsB,GAAG,KAAK,CAAC;QACxC,CAAC;QACD,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACjB,IAAI,CAAC,MAAM,CAAC,uBAAuB,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAC3D,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QAC1B,CAAC;IACL,CAAC;CACJ","sourcesContent":["import { type Scene } from \"../../../scene.pure\";\nimport { type AbstractMesh } from \"../../../Meshes/abstractMesh.pure\";\nimport { type HtmlTexture } from \"./htmlTexture\";\nimport { type Observer } from \"../../../Misc/observable.pure\";\nimport { type Nullable } from \"../../../types\";\n\nimport { Matrix, Vector3 } from \"../../../Maths/math.vector.pure\";\nimport { Viewport } from \"../../../Maths/math.viewport\";\n\n// Attribution: the overlay-and-hit-test technique used here is prior art from `three-html-render`\n// (Palash Bansal, MIT), three.js' `InteractionManager`, and Jake Archibald's `curved-markup` demo.\n// The implementation below is a clean-room rewrite against Babylon.js' own projection APIs.\n\n/**\n * Builds the CSS `transform` string that positions an overlay element over the projected face of a mesh.\n *\n * The transform is applied with `transform-origin: 0 0`. Reading right to left it centers the element on\n * its own origin, scales and rotates it to match the projected face, then translates it to the projected\n * center (in CSS pixels, top-left origin).\n * @param centerX projected center X in CSS pixels\n * @param centerY projected center Y in CSS pixels\n * @param scaleX horizontal scale to apply to the element\n * @param scaleY vertical scale to apply to the element\n * @param rotation in-plane rotation in radians\n * @param elementWidth element width in pixels\n * @param elementHeight element height in pixels\n * @returns the CSS transform string\n */\nexport function ComputeOverlayCssTransform(\n centerX: number,\n centerY: number,\n scaleX: number,\n scaleY: number,\n rotation: number,\n elementWidth: number,\n elementHeight: number\n): string {\n return `translate(${centerX}px, ${centerY}px) rotate(${rotation}rad) scale(${scaleX}, ${scaleY}) translate(${-elementWidth / 2}px, ${-elementHeight / 2}px)`;\n}\n\n/**\n * Options for {@link HtmlInteractionManager}.\n */\nexport interface IHtmlInteractionManagerOptions {\n /** The DOM element to overlay (defaults to the texture's element). */\n targetElement?: HTMLElement;\n /** Whether the overlay element captures pointer events so the browser hit-tests it natively (default true). */\n enablePointerEvents?: boolean;\n}\n\n/**\n * Overlays the live HTML element of an {@link HtmlTexture} on top of the projected face of a planar mesh,\n * so the browser hit-tests the real DOM element natively (enabling focus, text selection and form input).\n *\n * Each frame the mesh face is projected to screen space and the element is translated, scaled and rotated\n * to match. This is best suited to planar, camera-facing surfaces. For arbitrary or curved meshes, use the\n * UV-based {@link HtmlRaycastInteractionManager} instead.\n *\n * The overlay is screen-aligned (position, size and in-plane rotation); it does not apply a full\n * perspective skew, so steeply oblique faces will not be perspective-correct.\n */\nexport class HtmlInteractionManager {\n private readonly _scene: Scene;\n private readonly _mesh: AbstractMesh;\n private readonly _element: HTMLElement;\n private _observer: Nullable<Observer<Scene>>;\n private _previousTransform: string = \"\";\n\n // Reused across frames to avoid per-frame allocations.\n private readonly _viewport = new Viewport(0, 0, 0, 0);\n private readonly _center = new Vector3();\n private readonly _xAxis = new Vector3();\n private readonly _yAxis = new Vector3();\n private readonly _projectedCenter = new Vector3();\n private readonly _projectedX = new Vector3();\n private readonly _projectedY = new Vector3();\n private readonly _corner = new Vector3();\n private _restoreInertOnDispose: boolean = false;\n\n /**\n * Creates an overlay interaction manager.\n * @param scene the scene whose camera drives the projection\n * @param texture the HTML texture whose element should be overlaid\n * @param mesh the planar mesh displaying the texture\n * @param options optional configuration\n */\n constructor(scene: Scene, texture: HtmlTexture, mesh: AbstractMesh, options: IHtmlInteractionManagerOptions = {}) {\n this._scene = scene;\n this._mesh = mesh;\n this._element = options.targetElement ?? texture.element;\n\n this._element.style.position = \"absolute\";\n this._element.style.left = \"0px\";\n this._element.style.top = \"0px\";\n this._element.style.transformOrigin = \"0 0\";\n this._element.style.pointerEvents = (options.enablePointerEvents ?? true) ? \"auto\" : \"none\";\n\n // This manager relies on native browser hit testing of the element. HtmlTexture marks the element\n // `inert` while hosting it inside the rendering canvas (so it does not steal input from the canvas);\n // clear that here so native interaction works, and restore it on dispose.\n this._restoreInertOnDispose = this._element.hasAttribute(\"inert\");\n if (this._restoreInertOnDispose) {\n this._element.removeAttribute(\"inert\");\n }\n\n this._observer = scene.onAfterRenderObservable.add(() => this._update());\n }\n\n private _update(): void {\n const camera = this._scene.activeCamera;\n const engine = this._scene.getEngine();\n if (!camera) {\n return;\n }\n\n const boundingBox = this._mesh.getBoundingInfo().boundingBox;\n const world = this._mesh.getWorldMatrix();\n const extend = boundingBox.extendSize;\n\n this._center.copyFrom(boundingBox.centerWorld);\n // World-space half-axis directions of the mesh face.\n Vector3.TransformNormalFromFloatsToRef(extend.x, 0, 0, world, this._xAxis);\n Vector3.TransformNormalFromFloatsToRef(0, extend.y, 0, world, this._yAxis);\n\n const transform = this._scene.getTransformMatrix();\n camera.viewport.toGlobalToRef(engine.getRenderWidth(), engine.getRenderHeight(), this._viewport);\n\n Vector3.ProjectToRef(this._center, Matrix.IdentityReadOnly, transform, this._viewport, this._projectedCenter);\n this._center.addToRef(this._xAxis, this._corner);\n Vector3.ProjectToRef(this._corner, Matrix.IdentityReadOnly, transform, this._viewport, this._projectedX);\n this._center.addToRef(this._yAxis, this._corner);\n Vector3.ProjectToRef(this._corner, Matrix.IdentityReadOnly, transform, this._viewport, this._projectedY);\n\n // Hide the overlay when the face is behind the camera.\n if (this._projectedCenter.z < 0 || this._projectedCenter.z > 1) {\n this._setTransform(\"translate(-99999px, -99999px)\");\n return;\n }\n\n const dpr = engine.getHardwareScalingLevel();\n const centerX = this._projectedCenter.x * dpr;\n const centerY = this._projectedCenter.y * dpr;\n\n const halfWidth = Math.hypot(this._projectedX.x - this._projectedCenter.x, this._projectedX.y - this._projectedCenter.y) * dpr;\n const halfHeight = Math.hypot(this._projectedY.x - this._projectedCenter.x, this._projectedY.y - this._projectedCenter.y) * dpr;\n const rotation = Math.atan2(this._projectedX.y - this._projectedCenter.y, this._projectedX.x - this._projectedCenter.x);\n\n const elementWidth = this._element.offsetWidth || 1;\n const elementHeight = this._element.offsetHeight || 1;\n const scaleX = (2 * halfWidth) / elementWidth;\n const scaleY = (2 * halfHeight) / elementHeight;\n\n this._setTransform(ComputeOverlayCssTransform(centerX, centerY, scaleX, scaleY, rotation, elementWidth, elementHeight));\n }\n\n private _setTransform(transform: string): void {\n if (transform === this._previousTransform) {\n return;\n }\n this._previousTransform = transform;\n this._element.style.transform = transform;\n }\n\n /**\n * Detaches the manager and stops updating the overlay.\n */\n public dispose(): void {\n if (this._restoreInertOnDispose) {\n this._element.setAttribute(\"inert\", \"\");\n this._restoreInertOnDispose = false;\n }\n if (this._observer) {\n this._scene.onAfterRenderObservable.remove(this._observer);\n this._observer = null;\n }\n }\n}\n"]}
@@ -0,0 +1,84 @@
1
+ import { type Scene } from "../../../scene.pure.js";
2
+ import { type AbstractMesh } from "../../../Meshes/abstractMesh.pure.js";
3
+ import { type HtmlTexture } from "./htmlTexture.js";
4
+ /**
5
+ * Maps a UV coordinate sampled from an {@link HtmlTexture} to a pixel position inside the source element.
6
+ *
7
+ * When `invertY` is true (the default, matching {@link HtmlTexture}'s default upload orientation) the V
8
+ * axis is flipped so that `v = 1` maps to the top of the element, matching the top-left origin used by
9
+ * DOM layout.
10
+ * @param u horizontal texture coordinate, normally in [0, 1]
11
+ * @param v vertical texture coordinate, normally in [0, 1]
12
+ * @param width element width in pixels
13
+ * @param height element height in pixels
14
+ * @param invertY whether the texture content is stored Y-inverted (default true)
15
+ * @returns the pixel position `{ x, y }` within the element
16
+ */
17
+ export declare function GetElementPixelFromUv(u: number, v: number, width: number, height: number, invertY?: boolean): {
18
+ x: number;
19
+ y: number;
20
+ };
21
+ /**
22
+ * Options for {@link HtmlRaycastInteractionManager}.
23
+ */
24
+ export interface IHtmlRaycastInteractionManagerOptions {
25
+ /** The DOM element that receives the forwarded pointer events (defaults to the texture's element). */
26
+ targetElement?: HTMLElement;
27
+ /** Whether hits on back-facing geometry are ignored (default true). */
28
+ backFaceCulling?: boolean;
29
+ /** Whether the texture content is stored Y-inverted, used when mapping UVs to element pixels (default true). */
30
+ invertY?: boolean;
31
+ }
32
+ /**
33
+ * Routes Babylon pointer events to a live HTML element rendered through an {@link HtmlTexture}.
34
+ *
35
+ * On every pointer event the scene is picked; when the configured mesh is hit, the picked UV is mapped to
36
+ * a pixel inside the source element and an equivalent DOM pointer (and mouse) event is dispatched there.
37
+ * This works for arbitrary meshes (planes, boxes, curved surfaces) because it relies on UV coordinates
38
+ * rather than a flat CSS overlay. For a perspective-correct overlay on planar surfaces, see
39
+ * {@link HtmlInteractionManager}.
40
+ */
41
+ export declare class HtmlRaycastInteractionManager {
42
+ private readonly _scene;
43
+ private readonly _mesh;
44
+ private readonly _element;
45
+ private readonly _backFaceCulling;
46
+ private readonly _invertY;
47
+ private _observer;
48
+ private _downTarget;
49
+ private readonly _containHandler;
50
+ private readonly _rectCache;
51
+ private _rectCacheValid;
52
+ private readonly _invalidateRectCache;
53
+ private _resizeObserver;
54
+ private _mutationObserver;
55
+ /**
56
+ * Creates a raycast interaction manager.
57
+ * @param scene the scene to listen to for pointer events
58
+ * @param texture the HTML texture whose element should receive the forwarded events
59
+ * @param mesh the mesh displaying the texture; only hits on this mesh are forwarded
60
+ * @param options optional configuration
61
+ */
62
+ constructor(scene: Scene, texture: HtmlTexture, mesh: AbstractMesh, options?: IHtmlRaycastInteractionManagerOptions);
63
+ private _onPointer;
64
+ private _isBackFace;
65
+ private _dispatch;
66
+ /**
67
+ * Resolves the deepest descendant of the hosted element whose layout box contains the given client point.
68
+ * @param clientX horizontal client coordinate
69
+ * @param clientY vertical client coordinate
70
+ * @returns the deepest matching element, or the hosted element itself when no descendant matches
71
+ */
72
+ private _resolveTarget;
73
+ /**
74
+ * Returns the client rect of an element, served from a cache that is rebuilt only when layout may have
75
+ * changed (see the constructor). This keeps pointer routing free of per-event layout reads.
76
+ * @param element the element whose bounding rect is needed
77
+ * @returns the element's cached {@link DOMRect}
78
+ */
79
+ private _getRect;
80
+ /**
81
+ * Detaches the manager and stops forwarding pointer events.
82
+ */
83
+ dispose(): void;
84
+ }
@@ -0,0 +1,246 @@
1
+ import { PointerEventTypes } from "../../../Events/pointerEvents.js";
2
+ // Attribution: the raycast/UV event-routing technique used here is prior art from
3
+ // `three-html-render` (Palash Bansal, MIT) and Jake Archibald's `curved-markup` demo.
4
+ // The implementation below is a clean-room rewrite against Babylon.js' own picking APIs.
5
+ /**
6
+ * Maps a UV coordinate sampled from an {@link HtmlTexture} to a pixel position inside the source element.
7
+ *
8
+ * When `invertY` is true (the default, matching {@link HtmlTexture}'s default upload orientation) the V
9
+ * axis is flipped so that `v = 1` maps to the top of the element, matching the top-left origin used by
10
+ * DOM layout.
11
+ * @param u horizontal texture coordinate, normally in [0, 1]
12
+ * @param v vertical texture coordinate, normally in [0, 1]
13
+ * @param width element width in pixels
14
+ * @param height element height in pixels
15
+ * @param invertY whether the texture content is stored Y-inverted (default true)
16
+ * @returns the pixel position `{ x, y }` within the element
17
+ */
18
+ export function GetElementPixelFromUv(u, v, width, height, invertY = true) {
19
+ return { x: u * width, y: (invertY ? 1 - v : v) * height };
20
+ }
21
+ /**
22
+ * Routes Babylon pointer events to a live HTML element rendered through an {@link HtmlTexture}.
23
+ *
24
+ * On every pointer event the scene is picked; when the configured mesh is hit, the picked UV is mapped to
25
+ * a pixel inside the source element and an equivalent DOM pointer (and mouse) event is dispatched there.
26
+ * This works for arbitrary meshes (planes, boxes, curved surfaces) because it relies on UV coordinates
27
+ * rather than a flat CSS overlay. For a perspective-correct overlay on planar surfaces, see
28
+ * {@link HtmlInteractionManager}.
29
+ */
30
+ export class HtmlRaycastInteractionManager {
31
+ /**
32
+ * Creates a raycast interaction manager.
33
+ * @param scene the scene to listen to for pointer events
34
+ * @param texture the HTML texture whose element should receive the forwarded events
35
+ * @param mesh the mesh displaying the texture; only hits on this mesh are forwarded
36
+ * @param options optional configuration
37
+ */
38
+ constructor(scene, texture, mesh, options = {}) {
39
+ this._rectCache = new Map();
40
+ this._rectCacheValid = false;
41
+ this._resizeObserver = null;
42
+ this._mutationObserver = null;
43
+ this._scene = scene;
44
+ this._mesh = mesh;
45
+ this._element = options.targetElement ?? texture.element;
46
+ this._backFaceCulling = options.backFaceCulling ?? true;
47
+ this._invertY = options.invertY ?? true;
48
+ // The hosted element is a child of the engine's rendering canvas, where Babylon's own input
49
+ // listeners live. The synthetic events we dispatch bubble, so without this they would reach the
50
+ // canvas and be re-interpreted as real input - moving the camera and re-entering this manager in a
51
+ // feedback loop. Stopping propagation at the host element keeps them inside the HTML subtree (so
52
+ // event delegation still works) while never leaking back to the engine.
53
+ this._containHandler = (evt) => evt.stopPropagation();
54
+ for (const type of _ContainedEventTypes) {
55
+ this._element.addEventListener(type, this._containHandler);
56
+ }
57
+ // Resolving the target reads layout (getBoundingClientRect) for the element and its descendants on
58
+ // every pointer event. Cache those rects and only re-read them when layout can actually change - on
59
+ // size changes, DOM mutations, or viewport scroll/resize - so high-frequency pointermove events stay
60
+ // pure arithmetic instead of forcing a synchronous reflow each time.
61
+ this._invalidateRectCache = () => {
62
+ this._rectCacheValid = false;
63
+ };
64
+ if (typeof ResizeObserver !== "undefined") {
65
+ this._resizeObserver = new ResizeObserver(this._invalidateRectCache);
66
+ this._resizeObserver.observe(this._element);
67
+ }
68
+ if (typeof MutationObserver !== "undefined") {
69
+ this._mutationObserver = new MutationObserver(this._invalidateRectCache);
70
+ this._mutationObserver.observe(this._element, { attributes: true, childList: true, characterData: true, subtree: true });
71
+ }
72
+ if (typeof window !== "undefined") {
73
+ window.addEventListener("resize", this._invalidateRectCache);
74
+ window.addEventListener("scroll", this._invalidateRectCache, true);
75
+ }
76
+ this._observer = scene.onPointerObservable.add((pointerInfo) => this._onPointer(pointerInfo));
77
+ }
78
+ _onPointer(pointerInfo) {
79
+ const domName = _PointerTypeToDomEvent(pointerInfo.type);
80
+ if (!domName) {
81
+ return;
82
+ }
83
+ const pickInfo = pointerInfo.pickInfo;
84
+ if (!pickInfo || !pickInfo.hit || pickInfo.pickedMesh !== this._mesh) {
85
+ return;
86
+ }
87
+ if (this._backFaceCulling && this._isBackFace(pointerInfo)) {
88
+ return;
89
+ }
90
+ const uv = pickInfo.getTextureCoordinates();
91
+ if (!uv) {
92
+ return;
93
+ }
94
+ const rect = this._getRect(this._element);
95
+ const { x, y } = GetElementPixelFromUv(uv.x, uv.y, rect.width || this._element.offsetWidth, rect.height || this._element.offsetHeight, this._invertY);
96
+ this._dispatch(domName, rect.left + x, rect.top + y, pointerInfo);
97
+ }
98
+ _isBackFace(pointerInfo) {
99
+ const pickInfo = pointerInfo.pickInfo;
100
+ const normal = pickInfo.getNormal(true, true);
101
+ if (!normal || !pickInfo.pickedPoint) {
102
+ return false;
103
+ }
104
+ const camera = this._scene.activeCamera;
105
+ if (!camera) {
106
+ return false;
107
+ }
108
+ // The face is back-facing when its world normal points away from the camera-to-surface direction.
109
+ const toSurface = pickInfo.pickedPoint.subtract(camera.globalPosition);
110
+ return normal.dot(toSurface) > 0;
111
+ }
112
+ _dispatch(domName, clientX, clientY, pointerInfo) {
113
+ if (typeof PointerEvent === "undefined") {
114
+ return;
115
+ }
116
+ const sourceEvent = pointerInfo.event;
117
+ // document.elementFromPoint is unreliable here: the element is hosted inside the engine's rendering
118
+ // canvas (whose painted WebGL/WebGPU content covers the DOM children) and is marked `inert`, so the
119
+ // browser's own hit testing skips it. Resolve the deepest descendant under the mapped point manually
120
+ // from the laid-out subtree instead; synthetic dispatch still triggers listeners on inert elements.
121
+ const target = this._resolveTarget(clientX, clientY);
122
+ const init = {
123
+ bubbles: true,
124
+ cancelable: true,
125
+ clientX,
126
+ clientY,
127
+ button: sourceEvent.button ?? 0,
128
+ buttons: sourceEvent.buttons ?? 0,
129
+ pointerId: sourceEvent.pointerId ?? 1,
130
+ pointerType: sourceEvent.pointerType ?? "mouse",
131
+ };
132
+ target.dispatchEvent(new PointerEvent(domName, init));
133
+ const mouseName = _PointerToMouseEvent(domName);
134
+ if (mouseName) {
135
+ target.dispatchEvent(new MouseEvent(mouseName, init));
136
+ }
137
+ // The browser synthesizes a "click" from a native down/up pair, but synthetic mouse events do not,
138
+ // so we emit it ourselves: on pointerdown remember the target, then on pointerup dispatch a click
139
+ // when the release lands on the same element (matching native click semantics closely enough).
140
+ // Native browsers only synthesize "click" for the primary button, so gate on button 0.
141
+ if (domName === "pointerdown") {
142
+ this._downTarget = target;
143
+ }
144
+ else if (domName === "pointerup") {
145
+ if (this._downTarget === target && init.button === 0) {
146
+ target.dispatchEvent(new MouseEvent("click", init));
147
+ }
148
+ this._downTarget = null;
149
+ }
150
+ }
151
+ /**
152
+ * Resolves the deepest descendant of the hosted element whose layout box contains the given client point.
153
+ * @param clientX horizontal client coordinate
154
+ * @param clientY vertical client coordinate
155
+ * @returns the deepest matching element, or the hosted element itself when no descendant matches
156
+ */
157
+ _resolveTarget(clientX, clientY) {
158
+ let target = this._element;
159
+ let advanced = true;
160
+ while (advanced) {
161
+ advanced = false;
162
+ const children = target.children;
163
+ for (let i = children.length - 1; i >= 0; i--) {
164
+ const child = children[i];
165
+ const rect = this._getRect(child);
166
+ if (clientX >= rect.left && clientX <= rect.right && clientY >= rect.top && clientY <= rect.bottom) {
167
+ target = child;
168
+ advanced = true;
169
+ break;
170
+ }
171
+ }
172
+ }
173
+ return target;
174
+ }
175
+ /**
176
+ * Returns the client rect of an element, served from a cache that is rebuilt only when layout may have
177
+ * changed (see the constructor). This keeps pointer routing free of per-event layout reads.
178
+ * @param element the element whose bounding rect is needed
179
+ * @returns the element's cached {@link DOMRect}
180
+ */
181
+ _getRect(element) {
182
+ if (!this._rectCacheValid) {
183
+ this._rectCache.clear();
184
+ this._rectCacheValid = true;
185
+ }
186
+ let rect = this._rectCache.get(element);
187
+ if (!rect) {
188
+ rect = element.getBoundingClientRect();
189
+ this._rectCache.set(element, rect);
190
+ }
191
+ return rect;
192
+ }
193
+ /**
194
+ * Detaches the manager and stops forwarding pointer events.
195
+ */
196
+ dispose() {
197
+ for (const type of _ContainedEventTypes) {
198
+ this._element.removeEventListener(type, this._containHandler);
199
+ }
200
+ if (this._resizeObserver) {
201
+ this._resizeObserver.disconnect();
202
+ this._resizeObserver = null;
203
+ }
204
+ if (this._mutationObserver) {
205
+ this._mutationObserver.disconnect();
206
+ this._mutationObserver = null;
207
+ }
208
+ if (typeof window !== "undefined") {
209
+ window.removeEventListener("resize", this._invalidateRectCache);
210
+ window.removeEventListener("scroll", this._invalidateRectCache, true);
211
+ }
212
+ this._rectCache.clear();
213
+ if (this._observer) {
214
+ this._scene.onPointerObservable.remove(this._observer);
215
+ this._observer = null;
216
+ }
217
+ }
218
+ }
219
+ function _PointerTypeToDomEvent(type) {
220
+ switch (type) {
221
+ case PointerEventTypes.POINTERDOWN:
222
+ return "pointerdown";
223
+ case PointerEventTypes.POINTERUP:
224
+ return "pointerup";
225
+ case PointerEventTypes.POINTERMOVE:
226
+ return "pointermove";
227
+ default:
228
+ return null;
229
+ }
230
+ }
231
+ function _PointerToMouseEvent(domName) {
232
+ switch (domName) {
233
+ case "pointerdown":
234
+ return "mousedown";
235
+ case "pointerup":
236
+ return "mouseup";
237
+ case "pointermove":
238
+ return "mousemove";
239
+ default:
240
+ return null;
241
+ }
242
+ }
243
+ // Synthetic event types we dispatch into the hosted element; they are contained at the host so they do
244
+ // not bubble back into the engine's rendering canvas (see the constructor for the rationale).
245
+ const _ContainedEventTypes = ["pointerdown", "pointerup", "pointermove", "mousedown", "mouseup", "mousemove", "click"];
246
+ //# sourceMappingURL=htmlRaycastInteractionManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"htmlRaycastInteractionManager.js","sourceRoot":"","sources":["../../../../../../dev/core/src/Materials/Textures/HTML/htmlRaycastInteractionManager.ts"],"names":[],"mappings":"AAIA,OAAO,EAAoB,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAGpF,kFAAkF;AAClF,sFAAsF;AACtF,yFAAyF;AAEzF;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,qBAAqB,CAAC,CAAS,EAAE,CAAS,EAAE,KAAa,EAAE,MAAc,EAAE,UAAmB,IAAI;IAC9G,OAAO,EAAE,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC;AAC/D,CAAC;AAcD;;;;;;;;GAQG;AACH,MAAM,OAAO,6BAA6B;IAetC;;;;;;OAMG;IACH,YAAY,KAAY,EAAE,OAAoB,EAAE,IAAkB,EAAE,UAAiD,EAAE;QAbtG,eAAU,GAAG,IAAI,GAAG,EAAwB,CAAC;QACtD,oBAAe,GAAG,KAAK,CAAC;QAExB,oBAAe,GAA6B,IAAI,CAAC;QACjD,sBAAiB,GAA+B,IAAI,CAAC;QAUzD,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,OAAO,CAAC;QACzD,IAAI,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AC","sourcesContent":["import { type Scene } from \"../../../scene.pure\";\nimport { type AbstractMesh } from \"../../../Meshes/abstractMesh.pure\";\nimport { type HtmlTexture } from \"./htmlTexture\";\nimport { type Observer } from \"../../../Misc/observable.pure\";\nimport { type PointerInfo, PointerEventTypes } from \"../../../Events/pointerEvents\";\nimport { type Nullable } from \"../../../types\";\n\n// Attribution: the raycast/UV event-routing technique used here is prior art from\n// `three-html-render` (Palash Bansal, MIT) and Jake Archibald's `curved-markup` demo.\n// The implementation below is a clean-room rewrite against Babylon.js' own picking APIs.\n\n/**\n * Maps a UV coordinate sampled from an {@link HtmlTexture} to a pixel position inside the source element.\n *\n * When `invertY` is true (the default, matching {@link HtmlTexture}'s default upload orientation) the V\n * axis is flipped so that `v = 1` maps to the top of the element, matching the top-left origin used by\n * DOM layout.\n * @param u horizontal texture coordinate, normally in [0, 1]\n * @param v vertical texture coordinate, normally in [0, 1]\n * @param width element width in pixels\n * @param height element height in pixels\n * @param invertY whether the texture content is stored Y-inverted (default true)\n * @returns the pixel position `{ x, y }` within the element\n */\nexport function GetElementPixelFromUv(u: number, v: number, width: number, height: number, invertY: boolean = true): { x: number; y: number } {\n return { x: u * width, y: (invertY ? 1 - v : v) * height };\n}\n\n/**\n * Options for {@link HtmlRaycastInteractionManager}.\n */\nexport interface IHtmlRaycastInteractionManagerOptions {\n /** The DOM element that receives the forwarded pointer events (defaults to the texture's element). */\n targetElement?: HTMLElement;\n /** Whether hits on back-facing geometry are ignored (default true). */\n backFaceCulling?: boolean;\n /** Whether the texture content is stored Y-inverted, used when mapping UVs to element pixels (default true). */\n invertY?: boolean;\n}\n\n/**\n * Routes Babylon pointer events to a live HTML element rendered through an {@link HtmlTexture}.\n *\n * On every pointer event the scene is picked; when the configured mesh is hit, the picked UV is mapped to\n * a pixel inside the source element and an equivalent DOM pointer (and mouse) event is dispatched there.\n * This works for arbitrary meshes (planes, boxes, curved surfaces) because it relies on UV coordinates\n * rather than a flat CSS overlay. For a perspective-correct overlay on planar surfaces, see\n * {@link HtmlInteractionManager}.\n */\nexport class HtmlRaycastInteractionManager {\n private readonly _scene: Scene;\n private readonly _mesh: AbstractMesh;\n private readonly _element: HTMLElement;\n private readonly _backFaceCulling: boolean;\n private readonly _invertY: boolean;\n private _observer: Nullable<Observer<PointerInfo>>;\n private _downTarget: Nullable<EventTarget>;\n private readonly _containHandler: (evt: Event) => void;\n private readonly _rectCache = new Map<HTMLElement, DOMRect>();\n private _rectCacheValid = false;\n private readonly _invalidateRectCache: () => void;\n private _resizeObserver: Nullable<ResizeObserver> = null;\n private _mutationObserver: Nullable<MutationObserver> = null;\n\n /**\n * Creates a raycast interaction manager.\n * @param scene the scene to listen to for pointer events\n * @param texture the HTML texture whose element should receive the forwarded events\n * @param mesh the mesh displaying the texture; only hits on this mesh are forwarded\n * @param options optional configuration\n */\n constructor(scene: Scene, texture: HtmlTexture, mesh: AbstractMesh, options: IHtmlRaycastInteractionManagerOptions = {}) {\n this._scene = scene;\n this._mesh = mesh;\n this._element = options.targetElement ?? texture.element;\n this._backFaceCulling = options.backFaceCulling ?? true;\n this._invertY = options.invertY ?? true;\n\n // The hosted element is a child of the engine's rendering canvas, where Babylon's own input\n // listeners live. The synthetic events we dispatch bubble, so without this they would reach the\n // canvas and be re-interpreted as real input - moving the camera and re-entering this manager in a\n // feedback loop. Stopping propagation at the host element keeps them inside the HTML subtree (so\n // event delegation still works) while never leaking back to the engine.\n this._containHandler = (evt: Event) => evt.stopPropagation();\n for (const type of _ContainedEventTypes) {\n this._element.addEventListener(type, this._containHandler);\n }\n\n // Resolving the target reads layout (getBoundingClientRect) for the element and its descendants on\n // every pointer event. Cache those rects and only re-read them when layout can actually change - on\n // size changes, DOM mutations, or viewport scroll/resize - so high-frequency pointermove events stay\n // pure arithmetic instead of forcing a synchronous reflow each time.\n this._invalidateRectCache = () => {\n this._rectCacheValid = false;\n };\n if (typeof ResizeObserver !== \"undefined\") {\n this._resizeObserver = new ResizeObserver(this._invalidateRectCache);\n this._resizeObserver.observe(this._element);\n }\n if (typeof MutationObserver !== \"undefined\") {\n this._mutationObserver = new MutationObserver(this._invalidateRectCache);\n this._mutationObserver.observe(this._element, { attributes: true, childList: true, characterData: true, subtree: true });\n }\n if (typeof window !== \"undefined\") {\n window.addEventListener(\"resize\", this._invalidateRectCache);\n window.addEventListener(\"scroll\", this._invalidateRectCache, true);\n }\n\n this._observer = scene.onPointerObservable.add((pointerInfo) => this._onPointer(pointerInfo));\n }\n\n private _onPointer(pointerInfo: PointerInfo): void {\n const domName = _PointerTypeToDomEvent(pointerInfo.type);\n if (!domName) {\n return;\n }\n\n const pickInfo = pointerInfo.pickInfo;\n if (!pickInfo || !pickInfo.hit || pickInfo.pickedMesh !== this._mesh) {\n return;\n }\n\n if (this._backFaceCulling && this._isBackFace(pointerInfo)) {\n return;\n }\n\n const uv = pickInfo.getTextureCoordinates();\n if (!uv) {\n return;\n }\n\n const rect = this._getRect(this._element);\n const { x, y } = GetElementPixelFromUv(uv.x, uv.y, rect.width || this._element.offsetWidth, rect.height || this._element.offsetHeight, this._invertY);\n\n this._dispatch(domName, rect.left + x, rect.top + y, pointerInfo);\n }\n\n private _isBackFace(pointerInfo: PointerInfo): boolean {\n const pickInfo = pointerInfo.pickInfo!;\n const normal = pickInfo.getNormal(true, true);\n if (!normal || !pickInfo.pickedPoint) {\n return false;\n }\n const camera = this._scene.activeCamera;\n if (!camera) {\n return false;\n }\n // The face is back-facing when its world normal points away from the camera-to-surface direction.\n const toSurface = pickInfo.pickedPoint.subtract(camera.globalPosition);\n return normal.dot(toSurface) > 0;\n }\n\n private _dispatch(domName: string, clientX: number, clientY: number, pointerInfo: PointerInfo): void {\n if (typeof PointerEvent === \"undefined\") {\n return;\n }\n\n const sourceEvent = pointerInfo.event as Partial<PointerEvent>;\n // document.elementFromPoint is unreliable here: the element is hosted inside the engine's rendering\n // canvas (whose painted WebGL/WebGPU content covers the DOM children) and is marked `inert`, so the\n // browser's own hit testing skips it. Resolve the deepest descendant under the mapped point manually\n // from the laid-out subtree instead; synthetic dispatch still triggers listeners on inert elements.\n const target = this._resolveTarget(clientX, clientY);\n\n const init: PointerEventInit = {\n bubbles: true,\n cancelable: true,\n clientX,\n clientY,\n button: sourceEvent.button ?? 0,\n buttons: sourceEvent.buttons ?? 0,\n pointerId: sourceEvent.pointerId ?? 1,\n pointerType: sourceEvent.pointerType ?? \"mouse\",\n };\n\n target.dispatchEvent(new PointerEvent(domName, init));\n\n const mouseName = _PointerToMouseEvent(domName);\n if (mouseName) {\n target.dispatchEvent(new MouseEvent(mouseName, init));\n }\n\n // The browser synthesizes a \"click\" from a native down/up pair, but synthetic mouse events do not,\n // so we emit it ourselves: on pointerdown remember the target, then on pointerup dispatch a click\n // when the release lands on the same element (matching native click semantics closely enough).\n // Native browsers only synthesize \"click\" for the primary button, so gate on button 0.\n if (domName === \"pointerdown\") {\n this._downTarget = target;\n } else if (domName === \"pointerup\") {\n if (this._downTarget === target && init.button === 0) {\n target.dispatchEvent(new MouseEvent(\"click\", init));\n }\n this._downTarget = null;\n }\n }\n\n /**\n * Resolves the deepest descendant of the hosted element whose layout box contains the given client point.\n * @param clientX horizontal client coordinate\n * @param clientY vertical client coordinate\n * @returns the deepest matching element, or the hosted element itself when no descendant matches\n */\n private _resolveTarget(clientX: number, clientY: number): HTMLElement {\n let target: HTMLElement = this._element;\n let advanced = true;\n while (advanced) {\n advanced = false;\n const children = target.children;\n for (let i = children.length - 1; i >= 0; i--) {\n const child = children[i] as HTMLElement;\n const rect = this._getRect(child);\n if (clientX >= rect.left && clientX <= rect.right && clientY >= rect.top && clientY <= rect.bottom) {\n target = child;\n advanced = true;\n break;\n }\n }\n }\n return target;\n }\n\n /**\n * Returns the client rect of an element, served from a cache that is rebuilt only when layout may have\n * changed (see the constructor). This keeps pointer routing free of per-event layout reads.\n * @param element the element whose bounding rect is needed\n * @returns the element's cached {@link DOMRect}\n */\n private _getRect(element: HTMLElement): DOMRect {\n if (!this._rectCacheValid) {\n this._rectCache.clear();\n this._rectCacheValid = true;\n }\n let rect = this._rectCache.get(element);\n if (!rect) {\n rect = element.getBoundingClientRect();\n this._rectCache.set(element, rect);\n }\n return rect;\n }\n\n /**\n * Detaches the manager and stops forwarding pointer events.\n */\n public dispose(): void {\n for (const type of _ContainedEventTypes) {\n this._element.removeEventListener(type, this._containHandler);\n }\n if (this._resizeObserver) {\n this._resizeObserver.disconnect();\n this._resizeObserver = null;\n }\n if (this._mutationObserver) {\n this._mutationObserver.disconnect();\n this._mutationObserver = null;\n }\n if (typeof window !== \"undefined\") {\n window.removeEventListener(\"resize\", this._invalidateRectCache);\n window.removeEventListener(\"scroll\", this._invalidateRectCache, true);\n }\n this._rectCache.clear();\n if (this._observer) {\n this._scene.onPointerObservable.remove(this._observer);\n this._observer = null;\n }\n }\n}\n\nfunction _PointerTypeToDomEvent(type: number): Nullable<string> {\n switch (type) {\n case PointerEventTypes.POINTERDOWN:\n return \"pointerdown\";\n case PointerEventTypes.POINTERUP:\n return \"pointerup\";\n case PointerEventTypes.POINTERMOVE:\n return \"pointermove\";\n default:\n return null;\n }\n}\n\nfunction _PointerToMouseEvent(domName: string): Nullable<string> {\n switch (domName) {\n case \"pointerdown\":\n return \"mousedown\";\n case \"pointerup\":\n return \"mouseup\";\n case \"pointermove\":\n return \"mousemove\";\n default:\n return null;\n }\n}\n\n// Synthetic event types we dispatch into the hosted element; they are contained at the host so they do\n// not bubble back into the engine's rendering canvas (see the constructor for the rationale).\nconst _ContainedEventTypes = [\"pointerdown\", \"pointerup\", \"pointermove\", \"mousedown\", \"mouseup\", \"mousemove\", \"click\"] as const;\n"]}