laymur 0.6.2 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { PlaneGeometry, Color, Scene, OrthographicCamera, ShaderMaterial, Vector4, Mesh, Texture, Vector2, Vector3, Matrix2, Matrix3, Matrix4, MathUtils, Quaternion, CanvasTexture, LinearFilter, WebGLRenderTarget, UnsignedByteType, RGBAFormat, PerspectiveCamera, InstancedBufferGeometry, InstancedBufferAttribute } from 'three';
1
+ import { Color, PlaneGeometry, Scene, OrthographicCamera, ShaderMaterial, Vector4, Mesh, Texture, Vector2, Vector3, Matrix2, Matrix3, Matrix4, MathUtils, Quaternion, CanvasTexture, LinearFilter, WebGLRenderTarget, UnsignedByteType, RGBAFormat, PerspectiveCamera, InstancedBufferGeometry, InstancedBufferAttribute } from 'three';
2
2
 
3
3
  /**
4
4
  * Mathematical expression builder for constraint equations in the UI layout system.
@@ -1815,12 +1815,7 @@ function resolvePriority(priority) {
1815
1815
  return priority ?? UIPriority.P0;
1816
1816
  }
1817
1817
 
1818
- /**
1819
- * Database of predefined color names mapped to their hexadecimal RGB values.
1820
- * Used for creating UIColor instances from named colors.
1821
- */
1822
- const COLOR_NAMES = {
1823
- // Basic colors
1818
+ const COLORS = {
1824
1819
  black: 0x000000,
1825
1820
  white: 0xffffff,
1826
1821
  red: 0xff0000,
@@ -1829,7 +1824,6 @@ const COLOR_NAMES = {
1829
1824
  yellow: 0xffff00,
1830
1825
  cyan: 0x00ffff,
1831
1826
  magenta: 0xff00ff,
1832
- // Extended colors
1833
1827
  gray: 0x808080,
1834
1828
  grey: 0x808080,
1835
1829
  silver: 0xc0c0c0,
@@ -1841,7 +1835,6 @@ const COLOR_NAMES = {
1841
1835
  navy: 0x000080,
1842
1836
  fuchsia: 0xff00ff,
1843
1837
  purple: 0x800080,
1844
- // Additional common colors
1845
1838
  orange: 0xffa500,
1846
1839
  pink: 0xffc0cb,
1847
1840
  brown: 0xa52a2a,
@@ -1863,37 +1856,23 @@ const COLOR_NAMES = {
1863
1856
  snow: 0xfffafa,
1864
1857
  };
1865
1858
  /**
1866
- * Events that can be emitted by UIColor instances.
1859
+ * Events emitted by UIColor.
1867
1860
  */
1868
1861
  var UIColorEvent;
1869
1862
  (function (UIColorEvent) {
1870
- /** Emitted when any color property (r, g, b, a) changes. */
1863
+ /** Emitted when color components change. */
1871
1864
  UIColorEvent["CHANGE"] = "change";
1872
1865
  })(UIColorEvent || (UIColorEvent = {}));
1873
1866
  /**
1874
- * Event-based RGBA color class with support for multiple color formats and named colors.
1875
- *
1876
- * UIColor provides a comprehensive color management system with support for RGB, HSL,
1877
- * hexadecimal formats, and named colors. It extends Eventail to emit change events
1878
- * when color properties are modified, enabling reactive color updates throughout
1879
- * the UI system.
1880
- *
1881
- * Color components are stored as normalized values (0-1) for precision and
1882
- * compatibility with graphics APIs.
1883
- *
1884
- * @see {@link UIColorEvent} - Events emitted by color instances
1885
- * @see {@link UIColorName} - Supported named color values
1867
+ * Event-based RGBA color with support for RGB, HSL, hex, and named colors.
1868
+ * Color components are stored as normalized values (0-1).
1886
1869
  */
1887
1870
  class UIColor extends i {
1888
1871
  constructor(...args) {
1889
1872
  super();
1890
- /** Internal storage for the red component (0-1). */
1891
1873
  this._private_rInternal = 1;
1892
- /** Internal storage for the green component (0-1). */
1893
1874
  this._private_gInternal = 1;
1894
- /** Internal storage for the blue component (0-1). */
1895
1875
  this._private_bInternal = 1;
1896
- /** Internal storage for the alpha component (0-1). */
1897
1876
  this._private_aInternal = 1;
1898
1877
  if (args.length >= 3 && typeof args[0] === "number") {
1899
1878
  const [r, g, b, a = 1] = args;
@@ -1912,85 +1891,70 @@ class UIColor extends i {
1912
1891
  }
1913
1892
  }
1914
1893
  }
1915
- /**
1916
- * Gets the red component value.
1917
- * @returns The red component (0-1)
1918
- */
1894
+ /** Red component (0-1). */
1919
1895
  get r() {
1920
1896
  return this._private_rInternal;
1921
1897
  }
1922
- /**
1923
- * Gets the green component value.
1924
- * @returns The green component (0-1)
1925
- */
1898
+ /** Green component (0-1). */
1926
1899
  get g() {
1927
1900
  return this._private_gInternal;
1928
1901
  }
1929
- /**
1930
- * Gets the blue component value.
1931
- * @returns The blue component (0-1)
1932
- */
1902
+ /** Blue component (0-1). */
1933
1903
  get b() {
1934
1904
  return this._private_bInternal;
1935
1905
  }
1936
- /**
1937
- * Gets the alpha component value.
1938
- * @returns The alpha component (0-1)
1939
- */
1906
+ /** Alpha component (0-1). */
1940
1907
  get a() {
1941
1908
  return this._private_aInternal;
1942
1909
  }
1943
- /**
1944
- * Sets the red component value and emits change event.
1945
- * @param value - The red component (0-1)
1946
- */
1947
1910
  set r(value) {
1948
- this._private_rInternal = value;
1949
- this.emit(UIColorEvent.CHANGE, this);
1911
+ if (value !== this._private_rInternal) {
1912
+ this._private_rInternal = value;
1913
+ this.emit(UIColorEvent.CHANGE, this);
1914
+ }
1950
1915
  }
1951
- /**
1952
- * Sets the green component value and emits change event.
1953
- * @param value - The green component (0-1)
1954
- */
1955
1916
  set g(value) {
1956
- this._private_gInternal = value;
1957
- this.emit(UIColorEvent.CHANGE, this);
1917
+ if (value !== this._private_gInternal) {
1918
+ this._private_gInternal = value;
1919
+ this.emit(UIColorEvent.CHANGE, this);
1920
+ }
1958
1921
  }
1959
- /**
1960
- * Sets the blue component value and emits change event.
1961
- * @param value - The blue component (0-1)
1962
- */
1963
1922
  set b(value) {
1964
- this._private_bInternal = value;
1965
- this.emit(UIColorEvent.CHANGE, this);
1923
+ if (value !== this._private_bInternal) {
1924
+ this._private_bInternal = value;
1925
+ this.emit(UIColorEvent.CHANGE, this);
1926
+ }
1966
1927
  }
1967
- /**
1968
- * Sets the alpha component value and emits change event.
1969
- * @param value - The alpha component (0-1)
1970
- */
1971
1928
  set a(value) {
1972
- this._private_aInternal = value;
1973
- this.emit(UIColorEvent.CHANGE, this);
1929
+ if (value !== this._private_aInternal) {
1930
+ this._private_aInternal = value;
1931
+ this.emit(UIColorEvent.CHANGE, this);
1932
+ }
1974
1933
  }
1975
1934
  /**
1976
- * Sets all color components at once and emits change event.
1977
- * @param r - Red component (0-1)
1978
- * @param g - Green component (0-1)
1979
- * @param b - Blue component (0-1)
1980
- * @param a - Alpha component (0-1), defaults to current alpha value
1981
- * @returns This UIColor instance for method chaining
1935
+ * Sets all color components.
1936
+ *
1937
+ * @param r - Red (0-1)
1938
+ * @param g - Green (0-1)
1939
+ * @param b - Blue (0-1)
1940
+ * @param a - Alpha (0-1)
1941
+ * @returns This instance
1982
1942
  */
1983
1943
  set(r, g, b, a = this._private_aInternal) {
1984
- this._private_rInternal = r;
1985
- this._private_gInternal = g;
1986
- this._private_bInternal = b;
1987
- this._private_aInternal = a;
1988
- this.emit(UIColorEvent.CHANGE, this);
1944
+ if (r !== this._private_rInternal ||
1945
+ g !== this._private_gInternal ||
1946
+ b !== this._private_bInternal ||
1947
+ a !== this._private_aInternal) {
1948
+ this._private_rInternal = r;
1949
+ this._private_gInternal = g;
1950
+ this._private_bInternal = b;
1951
+ this._private_aInternal = a;
1952
+ this.emit(UIColorEvent.CHANGE, this);
1953
+ }
1989
1954
  return this;
1990
1955
  }
1991
1956
  /**
1992
- * Converts the color to a 32-bit hexadecimal RGBA value.
1993
- * @returns Hexadecimal RGBA value with alpha in the most significant byte
1957
+ * Returns 32-bit hex RGBA value (alpha in MSB).
1994
1958
  */
1995
1959
  getHexRGBA() {
1996
1960
  return ((Math.round(this._private_rInternal * 255) << 24) |
@@ -1999,8 +1963,7 @@ class UIColor extends i {
1999
1963
  Math.round(this._private_aInternal * 255));
2000
1964
  }
2001
1965
  /**
2002
- * Converts the color to a 24-bit hexadecimal RGB value (ignores alpha).
2003
- * @returns Hexadecimal RGB value (e.g., 0xFF0000 for red)
1966
+ * Returns 24-bit hex RGB value.
2004
1967
  */
2005
1968
  getHexRGB() {
2006
1969
  return ((Math.round(this._private_rInternal * 255) << 16) |
@@ -2008,8 +1971,9 @@ class UIColor extends i {
2008
1971
  Math.round(this._private_bInternal * 255));
2009
1972
  }
2010
1973
  /**
2011
- * Converts the color to HSL (Hue, Saturation, Lightness) format.
2012
- * @returns Object containing h (0-360), s (0-1), and l (0-1) values
1974
+ * Converts to HSL format.
1975
+ *
1976
+ * @returns Object with h (0-360), s (0-1), l (0-1)
2013
1977
  */
2014
1978
  getHSL() {
2015
1979
  const { _private_rInternal: r, _private_gInternal: g, _private_bInternal: b } = this;
@@ -2034,39 +1998,59 @@ class UIColor extends i {
2034
1998
  return { h, s, l };
2035
1999
  }
2036
2000
  /**
2037
- * Sets the color from a 32-bit hexadecimal RGBA value and emits change event.
2038
- * @param hex - Hexadecimal RGBA value with alpha in the most significant byte
2039
- * @returns This UIColor instance for method chaining
2001
+ * Sets color from 32-bit hex RGBA value.
2002
+ *
2003
+ * @param hex - Hex RGBA (alpha in MSB)
2004
+ * @returns This instance
2040
2005
  */
2041
2006
  setHexRGBA(hex) {
2042
- this._private_rInternal = ((hex >> 16) & 0xff) / 255;
2043
- this._private_gInternal = ((hex >> 8) & 0xff) / 255;
2044
- this._private_bInternal = (hex & 0xff) / 255;
2045
- this._private_aInternal = ((hex >> 24) & 0xff) / 255;
2046
- this.emit(UIColorEvent.CHANGE, this);
2007
+ const r = ((hex >> 16) & 0xff) / 255;
2008
+ const g = ((hex >> 8) & 0xff) / 255;
2009
+ const b = (hex & 0xff) / 255;
2010
+ const a = ((hex >> 24) & 0xff) / 255;
2011
+ if (r !== this._private_rInternal ||
2012
+ g !== this._private_gInternal ||
2013
+ b !== this._private_bInternal ||
2014
+ a !== this._private_aInternal) {
2015
+ this._private_rInternal = r;
2016
+ this._private_gInternal = g;
2017
+ this._private_bInternal = b;
2018
+ this._private_aInternal = a;
2019
+ this.emit(UIColorEvent.CHANGE, this);
2020
+ }
2047
2021
  return this;
2048
2022
  }
2049
2023
  /**
2050
- * Sets the color from a 24-bit hexadecimal RGB value and emits change event.
2051
- * @param hex - Hexadecimal RGB value (e.g., 0xFF0000 for red)
2052
- * @param a - Alpha value (0-1), defaults to current alpha
2053
- * @returns This UIColor instance for method chaining
2024
+ * Sets color from 24-bit hex RGB value.
2025
+ *
2026
+ * @param hex - Hex RGB (e.g., 0xFF0000)
2027
+ * @param a - Alpha (0-1)
2028
+ * @returns This instance
2054
2029
  */
2055
2030
  setHexRGB(hex, a = this._private_aInternal) {
2056
- this._private_rInternal = ((hex >> 16) & 0xff) / 255;
2057
- this._private_gInternal = ((hex >> 8) & 0xff) / 255;
2058
- this._private_bInternal = (hex & 0xff) / 255;
2059
- this._private_aInternal = a;
2060
- this.emit(UIColorEvent.CHANGE, this);
2031
+ const r = ((hex >> 16) & 0xff) / 255;
2032
+ const g = ((hex >> 8) & 0xff) / 255;
2033
+ const b = (hex & 0xff) / 255;
2034
+ if (r !== this._private_rInternal ||
2035
+ g !== this._private_gInternal ||
2036
+ b !== this._private_bInternal ||
2037
+ a !== this._private_aInternal) {
2038
+ this._private_rInternal = r;
2039
+ this._private_gInternal = g;
2040
+ this._private_bInternal = b;
2041
+ this._private_aInternal = a;
2042
+ this.emit(UIColorEvent.CHANGE, this);
2043
+ }
2061
2044
  return this;
2062
2045
  }
2063
2046
  /**
2064
- * Sets the color from HSL (Hue, Saturation, Lightness) values and emits change event.
2065
- * @param h - Hue value (0-360)
2066
- * @param s - Saturation value (0-1)
2067
- * @param l - Lightness value (0-1)
2068
- * @param a - Alpha value (0-1), defaults to current alpha
2069
- * @returns This UIColor instance for method chaining
2047
+ * Sets color from HSL values.
2048
+ *
2049
+ * @param h - Hue (0-360)
2050
+ * @param s - Saturation (0-1)
2051
+ * @param l - Lightness (0-1)
2052
+ * @param a - Alpha (0-1)
2053
+ * @returns This instance
2070
2054
  */
2071
2055
  setHSL(h, s, l, a = this._private_aInternal) {
2072
2056
  const c = (1 - Math.abs(2 * l - 1)) * s;
@@ -2087,30 +2071,41 @@ class UIColor extends i {
2087
2071
  return this.set(r + m, g + m, b + m, a);
2088
2072
  }
2089
2073
  /**
2090
- * Sets the color from a predefined color name and emits change event.
2091
- * @param colorName - Name of the color from the predefined color database
2092
- * @param a - Alpha value (0-1), defaults to current alpha
2093
- * @returns This UIColor instance for method chaining
2094
- * @throws Will throw an error if the color name is not recognized
2074
+ * Sets color from predefined name.
2075
+ *
2076
+ * @param colorName - Color name
2077
+ * @param a - Alpha (0-1)
2078
+ * @returns This instance
2079
+ * @throws Error if color name is unknown
2095
2080
  */
2096
2081
  setName(colorName, a = this._private_aInternal) {
2097
2082
  const normalizedName = colorName.toLowerCase().trim();
2098
- const hex = COLOR_NAMES[normalizedName];
2083
+ const hex = COLORS[normalizedName];
2099
2084
  if (hex === undefined) {
2100
2085
  throw new Error(`Unknown color name: "${colorName}".`);
2101
2086
  }
2102
2087
  return this.setHexRGB(hex, a);
2103
2088
  }
2104
2089
  /**
2105
- * Copies color values from another UIColor or Three.js Color instance and emits change event.
2106
- * @param color - The color to copy from (UIColor or Three.js Color)
2107
- * @returns This UIColor instance for method chaining
2090
+ * Returns CSS rgba() string.
2091
+ */
2092
+ toCSSColor() {
2093
+ const r = Math.round(this._private_rInternal * 255);
2094
+ const g = Math.round(this._private_gInternal * 255);
2095
+ const b = Math.round(this._private_bInternal * 255);
2096
+ return `rgba(${r}, ${g}, ${b}, ${this._private_aInternal})`;
2097
+ }
2098
+ /**
2099
+ * Copies values from another color.
2100
+ *
2101
+ * @param color - UIColor or Three.js Color
2102
+ * @returns This instance
2108
2103
  */
2109
2104
  copy(color) {
2110
2105
  if (color instanceof UIColor) {
2111
- if (this._private_rInternal !== color._private_rInternal &&
2112
- this._private_gInternal !== color._private_gInternal &&
2113
- this._private_bInternal !== color._private_bInternal &&
2106
+ if (this._private_rInternal !== color._private_rInternal ||
2107
+ this._private_gInternal !== color._private_gInternal ||
2108
+ this._private_bInternal !== color._private_bInternal ||
2114
2109
  this._private_aInternal !== color._private_aInternal) {
2115
2110
  this._private_rInternal = color._private_rInternal;
2116
2111
  this._private_gInternal = color._private_gInternal;
@@ -2120,9 +2115,9 @@ class UIColor extends i {
2120
2115
  }
2121
2116
  }
2122
2117
  else {
2123
- if (this._private_rInternal !== color.r &&
2124
- this._private_gInternal !== color.g &&
2125
- this._private_bInternal !== color.b &&
2118
+ if (this._private_rInternal !== color.r ||
2119
+ this._private_gInternal !== color.g ||
2120
+ this._private_bInternal !== color.b ||
2126
2121
  this._private_aInternal !== 1) {
2127
2122
  this._private_rInternal = color.r;
2128
2123
  this._private_gInternal = color.g;
@@ -2134,18 +2129,7 @@ class UIColor extends i {
2134
2129
  return this;
2135
2130
  }
2136
2131
  /**
2137
- * Converts the color to a CSS rgba() string.
2138
- * @returns CSS rgba string (e.g., "rgba(255, 0, 0, 1)")
2139
- */
2140
- toCSSColor() {
2141
- const r = Math.round(this._private_rInternal * 255);
2142
- const g = Math.round(this._private_gInternal * 255);
2143
- const b = Math.round(this._private_bInternal * 255);
2144
- return `rgba(${r}, ${g}, ${b}, ${this._private_aInternal})`;
2145
- }
2146
- /**
2147
- * Creates a new UIColor instance with identical color values.
2148
- * @returns A new UIColor instance with the same RGBA values
2132
+ * Creates a copy of this color.
2149
2133
  */
2150
2134
  clone() {
2151
2135
  return new UIColor(this._private_rInternal, this._private_gInternal, this._private_bInternal, this._private_aInternal);
@@ -2185,6 +2169,7 @@ var fragmentShader = "in vec2 io_UV;\n\nuniform float alphaTest;\n\n#include <co
2185
2169
  var vertexShader = "out vec2 io_UV;\n#include <common>\n\nvoid main() {\n #include <begin_vertex>\n io_UV = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n";
2186
2170
 
2187
2171
  const DEFAULT_ALPHA_TEST = 0.25;
2172
+ const COLOR = new Color();
2188
2173
  const UNIFROM_TYPE_MAP = new Map([
2189
2174
  [UIColor, "vec4"],
2190
2175
  [Color, "vec3"],
@@ -2274,7 +2259,9 @@ class UISceneWrapper {
2274
2259
  throw new Error(`Uniform ${uniform} not found in material`);
2275
2260
  }
2276
2261
  if (value instanceof UIColor) {
2277
- descriptor.material.uniforms[uniform].value.set(value.r, value.g, value.b, value.a);
2262
+ const linear = COLOR.set(value.r, value.g, value.b);
2263
+ linear.convertSRGBToLinear();
2264
+ descriptor.material.uniforms[uniform].value.set(linear.r, linear.g, linear.b, value.a);
2278
2265
  }
2279
2266
  else {
2280
2267
  descriptor.material.uniforms[uniform].value = value;
@@ -6707,6 +6694,7 @@ class UIResizePolicyNone extends UIResizePolicy {
6707
6694
  }
6708
6695
  }
6709
6696
 
6697
+ const TEMP_POSITION = new Vector3();
6710
6698
  /**
6711
6699
  * UI layer that covers the full browser window.
6712
6700
  *
@@ -6792,10 +6780,24 @@ class UIFullscreenLayer extends UILayer {
6792
6780
  * @returns 2D position in layer space
6793
6781
  */
6794
6782
  projectWorldPosition(position, camera) {
6795
- const projectedPosition = position.project(camera);
6783
+ const projectedPosition = TEMP_POSITION.copy(position).project(camera);
6796
6784
  projectedPosition.x = MathUtils.mapLinear(projectedPosition.x, -1, 1, this.x, this.width);
6797
6785
  projectedPosition.y = MathUtils.mapLinear(projectedPosition.y, -1, 1, this.y, this.height);
6798
- return projectedPosition;
6786
+ return new Vector2(projectedPosition.x, projectedPosition.y);
6787
+ }
6788
+ /**
6789
+ * Projects 2D layer position to 3D world coordinates.
6790
+ *
6791
+ * @param position - 2D position in layer space
6792
+ * @param camera - Camera for unprojection
6793
+ * @param z - z in NDC space (defaults to -1)
6794
+ * @returns 3D position in world space
6795
+ */
6796
+ projectLayerPosition(position, camera, z = -1) {
6797
+ TEMP_POSITION.x = MathUtils.mapLinear(position.x, this.x, this.width, -1, 1);
6798
+ TEMP_POSITION.y = MathUtils.mapLinear(position.y, this.y, this.height, -1, 1);
6799
+ TEMP_POSITION.z = z;
6800
+ return TEMP_POSITION.unproject(camera);
6799
6801
  }
6800
6802
  _private_handleWindowInput(event, pointerFunctionName, inputEvent) {
6801
6803
  const rect = event.target instanceof HTMLElement
@@ -6808,10 +6810,10 @@ class UIFullscreenLayer extends UILayer {
6808
6810
  const scale = this._private_resizePolicyInternal["calculateScaleInternal"](window.innerWidth, window.innerHeight);
6809
6811
  const scaledX = x * scale;
6810
6812
  const scaledY = y * scale;
6811
- this[pointerFunctionName](scaledX, scaledY, event.pointerId);
6812
6813
  if (this.mode === UIMode.INTERACTIVE) {
6813
6814
  this.emit(inputEvent, scaledX, scaledY, event.pointerId);
6814
6815
  }
6816
+ this[pointerFunctionName](scaledX, scaledY, event.pointerId);
6815
6817
  }
6816
6818
  }
6817
6819
 
@@ -6984,9 +6986,6 @@ function buildMaterial(source, uniformLayout, varyingLayout, transparency) {
6984
6986
  gl_FragColor = linearToOutputTexel(diffuseColor);
6985
6987
  }
6986
6988
  `;
6987
- console.log(vertexShader);
6988
- console.log(fragmentShader);
6989
- console.log(uniforms);
6990
6989
  return new ShaderMaterial({
6991
6990
  uniforms,
6992
6991
  vertexShader,