laymur 0.26.0 → 0.27.1

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 (140) hide show
  1. package/README.md +80 -36
  2. package/dist/core/constraints/UICustomConstraint.d.ts +1 -1
  3. package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +1 -1
  4. package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +1 -1
  5. package/dist/core/constraints/builders/UIConstraint2DBuilder.d.ts +2 -3
  6. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.d.ts +2 -2
  7. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.js +24 -12
  8. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +10 -0
  9. package/dist/core/elements/UIDummy/UIDummy.d.ts +42 -5
  10. package/dist/core/elements/UIDummy/UIDummy.js +119 -25
  11. package/dist/core/elements/UIElement/UIElement.Internal.d.ts +1 -1
  12. package/dist/core/elements/UIElement/UIElement.d.ts +14 -0
  13. package/dist/core/elements/UIElement/UIElement.js +34 -3
  14. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +1 -1
  15. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +1 -1
  16. package/dist/core/elements/UIGraphics/UIGraphics.js +2 -2
  17. package/dist/core/elements/UIImage/UIImage.d.ts +2 -2
  18. package/dist/core/elements/UIImage/UIImage.js +10 -8
  19. package/dist/core/elements/UINineSlice/UINineSlice.d.ts +4 -4
  20. package/dist/core/elements/UINineSlice/UINineSlice.js +11 -9
  21. package/dist/core/elements/UIProgress/UIProgress.d.ts +2 -2
  22. package/dist/core/elements/UIProgress/UIProgress.js +15 -10
  23. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +1 -1
  24. package/dist/core/elements/UIScene/UIScene.d.ts +3 -2
  25. package/dist/core/elements/UIScene/UIScene.js +18 -12
  26. package/dist/core/elements/UIText/UIText.Measuring.d.ts +1 -1
  27. package/dist/core/elements/UIText/UIText.d.ts +3 -3
  28. package/dist/core/elements/UIText/UIText.js +6 -6
  29. package/dist/core/index.d.ts +44 -41
  30. package/dist/core/index.js +12 -9
  31. package/dist/core/layers/UILayer/UILayer.d.ts +40 -4
  32. package/dist/core/layers/UILayer/UILayer.js +70 -9
  33. package/dist/core/layers/UIViewportLayer/UIViewportLayer.Internal.d.ts +27 -0
  34. package/dist/core/layers/{UIFullscreenLayer/UIFullscreenLayer.Internal.js → UIViewportLayer/UIViewportLayer.Internal.js} +1 -1
  35. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +98 -0
  36. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +251 -0
  37. package/dist/core/miscellaneous/asserts.d.ts +14 -0
  38. package/dist/core/miscellaneous/asserts.js +28 -0
  39. package/dist/core/miscellaneous/canvas.js +10 -2
  40. package/dist/core/miscellaneous/color/UIColor.js +4 -2
  41. package/dist/core/miscellaneous/computeTransform.d.ts +12 -0
  42. package/dist/core/miscellaneous/computeTransform.js +20 -0
  43. package/dist/core/miscellaneous/context-monitor/UIContextMonitor.d.ts +1 -0
  44. package/dist/core/miscellaneous/context-monitor/UIContextMonitor.js +60 -0
  45. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.js +2 -1
  46. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.js +1 -1
  47. package/dist/core/miscellaneous/mask/UIMaskMaterialPatch.js +2 -1
  48. package/dist/core/miscellaneous/resize-policy/UIResizePolicy.d.ts +7 -4
  49. package/dist/core/miscellaneous/resize-policy/UIResizePolicy.js +13 -6
  50. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCover.d.ts +1 -1
  51. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCover.js +3 -3
  52. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCross.js +2 -2
  53. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCrossInverted.js +2 -2
  54. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFit.d.ts +1 -1
  55. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFit.js +3 -3
  56. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedHeight.js +2 -2
  57. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedWidth.js +2 -2
  58. package/dist/core/miscellaneous/text-style/UITextStyle.js +2 -2
  59. package/dist/core/miscellaneous/texture/UITextureView.d.ts +4 -1
  60. package/dist/core/miscellaneous/texture/UITextureView.js +10 -7
  61. package/dist/core/miscellaneous/viewport/UIElementViewport.d.ts +31 -0
  62. package/dist/core/miscellaneous/viewport/UIElementViewport.js +54 -0
  63. package/dist/core/miscellaneous/viewport/UIViewport.d.ts +37 -0
  64. package/dist/core/miscellaneous/viewport/UIViewport.js +31 -0
  65. package/dist/core/miscellaneous/viewport/UIWindowViewport.d.ts +13 -0
  66. package/dist/core/miscellaneous/viewport/UIWindowViewport.js +25 -0
  67. package/dist/core/miscellaneous/webglCapabilities.js +61 -55
  68. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +36 -5
  69. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +23 -2
  70. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +153 -15
  71. package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.Internal.d.ts +1 -1
  72. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +2 -0
  73. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +3 -0
  74. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +36 -16
  75. package/dist/debug/UIAspectDebug.js +8 -7
  76. package/dist/debug/UIHeightDebug.js +8 -4
  77. package/dist/debug/UIHorizontalDistanceDebug.js +9 -7
  78. package/dist/debug/UIHorizontalInterpolationDebug.js +16 -10
  79. package/dist/debug/UILayerDebug.d.ts +7 -0
  80. package/dist/debug/UILayerDebug.js +21 -0
  81. package/dist/debug/UIVerticalDistanceDebug.js +9 -7
  82. package/dist/debug/UIVerticalInterpolationDebug.js +16 -10
  83. package/dist/debug/UIWidthDebug.js +8 -4
  84. package/dist/debug/index.d.ts +1 -1
  85. package/dist/debug/miscellaneous.d.ts +7 -0
  86. package/dist/debug/miscellaneous.js +11 -1
  87. package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.d.ts +1 -1
  88. package/dist/particles/behaviorModules/UIBehaviorModule.d.ts +5 -1
  89. package/dist/particles/behaviorModules/UIBehaviorPointGravity.d.ts +2 -2
  90. package/dist/particles/behaviorModules/UIBehaviorPointGravity.js +13 -10
  91. package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.d.ts +1 -1
  92. package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.js +6 -4
  93. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.d.ts +1 -1
  94. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.js +4 -3
  95. package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.d.ts +2 -2
  96. package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.js +8 -8
  97. package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.d.ts +4 -3
  98. package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.js +8 -5
  99. package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.d.ts +1 -1
  100. package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.js +8 -8
  101. package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.d.ts +7 -5
  102. package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.js +12 -8
  103. package/dist/particles/elements/UIEmitter.Internal.d.ts +15 -2
  104. package/dist/particles/elements/UIEmitter.Internal.js +6 -0
  105. package/dist/particles/elements/UIEmitter.d.ts +19 -3
  106. package/dist/particles/elements/UIEmitter.js +100 -54
  107. package/dist/particles/index.d.ts +3 -2
  108. package/dist/particles/index.js +1 -0
  109. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.d.ts +8 -2
  110. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.js +16 -11
  111. package/dist/particles/instancedParticle/UIInstancedParticle.d.ts +7 -0
  112. package/dist/particles/instancedParticle/UIInstancedParticle.js +37 -2
  113. package/dist/particles/miscellaneous/miscellaneous.d.ts +34 -0
  114. package/dist/particles/miscellaneous/miscellaneous.js +72 -0
  115. package/dist/particles/renderingModules/UIRenderingColorOverLife.js +2 -2
  116. package/dist/particles/renderingModules/UIRenderingColorOverVelocity.js +2 -3
  117. package/dist/particles/renderingModules/UIRenderingModule.d.ts +3 -2
  118. package/dist/particles/renderingModules/UIRenderingModule.js +2 -1
  119. package/dist/particles/renderingModules/UIRenderingTexture.js +2 -1
  120. package/dist/particles/shaders/UIRenderingColorOverLife.d.ts +1 -2
  121. package/dist/particles/shaders/UIRenderingColorOverLife.js +5 -3
  122. package/dist/particles/shaders/UIRenderingColorOverVelocity.d.ts +1 -9
  123. package/dist/particles/shaders/UIRenderingColorOverVelocity.js +6 -13
  124. package/dist/particles/spawnModules/UISpawnModule.d.ts +5 -2
  125. package/dist/particles/spawnModules/UISpawnRandomLifetime.d.ts +2 -2
  126. package/dist/particles/spawnModules/UISpawnRandomLifetime.js +5 -5
  127. package/dist/particles/spawnModules/UISpawnRandomRotation.d.ts +1 -1
  128. package/dist/particles/spawnModules/UISpawnRandomRotation.js +5 -3
  129. package/dist/particles/spawnModules/UISpawnRandomScale.d.ts +1 -1
  130. package/dist/particles/spawnModules/UISpawnRandomScale.js +4 -3
  131. package/dist/particles/spawnModules/UISpawnRandomTorque.d.ts +1 -1
  132. package/dist/particles/spawnModules/UISpawnRandomTorque.js +5 -3
  133. package/dist/particles/spawnModules/UISpawnRandomVelocity.d.ts +1 -1
  134. package/dist/particles/spawnModules/UISpawnRandomVelocity.js +6 -4
  135. package/dist/particles/spawnModules/UISpawnRectangle.d.ts +1 -1
  136. package/dist/particles/spawnModules/UISpawnRectangle.js +6 -4
  137. package/package.json +34 -22
  138. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.Internal.d.ts +0 -21
  139. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.d.ts +0 -63
  140. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.js +0 -144
