laymur 0.25.2 → 0.27.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 (186) hide show
  1. package/README.md +114 -34
  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/UIAnchor/UIAnchor.d.ts +2 -2
  7. package/dist/core/elements/UIAnchor/UIAnchor.js +3 -2
  8. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.d.ts +2 -2
  9. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.js +24 -12
  10. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +13 -0
  11. package/dist/core/elements/UIDummy/UIDummy.d.ts +74 -5
  12. package/dist/core/elements/UIDummy/UIDummy.js +222 -29
  13. package/dist/core/elements/UIElement/UIElement.Internal.d.ts +1 -1
  14. package/dist/core/elements/UIElement/UIElement.d.ts +37 -1
  15. package/dist/core/elements/UIElement/UIElement.js +84 -6
  16. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +1 -1
  17. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +1 -1
  18. package/dist/core/elements/UIGraphics/UIGraphics.js +4 -4
  19. package/dist/core/elements/UIImage/UIImage.d.ts +2 -2
  20. package/dist/core/elements/UIImage/UIImage.js +10 -8
  21. package/dist/core/elements/UINineSlice/UINineSlice.d.ts +4 -4
  22. package/dist/core/elements/UINineSlice/UINineSlice.js +11 -9
  23. package/dist/core/elements/UIProgress/UIProgress.d.ts +2 -2
  24. package/dist/core/elements/UIProgress/UIProgress.js +17 -15
  25. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +1 -1
  26. package/dist/core/elements/UIScene/UIScene.d.ts +3 -2
  27. package/dist/core/elements/UIScene/UIScene.js +21 -14
  28. package/dist/core/elements/UIText/UIText.Measuring.d.ts +1 -1
  29. package/dist/core/elements/UIText/UIText.d.ts +3 -3
  30. package/dist/core/elements/UIText/UIText.js +8 -8
  31. package/dist/core/index.d.ts +60 -11
  32. package/dist/core/index.js +22 -7
  33. package/dist/core/layers/UILayer/UILayer.d.ts +45 -5
  34. package/dist/core/layers/UILayer/UILayer.js +103 -10
  35. package/dist/core/layers/UIViewportLayer/UIViewportLayer.Internal.d.ts +27 -0
  36. package/dist/core/layers/{UIFullscreenLayer/UIFullscreenLayer.Internal.js → UIViewportLayer/UIViewportLayer.Internal.js} +1 -1
  37. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +98 -0
  38. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +251 -0
  39. package/dist/core/miscellaneous/UIAffineMatrix3.d.ts +13 -0
  40. package/dist/core/miscellaneous/UIAffineMatrix3.js +13 -0
  41. package/dist/core/miscellaneous/area/UIArea.d.ts +10 -1
  42. package/dist/core/miscellaneous/area/UIArea.js +4 -1
  43. package/dist/core/miscellaneous/area/UIAreaCircle.d.ts +1 -0
  44. package/dist/core/miscellaneous/area/UIAreaCircle.js +0 -1
  45. package/dist/core/miscellaneous/area/UIAreaPolygon.d.ts +1 -0
  46. package/dist/core/miscellaneous/area/UIAreaPolygon.js +0 -1
  47. package/dist/core/miscellaneous/area/UIAreaRectangle.d.ts +1 -0
  48. package/dist/core/miscellaneous/area/UIAreaRectangle.js +0 -1
  49. package/dist/core/miscellaneous/area/UIAreaRoundedRectangle.d.ts +1 -0
  50. package/dist/core/miscellaneous/area/UIAreaRoundedRectangle.js +0 -1
  51. package/dist/core/miscellaneous/asserts.d.ts +23 -0
  52. package/dist/core/miscellaneous/asserts.js +44 -0
  53. package/dist/core/miscellaneous/canvas.js +7 -2
  54. package/dist/core/miscellaneous/color/UIColor.js +5 -2
  55. package/dist/core/miscellaneous/computeTransform.d.ts +12 -0
  56. package/dist/core/miscellaneous/computeTransform.js +20 -0
  57. package/dist/core/miscellaneous/context-monitor/UIContextMonitor.d.ts +1 -0
  58. package/dist/core/miscellaneous/context-monitor/UIContextMonitor.js +60 -0
  59. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.d.ts +17 -3
  60. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.js +88 -26
  61. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Layout.d.ts +9 -0
  62. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Layout.js +29 -6
  63. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.js +51 -11
  64. package/dist/core/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.Internal.d.ts +2 -0
  65. package/dist/core/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.js +47 -6
  66. package/dist/core/miscellaneous/generic-plane/shared.d.ts +15 -3
  67. package/dist/core/miscellaneous/generic-plane/shared.js +38 -7
  68. package/dist/core/miscellaneous/generic-plane/varyingPacking.d.ts +46 -0
  69. package/dist/core/miscellaneous/generic-plane/varyingPacking.js +170 -0
  70. package/dist/core/miscellaneous/mask/UIMask.Internal.d.ts +61 -0
  71. package/dist/core/miscellaneous/mask/UIMask.Internal.js +56 -0
  72. package/dist/core/miscellaneous/mask/UIMask.d.ts +84 -0
  73. package/dist/core/miscellaneous/mask/UIMask.js +265 -0
  74. package/dist/core/miscellaneous/mask/UIMaskMaterialPatch.d.ts +50 -0
  75. package/dist/core/miscellaneous/mask/UIMaskMaterialPatch.js +233 -0
  76. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunction.d.ts +10 -1
  77. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunction.js +7 -1
  78. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.d.ts +2 -0
  79. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.js +0 -1
  80. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.d.ts +2 -0
  81. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.js +0 -1
  82. package/dist/core/miscellaneous/micro/UIMicro.Internal.d.ts +3 -0
  83. package/dist/core/miscellaneous/micro/UIMicro.Internal.js +1 -0
  84. package/dist/core/miscellaneous/micro/UIMicro.d.ts +18 -1
  85. package/dist/core/miscellaneous/micro/UIMicro.js +23 -2
  86. package/dist/core/miscellaneous/resize-policy/UIResizePolicy.d.ts +19 -1
  87. package/dist/core/miscellaneous/resize-policy/UIResizePolicy.js +16 -3
  88. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCover.js +2 -2
  89. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCross.js +2 -2
  90. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCrossInverted.js +2 -2
  91. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFit.js +2 -2
  92. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedHeight.js +2 -2
  93. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedWidth.js +2 -2
  94. package/dist/core/miscellaneous/text-style/UITextStyle.js +2 -2
  95. package/dist/core/miscellaneous/texture/UITextureView.d.ts +6 -2
  96. package/dist/core/miscellaneous/texture/UITextureView.js +13 -9
  97. package/dist/core/miscellaneous/viewport/UIElementViewport.d.ts +31 -0
  98. package/dist/core/miscellaneous/viewport/UIElementViewport.js +54 -0
  99. package/dist/core/miscellaneous/viewport/UIViewport.d.ts +37 -0
  100. package/dist/core/miscellaneous/viewport/UIViewport.js +31 -0
  101. package/dist/core/miscellaneous/viewport/UIWindowViewport.d.ts +13 -0
  102. package/dist/core/miscellaneous/viewport/UIWindowViewport.js +25 -0
  103. package/dist/core/miscellaneous/webglCapabilities.js +61 -55
  104. package/dist/core/shaders/UIMask.d.ts +15 -0
  105. package/dist/core/shaders/UIMask.js +45 -0
  106. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +36 -5
  107. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +23 -2
  108. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +153 -15
  109. package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.Internal.d.ts +3 -1
  110. package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.d.ts +1 -0
  111. package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.js +3 -0
  112. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +2 -0
  113. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +3 -0
  114. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +36 -16
  115. package/dist/debug/UIAspectDebug.js +4 -0
  116. package/dist/debug/UIHeightDebug.js +4 -0
  117. package/dist/debug/UIHorizontalDistanceDebug.js +5 -0
  118. package/dist/debug/UIHorizontalInterpolationDebug.js +5 -0
  119. package/dist/debug/UILayerDebug.d.ts +7 -0
  120. package/dist/debug/UILayerDebug.js +21 -0
  121. package/dist/debug/UIVerticalDistanceDebug.js +5 -0
  122. package/dist/debug/UIVerticalInterpolationDebug.js +5 -0
  123. package/dist/debug/UIWidthDebug.js +4 -0
  124. package/dist/debug/index.d.ts +2 -0
  125. package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.d.ts +8 -1
  126. package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.js +0 -2
  127. package/dist/particles/behaviorModules/UIBehaviorModule.d.ts +12 -2
  128. package/dist/particles/behaviorModules/UIBehaviorModule.js +2 -1
  129. package/dist/particles/behaviorModules/UIBehaviorPointGravity.d.ts +8 -1
  130. package/dist/particles/behaviorModules/UIBehaviorPointGravity.js +13 -12
  131. package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.d.ts +7 -0
  132. package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.js +6 -6
  133. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.d.ts +7 -0
  134. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.js +4 -5
  135. package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.d.ts +8 -1
  136. package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.js +8 -10
  137. package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.d.ts +10 -2
  138. package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.js +8 -7
  139. package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.d.ts +7 -0
  140. package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.js +8 -10
  141. package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.d.ts +13 -4
  142. package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.js +12 -10
  143. package/dist/particles/elements/UIEmitter.Internal.d.ts +21 -1
  144. package/dist/particles/elements/UIEmitter.Internal.js +6 -0
  145. package/dist/particles/elements/UIEmitter.d.ts +20 -11
  146. package/dist/particles/elements/UIEmitter.js +100 -54
  147. package/dist/particles/index.d.ts +7 -1
  148. package/dist/particles/index.js +4 -0
  149. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.d.ts +19 -4
  150. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.js +117 -83
  151. package/dist/particles/instancedParticle/UIInstancedParticle.d.ts +8 -0
  152. package/dist/particles/instancedParticle/UIInstancedParticle.js +41 -4
  153. package/dist/particles/miscellaneous/miscellaneous.d.ts +34 -0
  154. package/dist/particles/miscellaneous/miscellaneous.js +72 -0
  155. package/dist/particles/renderingModules/UIRenderingColorOverLife.d.ts +4 -0
  156. package/dist/particles/renderingModules/UIRenderingColorOverLife.js +2 -4
  157. package/dist/particles/renderingModules/UIRenderingColorOverVelocity.d.ts +4 -0
  158. package/dist/particles/renderingModules/UIRenderingColorOverVelocity.js +2 -5
  159. package/dist/particles/renderingModules/UIRenderingModule.d.ts +11 -1
  160. package/dist/particles/renderingModules/UIRenderingModule.js +4 -1
  161. package/dist/particles/renderingModules/UIRenderingTexture.d.ts +4 -0
  162. package/dist/particles/renderingModules/UIRenderingTexture.js +2 -1
  163. package/dist/particles/shaders/UIRenderingColorOverLife.d.ts +1 -2
  164. package/dist/particles/shaders/UIRenderingColorOverLife.js +5 -3
  165. package/dist/particles/shaders/UIRenderingColorOverVelocity.d.ts +1 -9
  166. package/dist/particles/shaders/UIRenderingColorOverVelocity.js +6 -13
  167. package/dist/particles/spawnModules/UISpawnModule.d.ts +16 -1
  168. package/dist/particles/spawnModules/UISpawnModule.js +3 -1
  169. package/dist/particles/spawnModules/UISpawnOffset.d.ts +7 -0
  170. package/dist/particles/spawnModules/UISpawnOffset.js +0 -2
  171. package/dist/particles/spawnModules/UISpawnRandomLifetime.d.ts +8 -1
  172. package/dist/particles/spawnModules/UISpawnRandomLifetime.js +5 -7
  173. package/dist/particles/spawnModules/UISpawnRandomRotation.d.ts +7 -0
  174. package/dist/particles/spawnModules/UISpawnRandomRotation.js +5 -5
  175. package/dist/particles/spawnModules/UISpawnRandomScale.d.ts +7 -0
  176. package/dist/particles/spawnModules/UISpawnRandomScale.js +4 -5
  177. package/dist/particles/spawnModules/UISpawnRandomTorque.d.ts +7 -0
  178. package/dist/particles/spawnModules/UISpawnRandomTorque.js +5 -5
  179. package/dist/particles/spawnModules/UISpawnRandomVelocity.d.ts +7 -0
  180. package/dist/particles/spawnModules/UISpawnRandomVelocity.js +6 -6
  181. package/dist/particles/spawnModules/UISpawnRectangle.d.ts +7 -0
  182. package/dist/particles/spawnModules/UISpawnRectangle.js +6 -6
  183. package/package.json +13 -4
  184. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.Internal.d.ts +0 -21
  185. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.d.ts +0 -63
  186. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.js +0 -144
