laymur 0.2.11 → 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
  /**
@@ -4188,24 +4196,13 @@ class UIElement extends UIAnchor {
4188
4196
  }
4189
4197
  return isClicked;
4190
4198
  }
4191
- /**
4192
- * Internal method called before rendering each frame.
4193
- *
4194
- * This is a hook method that can be overridden by subclasses to perform
4195
- * pre-render operations such as updating animations, transforms, or
4196
- * other dynamic properties.
4197
- *
4198
- * @param renderer - The WebGL renderer instance
4199
- * @param deltaTime - Time elapsed since the last frame in seconds
4200
- */
4201
- ["onBeforeRenderInternal"](
4202
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- empty basic method, parameters unused
4203
- renderer,
4204
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- empty basic method, parameters unused
4205
- deltaTime) { }
4206
4199
  }
4207
4200
 
4208
- 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;
4209
4206
  class UIMaterial extends ShaderMaterial {
4210
4207
  constructor(map) {
4211
4208
  super({
@@ -4215,27 +4212,8 @@ class UIMaterial extends ShaderMaterial {
4215
4212
  color: { value: new Color(1.0, 1.0, 1.0) },
4216
4213
  alphaTest: { value: DEFAULT_ALPHA_TEST$1 },
4217
4214
  },
4218
- vertexShader: /* glsl */ `
4219
- varying vec2 vUv;
4220
- void main() {
4221
- vUv = uv;
4222
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
4223
- }
4224
- `,
4225
- fragmentShader: /* glsl */ `
4226
- uniform sampler2D map;
4227
- uniform float opacity;
4228
- uniform vec3 color;
4229
- uniform float alphaTest;
4230
- varying vec2 vUv;
4231
-
4232
- void main() {
4233
- vec4 diffuseColor = texture2D(map, vUv);
4234
- #include <alphatest_fragment>
4235
- gl_FragColor = vec4(diffuseColor.rgb * color, diffuseColor.a * opacity);
4236
- #include <colorspace_fragment>
4237
- }
4238
- `,
4215
+ vertexShader: vertexShader$1,
4216
+ fragmentShader: fragmentShader$1,
4239
4217
  transparent: false,
4240
4218
  alphaTest: DEFAULT_ALPHA_TEST$1,
4241
4219
  lights: false,
@@ -4400,8 +4378,11 @@ class UIImage extends UIElement {
4400
4378
  }
4401
4379
  }
4402
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
+
4403
4385
  const DEFAULT_ALPHA_TEST = 0.025;
4404
- const DEFAULT_SLICE_BORDER = 0.25;
4405
4386
  class UINineSliceMaterial extends ShaderMaterial {
4406
4387
  constructor(texture) {
4407
4388
  super({
@@ -4411,83 +4392,15 @@ class UINineSliceMaterial extends ShaderMaterial {
4411
4392
  opacity: { value: 1.0 },
4412
4393
  alphaTest: { value: DEFAULT_ALPHA_TEST },
4413
4394
  sliceBorders: {
4414
- value: new Vector4(DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER),
4415
- }, // left, right, top, bottom (в UV)
4416
- atlasRect: { value: new Vector4(0, 0, 1, 1) }, // x, y, w, h (в UV)
4417
- 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)
4418
4398
  textureSize: {
4419
4399
  value: new Vector2(texture.image.width, texture.image.height),
4420
4400
  },
4421
4401
  },
4422
- vertexShader: /* glsl */ `
4423
- varying vec2 vUv;
4424
- void main() {
4425
- vUv = uv;
4426
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
4427
- }
4428
- `,
4429
- fragmentShader: /* glsl */ `
4430
- uniform sampler2D map;
4431
- uniform vec3 color;
4432
- uniform float opacity;
4433
- uniform float alphaTest;
4434
- uniform vec4 sliceBorders;
4435
- uniform vec4 atlasRect;
4436
- uniform vec2 quadSize;
4437
- uniform vec2 textureSize;
4438
- varying vec2 vUv;
4439
-
4440
- vec2 calcNineSliceUV(vec2 uv) {
4441
- vec2 localUV = uv;
4442
-
4443
- float left = sliceBorders.x;
4444
- float right = sliceBorders.y;
4445
- float top = sliceBorders.z;
4446
- float bottom = sliceBorders.w;
4447
-
4448
- vec2 texPx = textureSize * atlasRect.zw;
4449
-
4450
- float leftPx = left * texPx.x;
4451
- float rightPx = right * texPx.x;
4452
- float topPx = top * texPx.y;
4453
- float bottomPx = bottom * texPx.y;
4454
-
4455
- float lB = leftPx / quadSize.x;
4456
- float rB = 1.0 - (rightPx / quadSize.x);
4457
- float tB = topPx / quadSize.y;
4458
- float bB = 1.0 - (bottomPx / quadSize.y);
4459
-
4460
- float regionL = step(uv.x, lB);
4461
- float regionR = step(rB, uv.x);
4462
- float regionM = 1.0 - regionL - regionR;
4463
-
4464
- float xL = uv.x * (left / lB);
4465
- float xR = 1.0 - right + ((uv.x - rB) / (1.0 - rB)) * right;
4466
- float xM = left + ((uv.x - lB) / (rB - lB)) * (1.0 - left - right);
4467
-
4468
- localUV.x = regionL * xL + regionM * xM + regionR * xR;
4469
-
4470
- float regionT = step(uv.y, tB);
4471
- float regionBtm = step(bB, uv.y);
4472
- float regionC = 1.0 - regionT - regionBtm;
4473
-
4474
- float yT = uv.y * (top / tB);
4475
- float yB = 1.0 - bottom + ((uv.y - bB) / (1.0 - bB)) * bottom;
4476
- float yC = top + ((uv.y - tB) / (bB - tB)) * (1.0 - top - bottom);
4477
-
4478
- localUV.y = regionT * yT + regionC * yC + regionBtm * yB;
4479
-
4480
- return atlasRect.xy + localUV * atlasRect.zw;
4481
- }
4482
-
4483
- void main() {
4484
- vec2 uv = calcNineSliceUV(vUv);
4485
- vec4 texel = texture2D(map, uv);
4486
- if (texel.a < alphaTest) discard;
4487
- gl_FragColor = vec4(texel.rgb * color, texel.a * opacity);
4488
- #include <colorspace_fragment>
4489
- }
4490
- `,
4402
+ vertexShader,
4403
+ fragmentShader,
4491
4404
  transparent: false,
4492
4405
  alphaTest: DEFAULT_ALPHA_TEST,
4493
4406
  lights: false,
@@ -4496,6 +4409,22 @@ class UINineSliceMaterial extends ShaderMaterial {
4496
4409
  depthTest: true,
4497
4410
  });
4498
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
+ }
4499
4428
  getTexture() {
4500
4429
  return this.uniforms.map.value;
4501
4430
  }
@@ -4508,23 +4437,43 @@ class UINineSliceMaterial extends ShaderMaterial {
4508
4437
  getTransparency() {
4509
4438
  return this.transparent;
4510
4439
  }
4511
- 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) {
4512
4461
  this.uniforms.sliceBorders.value.set(l, r, t, b);
4513
4462
  this.uniformsNeedUpdate = true;
4514
4463
  return this;
4515
4464
  }
4516
- setQuadSize(width, height) {
4517
- this.uniforms.quadSize.value.set(width, height);
4465
+ setQuadSize(w, h) {
4466
+ this.uniforms.quadSize.value.set(w, h);
4518
4467
  this.uniformsNeedUpdate = true;
4519
4468
  return this;
4520
4469
  }
4521
- setAtlasRect(x, y, w, h) {
4522
- this.uniforms.atlasRect.value.set(x, y, w, h);
4470
+ setTexture(value) {
4471
+ this.uniforms.map.value = value;
4523
4472
  this.uniformsNeedUpdate = true;
4524
4473
  return this;
4525
4474
  }
4526
4475
  setColor(color) {
4527
- this.uniforms.color.value.copy(color);
4476
+ this.uniforms.color.value.setHex(color);
4528
4477
  this.uniformsNeedUpdate = true;
4529
4478
  return this;
4530
4479
  }
@@ -4533,25 +4482,256 @@ class UINineSliceMaterial extends ShaderMaterial {
4533
4482
  this.uniformsNeedUpdate = true;
4534
4483
  return this;
4535
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
+ }
4494
+ }
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
+ };
4536
4552
  }
4537
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
+ */
4538
4562
  class UINineSlice extends UIElement {
4539
- 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 = {}) {
4540
4571
  const w = texture.image.width;
4541
4572
  const h = texture.image.height;
4542
4573
  const material = new UINineSliceMaterial(texture);
4543
4574
  const object = new Mesh(geometry, material);
4544
- 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);
4545
4584
  this._private_material = material;
4546
4585
  this._private_textureInternal = texture;
4586
+ const sliceBorder = resolveNineSliceBorder(options.sliceBorder);
4587
+ this._private_material.setSliceBorder(sliceBorder.left, sliceBorder.right, sliceBorder.top, sliceBorder.bottom);
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();
4547
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
+ */
4548
4730
  destroy() {
4731
+ this.layer.off(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
4549
4732
  this._private_material.dispose();
4550
4733
  super.destroy();
4551
4734
  }
4552
- ["onBeforeRenderInternal"](renderer, deltaTime) {
4553
- this._private_material.setQuadSize(this.width * this.micro.scaleX, this.height * this.micro.scaleY);
4554
- }
4555
4735
  }
4556
4736
 
4557
4737
  class UIProgressMaterial extends ShaderMaterial {
@@ -4564,58 +4744,14 @@ class UIProgressMaterial extends ShaderMaterial {
4564
4744
  progress: { value: 1 },
4565
4745
  backgroundColor: { value: new Color(1, 1, 1) },
4566
4746
  foregroundColor: { value: new Color(1, 1, 1) },
4567
- color: { value: new Color(1, 1, 1) },
4568
4747
  backgroundOpacity: { value: 1 },
4569
4748
  foregroundOpacity: { value: 1 },
4570
- opacity: { value: 1 },
4571
4749
  angle: { value: 0 },
4572
4750
  direction: { value: 1 },
4573
4751
  },
4574
4752
  ]),
4575
- vertexShader: /* glsl */ `
4576
- varying vec2 vUv;
4577
- void main() {
4578
- vUv = uv;
4579
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
4580
- }
4581
- `,
4582
- fragmentShader: /* glsl */ `
4583
- uniform sampler2D background;
4584
- uniform sampler2D foreground;
4585
- uniform float progress;
4586
- uniform vec3 backgroundColor;
4587
- uniform vec3 foregroundColor;
4588
- uniform vec3 color;
4589
- uniform float backgroundOpacity;
4590
- uniform float foregroundOpacity;
4591
- uniform float opacity;
4592
- uniform float angle;
4593
- uniform float direction;
4594
- varying vec2 vUv;
4595
-
4596
- void main() {
4597
- vec4 backgroundTexture = texture2D(background, vUv);
4598
- vec4 foregroundTexture = texture2D(foreground, vUv);
4599
-
4600
- // Apply common color and opacity
4601
- vec3 finalBackgroundColor = backgroundTexture.rgb * backgroundColor * color;
4602
- vec3 finalForegroundColor = foregroundTexture.rgb * foregroundColor * color;
4603
- float finalBackgroundOpacity = backgroundTexture.a * backgroundOpacity * opacity;
4604
- float finalForegroundOpacity = foregroundTexture.a * foregroundOpacity * opacity;
4605
-
4606
- vec4 backgroundColor = vec4(finalBackgroundColor, finalBackgroundOpacity);
4607
- vec4 foregroundColor = vec4(finalForegroundColor, finalForegroundOpacity);
4608
-
4609
- // Calculate progress threshold with angle and direction
4610
- float adjustedX = mix(vUv.x, 1.0 - vUv.x, step(0.0, -direction));
4611
- float tanAngle = tan(angle);
4612
- float threshold = adjustedX - (vUv.y - 0.5) * tanAngle * sign(direction);
4613
- float progressMask = smoothstep(threshold - 0.01, threshold + 0.01, progress);
4614
-
4615
- gl_FragColor = mix(backgroundColor, foregroundColor, progressMask);
4616
- #include <colorspace_fragment>
4617
- }
4618
- `,
4753
+ vertexShader,
4754
+ fragmentShader,
4619
4755
  transparent: true,
4620
4756
  lights: false,
4621
4757
  fog: false,
@@ -4889,6 +5025,22 @@ class UIScene extends UIElement {
4889
5025
  this._private_propertyChanged = false;
4890
5026
  /** Flag indicating if a manual render has been requested. */
4891
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);
4892
5044
  this._private_material = material;
4893
5045
  this._private_renderTarget = renderTarget;
4894
5046
  this._private_sceneInternal = options.scene ?? new Scene();
@@ -5056,6 +5208,7 @@ class UIScene extends UIElement {
5056
5208
  * After calling this method, the scene element should not be used anymore.
5057
5209
  */
5058
5210
  destroy() {
5211
+ this.layer.off(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
5059
5212
  this._private_material.dispose();
5060
5213
  this._private_renderTarget.dispose();
5061
5214
  super.destroy();
@@ -5069,30 +5222,6 @@ class UIScene extends UIElement {
5069
5222
  requestRender() {
5070
5223
  this._private_renderRequired = true;
5071
5224
  }
5072
- /**
5073
- * Internal method called before rendering each frame.
5074
- *
5075
- * This method handles the conditional rendering of the 3D scene based on
5076
- * the current update mode. It manages frame counting, property change detection,
5077
- * and manual render requests to optimize performance.
5078
- *
5079
- * @param renderer - The WebGL renderer instance
5080
- */
5081
- ["onBeforeRenderInternal"](renderer) {
5082
- if (this._private_updateModeInternal === UISceneUpdateMode.EACH_FRAME ||
5083
- (this._private_updateModeInternal === UISceneUpdateMode.EACH_SECOND_FRAME && this._private_frameBoolean) ||
5084
- (this._private_updateModeInternal === UISceneUpdateMode.PROPERTY_CHANGED && this._private_propertyChanged) ||
5085
- (this._private_updateModeInternal === UISceneUpdateMode.MANUAL && this._private_renderRequired)) {
5086
- this._private_renderRequired = false;
5087
- this._private_propertyChanged = false;
5088
- this._private_frameBoolean = !this._private_frameBoolean;
5089
- this._private_renderTarget.setSize(this.width * this._private_resolutionFactorInternal, this.height * this._private_resolutionFactorInternal);
5090
- renderer.setClearColor(this._private_clearColorInternal, this.clearAlpha);
5091
- renderer.setRenderTarget(this._private_renderTarget);
5092
- renderer.clear(true, true, false);
5093
- renderer.render(this._private_sceneInternal, this._private_cameraInternal);
5094
- }
5095
- }
5096
5225
  }
5097
5226
 
5098
5227
  function isUITextStyle(obj) {
@@ -5386,7 +5515,7 @@ class UIText extends UIElement {
5386
5515
  * @param y - Initial y-coordinate position (defaults to 0)
5387
5516
  * @throws Will throw an error if canvas context creation fails
5388
5517
  */
5389
- constructor(layer, content, options = {}, x = 0, y = 0) {
5518
+ constructor(layer, content, options = {}) {
5390
5519
  const canvas = document.createElement("canvas");
5391
5520
  const context = canvas.getContext("2d");
5392
5521
  if (!context) {
@@ -5395,11 +5524,18 @@ class UIText extends UIElement {
5395
5524
  const texture = new CanvasTexture(canvas);
5396
5525
  const material = new UIMaterial(texture);
5397
5526
  const object = new Mesh(geometry, material);
5398
- super(layer, x, y, 2, 2, object);
5527
+ super(layer, options.x ?? 0, options.y ?? 0, 2, 2, object);
5399
5528
  /** Target aspect ratio for the text element based on content. */
5400
5529
  this._private_targetAspectRatio = 1;
5401
5530
  /** Last calculated aspect ratio for comparison during rendering. */
5402
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);
5403
5539
  this._private_material = material;
5404
5540
  this._private_texture = texture;
5405
5541
  this._private_canvas = canvas;
@@ -5521,31 +5657,12 @@ class UIText extends UIElement {
5521
5657
  * the text element should not be used anymore.
5522
5658
  */
5523
5659
  destroy() {
5660
+ this.layer.off(UILayerEvent.WILL_RENDER, this._private_onWillRenderInternal);
5524
5661
  this._private_material.dispose();
5525
5662
  this._private_texture.dispose();
5526
5663
  this._private_canvas.remove();
5527
5664
  super.destroy();
5528
5665
  }
5529
- /**
5530
- * Internal method called before rendering each frame.
5531
- *
5532
- * Updates the element's height to maintain the correct aspect ratio
5533
- * based on the text content dimensions. This ensures the text is
5534
- * displayed without distortion.
5535
- *
5536
- * @param renderer - The WebGL renderer instance
5537
- * @param deltaTime - Time elapsed since the last frame in seconds
5538
- */
5539
- ["onBeforeRenderInternal"](
5540
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- parameter required by parent
5541
- renderer,
5542
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- parameter required by parent
5543
- deltaTime) {
5544
- if (this._private_lastAspectRatio !== this.width / this.height) {
5545
- this._private_lastAspectRatio = this._private_targetAspectRatio;
5546
- this.height = this.width / this._private_targetAspectRatio;
5547
- }
5548
- }
5549
5666
  /**
5550
5667
  * Rebuilds the text canvas and texture based on current content and settings.
5551
5668
  *