@@ -0,0 +1,98 @@
1
+ import type { FerrsignView1 } from "ferrsign";
2
+ import type { Camera, Vector2, WebGLRenderer } from "three";
3
+ import { Ray, Vector3 } from "three";
4
+ import type { UIResizePolicy } from "../../miscellaneous/resize-policy/UIResizePolicy.js";
5
+ import type { UIViewport } from "../../miscellaneous/viewport/UIViewport.js";
6
+ import { UILayer } from "../UILayer/UILayer.js";
7
+ import type { UIViewportLayerInputEventData, UIViewportLayerOptions } from "./UIViewportLayer.Internal.js";
8
+ /**
9
+ * Layer covering a part of the page given by its viewport, the whole window by default. Follows
10
+ * the viewport size through the resize policy and takes pointer input from the window, relative
11
+ * to the viewport.
12
+ */
13
+ export declare class UIViewportLayer extends UILayer {
14
+ private readonly viewportInternal;
15
+ private readonly ownsViewport;
16
+ private resizePolicyInternal;
17
+ /** Policy version the layer was last sized by; -1 forces a resize */
18
+ private appliedPolicyVersion;
19
+ /** Pointers the layer cancelled while pressed: their late release is not reported */
20
+ private readonly abandonedPointers;
21
+ private readonly signalPointerPressedInternal;
22
+ private readonly signalPointerMovedInternal;
23
+ private readonly signalPointerReleasedInternal;
24
+ private readonly signalPointerCancelledInternal;
25
+ constructor(options?: Partial<UIViewportLayerOptions>);
26
+ /** Part of the page the layer covers */
27
+ get viewport(): UIViewport;
28
+ /** Current resize policy */
29
+ get resizePolicy(): UIResizePolicy;
30
+ /** Signal fired when pointer is pressed */
31
+ get signalPointerPressed(): FerrsignView1<UIViewportLayerInputEventData>;
32
+ /** Signal fired when pointer is moved */
33
+ get signalPointerMoved(): FerrsignView1<UIViewportLayerInputEventData>;
34
+ /** Signal fired when pointer is released */
35
+ get signalPointerReleased(): FerrsignView1<UIViewportLayerInputEventData>;
36
+ /**
37
+ * Signal fired when a pressed pointer is cancelled and no release follows: by the browser (a
38
+ * system gesture, scrolling, a popup), or by the layer when the page is hidden or the window
39
+ * loses focus, since the browser may then never report the release. The position is the last
40
+ * known one and may be meaningless.
41
+ */
42
+ get signalPointerCancelled(): FerrsignView1<UIViewportLayerInputEventData>;
43
+ /** Updates resize policy */
44
+ set resizePolicy(value: UIResizePolicy);
45
+ /**
46
+ * Removes event listeners and destroys the layer with its elements and constraints. The
47
+ * viewport is destroyed too, unless it was passed in the options.
48
+ */
49
+ destroy(): void;
50
+ /**
51
+ * Renders layer and all visible elements
52
+ * @param renderer Three.js WebGL renderer
53
+ * @param deltaTime Time since last frame in seconds
54
+ */
55
+ render(renderer: WebGLRenderer, deltaTime: number): void;
56
+ /**
57
+ * Projects 3D world position to 2D layer coordinates
58
+ * @param position World position
59
+ * @param camera Three.js camera
60
+ * @param result Reusable vector instance
61
+ * @returns Layer coordinates
62
+ */
63
+ projectWorldPosition(position: Vector3, camera: Camera, result?: Vector3): Vector3;
64
+ /**
65
+ * Projects 2D layer coordinates to 3D world position
66
+ * @param position Layer coordinates
67
+ * @param camera Three.js camera
68
+ * @param z Depth coordinate
69
+ * @param result Reusable vector instance
70
+ * @returns World position
71
+ */
72
+ projectLayerPosition(position: Vector2, camera: Camera, z?: number, result?: Vector3): Vector3;
73
+ /**
74
+ * Builds ray from layer position through camera
75
+ * @param position Layer coordinates
76
+ * @param camera Three.js camera
77
+ * @param result Reusable ray instance
78
+ * @returns Ray from near to far plane
79
+ */
80
+ buildRay(position: Vector2, camera: Camera, result?: Ray): Ray;
81
+ protected reportPointerCancelledByLayer(x: number, y: number, identifier: number): void;
82
+ private readonly onResize;
83
+ /**
84
+ * The release of a pointer pressed when the page is hidden or the window loses focus is often
85
+ * never reported, leaving it captured: such pointers are cancelled right away
86
+ */
87
+ private readonly onFocusLost;
88
+ private readonly onVisibilityChange;
89
+ private readonly onPointerDown;
90
+ private readonly onPointerMove;
91
+ private readonly onPointerUp;
92
+ private readonly onPointerCancel;
93
+ /**
94
+ * Fires the layer signal of a window pointer event, if the layer takes input
95
+ * @returns Position in layer coordinates, to pass on to the elements; null if ignored
96
+ */
97
+ private reportPointer;
98
+ }
@@ -0,0 +1,251 @@
1
+ import { Ferrsign1 } from "ferrsign";
2
+ import { MathUtils, Ray, Vector3 } from "three";
3
+ import { assertValidNumber } from "../../miscellaneous/asserts.js";
4
+ import { isUIModeInteractive } from "../../miscellaneous/UIMode.js";
5
+ import { UIWindowViewport } from "../../miscellaneous/viewport/UIWindowViewport.js";
6
+ import { UILayer } from "../UILayer/UILayer.js";
7
+ import { VIEWPORT_LAYER_DEFAULT_RESIZE_POLICY } from "./UIViewportLayer.Internal.js";
8
+ const TEMP_RAY_NEAR = new Vector3();
9
+ const TEMP_RAY_FAR = new Vector3();
10
+ /**
11
+ * Layer covering a part of the page given by its viewport, the whole window by default. Follows
12
+ * the viewport size through the resize policy and takes pointer input from the window, relative
13
+ * to the viewport.
14
+ */
15
+ export class UIViewportLayer extends UILayer {
16
+ constructor(options) {
17
+ const resizePolicy = options?.resizePolicy ?? VIEWPORT_LAYER_DEFAULT_RESIZE_POLICY;
18
+ const viewport = options?.viewport ?? new UIWindowViewport();
19
+ if (viewport.destroyed) {
20
+ throw new Error("UIViewportLayer.constructor.options.viewport: viewport is destroyed");
21
+ }
22
+ // A viewport not laid out yet has no size: the layer starts with the default one and
23
+ // resizes once the viewport gets its size
24
+ const hasSize = viewport.width > 0 && viewport.height > 0;
25
+ const scale = hasSize ? resizePolicy.calculateScale(viewport.width, viewport.height) : 1;
26
+ super({
27
+ ...(hasSize ? { width: viewport.width * scale, height: viewport.height * scale } : {}),
28
+ ...options,
29
+ });
30
+ /** Pointers the layer cancelled while pressed: their late release is not reported */
31
+ this.abandonedPointers = new Set();
32
+ this.signalPointerPressedInternal = new Ferrsign1();
33
+ this.signalPointerMovedInternal = new Ferrsign1();
34
+ this.signalPointerReleasedInternal = new Ferrsign1();
35
+ this.signalPointerCancelledInternal = new Ferrsign1();
36
+ this.onResize = () => {
37
+ const { width, height } = this.viewportInternal;
38
+ if (width <= 0 || height <= 0) {
39
+ // Hidden or not laid out: keep the last size until the viewport gets one. Input is
40
+ // ignored meanwhile, so a press held now would never end
41
+ this.cancelPointers();
42
+ return;
43
+ }
44
+ const scale = this.resizePolicy.calculateScale(width, height);
45
+ this.appliedPolicyVersion = this.resizePolicy.version;
46
+ this.resizeInternal(width * scale, height * scale);
47
+ };
48
+ /**
49
+ * The release of a pointer pressed when the page is hidden or the window loses focus is often
50
+ * never reported, leaving it captured: such pointers are cancelled right away
51
+ */
52
+ this.onFocusLost = () => {
53
+ this.cancelPointers();
54
+ };
55
+ this.onVisibilityChange = () => {
56
+ if (document.visibilityState === "hidden") {
57
+ this.onFocusLost();
58
+ }
59
+ };
60
+ this.onPointerDown = (event) => {
61
+ this.abandonedPointers.delete(event.pointerId);
62
+ const position = this.reportPointer(event, this.signalPointerPressedInternal);
63
+ if (position !== null) {
64
+ this.inputWrapperInternal.onPointerDown(position.x, position.y, event.pointerId);
65
+ }
66
+ };
67
+ this.onPointerMove = (event) => {
68
+ // A cancelled press stays cancelled until it ends: its moves would bring it back over the
69
+ // elements, while its release is not reported. A move with no button down means the press
70
+ // ended unseen, as when released outside the window
71
+ if (this.abandonedPointers.has(event.pointerId)) {
72
+ if (event.buttons !== 0) {
73
+ return;
74
+ }
75
+ this.abandonedPointers.delete(event.pointerId);
76
+ }
77
+ const position = this.reportPointer(event, this.signalPointerMovedInternal);
78
+ if (position !== null) {
79
+ this.inputWrapperInternal.onPointerMove(position.x, position.y, event.pointerId);
80
+ }
81
+ };
82
+ this.onPointerUp = (event) => {
83
+ if (this.abandonedPointers.delete(event.pointerId)) {
84
+ return;
85
+ }
86
+ const position = this.reportPointer(event, this.signalPointerReleasedInternal);
87
+ if (position !== null) {
88
+ this.inputWrapperInternal.onPointerUp(position.x, position.y, event.pointerId);
89
+ }
90
+ };
91
+ this.onPointerCancel = (event) => {
92
+ if (this.abandonedPointers.delete(event.pointerId)) {
93
+ return;
94
+ }
95
+ if (this.reportPointer(event, this.signalPointerCancelledInternal) !== null) {
96
+ this.inputWrapperInternal.onPointerCancel(event.pointerId);
97
+ }
98
+ };
99
+ this.viewportInternal = viewport;
100
+ this.ownsViewport = options?.viewport === undefined;
101
+ this.resizePolicyInternal = resizePolicy;
102
+ this.appliedPolicyVersion = hasSize ? resizePolicy.version : -1;
103
+ viewport.signalResized.on(this.onResize);
104
+ window.addEventListener("pointerdown", this.onPointerDown);
105
+ window.addEventListener("pointermove", this.onPointerMove);
106
+ window.addEventListener("pointerup", this.onPointerUp);
107
+ window.addEventListener("pointercancel", this.onPointerCancel);
108
+ window.addEventListener("blur", this.onFocusLost);
109
+ document.addEventListener("visibilitychange", this.onVisibilityChange);
110
+ }
111
+ /** Part of the page the layer covers */
112
+ get viewport() {
113
+ return this.viewportInternal;
114
+ }
115
+ /** Current resize policy */
116
+ get resizePolicy() {
117
+ return this.resizePolicyInternal;
118
+ }
119
+ /** Signal fired when pointer is pressed */
120
+ get signalPointerPressed() {
121
+ return this.signalPointerPressedInternal;
122
+ }
123
+ /** Signal fired when pointer is moved */
124
+ get signalPointerMoved() {
125
+ return this.signalPointerMovedInternal;
126
+ }
127
+ /** Signal fired when pointer is released */
128
+ get signalPointerReleased() {
129
+ return this.signalPointerReleasedInternal;
130
+ }
131
+ /**
132
+ * Signal fired when a pressed pointer is cancelled and no release follows: by the browser (a
133
+ * system gesture, scrolling, a popup), or by the layer when the page is hidden or the window
134
+ * loses focus, since the browser may then never report the release. The position is the last
135
+ * known one and may be meaningless.
136
+ */
137
+ get signalPointerCancelled() {
138
+ return this.signalPointerCancelledInternal;
139
+ }
140
+ /** Updates resize policy */
141
+ set resizePolicy(value) {
142
+ if (this.resizePolicyInternal !== value) {
143
+ this.resizePolicyInternal = value;
144
+ this.appliedPolicyVersion = -1;
145
+ }
146
+ }
147
+ /**
148
+ * Removes event listeners and destroys the layer with its elements and constraints. The
149
+ * viewport is destroyed too, unless it was passed in the options.
150
+ */
151
+ destroy() {
152
+ if (this.destroyed) {
153
+ return;
154
+ }
155
+ this.viewportInternal.signalResized.off(this.onResize);
156
+ if (this.ownsViewport) {
157
+ this.viewportInternal.destroy();
158
+ }
159
+ window.removeEventListener("pointerdown", this.onPointerDown);
160
+ window.removeEventListener("pointermove", this.onPointerMove);
161
+ window.removeEventListener("pointerup", this.onPointerUp);
162
+ window.removeEventListener("pointercancel", this.onPointerCancel);
163
+ window.removeEventListener("blur", this.onFocusLost);
164
+ document.removeEventListener("visibilitychange", this.onVisibilityChange);
165
+ this.abandonedPointers.clear();
166
+ super.destroy();
167
+ }
168
+ /**
169
+ * Renders layer and all visible elements
170
+ * @param renderer Three.js WebGL renderer
171
+ * @param deltaTime Time since last frame in seconds
172
+ */
173
+ render(renderer, deltaTime) {
174
+ assertValidNumber(deltaTime, "UIViewportLayer.render.deltaTime");
175
+ if (this.destroyed) {
176
+ return;
177
+ }
178
+ if (this.resizePolicy.version !== this.appliedPolicyVersion) {
179
+ this.onResize();
180
+ }
181
+ super.renderInternal(renderer, deltaTime);
182
+ }
183
+ /**
184
+ * Projects 3D world position to 2D layer coordinates
185
+ * @param position World position
186
+ * @param camera Three.js camera
187
+ * @param result Reusable vector instance
188
+ * @returns Layer coordinates
189
+ */
190
+ projectWorldPosition(position, camera, result = new Vector3()) {
191
+ result.copy(position).project(camera);
192
+ result.x = MathUtils.mapLinear(result.x, -1, 1, this.x, this.oppositeX);
193
+ result.y = MathUtils.mapLinear(result.y, -1, 1, this.y, this.oppositeY);
194
+ return result;
195
+ }
196
+ /**
197
+ * Projects 2D layer coordinates to 3D world position
198
+ * @param position Layer coordinates
199
+ * @param camera Three.js camera
200
+ * @param z Depth coordinate
201
+ * @param result Reusable vector instance
202
+ * @returns World position
203
+ */
204
+ projectLayerPosition(position, camera, z = -1, result = new Vector3()) {
205
+ return result
206
+ .set(MathUtils.mapLinear(position.x, this.x, this.oppositeX, -1, 1), MathUtils.mapLinear(position.y, this.y, this.oppositeY, -1, 1), z)
207
+ .unproject(camera);
208
+ }
209
+ /**
210
+ * Builds ray from layer position through camera
211
+ * @param position Layer coordinates
212
+ * @param camera Three.js camera
213
+ * @param result Reusable ray instance
214
+ * @returns Ray from near to far plane
215
+ */
216
+ buildRay(position, camera, result = new Ray()) {
217
+ const near = this.projectLayerPosition(position, camera, -1, TEMP_RAY_NEAR);
218
+ const far = this.projectLayerPosition(position, camera, 1, TEMP_RAY_FAR);
219
+ return result.set(near, far.sub(near).normalize());
220
+ }
221
+ reportPointerCancelledByLayer(x, y, identifier) {
222
+ this.abandonedPointers.add(identifier);
223
+ this.signalPointerCancelledInternal.emit({ x, y, identifier, layer: this });
224
+ }
225
+ /**
226
+ * Fires the layer signal of a window pointer event, if the layer takes input
227
+ * @returns Position in layer coordinates, to pass on to the elements; null if ignored
228
+ */
229
+ reportPointer(event, signal) {
230
+ // Relative to the viewport, not to event.target: an HTML element over the canvas (a store
231
+ // button, an ad network overlay) would shift the position by its own offset
232
+ const viewport = this.viewportInternal;
233
+ if (viewport.width <= 0 || viewport.height <= 0 || !isUIModeInteractive(this.mode)) {
234
+ return null;
235
+ }
236
+ // A new object: a signal handler may cause another pointer event before it is passed on
237
+ const position = viewport.toLocal(event.clientX, event.clientY, { x: 0, y: 0 });
238
+ // Reading the box may have resized the viewport
239
+ if (viewport.width <= 0 || viewport.height <= 0) {
240
+ return null;
241
+ }
242
+ position.x *= this.width / viewport.width;
243
+ position.y *= this.height / viewport.height;
244
+ signal.emit({ x: position.x, y: position.y, identifier: event.pointerId, layer: this });
245
+ // A handler may have turned input off or destroyed the layer
246
+ if (this.destroyed || !isUIModeInteractive(this.mode)) {
247
+ return null;
248
+ }
249
+ return position;
250
+ }
251
+ }
@@ -79,3 +79,17 @@ export declare function assertValidConstraintElement(element: UILayerElement, su
79
79
  * @throws If the layer is destroyed or a variable does not exist
80
80
  */
81
81
  export declare function assertValidConstraintExpressions(layer: UILayer, expressions: UIExpression[], subject: string): UILayer;
82
+ /**
83
+ * Validates value is within [0, 1].
84
+ * @param value Number to validate
85
+ * @param subject Value name for error messages
86
+ * @throws If value is invalid or outside [0, 1]
87
+ */
88
+ export declare function assertValidUnitNumber(value: number, subject: string): void;
89
+ /**
90
+ * Validates value is a non-negative integer.
91
+ * @param value Number to validate
92
+ * @param subject Value name for error messages
93
+ * @throws If value is invalid, negative or fractional
94
+ */
95
+ export declare function assertValidNonNegativeInteger(value: number, subject: string): void;
@@ -245,3 +245,31 @@ function assertNotDestroyed(value, name, subject) {
245
245
  throw new Error(`${subject}: ${name} is destroyed`);
246
246
  }
247
247
  }
