laymur 0.13.8 → 0.14.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.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { MathUtils, Color, Vector4, Texture, Vector2, Vector3, Matrix3, Matrix4, PlaneGeometry, InstancedBufferGeometry, ShaderMaterial, InstancedBufferAttribute, Mesh, Scene, OrthographicCamera, Quaternion, CanvasTexture, SRGBColorSpace, NoColorSpace, LinearFilter, WebGLRenderTarget, UnsignedByteType, RGBAFormat, PerspectiveCamera, Ray } from 'three';
1
+ import { MathUtils, Color, Vector4, Texture, Vector2, Vector3, Matrix3, Matrix4, PlaneGeometry, InstancedBufferGeometry, ShaderMaterial, InstancedBufferAttribute, Mesh, Scene, OrthographicCamera, Quaternion, CanvasTexture, UVMapping, ClampToEdgeWrapping, LinearFilter, RGBAFormat, UnsignedByteType, SRGBColorSpace, NoColorSpace, WebGLRenderTarget, PerspectiveCamera, Ray } from 'three';
2
2
 
3
3
  class t {
4
4
  constructor(t, i) {
@@ -2556,19 +2556,99 @@ class UIColor {
2556
2556
  }
2557
2557
  }
2558
2558
 
2559
- const checkSRGBSupport = () => {
2560
- var _a;
2559
+ /**
2560
+ * Checks if WebGL2 is available.
2561
+ */
2562
+ function checkWebGL2Support() {
2561
2563
  const canvas = document.createElement("canvas");
2562
- const gl = (_a = canvas.getContext("webgl2")) !== null && _a !== void 0 ? _a : canvas.getContext("webgl");
2563
- if (!gl) {
2564
+ return canvas.getContext("webgl2") !== null;
2565
+ }
2566
+ /**
2567
+ * Checks real OffscreenCanvas support as a texture source
2568
+ */
2569
+ function checkOffscreenCanvasSupport() {
2570
+ var _a;
2571
+ if (typeof OffscreenCanvas === "undefined") return false;
2572
+ const testCanvasElement = document.createElement("canvas");
2573
+ testCanvasElement.width = 4;
2574
+ testCanvasElement.height = 4;
2575
+ const gl = (_a = testCanvasElement.getContext("webgl")) !== null && _a !== void 0 ? _a : testCanvasElement.getContext("webgl2");
2576
+ if (!gl) return false;
2577
+ try {
2578
+ const offscreenCanvas = new OffscreenCanvas(4, 4);
2579
+ const offscreenContext = offscreenCanvas.getContext("2d");
2580
+ if (!offscreenContext) return false;
2581
+ offscreenContext.fillStyle = "#ff0000";
2582
+ offscreenContext.fillRect(0, 0, 4, 4);
2583
+ const texture = gl.createTexture();
2584
+ gl.bindTexture(gl.TEXTURE_2D, texture);
2585
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, offscreenCanvas);
2586
+ if (gl.getError() !== gl.NO_ERROR) {
2587
+ gl.deleteTexture(texture);
2588
+ return false;
2589
+ }
2590
+ const framebuffer = gl.createFramebuffer();
2591
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
2592
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
2593
+ if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) {
2594
+ gl.deleteFramebuffer(framebuffer);
2595
+ gl.deleteTexture(texture);
2596
+ return false;
2597
+ }
2598
+ const pixelData = new Uint8Array(4);
2599
+ gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelData);
2600
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
2601
+ gl.deleteFramebuffer(framebuffer);
2602
+ gl.deleteTexture(texture);
2603
+ // Check that the read pixel is red (with tolerance)
2604
+ const isRedPixel = pixelData[0] > 200 && pixelData[1] < 50 && pixelData[2] < 50 && pixelData[3] > 200;
2605
+ if (!isRedPixel) return false;
2606
+ return true;
2607
+ } catch (error) {
2564
2608
  return false;
2565
2609
  }
2610
+ }
2611
+ /**
2612
+ * Checks sRGB support
2613
+ */
2614
+ function checkSRGBSupport() {
2615
+ var _a;
2616
+ const canvasElement = document.createElement("canvas");
2617
+ const gl = (_a = canvasElement.getContext("webgl2")) !== null && _a !== void 0 ? _a : canvasElement.getContext("webgl");
2618
+ if (!gl) return false;
2566
2619
  if (gl instanceof WebGL2RenderingContext) {
2567
- return true;
2620
+ try {
2621
+ const texture = gl.createTexture();
2622
+ gl.bindTexture(gl.TEXTURE_2D, texture);
2623
+ gl.texStorage2D(gl.TEXTURE_2D, 1, gl.SRGB8_ALPHA8, 2, 2);
2624
+ const errorCode = gl.getError();
2625
+ gl.deleteTexture(texture);
2626
+ return errorCode === gl.NO_ERROR;
2627
+ } catch (_b) {
2628
+ return false;
2629
+ }
2568
2630
  }
2569
- return gl.getExtension("EXT_sRGB") !== null;
2570
- };
2631
+ const extension = gl.getExtension("EXT_sRGB");
2632
+ if (!extension) return false;
2633
+ try {
2634
+ const texture = gl.createTexture();
2635
+ gl.bindTexture(gl.TEXTURE_2D, texture);
2636
+ gl.texImage2D(gl.TEXTURE_2D, 0, extension.SRGB_ALPHA_EXT, 2, 2, 0, extension.SRGB_ALPHA_EXT, gl.UNSIGNED_BYTE, null);
2637
+ const errorCode = gl.getError();
2638
+ gl.deleteTexture(texture);
2639
+ return errorCode === gl.NO_ERROR;
2640
+ } catch (_c) {
2641
+ return false;
2642
+ }
2643
+ }
2644
+ const WEBGL2_SUPPORTED = checkWebGL2Support();
2645
+ const OFFSCREEN_CANVAS_SUPPORTED = checkOffscreenCanvasSupport();
2571
2646
  const SRGB_SUPPORTED = checkSRGBSupport();