@@ -1,5 +1,6 @@
1
1
  import type { FerrsignView3 } from "ferrsign";
2
- import { Matrix3, Texture, Vector2 } from "three";
2
+ import type { Matrix3 } from "three";
3
+ import { Texture, Vector2 } from "three";
3
4
  import type { UITextureConfig, UITextureTrim } from "./UITextureView.Internal.js";
4
5
  /** Wrapper for Three.js texture with atlas and trim support. Does not own the underlying texture; user must dispose it. */
5
6
  export declare class UITextureView {
@@ -26,7 +27,10 @@ export declare class UITextureView {
26
27
  get width(): number;
27
28
  /** Original height before trimming in world units */
28
29
  get height(): number;
29
- /** Visible width after trimming in world units */
30
+ /**
31
+ * Visible width after trimming in world units. The frame size is the unrotated sprite size,
32
+ * also for a rotated frame, as in the UV transform
33
+ */
30
34
  get trimmedWidth(): number;
31
35
  /** Visible height after trimming in world units */
32
36
  get trimmedHeight(): number;
@@ -1,6 +1,7 @@
1
1
  import { Ferrsign3 } from "ferrsign";
2
- import { Matrix3, Texture, Vector2 } from "three";
2
+ import { Texture, Vector2 } from "three";
3
3
  import { assertValidNonNegativeNumber, assertValidPositiveNumber } from "../asserts.js";
4
+ import { UIAffineMatrix3 } from "../UIAffineMatrix3.js";
4
5
  import { getTextureImageSize, TEXTURE_DEFAULT_SIZE, TEXTURE_DEFAULT_TEXTURE, } from "./UITextureView.Internal.js";
5
6
  /** Wrapper for Three.js texture with atlas and trim support. Does not own the underlying texture; user must dispose it. */
6
7
  export class UITextureView {
@@ -35,15 +36,16 @@ export class UITextureView {
35
36
  get height() {
36
37
  return this.sourceHeight / this.scaleInternal;
37
38
  }
38
- /** Visible width after trimming in world units */
39
+ /**
40
+ * Visible width after trimming in world units. The frame size is the unrotated sprite size,
41
+ * also for a rotated frame, as in the UV transform
42
+ */
39
43
  get trimmedWidth() {
40
- const frameSize = this.rotatedInternal ? this.frameHeight : this.frameWidth;
41
- return frameSize / this.scaleInternal;
44
+ return this.frameWidth / this.scaleInternal;
42
45
  }
43
46
  /** Visible height after trimming in world units */
44
47
  get trimmedHeight() {
45
- const frameSize = this.rotatedInternal ? this.frameWidth : this.frameHeight;
46
- return frameSize / this.scaleInternal;
48
+ return this.frameHeight / this.scaleInternal;
47
49
  }
48
50
  /** Underlying Three.js texture */
49
51
  get texture() {
@@ -97,7 +99,7 @@ export class UITextureView {
97
99
  * @param result Matrix to store result in
98
100
  * @returns UV transform matrix
99
101
  */
100
- calculateUVTransform(result = new Matrix3()) {
102
+ calculateUVTransform(result = new UIAffineMatrix3()) {
101
103
  const { w: atlasWidth, h: atlasHeight } = getTextureImageSize(this.textureInternal);
102
104
  if (this.rotatedInternal) {
103
105
  const physicalWidth = this.frameHeight;
@@ -194,11 +196,13 @@ export class UITextureView {
194
196
  if (this.texture !== config.texture) {
195
197
  this.textureInternal = config.texture;
196
198
  this.textureDirtyInternal = true;
199
+ // The UV transform divides by the atlas size, so a new atlas can change it under the same frame.
200
+ this.uvTransformDirtyInternal = true;
197
201
  }
198
202
  const scale = config.scale ?? 1;
199
203
  const rotated = config.rotated ?? false;
200
- if (this.frameX !== 0 ||
201
- this.frameY !== 0 ||
204
+ if (this.frameX !== config.frame.x ||
205
+ this.frameY !== config.frame.y ||
202
206
  this.frameWidth !== config.frame.w ||
203
207
  this.frameHeight !== config.frame.h ||
204
208
  this.scaleInternal !== scale ||
@@ -0,0 +1,31 @@
1
+ import { UIViewport } from "./UIViewport.js";
2
+ /**
3
+ * An element of the page, usually the canvas of the renderer: for a canvas that does not fill
4
+ * the window, such as an editor preview or a canvas with page margins. The size follows the
5
+ * element's box as laid out, including CSS transforms.
6
+ */
7
+ export declare class UIElementViewport extends UIViewport {
8
+ private readonly elementInternal;
9
+ private readonly observer;
10
+ private widthInternal;
11
+ private heightInternal;
12
+ /**
13
+ * @param element Element whose box the viewport follows
14
+ */
15
+ constructor(element: Element);
16
+ /** Element the viewport follows */
17
+ get element(): Element;
18
+ get width(): number;
19
+ get height(): number;
20
+ /**
21
+ * The box is read on every call: the element may move without resizing, as on scroll, or
22
+ * change size through a CSS transform, which the resize observer does not report
23
+ */
24
+ toLocal<T extends {
25
+ x: number;
26
+ y: number;
27
+ }>(clientX: number, clientY: number, result: T): T;
28
+ protected destroyInternal(): void;
29
+ private readonly onResize;
30
+ private applySize;
31
+ }
@@ -0,0 +1,54 @@
1
+ import { UIViewport } from "./UIViewport.js";
2
+ /**
3
+ * An element of the page, usually the canvas of the renderer: for a canvas that does not fill
4
+ * the window, such as an editor preview or a canvas with page margins. The size follows the
5
+ * element's box as laid out, including CSS transforms.
6
+ */
7
+ export class UIElementViewport extends UIViewport {
8
+ /**
9
+ * @param element Element whose box the viewport follows
10
+ */
11
+ constructor(element) {
12
+ super();
13
+ this.onResize = () => {
14
+ this.applySize(this.elementInternal.getBoundingClientRect());
15
+ };
16
+ this.elementInternal = element;
17
+ const rect = element.getBoundingClientRect();
18
+ this.widthInternal = rect.width;
19
+ this.heightInternal = rect.height;
20
+ this.observer = new ResizeObserver(this.onResize);
21
+ this.observer.observe(element);
22
+ }
23
+ /** Element the viewport follows */
24
+ get element() {
25
+ return this.elementInternal;
26
+ }
27
+ get width() {
28
+ return this.widthInternal;
29
+ }
30
+ get height() {
31
+ return this.heightInternal;
32
+ }
33
+ /**
34
+ * The box is read on every call: the element may move without resizing, as on scroll, or
35
+ * change size through a CSS transform, which the resize observer does not report
36
+ */
37
+ toLocal(clientX, clientY, result) {
38
+ const rect = this.elementInternal.getBoundingClientRect();
39
+ this.applySize(rect);
40
+ result.x = clientX - rect.left;
41
+ result.y = rect.bottom - clientY;
42
+ return result;
43
+ }
44
+ destroyInternal() {
45
+ this.observer.disconnect();
46
+ }
47
+ applySize(rect) {
48
+ if (rect.width !== this.widthInternal || rect.height !== this.heightInternal) {
49
+ this.widthInternal = rect.width;
50
+ this.heightInternal = rect.height;
51
+ this.signalResizedInternal.emit();
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,37 @@
1
+ import type { FerrsignView0 } from "ferrsign";
2
+ import { Ferrsign0 } from "ferrsign";
3
+ /**
4
+ * Part of the page a layer covers: gives the layer its size and the origin of input positions.
5
+ * Sizes and positions are in CSS pixels. Subclass it for a custom area and pass it to
6
+ * `UIViewportLayer`.
7
+ *
8
+ * A viewport passed to a layer belongs to the caller, who destroys it after the layers using it;
9
+ * one viewport may serve several layers.
10
+ */
11
+ export declare abstract class UIViewport {
12
+ protected readonly signalResizedInternal: Ferrsign0;
13
+ private destroyedInternal;
14
+ /** Signal fired when the size changes */
15
+ get signalResized(): FerrsignView0;
16
+ /** Whether the viewport is destroyed */
17
+ get destroyed(): boolean;
18
+ /** Width in CSS pixels; 0 while the area is not laid out */
19
+ abstract get width(): number;
20
+ /** Height in CSS pixels; 0 while the area is not laid out */
21
+ abstract get height(): number;
22
+ /** Stops tracking the size. Does nothing if the viewport is already destroyed. */
23
+ destroy(): void;
24
+ /**
25
+ * Converts a position on the page to the viewport: origin at the bottom-left corner, y up.
26
+ * @param clientX Horizontal position relative to the browser viewport, as in `PointerEvent`
27
+ * @param clientY Vertical position relative to the browser viewport, as in `PointerEvent`
28
+ * @param result Vector to write into
29
+ * @returns The result vector
30
+ */
31
+ abstract toLocal<T extends {
32
+ x: number;
33
+ y: number;
34
+ }>(clientX: number, clientY: number, result: T): T;
35
+ /** Releases what the viewport listens to */
36
+ protected abstract destroyInternal(): void;
37
+ }
@@ -0,0 +1,31 @@
1
+ import { Ferrsign0 } from "ferrsign";
2
+ /**
3
+ * Part of the page a layer covers: gives the layer its size and the origin of input positions.
4
+ * Sizes and positions are in CSS pixels. Subclass it for a custom area and pass it to
5
+ * `UIViewportLayer`.
6
+ *
7
+ * A viewport passed to a layer belongs to the caller, who destroys it after the layers using it;
8
+ * one viewport may serve several layers.
9
+ */
10
+ export class UIViewport {
11
+ constructor() {
12
+ this.signalResizedInternal = new Ferrsign0();
13
+ this.destroyedInternal = false;
14
+ }
15
+ /** Signal fired when the size changes */
16
+ get signalResized() {
17
+ return this.signalResizedInternal;
18
+ }
19
+ /** Whether the viewport is destroyed */
20
+ get destroyed() {
21
+ return this.destroyedInternal;
22
+ }
23
+ /** Stops tracking the size. Does nothing if the viewport is already destroyed. */
24
+ destroy() {
25
+ if (this.destroyedInternal) {
26
+ return;
27
+ }
28
+ this.destroyedInternal = true;
29
+ this.destroyInternal();
30
+ }
31
+ }
@@ -0,0 +1,13 @@
1
+ import { UIViewport } from "./UIViewport.js";
2
+ /** The whole browser window */
3
+ export declare class UIWindowViewport extends UIViewport {
4
+ constructor();
5
+ get width(): number;
6
+ get height(): number;
7
+ toLocal<T extends {
8
+ x: number;
9
+ y: number;
10
+ }>(clientX: number, clientY: number, result: T): T;
11
+ protected destroyInternal(): void;
12
+ private readonly onResize;
13
+ }
@@ -0,0 +1,25 @@
1
+ import { UIViewport } from "./UIViewport.js";
2
+ /** The whole browser window */
3
+ export class UIWindowViewport extends UIViewport {
4
+ constructor() {
5
+ super();
6
+ this.onResize = () => {
7
+ this.signalResizedInternal.emit();
8
+ };
9
+ window.addEventListener("resize", this.onResize);
10
+ }
11
+ get width() {
12
+ return window.innerWidth;
13
+ }
14
+ get height() {
15
+ return window.innerHeight;
16
+ }
17
+ toLocal(clientX, clientY, result) {
18
+ result.x = clientX;
19
+ result.y = window.innerHeight - clientY;
20
+ return result;
21
+ }
22
+ destroyInternal() {
23
+ window.removeEventListener("resize", this.onResize);
24
+ }
25
+ }
@@ -1,17 +1,18 @@
1
+ /** Discards errors left by earlier calls, so that the next getError reports only the next call */
2
+ function clearErrors(gl) {
3
+ while (gl.getError() !== gl.NO_ERROR) {
4
+ // Drain the error queue
5
+ }
6
+ }
1
7
  /**
2
8
  * Checks real OffscreenCanvas support as a texture source
3
9
  */
4
- function checkOffscreenCanvasSupportInternal() {
10
+ function probeOffscreenCanvas(gl) {
5
11
  if (typeof OffscreenCanvas === "undefined") {
6
12
  return false;
7
13
  }
8
- const testCanvasElement = document.createElement("canvas");
9
- testCanvasElement.width = 4;
10
- testCanvasElement.height = 4;
11
- const gl = testCanvasElement.getContext("webgl") ?? testCanvasElement.getContext("webgl2");
12
- if (!gl) {
13
- return false;
14
- }
14
+ const texture = gl.createTexture();
15
+ const framebuffer = gl.createFramebuffer();
15
16
  try {
16
17
  const offscreenCanvas = new OffscreenCanvas(4, 4);
17
18
  const offscreenContext = offscreenCanvas.getContext("2d");
@@ -20,82 +21,87 @@ function checkOffscreenCanvasSupportInternal() {
20
21
  }
21
22
  offscreenContext.fillStyle = "#ff0000";
22
23
  offscreenContext.fillRect(0, 0, 4, 4);
23
- const texture = gl.createTexture();
24
+ clearErrors(gl);
24
25
  gl.bindTexture(gl.TEXTURE_2D, texture);
25
26
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, offscreenCanvas);
26
27
  if (gl.getError() !== gl.NO_ERROR) {
27
- gl.deleteTexture(texture);
28
28
  return false;
29
29
  }
30
- const framebuffer = gl.createFramebuffer();
31
30
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
32
31
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
33
32
  if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) {
34
- gl.deleteFramebuffer(framebuffer);
35
- gl.deleteTexture(texture);
36
33
  return false;
37
34
  }
38
35
  const pixelData = new Uint8Array(4);
39
36
  gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelData);
37
+ return pixelData[0] > 200 && pixelData[1] < 50 && pixelData[2] < 50 && pixelData[3] > 200;
38
+ }
39
+ catch {
40
+ return false;
41
+ }
42
+ finally {
40
43
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
44
+ gl.bindTexture(gl.TEXTURE_2D, null);
41
45
  gl.deleteFramebuffer(framebuffer);
42
46
  gl.deleteTexture(texture);
43
- const isRedPixel = pixelData[0] > 200 && pixelData[1] < 50 && pixelData[2] < 50 && pixelData[3] > 200;
44
- if (!isRedPixel) {
45
- return false;
46
- }
47
- return true;
48
- }
49
- catch (error) {
50
- void error;
51
- return false;
52
47
  }
53
48
  }
54
49
  /**
55
50
  * Checks sRGB support
56
51
  */
57
- function checkSRGBSupportInternal() {
58
- const canvasElement = document.createElement("canvas");
59
- const gl = canvasElement.getContext("webgl2") ?? canvasElement.getContext("webgl");
60
- if (!gl) {
52
+ function probeSRGB(gl) {
53
+ const isWebGL2 = typeof WebGL2RenderingContext !== "undefined" && gl instanceof WebGL2RenderingContext;
54
+ const extension = isWebGL2 ? null : gl.getExtension("EXT_sRGB");
55
+ if (!isWebGL2 && !extension) {
61
56
  return false;
62
57
  }
63
- if (gl instanceof WebGL2RenderingContext) {
64
- try {
65
- const texture = gl.createTexture();
66
- gl.bindTexture(gl.TEXTURE_2D, texture);
58
+ const texture = gl.createTexture();
59
+ try {
60
+ clearErrors(gl);
61
+ gl.bindTexture(gl.TEXTURE_2D, texture);
62
+ if (isWebGL2) {
67
63
  gl.texStorage2D(gl.TEXTURE_2D, 1, gl.SRGB8_ALPHA8, 2, 2);
68
- const errorCode = gl.getError();
69
- gl.deleteTexture(texture);
70
- return errorCode === gl.NO_ERROR;
71
64
  }
72
- catch {
73
- return false;
65
+ else if (extension) {
66
+ gl.texImage2D(gl.TEXTURE_2D, 0, extension.SRGB_ALPHA_EXT, 2, 2, 0, extension.SRGB_ALPHA_EXT, gl.UNSIGNED_BYTE, null);
74
67
  }
68
+ return gl.getError() === gl.NO_ERROR;
75
69
  }
76
- const extension = gl.getExtension("EXT_sRGB");
77
- if (!extension) {
70
+ catch {
78
71
  return false;
79
72
  }
80
- try {
81
- const texture = gl.createTexture();
82
- gl.bindTexture(gl.TEXTURE_2D, texture);
83
- gl.texImage2D(gl.TEXTURE_2D, 0, extension.SRGB_ALPHA_EXT, 2, 2, 0, extension.SRGB_ALPHA_EXT, gl.UNSIGNED_BYTE, null);
84
- const errorCode = gl.getError();
73
+ finally {
74
+ gl.bindTexture(gl.TEXTURE_2D, null);
85
75
  gl.deleteTexture(texture);
86
- return errorCode === gl.NO_ERROR;
87
76
  }
88
- catch {
89
- return false;
77
+ }
78
+ /**
79
+ * Runs all checks on one temporary context and releases it right away. Browsers keep a limited
80
+ * number of live WebGL contexts and lose the oldest one when the limit is exceeded, so a probe
81
+ * context left for the garbage collector can cost the application its own context.
82
+ */
83
+ function probeCapabilities() {
84
+ const canvasElement = document.createElement("canvas");
85
+ canvasElement.width = 4;
86
+ canvasElement.height = 4;
87
+ const gl = canvasElement.getContext("webgl2") ?? canvasElement.getContext("webgl");
88
+ if (!gl) {
89
+ return { offscreenCanvas: false, srgb: false };
90
90
  }
91
+ try {
92
+ return {
93
+ offscreenCanvas: probeOffscreenCanvas(gl),
94
+ srgb: probeSRGB(gl),
95
+ };
96
+ }
97
+ finally {
98
+ gl.getExtension("WEBGL_lose_context")?.loseContext();
99
+ }
100
+ }
101
+ let CAPABILITIES_INTERNAL;
102
+ function getCapabilities() {
103
+ CAPABILITIES_INTERNAL ?? (CAPABILITIES_INTERNAL = probeCapabilities());
104
+ return CAPABILITIES_INTERNAL;
91
105
  }
92
- let OFFSCREEN_CANVAS_SUPPORTED_INTERNAL;
93
- let SRGB_SUPPORTED_INTERNAL;
94
- export const checkOffscreenCanvasSupport = () => {
95
- OFFSCREEN_CANVAS_SUPPORTED_INTERNAL ?? (OFFSCREEN_CANVAS_SUPPORTED_INTERNAL = checkOffscreenCanvasSupportInternal());
96
- return OFFSCREEN_CANVAS_SUPPORTED_INTERNAL;
97
- };
98
- export const checkSRGBSupport = () => {
99
- SRGB_SUPPORTED_INTERNAL ?? (SRGB_SUPPORTED_INTERNAL = checkSRGBSupportInternal());
100
- return SRGB_SUPPORTED_INTERNAL;
101
- };
106
+ export const checkOffscreenCanvasSupport = () => getCapabilities().offscreenCanvas;
107
+ export const checkSRGBSupport = () => getCapabilities().srgb;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Coverage of an analytic mask, shared by masked planes and patched external materials.
3
+ *
4
+ * Arguments:
5
+ * - p: point in mask-local space (centered, world units)
6
+ * - shape: (half width, half height, corner radius, softness); the radius is already clamped
7
+ * - mode: shape (0 = rectangle, 1 = ellipse) + 2 when inverted
8
+ *
9
+ * The edge is antialiased over a pixel through screen-space derivatives. On WebGL1 they need
10
+ * OES_standard_derivatives enabled for the program: the includer defines LAYMUR_MASK_DERIVATIVES
11
+ * only where it asked three.js to enable it. Without derivatives a world unit is taken as the
12
+ * pixel size.
13
+ */
14
+ declare const source: string;
15
+ export default source;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Coverage of an analytic mask, shared by masked planes and patched external materials.
3
+ *
4
+ * Arguments:
5
+ * - p: point in mask-local space (centered, world units)
6
+ * - shape: (half width, half height, corner radius, softness); the radius is already clamped
7
+ * - mode: shape (0 = rectangle, 1 = ellipse) + 2 when inverted
8
+ *
9
+ * The edge is antialiased over a pixel through screen-space derivatives. On WebGL1 they need
10
+ * OES_standard_derivatives enabled for the program: the includer defines LAYMUR_MASK_DERIVATIVES
11
+ * only where it asked three.js to enable it. Without derivatives a world unit is taken as the
12
+ * pixel size.
13
+ */
14
+ const source = `
15
+ float laymurMaskDistance(vec2 p, vec4 shape, float kind) {
16
+ if (kind > 0.5) {
17
+ // Divided one axis at a time: radii squared overflow mediump on large masks
18
+ vec2 radii = max(shape.xy, vec2(1e-4));
19
+ vec2 normalized = p / radii;
20
+ float k0 = length(normalized);
21
+ float k1 = length(normalized / radii);
22
+ return k1 > 0.0 ? k0 * (k0 - 1.0) / k1 : -min(radii.x, radii.y);
23
+ }
24
+ vec2 q = abs(p) - shape.xy + shape.z;
25
+ return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - shape.z;
26
+ }
27
+
28
+ float laymurMaskCoverage(vec2 p, vec4 shape, float mode) {
29
+ bool inverted = mode > 1.5;
30
+ float kind = inverted ? mode - 2.0 : mode;
31
+ float signedDistance = laymurMaskDistance(p, shape, kind);
32
+
33
+ #if __VERSION__ >= 300 || (defined(LAYMUR_MASK_DERIVATIVES) && defined(GL_OES_standard_derivatives))
34
+ // Euclidean per axis, so the band stays a pixel wide on rotated masks
35
+ float pixel = max(max(length(dFdx(p)), length(dFdy(p))), 1e-4);
36
+ #else
37
+ float pixel = 1.0;
38
+ #endif
39
+
40
+ float coverage = clamp((0.5 * pixel - signedDistance) / (shape.w + pixel), 0.0, 1.0);
41
+ coverage = coverage * coverage * (3.0 - 2.0 * coverage);
42
+ return inverted ? 1.0 - coverage : coverage;
43
+ }
44
+ `;
45
+ export default source;
@@ -1,5 +1,31 @@
1
+ /**
2
+ * Pointer callbacks of an input catcher. Positions are in layer coordinates.
3
+ *
4
+ * A catcher that catches a press captures the pointer: until it is released or cancelled, the
5
+ * moves, the release and the cancel of that pointer go to this catcher only, wherever they are,
6
+ * and exactly one release or cancel ends the capture.
7
+ */
8
+ export interface UIInputCatcherCallbacks {
9
+ /** Returns true to catch the press: catchers below do not get it, and the pointer is captured */
10
+ catchPointerDown: (x: number, y: number, identifier: number) => boolean;
11
+ /** Returns true to stop the event for catchers below; ignored for a captured pointer */
12
+ catchPointerMove: (x: number, y: number, identifier: number) => boolean;
13
+ /** Returns true to stop the event for catchers below; ignored for a captured pointer */
14
+ catchPointerUp: (x: number, y: number, identifier: number) => boolean;
15
+ /**
16
+ * The pointer is gone for this catcher: drop what is known of it. Called when a pressed
17
+ * pointer is cancelled (by the browser or by the layer), on the catcher that captured it, or
18
+ * on every active catcher if nobody did, since a cancel is not hit-tested; on every other
19
+ * active catcher when one captures the pointer; and on the catchers below one that catches a
20
+ * move or release of a pointer nobody captured. No position: the browser reports none worth
21
+ * using, and the catcher knows the last one it saw.
22
+ */
23
+ catchPointerCancel: (identifier: number) => void;
24
+ }
1
25
  export interface UIInputWrapperView {
2
- createInputCatcher(catchPointerDown: (x: number, y: number, identifier: number) => boolean, catchPointerMove: (x: number, y: number, identifier: number) => boolean, catchPointerUp: (x: number, y: number, identifier: number) => boolean, zIndex: number): number;
26
+ /** Bumped by every change to the draw order; compare with the last seen value */
27
+ readonly version: number;
28
+ createInputCatcher(callbacks: UIInputCatcherCallbacks, zIndex: number): number;
3
29
  destroyInputCatcher(handler: number): void;
4
30
  getZIndex(handler: number): number;
5
31
  getDepth(handler: number): number;
@@ -7,14 +33,19 @@ export interface UIInputWrapperView {
7
33
  getActive(handler: number): boolean;
8
34
  setActive(handler: number, active: boolean): void;
9
35
  }
10
- export interface UIInputWrapperCatcher {
36
+ export interface UIInputWrapperCatcher extends UIInputCatcherCallbacks {
11
37
  handler: number;
12
- catchPointerDown: (x: number, y: number, identifier: number) => boolean;
13
- catchPointerMove: (x: number, y: number, identifier: number) => boolean;
14
- catchPointerUp: (x: number, y: number, identifier: number) => boolean;
15
38
  /** Order requested by the user; equal values keep the order of creation */
16
39
  zIndex: number;
17
40
  /** Unique position in the sorted order, used as the rendering depth */
18
41
  depth: number;
19
42
  active: boolean;
20
43
  }
44
+ /** A pressed pointer, not released or cancelled yet */
45
+ export interface UIInputWrapperPointer {
46
+ /** Catcher that caught the press and holds the pointer; null if none did */
47
+ catcher: UIInputWrapperCatcher | null;
48
+ /** Last known position, reported when the layer cancels the pointer */
49
+ x: number;
50
+ y: number;
51
+ }
@@ -1,9 +1,16 @@
1
- import type { UIInputWrapperView } from "./UIInputWrapper.Internal.js";
1
+ import type { UIInputCatcherCallbacks, UIInputWrapperView } from "./UIInputWrapper.Internal.js";
2
2
  export declare class UIInputWrapper implements UIInputWrapperView {
3
+ private versionInternal;
3
4
  private zIndicesDirty;
4
5
  private readonly catchers;
6
+ /** Pressed pointers: the input wrapper is the one place that knows of them */
7
+ private readonly pointers;
8
+ /** Pointers seen moving while not pressed, such as a hovering mouse */
9
+ private readonly hoveringPointers;
5
10
  private lastHandler;
6
- createInputCatcher(catchPointerDown: (x: number, y: number, identifier: number) => boolean, catchPointerMove: (x: number, y: number, identifier: number) => boolean, catchPointerUp: (x: number, y: number, identifier: number) => boolean, zIndex: number): number;
11
+ /** Bumped by every change to the draw order; compare with the last seen value */
12
+ get version(): number;
13
+ createInputCatcher(callbacks: UIInputCatcherCallbacks, zIndex: number): number;
7
14
  destroyInputCatcher(handler: number): void;
8
15
  getZIndex(handler: number): number;
9
16
  getDepth(handler: number): number;
@@ -13,7 +20,21 @@ export declare class UIInputWrapper implements UIInputWrapperView {
13
20
  /**
14
21
  * Passes a pointer event from the top catcher down until one catches it. A handler may
15
22
  * destroy elements, so the catchers are walked over a copy and a destroyed one is skipped.
23
+ * @param pointer Pressed pointer the event belongs to: the walk stops once a handler ends it
24
+ * @returns The catcher that caught the event, or null
16
25
  */
17
26
  private dispatch;
27
+ /**
28
+ * Passes a cancel to the active catchers below one that caught an event: they did not see it,
29
+ * and the pointer is over the catcher above them now, or released
30
+ */
31
+ private cancelBelow;
32
+ /** Passes a cancel to every active catcher but one, top one first */
33
+ private broadcastCancel;
34
+ /**
35
+ * Frees the pointers held by a catcher that stops taking input; it resets its own state. The
36
+ * pointers stay pressed, for the layer, with no catcher.
37
+ */
38
+ private releaseCaptures;
18
39
  private ensureSorted;
19
40
  }