248
+ /**
249
+ * Validates value is within [0, 1].
250
+ * @param value Number to validate
251
+ * @param subject Value name for error messages
252
+ * @throws If value is invalid or outside [0, 1]
253
+ */
254
+ export function assertValidUnitNumber(value, subject) {
255
+ if (process.env.NODE_ENV !== "production") {
256
+ assertValidNumber(value, subject);
257
+ if (value < 0 || value > 1) {
258
+ throw new Error(`${subject}: value must be between 0 and 1, got ${value}`);
259
+ }
260
+ }
261
+ }
262
+ /**
263
+ * Validates value is a non-negative integer.
264
+ * @param value Number to validate
265
+ * @param subject Value name for error messages
266
+ * @throws If value is invalid, negative or fractional
267
+ */
268
+ export function assertValidNonNegativeInteger(value, subject) {
269
+ if (process.env.NODE_ENV !== "production") {
270
+ assertValidNonNegativeNumber(value, subject);
271
+ if (!Number.isInteger(value)) {
272
+ throw new Error(`${subject}: value must be an integer, got ${value}`);
273
+ }
274
+ }
275
+ }
@@ -1,4 +1,4 @@
1
- import { CanvasTexture, ClampToEdgeWrapping, LinearFilter, RGBAFormat, UnsignedByteType, UVMapping, SRGBColorSpace, NoColorSpace, } from "three";
1
+ import { CanvasTexture, ClampToEdgeWrapping, LinearFilter, NoColorSpace, RGBAFormat, SRGBColorSpace, UnsignedByteType, UVMapping, } from "three";
2
2
  import { checkOffscreenCanvasSupport, checkSRGBSupport } from "./webglCapabilities.js";
