laymur 0.2.10 → 0.2.12

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.
package/dist/index.js CHANGED
@@ -1826,7 +1826,12 @@ var UIAnchorMode;
1826
1826
  UIAnchorMode[UIAnchorMode["POSITION_ROTATION_SCALE"] = 1] = "POSITION_ROTATION_SCALE";
1827
1827
  })(UIAnchorMode || (UIAnchorMode = {}));
1828
1828
 
1829
- const tempColor = new Color();
1829
+ let originalClearColor = new Color();
1830
+ let originalClearAlpha = 0;
1831
+ let originalAutoClear = false;
1832
+ let originalAutoClearColor = false;
1833
+ let originalAutoClearDepth = false;
1834
+ let originalAutoClearStencil = false;
1830
1835
  class UISceneWrapper {
1831
1836
  constructor(w, h) {
1832
1837
  this._private_scene = new Scene();
@@ -1893,14 +1898,13 @@ class UISceneWrapper {
1893
1898
  getSortedVisibleElements() {
1894
1899
  return Array.from(this._private_elements.keys()).sort((a, b) => this.getZIndex(a) - this.getZIndex(b));
1895
1900
  }
1896
- render(renderer, deltaTime) {
1897
- const originalRenderTarget = renderer.getRenderTarget();
1898
- const originalClearColor = renderer.getClearColor(tempColor);
1899
- const originalClearAlpha = renderer.getClearAlpha();
1900
- const originalAutoClear = renderer.autoClear;
1901
- const originalAutoClearColor = renderer.autoClearColor;
1902
- const originalAutoClearDepth = renderer.autoClearDepth;
1903
- const originalAutoClearStencil = renderer.autoClearStencil;
1901
+ prepareRender(renderer) {
1902
+ originalClearColor = renderer.getClearColor(originalClearColor);
1903
+ originalClearAlpha = renderer.getClearAlpha();
1904
+ originalAutoClear = renderer.autoClear;
1905
+ originalAutoClearColor = renderer.autoClearColor;
1906
+ originalAutoClearDepth = renderer.autoClearDepth;
1907
+ originalAutoClearStencil = renderer.autoClearStencil;
1904
1908
  renderer.autoClear = false;
1905
1909
  renderer.autoClearColor = false;
1906
1910
  renderer.autoClearDepth = false;
@@ -1931,11 +1935,11 @@ class UISceneWrapper {
1931
1935
  object.scale.x = width;
1932
1936
  object.scale.y = height;
1933
1937
  object.rotation.z = micro.rotation;
1934
- element["onBeforeRenderInternal"](renderer, deltaTime);
1935
1938
  }
1939
+ }
1940
+ render(renderer) {
1936
1941
  renderer.clear(false, true, true);
1937
1942
  renderer.render(this._private_scene, this._private_camera);
1938
- renderer.setRenderTarget(originalRenderTarget);
1939
1943
  renderer.setClearColor(originalClearColor);
1940
1944
  renderer.setClearAlpha(originalClearAlpha);
1941
1945
  renderer.autoClear = originalAutoClear;
@@ -2307,6 +2311,8 @@ var UILayerEvent;
2307
2311
  UILayerEvent["ORIENTATION_CHANGE"] = "orientation_change";
2308
2312
  /** Emitted when the layer's visibility/interaction mode changes. */
2309
2313
  UILayerEvent["MODE_CHANGE"] = "mode_change";
2314
+ /** Emitted before the layer is rendered. */
2315
+ UILayerEvent["WILL_RENDER"] = "will_render";
2310
2316
  })(UILayerEvent || (UILayerEvent = {}));
2311
2317
  /**
2312
2318
  * Abstract base class for UI layout containers with constraint solving and rendering.
@@ -2429,7 +2435,9 @@ class UILayer extends i {
2429
2435
  */
2430
2436
  renderInternal(renderer, deltaTime) {
2431
2437
  if (this.mode !== UIMode.HIDDEN) {
2432
- this.sceneWrapper.render(renderer, deltaTime);
2438
+ this.sceneWrapper.prepareRender(renderer);
2439
+ this.emit(UILayerEvent.WILL_RENDER, renderer, deltaTime, this);
2440
+ this.sceneWrapper.render(renderer);
2433
2441
  }
2434
2442
  }
2435
2443
  /**
@@ -2459,7 +2467,6 @@ class UILayer extends i {
2459
2467
  * constraints that need to create variables or constraints.
2460
2468
  *
2461
2469
  * @returns The constraint solver wrapper instance
2462
- * @internal
2463
2470
  */
2464
2471
  ["getSolverWrapperInternal"]() {
2465
2472
  return this.solverWrapper;
@@ -2471,7 +2478,6 @@ class UILayer extends i {
2471
2478
  * that need to be added to or removed from the 3D scene.
2472
2479
  *
2473
2480
  * @returns The scene wrapper instance
2474
- * @internal
2475
2481
  */
2476
2482
  ["getSceneWrapperInternal"]() {
2477
2483
  return this.sceneWrapper;
@@ -2632,7 +2638,7 @@ class UISingleParameterConstraint extends UIConstraint {
2632
2638
  * other layout changes or for setting fixed dimensions in responsive layouts.
2633
2639
  *
2634
2640
  * @see {@link UISingleParameterConstraint} - Base class for single-parameter constraints
2635
- * @see {@link UIElement} - Elements that can have height constraints applied
2641
+ * @see {@link UIPlaneElement} - Elements that can have height constraints applied
2636
2642
  * @see {@link UIExpression} - Mathematical expressions for constraint equations
2637
2643
  */
2638
2644
  class UIHeightConstraint extends UISingleParameterConstraint {
@@ -2696,7 +2702,11 @@ function isUIPointElement(obj) {
2696
2702
  "xVariable" in obj &&
2697
2703
  typeof obj.xVariable === "number" &&
2698
2704
  "yVariable" in obj &&
2699
- typeof obj.yVariable === "number");
2705
+ typeof obj.yVariable === "number" &&
2706
+ "width" in obj &&
2707
+ typeof obj.width === "number" &&
2708
+ "height" in obj &&
2709
+ typeof obj.height === "number");
2700
2710
  }
2701
2711
  /**
2702
2712
  * Type guard to check if an object implements the UIPlaneElement interface.
@@ -2713,7 +2723,11 @@ function isUIPlaneElement(obj) {
2713
2723
  "wVariable" in obj &&
2714
2724
  typeof obj.wVariable === "number" &&
2715
2725
  "hVariable" in obj &&
2716
- typeof obj.hVariable === "number");
2726
+ typeof obj.hVariable === "number" &&
2727
+ "width" in obj &&
2728
+ typeof obj.width === "number" &&
2729
+ "height" in obj &&
2730
+ typeof obj.height === "number");
2717
2731
  }
2718
2732
  /**
2719
2733
  * Validates that two objects are valid constraint subjects from the same layer.
@@ -2807,7 +2821,7 @@ function assertValidNonNegativeNumber(value, subject) {
2807
2821
  }
2808
2822
 
2809
2823
  /** Default anchor point (0.5 = center) for elements when not specified. */
2810
- const DEFAULT_ANCHOR$2 = 0.5;
2824
+ const DEFAULT_ANCHOR$4 = 0.5;
2811
2825
  /**
2812
2826
  * Constraint that enforces horizontal distance between two UI elements.
2813
2827
  *
@@ -2839,8 +2853,8 @@ class UIHorizontalDistanceConstraint extends UISingleParameterConstraint {
2839
2853
  super(assertValidConstraintSubjects(_private_a, _private_b, "UIHorizontalDistanceConstraint"), options.priority, options.relation, options.orientation);
2840
2854
  this._private_a = _private_a;
2841
2855
  this._private_b = _private_b;
2842
- this._private_anchorAInternal = options.anchorA ?? DEFAULT_ANCHOR$2;
2843
- this._private_anchorBInternal = options.anchorB ?? DEFAULT_ANCHOR$2;
2856
+ this._private_anchorAInternal = options.anchorA ?? DEFAULT_ANCHOR$4;
2857
+ this._private_anchorBInternal = options.anchorB ?? DEFAULT_ANCHOR$4;
2844
2858
  this._private_distanceInternal = options.distance ?? 0;
2845
2859
  this.constraint = this.solverWrapper.createConstraint(this._private_buildLHS(), new UIExpression(this._private_distanceInternal), this.relationInternal, this.priorityInternal, this.isConstraintEnabled());
2846
2860
  }
@@ -3004,7 +3018,7 @@ class UIHorizontalProportionConstraint extends UISingleParameterConstraint {
3004
3018
  }
3005
3019
 
3006
3020
  /** Default anchor point (0.5 = center) for elements when not specified. */
3007
- const DEFAULT_ANCHOR$1 = 0.5;
3021
+ const DEFAULT_ANCHOR$3 = 0.5;
3008
3022
  /**
3009
3023
  * Constraint that enforces vertical distance between two UI elements.
3010
3024
  *
@@ -3036,8 +3050,8 @@ class UIVerticalDistanceConstraint extends UISingleParameterConstraint {
3036
3050
  super(assertValidConstraintSubjects(_private_a, _private_b, "UIVerticalDistanceConstraint"), options.priority, options.relation, options.orientation);
3037
3051
  this._private_a = _private_a;
3038
3052
  this._private_b = _private_b;
3039
- this._private_anchorAInternal = options.anchorA ?? DEFAULT_ANCHOR$1;
3040
- this._private_anchorBInternal = options.anchorB ?? DEFAULT_ANCHOR$1;
3053
+ this._private_anchorAInternal = options.anchorA ?? DEFAULT_ANCHOR$3;
3054
+ this._private_anchorBInternal = options.anchorB ?? DEFAULT_ANCHOR$3;
3041
3055
  this._private_distanceInternal = options.distance ?? 0;
3042
3056
  this.constraint = this.solverWrapper.createConstraint(this._private_buildLHS(), new UIExpression(this._private_distanceInternal), this.relationInternal, this.priorityInternal, this.isConstraintEnabled());
3043
3057
  }
@@ -3209,7 +3223,7 @@ class UIVerticalProportionConstraint extends UISingleParameterConstraint {
3209
3223
  * other layout changes or for setting fixed dimensions in responsive layouts.
3210
3224
  *
3211
3225
  * @see {@link UISingleParameterConstraint} - Base class for single-parameter constraints
3212
- * @see {@link UIElement} - Elements that can have width constraints applied
3226
+ * @see {@link UIPlaneElement} - Elements that can have width constraints applied
3213
3227
  * @see {@link UIExpression} - Mathematical expressions for constraint equations
3214
3228
  */
3215
3229
  class UIWidthConstraint extends UISingleParameterConstraint {
@@ -3363,7 +3377,7 @@ class UIConstraint2DBuilder {
3363
3377
  * ratios like 16:9 for video content.
3364
3378
  *
3365
3379
  * @see {@link UISingleParameterConstraint} - Base class for single-parameter constraints
3366
- * @see {@link UIElement} - Elements that can have aspect constraints applied
3380
+ * @see {@link UIPlaneElement} - Elements that can have aspect constraints applied
3367
3381
  * @see {@link UIExpression} - Mathematical expressions for constraint equations
3368
3382
  */
3369
3383
  class UIAspectConstraint extends UISingleParameterConstraint {
@@ -3416,6 +3430,124 @@ class UIAspectConstraint extends UISingleParameterConstraint {
3416
3430
  }
3417
3431
  }
3418
3432
 
3433
+ const DEFAULT_ANCHOR$2 = 0.5;
3434
+ /**
3435
+ * Builder for creating "cover" layout constraints that make an element cover its container.
3436
+ *
3437
+ * Cover layout ensures the active element is large enough to completely cover the passive
3438
+ * element while maintaining proportions. This is similar to CSS `background-size: cover`
3439
+ * or `object-fit: cover` behavior.
3440
+ *
3441
+ * The active element will be:
3442
+ * - Positioned at the specified anchor point within the passive element
3443
+ * - Sized to be at least as large as the passive element in both dimensions
3444
+ * - Scaled proportionally to maintain aspect ratio (if enabled)
3445
+ */
3446
+ class UICoverConstraintBuilder {
3447
+ /**
3448
+ * Creates a set of constraints for cover layout behavior.
3449
+ *
3450
+ * @param passive - The container element (what gets covered)
3451
+ * @param active - The content element (what does the covering)
3452
+ * @param options - Configuration options for the cover layout
3453
+ * @returns Object containing all created constraints
3454
+ */
3455
+ static build(passive, active, options = {}) {
3456
+ let activeAspectConstraint;
3457
+ if (options.keepActiveAspect === true) {
3458
+ activeAspectConstraint = new UIAspectConstraint(active, {
3459
+ orientation: options.orientation,
3460
+ });
3461
+ }
3462
+ const xConstraint = new UIHorizontalDistanceConstraint(passive, active, {
3463
+ anchorA: options.anchorH ?? DEFAULT_ANCHOR$2,
3464
+ anchorB: options.anchorH ?? DEFAULT_ANCHOR$2,
3465
+ orientation: options.orientation,
3466
+ });
3467
+ const yConstraint = new UIVerticalDistanceConstraint(passive, active, {
3468
+ anchorA: options.anchorV ?? DEFAULT_ANCHOR$2,
3469
+ anchorB: options.anchorV ?? DEFAULT_ANCHOR$2,
3470
+ orientation: options.orientation,
3471
+ });
3472
+ const widthConstraint = new UIHorizontalProportionConstraint(passive, active, {
3473
+ proportion: 1,
3474
+ relation: UIRelation.GREATER_THAN,
3475
+ orientation: options.orientation,
3476
+ });
3477
+ const heightConstraint = new UIVerticalProportionConstraint(passive, active, {
3478
+ proportion: 1,
3479
+ priority: UIPriority.P1,
3480
+ orientation: options.orientation,
3481
+ });
3482
+ return {
3483
+ activeAspectConstraint,
3484
+ xConstraint,
3485
+ yConstraint,
3486
+ widthConstraint,
3487
+ heightConstraint,
3488
+ };
3489
+ }
3490
+ }
3491
+
3492
+ const DEFAULT_ANCHOR$1 = 0.5;
3493
+ /**
3494
+ * Builder for creating "fit" layout constraints that make an element fit within its container.
3495
+ *
3496
+ * Fit layout ensures the active element is sized to completely fit within the passive
3497
+ * element while maintaining proportions. This is similar to CSS `background-size: contain`
3498
+ * or `object-fit: contain` behavior.
3499
+ *
3500
+ * The active element will be:
3501
+ * - Positioned at the specified anchor point within the passive element
3502
+ * - Sized to fit entirely within the passive element in both dimensions
3503
+ * - Scaled proportionally to maintain aspect ratio (if enabled)
3504
+ */
3505
+ class UIFitConstraintBuilder {
3506
+ /**
3507
+ * Creates a set of constraints for fit layout behavior.
3508
+ *
3509
+ * @param passive - The container element (what contains the content)
3510
+ * @param active - The content element (what gets fitted within the container)
3511
+ * @param options - Configuration options for the fit layout
3512
+ * @returns Object containing all created constraints
3513
+ */
3514
+ static build(passive, active, options = {}) {
3515
+ let activeAspectConstraint;
3516
+ if (options.keepActiveAspect === true) {
3517
+ activeAspectConstraint = new UIAspectConstraint(active, {
3518
+ orientation: options.orientation,
3519
+ });
3520
+ }
3521
+ const xConstraint = new UIHorizontalDistanceConstraint(passive, active, {
3522
+ anchorA: options.anchorH ?? DEFAULT_ANCHOR$1,
3523
+ anchorB: options.anchorH ?? DEFAULT_ANCHOR$1,
3524
+ orientation: options.orientation,
3525
+ });
3526
+ const yConstraint = new UIVerticalDistanceConstraint(passive, active, {
3527
+ anchorA: options.anchorV ?? DEFAULT_ANCHOR$1,
3528
+ anchorB: options.anchorV ?? DEFAULT_ANCHOR$1,
3529
+ orientation: options.orientation,
3530
+ });
3531
+ const widthConstraint = new UIHorizontalProportionConstraint(passive, active, {
3532
+ proportion: 1,
3533
+ relation: UIRelation.LESS_THAN,
3534
+ orientation: options.orientation,
3535
+ });
3536
+ const heightConstraint = new UIVerticalProportionConstraint(passive, active, {
3537
+ proportion: 1,
3538
+ priority: UIPriority.P1,
3539
+ orientation: options.orientation,
3540
+ });
3541
+ return {
3542
+ activeAspectConstraint,
3543
+ xConstraint,
3544
+ yConstraint,
3545
+ widthConstraint,
3546
+ heightConstraint,
3547
+ };
3548
+ }
3549
+ }
3550
+
3419
3551
  /**
3420
3552
  * Constraint that allows custom mathematical relationships between UI elements.
3421
3553
  *
@@ -4052,7 +4184,6 @@ class UIElement extends UIAnchor {
4052
4184
  * @param x - The x-coordinate of the click
4053
4185
  * @param y - The y-coordinate of the click
4054
4186
  * @returns True if the click was handled by this element
4055
- * @internal
4056
4187
  */
4057
4188
  ["onClickInternal"](x, y) {
4058
4189
  const isClicked = this.modeInternal === UIMode.INTERACTIVE &&
@@ -4065,25 +4196,13 @@ class UIElement extends UIAnchor {
4065
4196
  }
4066
4197
  return isClicked;
4067
4198
  }
4068
- /**
4069
- * Internal method called before rendering each frame.
4070
- *
4071
- * This is a hook method that can be overridden by subclasses to perform
4072
- * pre-render operations such as updating animations, transforms, or
4073
- * other dynamic properties.
4074
- *
4075
- * @param renderer - The WebGL renderer instance
4076
- * @param deltaTime - Time elapsed since the last frame in seconds
4077
- * @internal
4078
- */
4079
- ["onBeforeRenderInternal"](
4080
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- empty basic method, parameters unused
4081
- renderer,
4082
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- empty basic method, parameters unused
4083
- deltaTime) { }
4084
4199
  }
4085
4200
 
4086
- const DEFAULT_ALPHA_TEST$1 = 0.75;
4201
+ var fragmentShader$1 = "uniform sampler2D map;\nuniform float opacity;\nuniform vec3 color;\nuniform float alphaTest;\nvarying vec2 vUv;\n\nvoid main() {\n vec4 diffuseColor = texture2D(map, vUv);\n #include <alphatest_fragment>\n gl_FragColor = vec4(diffuseColor.rgb * color, diffuseColor.a * opacity);\n #include <colorspace_fragment>\n}\n";
4202
+
4203
+ var vertexShader$1 = "varying vec2 vUv;\nvoid main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n";
4204
+
4205
+ const DEFAULT_ALPHA_TEST$1 = 0.25;
4087
4206
  class UIMaterial extends ShaderMaterial {
4088
4207
  constructor(map) {
4089
4208
  super({
@@ -4093,27 +4212,8 @@ class UIMaterial extends ShaderMaterial {
4093
4212
  color: { value: new Color(1.0, 1.0, 1.0) },
4094
4213
  alphaTest: { value: DEFAULT_ALPHA_TEST$1 },
4095
4214
  },
4096
- vertexShader: /* glsl */ `
4097
- varying vec2 vUv;
4098
- void main() {
4099
- vUv = uv;
4100
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
4101
- }
4102
- `,
4103
- fragmentShader: /* glsl */ `
4104
- uniform sampler2D map;
4105
- uniform float opacity;
4106
- uniform vec3 color;
4107
- uniform float alphaTest;
4108
- varying vec2 vUv;
4109
-
4110
- void main() {
4111
- vec4 diffuseColor = texture2D(map, vUv);
4112
- #include <alphatest_fragment>
4113
- gl_FragColor = vec4(diffuseColor.rgb * color, diffuseColor.a * opacity);
4114
- #include <colorspace_fragment>
4115
- }
4116
- `,
4215
+ vertexShader: vertexShader$1,
4216
+ fragmentShader: fragmentShader$1,
4117
4217
  transparent: false,
4118
4218
  alphaTest: DEFAULT_ALPHA_TEST$1,
4119
4219
  lights: false,
@@ -4278,8 +4378,11 @@ class UIImage extends UIElement {
4278
4378
  }
4279
4379
  }
4280
4380
 
4381
+ var fragmentShader = "uniform sampler2D map;\nuniform vec3 color;\nuniform float opacity;\nuniform float alphaTest;\nuniform vec4 sliceBorders;\nuniform vec2 quadSize;\nuniform vec2 textureSize;\nvarying vec2 vUv;\n\nvec2 calculateNineSliceUV(vec2 uv) {\n vec2 localUV = uv;\n\n float left = sliceBorders.x;\n float right = sliceBorders.y;\n float top = sliceBorders.z;\n float bottom = sliceBorders.w;\n\n float leftPx = left * textureSize.x;\n float rightPx = right * textureSize.x;\n float topPx = top * textureSize.y;\n float bottomPx = bottom * textureSize.y;\n\n float lB = leftPx / quadSize.x;\n float rB = 1.0 - (rightPx / quadSize.x);\n float tB = topPx / quadSize.y;\n float bB = 1.0 - (bottomPx / quadSize.y);\n\n {\n float regionL = step(uv.x, lB);\n float regionR = step(rB, uv.x);\n float regionM = 1.0 - regionL - regionR;\n\n float xL = uv.x * (left / lB);\n float xR = 1.0 - right + ((uv.x - rB) / (1.0 - rB)) * right;\n float xM = left + ((uv.x - lB) / (rB - lB)) * (1.0 - left - right);\n\n localUV.x = regionL * xL + regionM * xM + regionR * xR;\n }\n\n {\n float regionT = step(uv.y, tB);\n float regionB = step(bB, uv.y);\n float regionC = 1.0 - regionT - regionB;\n\n float yT = uv.y * (top / tB);\n float yB = 1.0 - bottom + ((uv.y - bB) / (1.0 - bB)) * bottom;\n float yC = top + ((uv.y - tB) / (bB - tB)) * (1.0 - top - bottom);\n\n localUV.y = regionT * yT + regionC * yC + regionB * yB;\n }\n\n return localUV;\n}\n\nvoid main() {\n vec4 textureColor = texture2D(map, calculateNineSliceUV(vUv));\n if (textureColor.a < alphaTest) {\n discard;\n }\n gl_FragColor = vec4(textureColor.rgb * color, textureColor.a * opacity);\n #include <colorspace_fragment>\n}\n";
4382
+
4383
+ var vertexShader = "varying vec2 vUv;\nvoid main() {\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n";
4384
+
4281
4385
  const DEFAULT_ALPHA_TEST = 0.025;
4282
- const DEFAULT_SLICE_BORDER = 0.25;
4283
4386
  class UINineSliceMaterial extends ShaderMaterial {
4284
4387
  constructor(texture) {
4285
4388
  super({
@@ -4289,83 +4392,15 @@ class UINineSliceMaterial extends ShaderMaterial {
4289
4392
  opacity: { value: 1.0 },
4290
4393
  alphaTest: { value: DEFAULT_ALPHA_TEST },
4291
4394
  sliceBorders: {
4292
- value: new Vector4(DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER),
4293
- }, // left, right, top, bottom (в UV)
4294
- atlasRect: { value: new Vector4(0, 0, 1, 1) }, // x, y, w, h (в UV)
4295
- quadSize: { value: new Vector2(1, 1) },
4395
+ value: new Vector4(0, 0, 0, 0),
4396
+ }, // left, right, top, bottom (in UV)
4397
+ quadSize: { value: new Vector2(1, 1) }, // width, height (in World)
4296
4398
  textureSize: {
4297
4399
  value: new Vector2(texture.image.width, texture.image.height),
4298
4400
  },
4299
4401
  },
4300
- vertexShader: /* glsl */ `
4301
- varying vec2 vUv;
4302
- void main() {
4303
- vUv = uv;
4304
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
4305
- }
4306
- `,
4307
- fragmentShader: /* glsl */ `
4308
- uniform sampler2D map;
4309
- uniform vec3 color;
4310
- uniform float opacity;
4311
- uniform float alphaTest;
4312
- uniform vec4 sliceBorders;
4313
- uniform vec4 atlasRect;
4314
- uniform vec2 quadSize;
4315
- uniform vec2 textureSize;
4316
- varying vec2 vUv;
4317
-
4318
- vec2 calcNineSliceUV(vec2 uv) {
4319
- vec2 localUV = uv;
4320
-
4321
- float left = sliceBorders.x;
4322
- float right = sliceBorders.y;
4323
- float top = sliceBorders.z;
4324
- float bottom = sliceBorders.w;
4325
-
4326
- vec2 texPx = textureSize * atlasRect.zw;
4327
-
4328
- float leftPx = left * texPx.x;
4329
- float rightPx = right * texPx.x;
4330
- float topPx = top * texPx.y;
4331
- float bottomPx = bottom * texPx.y;
4332
-
4333
- float lB = leftPx / quadSize.x;
4334
- float rB = 1.0 - (rightPx / quadSize.x);
4335
- float tB = topPx / quadSize.y;
4336
- float bB = 1.0 - (bottomPx / quadSize.y);
4337
-
4338
- float regionL = step(uv.x, lB);
4339
- float regionR = step(rB, uv.x);
4340
- float regionM = 1.0 - regionL - regionR;
4341
-
4342
- float xL = uv.x * (left / lB);
4343
- float xR = 1.0 - right + ((uv.x - rB) / (1.0 - rB)) * right;
4344
- float xM = left + ((uv.x - lB) / (rB - lB)) * (1.0 - left - right);
4345
-
4346
- localUV.x = regionL * xL + regionM * xM + regionR * xR;
4347
-
4348
- float regionT = step(uv.y, tB);
4349
- float regionBtm = step(bB, uv.y);
4350
- float regionC = 1.0 - regionT - regionBtm;
4351
-
4352
- float yT = uv.y * (top / tB);
4353
- float yB = 1.0 - bottom + ((uv.y - bB) / (1.0 - bB)) * bottom;
4354
- float yC = top + ((uv.y - tB) / (bB - tB)) * (1.0 - top - bottom);
4355
-
4356
- localUV.y = regionT * yT + regionC * yC + regionBtm * yB;
4357
-
4358
- return atlasRect.xy + localUV * atlasRect.zw;
4359
- }
4360
-
4361
- void main() {
4362
- vec2 uv = calcNineSliceUV(vUv);
4363
- vec4 texel = texture2D(map, uv);
4364
- if (texel.a < alphaTest) discard;
4365
- gl_FragColor = vec4(texel.rgb * color, texel.a * opacity);
4366
- #include <colorspace_fragment>
4367
- }
4368
- `,
4402
+ vertexShader,
4403
+ fragmentShader,
4369
4404
  transparent: false,
4370
4405
  alphaTest: DEFAULT_ALPHA_TEST,
4371
4406
  lights: false,
@@ -4374,6 +4409,22 @@ class UINineSliceMaterial extends ShaderMaterial {
4374
4409
  depthTest: true,
4375
4410
  });
4376
4411
  }
4412
+ getSliceBorderLeft() {
4413
+ return this.uniforms.sliceBorders.value.x;
4414
+ }
4415
+ getSliceBorderRight() {
4416
+ return this.uniforms.sliceBorders.value.y;
4417
+ }
4418
+ getSliceBorderTop() {
4419
+ return this.uniforms.sliceBorders.value.z;
4420
+ }
4421
+ getSliceBorderBottom() {
4422
+ return this.uniforms.sliceBorders.value.w;
4423
+ }
4424
+ getQuadSize() {
4425
+ const quadSize = this.uniforms.quadSize.value;
4426
+ return { w: quadSize.x, h: quadSize.y };
4427
+ }
4377
4428
  getTexture() {
4378
4429
  return this.uniforms.map.value;
4379
4430
  }
@@ -4386,23 +4437,43 @@ class UINineSliceMaterial extends ShaderMaterial {
4386
4437
  getTransparency() {
4387
4438
  return this.transparent;
4388
4439
  }
4389
- setSliceBorders(l, r, t, b) {
4440
+ setSliceBorderLeft(l) {
4441
+ this.uniforms.sliceBorders.value.x = l;
4442
+ this.uniformsNeedUpdate = true;
4443
+ return this;
4444
+ }
4445
+ setSliceBorderRight(r) {
4446
+ this.uniforms.sliceBorders.value.y = r;
4447
+ this.uniformsNeedUpdate = true;
4448
+ return this;
4449
+ }
4450
+ setSliceBorderTop(t) {
4451
+ this.uniforms.sliceBorders.value.z = t;
4452
+ this.uniformsNeedUpdate = true;
4453
+ return this;
4454
+ }
4455
+ setSliceBorderBottom(b) {
4456
+ this.uniforms.sliceBorders.value.w = b;
4457
+ this.uniformsNeedUpdate = true;
4458
+ return this;
4459
+ }
4460
+ setSliceBorder(l, r, t, b) {
4390
4461
  this.uniforms.sliceBorders.value.set(l, r, t, b);
4391
4462
  this.uniformsNeedUpdate = true;
4392
4463
  return this;
4393
4464
  }
4394
- setQuadSize(width, height) {
4395
- this.uniforms.quadSize.value.set(width, height);
4465
+ setQuadSize(w, h) {
4466
+ this.uniforms.quadSize.value.set(w, h);
4396
4467
  this.uniformsNeedUpdate = true;
4397
4468
  return this;
4398
4469
  }
4399
- setAtlasRect(x, y, w, h) {
4400
- this.uniforms.atlasRect.value.set(x, y, w, h);
4470
+ setTexture(value) {
4471
+ this.uniforms.map.value = value;
4401
4472
  this.uniformsNeedUpdate = true;
4402
4473
  return this;
4403
4474
  }
4404
4475
  setColor(color) {
4405
- this.uniforms.color.value.copy(color);
4476
+ this.uniforms.color.value.setHex(color);
4406
4477
  this.uniformsNeedUpdate = true;
4407
4478
  return this;
4408
4479
  }
@@ -4411,25 +4482,256 @@ class UINineSliceMaterial extends ShaderMaterial {
4411
4482
  this.uniformsNeedUpdate = true;
4412
4483
  return this;
4413
4484
  }
4485
+ setTransparency(value) {
4486
+ if (this.transparent !== value) {
4487
+ this.transparent = value;
4488
+ this.uniforms.alphaTest.value = !value ? DEFAULT_ALPHA_TEST : 0;
4489
+ this.needsUpdate = true;
4490
+ this.uniformsNeedUpdate = true;
4491
+ }
4492
+ return this;
4493
+ }
4414
4494
  }
4415
4495
 
4496
+ const DEFAULT_SLICE_BORDER = 0.25;
4497
+ /**
4498
+ * Resolves various nine-slice border inputs into a normalized border object.
4499
+ *
4500
+ * @param value - The border value to resolve (can be number, partial object, or full object)
4501
+ * @returns Normalized border object with left, right, top, bottom properties
4502
+ */
4503
+ function resolveNineSliceBorder(value) {
4504
+ // Handle null/undefined
4505
+ if (value === null || value === undefined) {
4506
+ return {
4507
+ left: DEFAULT_SLICE_BORDER,
4508
+ right: DEFAULT_SLICE_BORDER,
4509
+ top: DEFAULT_SLICE_BORDER,
4510
+ bottom: DEFAULT_SLICE_BORDER,
4511
+ };
4512
+ }
4513
+ // Handle number
4514
+ if (typeof value === "number") {
4515
+ return {
4516
+ left: value,
4517
+ right: value,
4518
+ top: value,
4519
+ bottom: value,
4520
+ };
4521
+ }
4522
+ // Handle object
4523
+ if (typeof value === "object") {
4524
+ const obj = value;
4525
+ // Handle { horizontal, vertical } format
4526
+ if ("horizontal" in obj && "vertical" in obj) {
4527
+ const horizontal = typeof obj.horizontal === "number"
4528
+ ? obj.horizontal
4529
+ : DEFAULT_SLICE_BORDER;
4530
+ const vertical = typeof obj.vertical === "number" ? obj.vertical : DEFAULT_SLICE_BORDER;
4531
+ return {
4532
+ left: horizontal,
4533
+ right: horizontal,
4534
+ top: vertical,
4535
+ bottom: vertical,
4536
+ };
4537
+ }
4538
+ // Handle { left, right, top, bottom } format
4539
+ const left = typeof obj.left === "number" ? obj.left : DEFAULT_SLICE_BORDER;
4540
+ const right = typeof obj.right === "number" ? obj.right : DEFAULT_SLICE_BORDER;
4541
+ const top = typeof obj.top === "number" ? obj.top : DEFAULT_SLICE_BORDER;
4542
+ const bottom = typeof obj.bottom === "number" ? obj.bottom : DEFAULT_SLICE_BORDER;
4543
+ return { left, right, top, bottom };
4544
+ }
4545
+ // Fallback to default
4546
+ return {
4547
+ left: DEFAULT_SLICE_BORDER,
4548
+ right: DEFAULT_SLICE_BORDER,
4549
+ top: DEFAULT_SLICE_BORDER,
4550
+ bottom: DEFAULT_SLICE_BORDER,
4551
+ };
4552
+ }
4553
+
4554
+ /**
4555
+ * Nine-slice UI element for scalable images with preserved border regions.
4556
+ *
4557
+ * Nine-slice scaling divides an image into 9 regions: 4 corners, 4 edges, and 1 center.
4558
+ * The corners maintain their original size, edges scale in one dimension, and the center
4559
+ * scales in both dimensions. This technique is commonly used for creating scalable UI
4560
+ * panels, buttons, and borders that maintain visual quality at any size.
4561
+ */
4416
4562
  class UINineSlice extends UIElement {
4417
- constructor(layer, texture, x = 0, y = 0) {
4563
+ /**
4564
+ * Creates a new nine-slice UI element.
4565
+ *
4566
+ * @param layer - The UI layer to add this element to
4567
+ * @param texture - The texture to use for nine-slice rendering
4568
+ * @param options - Configuration options for the nine-slice element
4569
+ */
4570
+ constructor(layer, texture, options = {}) {
4418
4571
  const w = texture.image.width;
4419
4572
  const h = texture.image.height;
4420
4573
  const material = new UINineSliceMaterial(texture);
4421
4574
  const object = new Mesh(geometry, material);
4422
- super(layer, x, y, w, h, object);
4575
+ super(layer, options.x ?? 0, options.y ?? 0, w, h, object);
4576
+ /**
4577
+ * Internal render callback that updates the material's quad size.
4578
+ * This ensures the nine-slice scaling works correctly with micro transformations.
4579
+ */
4580
+ this._private_onWillRenderInternal = () => {
4581
+ this._private_material.setQuadSize(this.width * this.micro.scaleX, this.height * this.micro.scaleY);
4582
+ };
4583
+ this.layer.on(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
4423
4584
  this._private_material = material;
4424
4585
  this._private_textureInternal = texture;
4586
+ const sliceBorder = resolveNineSliceBorder(options.sliceBorder);
4587
+ this._private_material.setSliceBorder(sliceBorder.left, sliceBorder.right, sliceBorder.top, sliceBorder.bottom);
4425
4588
  }
4589
+ /**
4590
+ * Gets the left border size for nine-slice scaling.
4591
+ * @returns The left border size in texture UV coordinates
4592
+ */
4593
+ get sliceBorderLeft() {
4594
+ return this._private_material.getSliceBorderLeft();
4595
+ }
4596
+ /**
4597
+ * Gets the right border size for nine-slice scaling.
4598
+ * @returns The right border size in texture UV coordinates
4599
+ */
4600
+ get sliceBorderRight() {
4601
+ return this._private_material.getSliceBorderRight();
4602
+ }
4603
+ /**
4604
+ * Gets the top border size for nine-slice scaling.
4605
+ * @returns The top border size in texture UV coordinates
4606
+ */
4607
+ get sliceBorderTop() {
4608
+ return this._private_material.getSliceBorderTop();
4609
+ }
4610
+ /**
4611
+ * Gets the bottom border size for nine-slice scaling.
4612
+ * @returns The bottom border size in texture UV coordinates
4613
+ */
4614
+ get sliceBorderBottom() {
4615
+ return this._private_material.getSliceBorderBottom();
4616
+ }
4617
+ /**
4618
+ * Gets the current texture being displayed.
4619
+ * @returns The current Three.js texture
4620
+ */
4621
+ get texture() {
4622
+ return this._private_textureInternal;
4623
+ }
4624
+ /**
4625
+ * Gets the current color tint applied to the image.
4626
+ * @returns The color value as a number (e.g., 0xFFFFFF for white)
4627
+ */
4628
+ get color() {
4629
+ return this._private_material.getColor();
4630
+ }
4631
+ /**
4632
+ * Gets the current opacity level of the image.
4633
+ * @returns The opacity value between 0.0 (transparent) and 1.0 (opaque)
4634
+ */
4635
+ get opacity() {
4636
+ return this._private_material.getOpacity();
4637
+ }
4638
+ /**
4639
+ * Gets whether transparency is enabled for the image.
4640
+ * @returns True if transparency is enabled, false otherwise
4641
+ */
4642
+ get transparency() {
4643
+ return this._private_material.getTransparency();
4644
+ }
4645
+ /**
4646
+ * Sets the left border size for nine-slice scaling.
4647
+ * @param value - The left border size in texture UV coordinates
4648
+ */
4649
+ set sliceBorderLeft(value) {
4650
+ this._private_material.setSliceBorderLeft(value);
4651
+ }
4652
+ /**
4653
+ * Sets the right border size for nine-slice scaling.
4654
+ * @param value - The right border size in texture UV coordinates
4655
+ */
4656
+ set sliceBorderRight(value) {
4657
+ this._private_material.setSliceBorderRight(value);
4658
+ }
4659
+ /**
4660
+ * Sets the top border size for nine-slice scaling.
4661
+ * @param value - The top border size in texture UV coordinates
4662
+ */
4663
+ set sliceBorderTop(value) {
4664
+ this._private_material.setSliceBorderTop(value);
4665
+ }
4666
+ /**
4667
+ * Sets the bottom border size for nine-slice scaling.
4668
+ * @param value - The bottom border size in texture UV coordinates
4669
+ */
4670
+ set sliceBorderBottom(value) {
4671
+ this._private_material.setSliceBorderBottom(value);
4672
+ }
4673
+ /**
4674
+ * Sets a new texture for the image.
4675
+ *
4676
+ * When setting a new texture, the image will automatically resize to match
4677
+ * the new texture's dimensions.
4678
+ *
4679
+ * @param value - The new Three.js texture to display
4680
+ * @throws Will throw an error if the texture dimensions are not valid positive numbers
4681
+ * @see {@link assertValidPositiveNumber}
4682
+ */
4683
+ set texture(value) {
4684
+ const width = value.image.width;
4685
+ const height = value.image.height;
4686
+ assertValidPositiveNumber(width, "UINineSlice texture width");
4687
+ assertValidPositiveNumber(height, "UINineSlice texture height");
4688
+ this._private_material.setTexture(value);
4689
+ this.solverWrapper.suggestVariableValue(this.wVariable, width);
4690
+ this.solverWrapper.suggestVariableValue(this.hVariable, height);
4691
+ }
4692
+ /**
4693
+ * Sets the color tint applied to the image.
4694
+ * @param value - The color value as a number (e.g., 0xFFFFFF for white)
4695
+ */
4696
+ set color(value) {
4697
+ this._private_material.setColor(value);
4698
+ }
4699
+ /**
4700
+ * Sets the opacity level of the image.
4701
+ * @param value - The opacity value between 0.0 (transparent) and 1.0 (opaque)
4702
+ */
4703
+ set opacity(value) {
4704
+ this._private_material.setOpacity(value);
4705
+ }
4706
+ /**
4707
+ * Sets whether transparency is enabled for the image.
4708
+ * @param value - True to enable transparency, false to disable
4709
+ */
4710
+ set transparency(value) {
4711
+ this._private_material.setTransparency(value);
4712
+ }
4713
+ /**
4714
+ * Sets all four border sizes for nine-slice scaling at once.
4715
+ *
4716
+ * @param left - The left border size in texture UV coordinates
4717
+ * @param right - The right border size in texture UV coordinates
4718
+ * @param top - The top border size in texture UV coordinates
4719
+ * @param bottom - The bottom border size in texture UV coordinates
4720
+ */
4721
+ setSliceBorders(left, right, top, bottom) {
4722
+ this._private_material.setSliceBorder(left, right, top, bottom);
4723
+ }
4724
+ /**
4725
+ * Destroys the nine-slice element and cleans up resources.
4726
+ *
4727
+ * This method removes event listeners, disposes of the material,
4728
+ * and calls the parent destroy method.
4729
+ */
4426
4730
  destroy() {
4731
+ this.layer.off(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
4427
4732
  this._private_material.dispose();
4428
4733
  super.destroy();
4429
4734
  }
4430
- ["onBeforeRenderInternal"](renderer, deltaTime) {
4431
- this._private_material.setQuadSize(this.width * this.micro.scaleX, this.height * this.micro.scaleY);
4432
- }
4433
4735
  }
4434
4736
 
4435
4737
  class UIProgressMaterial extends ShaderMaterial {
@@ -4442,58 +4744,14 @@ class UIProgressMaterial extends ShaderMaterial {
4442
4744
  progress: { value: 1 },
4443
4745
  backgroundColor: { value: new Color(1, 1, 1) },
4444
4746
  foregroundColor: { value: new Color(1, 1, 1) },
4445
- color: { value: new Color(1, 1, 1) },
4446
4747
  backgroundOpacity: { value: 1 },
4447
4748
  foregroundOpacity: { value: 1 },
4448
- opacity: { value: 1 },
4449
4749
  angle: { value: 0 },
4450
4750
  direction: { value: 1 },
4451
4751
  },
4452
4752
  ]),
4453
- vertexShader: /* glsl */ `
4454
- varying vec2 vUv;
4455
- void main() {
4456
- vUv = uv;
4457
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
4458
- }
4459
- `,
4460
- fragmentShader: /* glsl */ `
4461
- uniform sampler2D background;
4462
- uniform sampler2D foreground;
4463
- uniform float progress;
4464
- uniform vec3 backgroundColor;
4465
- uniform vec3 foregroundColor;
4466
- uniform vec3 color;
4467
- uniform float backgroundOpacity;
4468
- uniform float foregroundOpacity;
4469
- uniform float opacity;
4470
- uniform float angle;
4471
- uniform float direction;
4472
- varying vec2 vUv;
4473
-
4474
- void main() {
4475
- vec4 backgroundTexture = texture2D(background, vUv);
4476
- vec4 foregroundTexture = texture2D(foreground, vUv);
4477
-
4478
- // Apply common color and opacity
4479
- vec3 finalBackgroundColor = backgroundTexture.rgb * backgroundColor * color;
4480
- vec3 finalForegroundColor = foregroundTexture.rgb * foregroundColor * color;
4481
- float finalBackgroundOpacity = backgroundTexture.a * backgroundOpacity * opacity;
4482
- float finalForegroundOpacity = foregroundTexture.a * foregroundOpacity * opacity;
4483
-
4484
- vec4 backgroundColor = vec4(finalBackgroundColor, finalBackgroundOpacity);
4485
- vec4 foregroundColor = vec4(finalForegroundColor, finalForegroundOpacity);
4486
-
4487
- // Calculate progress threshold with angle and direction
4488
- float adjustedX = mix(vUv.x, 1.0 - vUv.x, step(0.0, -direction));
4489
- float tanAngle = tan(angle);
4490
- float threshold = adjustedX - (vUv.y - 0.5) * tanAngle * sign(direction);
4491
- float progressMask = smoothstep(threshold - 0.01, threshold + 0.01, progress);
4492
-
4493
- gl_FragColor = mix(backgroundColor, foregroundColor, progressMask);
4494
- #include <colorspace_fragment>
4495
- }
4496
- `,
4753
+ vertexShader,
4754
+ fragmentShader,
4497
4755
  transparent: true,
4498
4756
  lights: false,
4499
4757
  fog: false,
@@ -4767,6 +5025,22 @@ class UIScene extends UIElement {
4767
5025
  this._private_propertyChanged = false;
4768
5026
  /** Flag indicating if a manual render has been requested. */
4769
5027
  this._private_renderRequired = false;
5028
+ this._private_onWillRenderInternal = (renderer) => {
5029
+ if (this._private_updateModeInternal === UISceneUpdateMode.EACH_FRAME ||
5030
+ (this._private_updateModeInternal === UISceneUpdateMode.EACH_SECOND_FRAME && this._private_frameBoolean) ||
5031
+ (this._private_updateModeInternal === UISceneUpdateMode.PROPERTY_CHANGED && this._private_propertyChanged) ||
5032
+ (this._private_updateModeInternal === UISceneUpdateMode.MANUAL && this._private_renderRequired)) {
5033
+ this._private_renderRequired = false;
5034
+ this._private_propertyChanged = false;
5035
+ this._private_frameBoolean = !this._private_frameBoolean;
5036
+ this._private_renderTarget.setSize(this.width * this._private_resolutionFactorInternal, this.height * this._private_resolutionFactorInternal);
5037
+ renderer.setClearColor(this._private_clearColorInternal, this.clearAlpha);
5038
+ renderer.setRenderTarget(this._private_renderTarget);
5039
+ renderer.clear(true, true, false);
5040
+ renderer.render(this._private_sceneInternal, this._private_cameraInternal);
5041
+ }
5042
+ };
5043
+ this.layer.on(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
4770
5044
  this._private_material = material;
4771
5045
  this._private_renderTarget = renderTarget;
4772
5046
  this._private_sceneInternal = options.scene ?? new Scene();
@@ -4934,6 +5208,7 @@ class UIScene extends UIElement {
4934
5208
  * After calling this method, the scene element should not be used anymore.
4935
5209
  */
4936
5210
  destroy() {
5211
+ this.layer.off(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
4937
5212
  this._private_material.dispose();
4938
5213
  this._private_renderTarget.dispose();
4939
5214
  super.destroy();
@@ -4947,31 +5222,6 @@ class UIScene extends UIElement {
4947
5222
  requestRender() {
4948
5223
  this._private_renderRequired = true;
4949
5224
  }
4950
- /**
4951
- * Internal method called before rendering each frame.
4952
- *
4953
- * This method handles the conditional rendering of the 3D scene based on
4954
- * the current update mode. It manages frame counting, property change detection,
4955
- * and manual render requests to optimize performance.
4956
- *
4957
- * @param renderer - The WebGL renderer instance
4958
- * @internal
4959
- */
4960
- ["onBeforeRenderInternal"](renderer) {
4961
- if (this._private_updateModeInternal === UISceneUpdateMode.EACH_FRAME ||
4962
- (this._private_updateModeInternal === UISceneUpdateMode.EACH_SECOND_FRAME && this._private_frameBoolean) ||
4963
- (this._private_updateModeInternal === UISceneUpdateMode.PROPERTY_CHANGED && this._private_propertyChanged) ||
4964
- (this._private_updateModeInternal === UISceneUpdateMode.MANUAL && this._private_renderRequired)) {
4965
- this._private_renderRequired = false;
4966
- this._private_propertyChanged = false;
4967
- this._private_frameBoolean = !this._private_frameBoolean;
4968
- this._private_renderTarget.setSize(this.width * this._private_resolutionFactorInternal, this.height * this._private_resolutionFactorInternal);
4969
- renderer.setClearColor(this._private_clearColorInternal, this.clearAlpha);
4970
- renderer.setRenderTarget(this._private_renderTarget);
4971
- renderer.clear(true, true, false);
4972
- renderer.render(this._private_sceneInternal, this._private_cameraInternal);
4973
- }
4974
- }
4975
5225
  }
4976
5226
 
4977
5227
  function isUITextStyle(obj) {
@@ -5265,7 +5515,7 @@ class UIText extends UIElement {
5265
5515
  * @param y - Initial y-coordinate position (defaults to 0)
5266
5516
  * @throws Will throw an error if canvas context creation fails
5267
5517
  */
5268
- constructor(layer, content, options = {}, x = 0, y = 0) {
5518
+ constructor(layer, content, options = {}) {
5269
5519
  const canvas = document.createElement("canvas");
5270
5520
  const context = canvas.getContext("2d");
5271
5521
  if (!context) {
@@ -5274,11 +5524,18 @@ class UIText extends UIElement {
5274
5524
  const texture = new CanvasTexture(canvas);
5275
5525
  const material = new UIMaterial(texture);
5276
5526
  const object = new Mesh(geometry, material);
5277
- super(layer, x, y, 2, 2, object);
5527
+ super(layer, options.x ?? 0, options.y ?? 0, 2, 2, object);
5278
5528
  /** Target aspect ratio for the text element based on content. */
5279
5529
  this._private_targetAspectRatio = 1;
5280
5530
  /** Last calculated aspect ratio for comparison during rendering. */
5281
5531
  this._private_lastAspectRatio = 1;
5532
+ this._private_onWillRenderInternal = () => {
5533
+ if (this._private_lastAspectRatio !== this.width / this.height) {
5534
+ this._private_lastAspectRatio = this._private_targetAspectRatio;
5535
+ this.height = this.width / this._private_targetAspectRatio;
5536
+ }
5537
+ };
5538
+ this.layer.on(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
5282
5539
  this._private_material = material;
5283
5540
  this._private_texture = texture;
5284
5541
  this._private_canvas = canvas;
@@ -5400,32 +5657,12 @@ class UIText extends UIElement {
5400
5657
  * the text element should not be used anymore.
5401
5658
  */
5402
5659
  destroy() {
5660
+ this.layer.off(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
5403
5661
  this._private_material.dispose();
5404
5662
  this._private_texture.dispose();
5405
5663
  this._private_canvas.remove();
5406
5664
  super.destroy();
5407
5665
  }
5408
- /**
5409
- * Internal method called before rendering each frame.
5410
- *
5411
- * Updates the element's height to maintain the correct aspect ratio
5412
- * based on the text content dimensions. This ensures the text is
5413
- * displayed without distortion.
5414
- *
5415
- * @param renderer - The WebGL renderer instance
5416
- * @param deltaTime - Time elapsed since the last frame in seconds
5417
- * @internal
5418
- */
5419
- ["onBeforeRenderInternal"](
5420
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- parameter required by parent
5421
- renderer,
5422
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- parameter required by parent
5423
- deltaTime) {
5424
- if (this._private_lastAspectRatio !== this.width / this.height) {
5425
- this._private_lastAspectRatio = this._private_targetAspectRatio;
5426
- this.height = this.width / this._private_targetAspectRatio;
5427
- }
5428
- }
5429
5666
  /**
5430
5667
  * Rebuilds the text canvas and texture based on current content and settings.
5431
5668
  *
@@ -5639,5 +5876,5 @@ function isUITextSpan(obj) {
5639
5876
  (!("style" in obj) || isUITextStyle(obj.style)));
5640
5877
  }
5641
5878
 
5642
- export { UIAnchor, UIAnchorMode, UIAspectConstraint, UIConstraint, UIConstraint2DBuilder, UICustomConstraint, UIDummy, UIElement, UIElementEvent, UIExpression, UIFullscreenLayer, UIHeightConstraint, UIHorizontalDistanceConstraint, UIHorizontalProportionConstraint, UIImage, UILayer, UILayerEvent, UIMicro, UIMode, UINineSlice, UIOrientation, UIPriority, UIProgress, UIRelation, UIScene, UISceneUpdateMode, UISingleParameterConstraint, UIText, UIVerticalDistanceConstraint, UIVerticalProportionConstraint, UIWidthConstraint, convertPriority, convertRelation, isUITextSpan, isUITextStyle, resolveOrientation, resolvePadding, resolvePriority, resolveRelation, resolveTextStyle };
5879
+ export { UIAnchor, UIAnchorMode, UIAspectConstraint, UIConstraint, UIConstraint2DBuilder, UICoverConstraintBuilder, UICustomConstraint, UIDummy, UIElement, UIElementEvent, UIExpression, UIFitConstraintBuilder, UIFullscreenLayer, UIHeightConstraint, UIHorizontalDistanceConstraint, UIHorizontalProportionConstraint, UIImage, UILayer, UILayerEvent, UIMicro, UIMode, UINineSlice, UIOrientation, UIPriority, UIProgress, UIRelation, UIScene, UISceneUpdateMode, UISingleParameterConstraint, UIText, UIVerticalDistanceConstraint, UIVerticalProportionConstraint, UIWidthConstraint, convertPriority, convertRelation, isUITextSpan, isUITextStyle, resolveOrientation, resolvePadding, resolvePriority, resolveRelation, resolveTextStyle };
5643
5880
  //# sourceMappingURL=index.js.map