2647
+ console.log("[WebGL] Capabilities:", {
2648
+ webgl2: WEBGL2_SUPPORTED,
2649
+ offscreenCanvas: OFFSCREEN_CANVAS_SUPPORTED,
2650
+ srgb: SRGB_SUPPORTED
2651
+ });
2572
2652
 
2573
2653
  const DEFAULT_ALPHA_TEST = 0.5;
2574
2654
  const GLSL_TYPE_INFO_FLOAT = Object.freeze({
@@ -4401,6 +4481,8 @@ class UILayer extends i {
4401
4481
  }
4402
4482
  }
4403
4483
 
4484
+ const EPSILON = 1e-6;
4485
+
4404
4486
  /**
4405
4487
  * Checks if object implements UILayerElement.
4406
4488
  * @param obj Object to check
@@ -4426,7 +4508,6 @@ function isUIPlaneElement(obj) {
4426
4508
  return typeof obj === "object" && obj !== null && "wVariable" in obj && typeof obj.wVariable === "number" && "hVariable" in obj && typeof obj.hVariable === "number" && "width" in obj && typeof obj.width === "number" && "height" in obj && typeof obj.height === "number";
4427
4509
  }
4428
4510
 
4429
- const EPSILON = 1e-6;
4430
4511
  /**
4431
4512
  * Validates value is finite number within safe range.
4432
4513
  * @param value Number to validate
@@ -6568,42 +6649,37 @@ class UIAnimatedImage extends UIElement {
6568
6649
  }
6569
6650
 
6570
6651
  /**
6571
- * Creates a canvas with OffscreenCanvas fallback to HTMLCanvasElement.
6572
- * Uses OffscreenCanvas when available, falls back to HTMLCanvasElement otherwise.
6652
+ * Creates a canvas using OffscreenCanvas if available, otherwise falls back to HTMLCanvasElement.
6573
6653
  *
6574
6654
  * @param width - Canvas width in pixels
6575
6655
  * @param height - Canvas height in pixels
6576
6656
  * @returns Canvas instance
6577
6657
  */
6578
6658
  function createCanvas(width, height) {
6579
- const canvas = document.createElement("canvas");
6580
- canvas.width = width;
6581
- canvas.height = height;
6582
- return canvas;
6659
+ if (OFFSCREEN_CANVAS_SUPPORTED) {
6660
+ return new OffscreenCanvas(width, height);
6661
+ }
6662
+ const canvasElement = document.createElement("canvas");
6663
+ canvasElement.width = width;
6664
+ canvasElement.height = height;
6665
+ return canvasElement;
6666
+ }
6667
+ /**
6668
+ * Creates a CanvasTexture with maximum WebGL1/WebGL2 compatibility.
6669
+ * - ClampToEdge wrapping (required for NPOT in WebGL1)
6670
+ * - Linear filtering without mipmaps (NPOT-safe)
6671
+ * - RGBA/UnsignedByte format (no extensions needed)
6672
+ * - sRGB colorspace only if supported
6673
+ *
6674
+ * @param canvas - Source canvas
6675
+ * @returns Configured CanvasTexture
6676
+ */
6677
+ function createCanvasTexture(canvas) {
6678
+ const texture = new CanvasTexture(canvas, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, LinearFilter, RGBAFormat, UnsignedByteType, 1);
6679
+ texture.colorSpace = SRGB_SUPPORTED ? SRGBColorSpace : NoColorSpace;
6680
+ texture.generateMipmaps = false;
6681
+ return texture;
6583
6682
  }
6584
- // /** Canvas type that works both in main thread and workers */
6585
- // export type UICanvas = OffscreenCanvas | HTMLCanvasElement;
6586
- // /** 2D rendering context type for UICanvas */
6587
- // export type UICanvasRenderingContext2D =
6588
- // | OffscreenCanvasRenderingContext2D
6589
- // | CanvasRenderingContext2D;
6590
- // /**
6591
- // * Creates a canvas with OffscreenCanvas fallback to HTMLCanvasElement.
6592
- // * Uses OffscreenCanvas when available, falls back to HTMLCanvasElement otherwise.
6593
- // *
6594
- // * @param width - Canvas width in pixels
6595
- // * @param height - Canvas height in pixels
6596
- // * @returns Canvas instance
6597
- // */
6598
- // export function createCanvas(width: number, height: number): UICanvas {
6599
- // if (typeof OffscreenCanvas !== "undefined") {
6600
- // return new OffscreenCanvas(width, height);
6601
- // }
6602
- // const canvas = document.createElement("canvas");
6603
- // canvas.width = width;
6604
- // canvas.height = height;
6605
- // return canvas;
6606
- // }
6607
6683
 
6608
6684
  const GRAPHICS_DEFAULT_WIDTH = 2;
6609
6685
  const GRAPHICS_DEFAULT_HEIGHT = 2;
@@ -6627,10 +6703,7 @@ class UIGraphics extends UIElement {
6627
6703
  if (!context) {
6628
6704
  throw new Error("UIGraphics.constructor: failed to get 2D context from canvas");
6629
6705
  }
6630
- const texture = new CanvasTexture(canvas);
6631
- texture.colorSpace = SRGB_SUPPORTED ? SRGBColorSpace : NoColorSpace;
6632
- texture.minFilter = LinearFilter;
6633
- texture.magFilter = LinearFilter;
6706
+ const texture = createCanvasTexture(canvas);
6634
6707
  texture.needsUpdate = true;
6635
6708
  super(layer, source$4, {
6636
6709
  texture: texture,
@@ -7235,7 +7308,7 @@ class UIProgressMaskFunctionDirectional extends UIProgressMaskFunction {
7235
7308
  /** Fill direction as normalized vector */
7236
7309
  this.direction = new Vector2();
7237
7310
  this._private_cachedDirection = new Vector2();
7238
- this.direction.copy(direction).normalize();
7311
+ this.direction.set(direction.x, direction.y).normalize();
7239
7312
  this._private_cachedDirection.copy(this.direction);
7240
7313
  }
7241
7314
  /** @internal */
@@ -8196,8 +8269,7 @@ class UIText extends UIElement {
8196
8269
  if (!context) {
8197
8270
  throw new Error("UIText.constructor: failed to create canvas context");
8198
8271
  }
8199
- const texture = new CanvasTexture(canvas);
8200
- texture.colorSpace = SRGB_SUPPORTED ? SRGBColorSpace : NoColorSpace;
8272
+ const texture = createCanvasTexture(canvas);
8201
8273
  texture.needsUpdate = true;
8202
8274
  const color = new UIColor(options.color);
8203
8275
  super(layer, source$4, {
@@ -8396,8 +8468,7 @@ class UIText extends UIElement {
8396
8468
  }
8397
8469
  if (resolutionDirty) {
8398
8470
  this._private_texture.dispose();
8399
- this._private_texture = new CanvasTexture(this._private_canvas);
8400
- this._private_texture.colorSpace = SRGB_SUPPORTED ? SRGBColorSpace : NoColorSpace;
8471
+ this._private_texture = createCanvasTexture(this._private_canvas);
8401
8472
  this._private_texture.needsUpdate = true;
8402
8473
  properties !== null && properties !== void 0 ? properties : properties = {};
8403
8474
  properties["texture"] = this._private_texture;