3
3
  /**
4
4
  * Creates a canvas using OffscreenCanvas if available, otherwise falls back to HTMLCanvasElement.
@@ -23,7 +23,12 @@ export function createCanvas(width, height) {
23
23
  * @returns 2D rendering context or null
24
24
  */
25
25
  export function getCanvas2DContext(canvas) {
26
- return canvas.getContext("2d");
26
+ // Each branch calls getContext on a concrete type: on the union, TypeScript picks the overload
27
+ // depending on the order it checks types in, and may return the generic RenderingContext
28
+ return isOffscreenCanvas(canvas) ? canvas.getContext("2d") : canvas.getContext("2d");
29
+ }
30
+ function isOffscreenCanvas(canvas) {
31
+ return typeof OffscreenCanvas !== "undefined" && canvas instanceof OffscreenCanvas;
27
32
  }
28
33
  /**
29
34
  * Creates a CanvasTexture with maximum WebGL1/WebGL2 compatibility.
@@ -39,5 +44,8 @@ export function createCanvasTexture(canvas) {
39
44
  const texture = new CanvasTexture(canvas, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearFilter, RGBAFormat, UnsignedByteType, 1);
40
45
  texture.colorSpace = checkSRGBSupport() ? SRGBColorSpace : NoColorSpace;
41
46
  texture.generateMipmaps = false;
47
+ // The typings of three 0.181 and later make CanvasTexture generic over its image, with
48
+ // HTMLCanvasElement as the default; earlier ones have no type parameter to name UICanvas
49
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion -- needed from three 0.181 on
42
50
  return texture;
43
51
  }
@@ -341,10 +341,12 @@ export class UIColor {
341
341
  /** Returns RGBA as 32-bit hex (RRGGBBAA) */
342
342
  getHexRGBA() {
343
343
  this.ensureRGBUpdatedFromHSL();
344
- return ((Math.round(this.rInternal * 255) << 24) |
344
+ // Unsigned: a red of 128 or more sets the sign bit
345
+ return (((Math.round(this.rInternal * 255) << 24) |
345
346
  (Math.round(this.gInternal * 255) << 16) |
346
347
  (Math.round(this.bInternal * 255) << 8) |
347
- Math.round(this.aInternal * 255));
348
+ Math.round(this.aInternal * 255)) >>>
349
+ 0);
348
350
  }
349
351
  /** Returns RGB as 24-bit hex (RRGGBB) */
350
352
  getHexRGB() {
@@ -1,5 +1,6 @@
1
1
  import { Matrix4 } from "three";
2
2
  import { UIMicroAnchorMode } from "../miscellaneous/micro/UIMicro.Internal.js";
3
+ import type { UITextureTrim } from "./texture/UITextureView.Internal.js";
3
4
  /**
4
5
  * Computes element transform matrix.
5
6
  * @returns Transform matrix (reused, do not store reference)
@@ -16,3 +17,14 @@ export declare function computeTransformMatrix(x: number, y: number, width: numb
16
17
  * @returns Transform matrix (reused, do not store reference)
17
18
  */
18
19
  export declare function computeTrimmedTransformMatrix(x: number, y: number, width: number, height: number, zIndex: number, microX: number, microY: number, microAnchorX: number, microAnchorY: number, microScaleX: number, microScaleY: number, microRotation: number, microAnchorMode: UIMicroAnchorMode, trimLeft: number, trimRight: number, trimTop: number, trimBottom: number): Matrix4;
20
+ /**
21
+ * Trim of a sprite drawn at a size other than its source size: the trim stretches with it.
22
+ * Returns a shared object, valid until the next call.
23
+ * @param trim Trim in source units
24
+ * @param width Drawn width
25
+ * @param height Drawn height
26
+ * @param sourceWidth Untrimmed source width
27
+ * @param sourceHeight Untrimmed source height
28
+ * @returns Trim in drawn units
29
+ */
30
+ export declare function scaleTrim(trim: Readonly<UITextureTrim>, width: number, height: number, sourceWidth: number, sourceHeight: number): Readonly<UITextureTrim>;
@@ -5,6 +5,7 @@ const TEMP_QUATERNION = new Quaternion();
5
5
  const TEMP_SCALE = new Vector3();
6
6
  const TEMP_MATRIX = new Matrix4();
7
7
  const Z_AXIS = new Vector3(0, 0, 1);
8
+ const TEMP_TRIM = { left: 0, right: 0, top: 0, bottom: 0 };
8
9
  /**
9
10
  * Computes element transform matrix.
10
11
  * @returns Transform matrix (reused, do not store reference)
@@ -77,3 +78,22 @@ export function computeTrimmedTransformMatrix(x, y, width, height, zIndex, micro
77
78
  TEMP_MATRIX.compose(TEMP_POSITION, TEMP_QUATERNION, TEMP_SCALE);
78
79
  return TEMP_MATRIX;
79
80
  }
81
+ /**
82
+ * Trim of a sprite drawn at a size other than its source size: the trim stretches with it.
83
+ * Returns a shared object, valid until the next call.
84
+ * @param trim Trim in source units
85
+ * @param width Drawn width
86
+ * @param height Drawn height
87
+ * @param sourceWidth Untrimmed source width
88
+ * @param sourceHeight Untrimmed source height
89
+ * @returns Trim in drawn units
90
+ */
91
+ export function scaleTrim(trim, width, height, sourceWidth, sourceHeight) {
92
+ const scaleX = sourceWidth > 0 ? width / sourceWidth : 1;
93
+ const scaleY = sourceHeight > 0 ? height / sourceHeight : 1;
94
+ TEMP_TRIM.left = trim.left * scaleX;
95
+ TEMP_TRIM.right = trim.right * scaleX;
96
+ TEMP_TRIM.top = trim.top * scaleY;
97
+ TEMP_TRIM.bottom = trim.bottom * scaleY;
98
+ return TEMP_TRIM;
99
+ }
@@ -0,0 +1,60 @@
1
+ import { Ferrsign0 } from "ferrsign";
2
+ const MONITORS = new WeakMap();
3
+ /**
4
+ * Tracks whether the WebGL context of a renderer is lost. One monitor per renderer, shared by
5
+ * every layer drawn with it.
6
+ *
7
+ * The renderer subscribes to the canvas events in its constructor, before any monitor exists, so
8
+ * by the time `signalRestored` fires the renderer has already recreated its GL state. Resources
9
+ * that live on the GPU only, such as render target contents, still have to be redrawn.
10
+ *
11
+ * The listeners stay on the canvas for its lifetime: the monitor holds no reference to the
12
+ * renderer and is collected together with the canvas.
13
+ * @internal
14
+ */
15
+ export class UIContextMonitor {
16
+ constructor(renderer) {
17
+ this.signalLostInternal = new Ferrsign0();
18
+ this.signalRestoredInternal = new Ferrsign0();
19
+ this.onContextLost = () => {
20
+ if (!this.lostInternal) {
21
+ this.lostInternal = true;
22
+ this.signalLostInternal.emit();
23
+ }
24
+ };
25
+ this.onContextRestored = () => {
26
+ if (this.lostInternal) {
27
+ this.lostInternal = false;
28
+ this.signalRestoredInternal.emit();
29
+ }
30
+ };
31
+ this.lostInternal = renderer.getContext().isContextLost();
32
+ renderer.domElement.addEventListener("webglcontextlost", this.onContextLost);
33
+ renderer.domElement.addEventListener("webglcontextrestored", this.onContextRestored);
34
+ }
35
+ /** Whether the context is lost: nothing drawn until it is restored */
36
+ get lost() {
37
+ return this.lostInternal;
38
+ }
39
+ /** Signal fired when the context is lost */
40
+ get signalLost() {
41
+ return this.signalLostInternal;
42
+ }
43
+ /** Signal fired when the context is restored, after the renderer has recreated its GL state */
44
+ get signalRestored() {
45
+ return this.signalRestoredInternal;
46
+ }
47
+ /**
48
+ * Monitor of the renderer, created on first request.
49
+ * @param renderer Renderer to monitor
50
+ * @returns The monitor shared by all users of the renderer
51
+ */
52
+ static for(renderer) {
53
+ let monitor = MONITORS.get(renderer);
54
+ if (monitor === undefined) {
55
+ monitor = new UIContextMonitor(renderer);
56
+ MONITORS.set(renderer, monitor);
57
+ }
58
+ return monitor;
59
+ }
60
+ }
@@ -212,7 +212,8 @@ export function buildMaterial(source, uniformProperties, layout, transparency, m
212
212
  premultipliedAlpha,
213
213
  });
214
214
  if (masked) {
215
- // fwidth on WebGL1 (three.js before r163); WebGL2 has derivatives built in
215
+ // fwidth on WebGL1 (three.js before r163); WebGL2 has derivatives built in. The typings of
216
+ // r163 and later no longer declare the flag
216
217
  material.extensions.derivatives = true;
217
218
  }
218
219
  return { material, varyingRowCount: packed.rowCount };
@@ -2,7 +2,7 @@ import { Mesh } from "three";
2
2
  import { assertVaryingBudget, assertVertexAttributeBudget } from "../../asserts.js";
3
3
  import { MASK_DATA_MODE_OFFSET, MASK_DATA_ROW0_OFFSET, MASK_DATA_ROW1_OFFSET, MASK_DATA_SHAPE_OFFSET, } from "../../mask/UIMask.Internal.js";
4
4
  import { arePropertiesPartiallyCompatible, cloneGLProperties, resolvePropertyUniform, TRANSFORM_ZINDEX_OFFSET, } from "../shared.js";
5
- import { BUILTIN_BLEND, BUILTIN_HSL, BUILTIN_MASK_MODE, BUILTIN_MASK_ROW0, BUILTIN_MASK_ROW1, BUILTIN_MASK_SHAPE, BUILTIN_TRANSFORM, BUILTIN_VISIBILITY, buildMaterial, buildPackedAttribute, buildPlaneLayout, CAPACITY_STEP, copyBetweenBuffers, INITIAL_CAPACITY, INSTANCED_PLANE_GEOMETRY, shiftBufferRegion, writePropertyToArray, } from "./UIGenericInstancedPlane.Internal.js";
5
+ import { buildMaterial, buildPackedAttribute, buildPlaneLayout, BUILTIN_BLEND, BUILTIN_HSL, BUILTIN_MASK_MODE, BUILTIN_MASK_ROW0, BUILTIN_MASK_ROW1, BUILTIN_MASK_SHAPE, BUILTIN_TRANSFORM, BUILTIN_VISIBILITY, CAPACITY_STEP, copyBetweenBuffers, INITIAL_CAPACITY, INSTANCED_PLANE_GEOMETRY, shiftBufferRegion, writePropertyToArray, } from "./UIGenericInstancedPlane.Internal.js";
6
6
  import { packedAttributeName, resolvePackedField } from "./UIGenericInstancedPlane.Layout.js";
7
7
  const TEMP_FIELD_VALUES = new Float32Array(16);
8
8
  /**
@@ -101,7 +101,8 @@ function install(material, uniforms, baseCacheKey) {
101
101
  ? previousCompile.toString()
102
102
  : baseCacheKey.call(material)) + CACHE_KEY_SUFFIX;
103
103
  if (material instanceof ShaderMaterial) {
104
- // fwidth on WebGL1 (three.js before r163); built-in materials fall back to world units
104
+ // fwidth on WebGL1 (three.js before r163); built-in materials fall back to world units.
105
+ // The typings of r163 and later no longer declare the flag
105
106
  material.extensions.derivatives = true;
106
107
  material.defines = { ...material.defines };
107
108
  material.defines["LAYMUR_MASK_DERIVATIVES"] = "";
@@ -1,13 +1,16 @@
1
1
  /**
2
2
  * Base class for viewport resize policies. Subclass it for a custom scaling rule and pass it to
3
- * `UIFullscreenLayer`.
3
+ * `UIViewportLayer`.
4
4
  */
5
5
  export declare abstract class UIResizePolicy {
6
+ private versionInternal;
6
7
  /**
7
- * Set it to true when a parameter of the policy changes; the layer then resizes on its next
8
- * render and resets the flag.
8
+ * Bumped when a parameter of the policy changes; every layer using the policy resizes on its
9
+ * next render. One policy may serve several layers.
9
10
  */
10
- dirty: boolean;
11
+ get version(): number;
12
+ /** Call it when a parameter of the policy changes */
13
+ protected markChanged(): void;
11
14
  /**
12
15
  * Scale from screen pixels to world units: the layer is the viewport size times this value.
13
16
  * @param width Viewport width in pixels
@@ -1,13 +1,20 @@
1
1
  /**
2
2
  * Base class for viewport resize policies. Subclass it for a custom scaling rule and pass it to
3
- * `UIFullscreenLayer`.
3
+ * `UIViewportLayer`.
4
4
  */
5
5
  export class UIResizePolicy {
6
6
  constructor() {
7
- /**
8
- * Set it to true when a parameter of the policy changes; the layer then resizes on its next
9
- * render and resets the flag.
10
- */
11
- this.dirty = false;
7
+ this.versionInternal = 0;
8
+ }
9
+ /**
10
+ * Bumped when a parameter of the policy changes; every layer using the policy resizes on its
11
+ * next render. One policy may serve several layers.
12
+ */
13
+ get version() {
14
+ return this.versionInternal;
15
+ }
16
+ /** Call it when a parameter of the policy changes */
17
+ markChanged() {
18
+ this.versionInternal++;
12
19
  }
13
20
  }
@@ -1,5 +1,5 @@
1
1
  import { UIResizePolicy } from "./UIResizePolicy.js";
2
- /** Scales area to cover rect preserving proportions */
2
+ /** Scales area so the rect covers it preserving proportions (like CSS object-fit: cover) */
3
3
  export declare class UIResizePolicyCover extends UIResizePolicy {
4
4
  private rectWidthInternal;
5
5
  private rectHeightInternal;