laymur 0.2.3 → 0.2.4

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/README.md CHANGED
@@ -37,6 +37,10 @@ npm install laymur
37
37
  - Three.js ^0.175.0 (peer dependency)
38
38
  - Modern JavaScript environment with ES2020+ support
39
39
 
40
+ ## Live Examples
41
+
42
+ 🎮 **[Live Examples](https://jango-git.github.io/laymur/)** - Explore constraint-based layouts, proportional positioning, and responsive UI components in action.
43
+
40
44
  ## Quick Start
41
45
 
42
46
  ```typescript
@@ -0,0 +1,11 @@
1
+ import type { Texture, WebGLRenderer } from "three";
2
+ import { Mesh } from "three";
3
+ import type { UILayer } from "../layers/UILayer";
4
+ import { UIElement } from "./UIElement";
5
+ export declare class UINineSlice extends UIElement<Mesh> {
6
+ private readonly material;
7
+ private readonly textureInternal;
8
+ constructor(layer: UILayer, texture: Texture, x?: number, y?: number);
9
+ destroy(): void;
10
+ protected ["onBeforeRenderInternal"](renderer: WebGLRenderer, deltaTime: number): void;
11
+ }
package/dist/index.d.ts CHANGED
@@ -13,6 +13,7 @@ export * from "./elements/UIAnchor";
13
13
  export * from "./elements/UIDummy";
14
14
  export * from "./elements/UIElement";
15
15
  export * from "./elements/UIImage";
16
+ export * from "./elements/UINineSline";
16
17
  export * from "./elements/UIProgress";
17
18
  export * from "./elements/UIScene";
18
19
  export * from "./elements/UIText";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Color, Scene, OrthographicCamera, MathUtils, ShaderMaterial, PlaneGeometry, Mesh, UniformsUtils, WebGLRenderTarget, UnsignedByteType, LinearFilter, RGBAFormat, PerspectiveCamera, CanvasTexture } from 'three';
1
+ import { Color, Scene, OrthographicCamera, MathUtils, ShaderMaterial, PlaneGeometry, Mesh, Vector2, Vector4, UniformsUtils, WebGLRenderTarget, UnsignedByteType, LinearFilter, RGBAFormat, PerspectiveCamera, CanvasTexture } from 'three';
2
2
 
3
3
  /**
4
4
  * Mathematical expression builder for constraint equations in the UI layout system.
@@ -3999,7 +3999,7 @@ class UIElement extends UIAnchor {
3999
3999
  deltaTime) { }
4000
4000
  }
4001
4001
 
4002
- const DEFAULT_ALPHA_TEST = 0.75;
4002
+ const DEFAULT_ALPHA_TEST$1 = 0.75;
4003
4003
  class UIMaterial extends ShaderMaterial {
4004
4004
  constructor(map) {
4005
4005
  super({
@@ -4007,7 +4007,7 @@ class UIMaterial extends ShaderMaterial {
4007
4007
  map: { value: map },
4008
4008
  opacity: { value: 1.0 },
4009
4009
  color: { value: new Color(1.0, 1.0, 1.0) },
4010
- alphaTest: { value: DEFAULT_ALPHA_TEST },
4010
+ alphaTest: { value: DEFAULT_ALPHA_TEST$1 },
4011
4011
  },
4012
4012
  vertexShader: /* glsl */ `
4013
4013
  varying vec2 vUv;
@@ -4031,7 +4031,7 @@ class UIMaterial extends ShaderMaterial {
4031
4031
  }
4032
4032
  `,
4033
4033
  transparent: false,
4034
- alphaTest: DEFAULT_ALPHA_TEST,
4034
+ alphaTest: DEFAULT_ALPHA_TEST$1,
4035
4035
  lights: false,
4036
4036
  fog: false,
4037
4037
  depthWrite: false,
@@ -4068,7 +4068,7 @@ class UIMaterial extends ShaderMaterial {
4068
4068
  setTransparency(value) {
4069
4069
  if (this.transparent !== value) {
4070
4070
  this.transparent = value;
4071
- this.uniforms.alphaTest.value = value ? DEFAULT_ALPHA_TEST : 0;
4071
+ this.uniforms.alphaTest.value = value ? DEFAULT_ALPHA_TEST$1 : 0;
4072
4072
  this.needsUpdate = true;
4073
4073
  this.uniformsNeedUpdate = true;
4074
4074
  }
@@ -4194,6 +4194,160 @@ class UIImage extends UIElement {
4194
4194
  }
4195
4195
  }
4196
4196
 
4197
+ const DEFAULT_ALPHA_TEST = 0.025;
4198
+ const DEFAULT_SLICE_BORDER = 0.25;
4199
+ class UINineSliceMaterial extends ShaderMaterial {
4200
+ constructor(texture) {
4201
+ super({
4202
+ uniforms: {
4203
+ map: { value: texture },
4204
+ color: { value: new Color(0xffffff) },
4205
+ opacity: { value: 1.0 },
4206
+ alphaTest: { value: DEFAULT_ALPHA_TEST },
4207
+ sliceBorders: {
4208
+ value: new Vector4(DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER),
4209
+ }, // left, right, top, bottom (в UV)
4210
+ atlasRect: { value: new Vector4(0, 0, 1, 1) }, // x, y, w, h (в UV)
4211
+ quadSize: { value: new Vector2(1, 1) },
4212
+ textureSize: {
4213
+ value: new Vector2(texture.image.width, texture.image.height),
4214
+ },
4215
+ },
4216
+ vertexShader: /* glsl */ `
4217
+ varying vec2 vUv;
4218
+ void main() {
4219
+ vUv = uv;
4220
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
4221
+ }
4222
+ `,
4223
+ fragmentShader: /* glsl */ `
4224
+ uniform sampler2D map;
4225
+ uniform vec3 color;
4226
+ uniform float opacity;
4227
+ uniform float alphaTest;
4228
+ uniform vec4 sliceBorders;
4229
+ uniform vec4 atlasRect;
4230
+ uniform vec2 quadSize;
4231
+ uniform vec2 textureSize;
4232
+ varying vec2 vUv;
4233
+
4234
+ vec2 calcNineSliceUV(vec2 uv) {
4235
+ vec2 localUV = uv;
4236
+
4237
+ float left = sliceBorders.x;
4238
+ float right = sliceBorders.y;
4239
+ float top = sliceBorders.z;
4240
+ float bottom = sliceBorders.w;
4241
+
4242
+ vec2 texPx = textureSize * atlasRect.zw;
4243
+
4244
+ float leftPx = left * texPx.x;
4245
+ float rightPx = right * texPx.x;
4246
+ float topPx = top * texPx.y;
4247
+ float bottomPx = bottom * texPx.y;
4248
+
4249
+ float lB = leftPx / quadSize.x;
4250
+ float rB = 1.0 - (rightPx / quadSize.x);
4251
+ float tB = topPx / quadSize.y;
4252
+ float bB = 1.0 - (bottomPx / quadSize.y);
4253
+
4254
+ float regionL = step(uv.x, lB);
4255
+ float regionR = step(rB, uv.x);
4256
+ float regionM = 1.0 - regionL - regionR;
4257
+
4258
+ float xL = uv.x * (left / lB);
4259
+ float xR = 1.0 - right + ((uv.x - rB) / (1.0 - rB)) * right;
4260
+ float xM = left + ((uv.x - lB) / (rB - lB)) * (1.0 - left - right);
4261
+
4262
+ localUV.x = regionL * xL + regionM * xM + regionR * xR;
4263
+
4264
+ float regionT = step(uv.y, tB);
4265
+ float regionBtm = step(bB, uv.y);
4266
+ float regionC = 1.0 - regionT - regionBtm;
4267
+
4268
+ float yT = uv.y * (top / tB);
4269
+ float yB = 1.0 - bottom + ((uv.y - bB) / (1.0 - bB)) * bottom;
4270
+ float yC = top + ((uv.y - tB) / (bB - tB)) * (1.0 - top - bottom);
4271
+
4272
+ localUV.y = regionT * yT + regionC * yC + regionBtm * yB;
4273
+
4274
+ return atlasRect.xy + localUV * atlasRect.zw;
4275
+ }
4276
+
4277
+ void main() {
4278
+ vec2 uv = calcNineSliceUV(vUv);
4279
+ vec4 texel = texture2D(map, uv);
4280
+ if (texel.a < alphaTest) discard;
4281
+ gl_FragColor = vec4(texel.rgb * color, texel.a * opacity);
4282
+ #include <colorspace_fragment>
4283
+ }
4284
+ `,
4285
+ transparent: false,
4286
+ alphaTest: DEFAULT_ALPHA_TEST,
4287
+ lights: false,
4288
+ fog: false,
4289
+ depthWrite: true,
4290
+ depthTest: true,
4291
+ });
4292
+ }
4293
+ getTexture() {
4294
+ return this.uniforms.map.value;
4295
+ }
4296
+ getColor() {
4297
+ return this.uniforms.color.value.getHex();
4298
+ }
4299
+ getOpacity() {
4300
+ return this.uniforms.opacity.value;
4301
+ }
4302
+ getTransparency() {
4303
+ return this.transparent;
4304
+ }
4305
+ setSliceBorders(l, r, t, b) {
4306
+ this.uniforms.sliceBorders.value.set(l, r, t, b);
4307
+ this.uniformsNeedUpdate = true;
4308
+ return this;
4309
+ }
4310
+ setQuadSize(width, height) {
4311
+ this.uniforms.quadSize.value.set(width, height);
4312
+ this.uniformsNeedUpdate = true;
4313
+ return this;
4314
+ }
4315
+ setAtlasRect(x, y, w, h) {
4316
+ this.uniforms.atlasRect.value.set(x, y, w, h);
4317
+ this.uniformsNeedUpdate = true;
4318
+ return this;
4319
+ }
4320
+ setColor(color) {
4321
+ this.uniforms.color.value.copy(color);
4322
+ this.uniformsNeedUpdate = true;
4323
+ return this;
4324
+ }
4325
+ setOpacity(opacity) {
4326
+ this.uniforms.opacity.value = opacity;
4327
+ this.uniformsNeedUpdate = true;
4328
+ return this;
4329
+ }
4330
+ }
4331
+
4332
+ class UINineSlice extends UIElement {
4333
+ constructor(layer, texture, x = 0, y = 0) {
4334
+ const w = texture.image.width;
4335
+ const h = texture.image.height;
4336
+ const material = new UINineSliceMaterial(texture);
4337
+ const object = new Mesh(geometry, material);
4338
+ super(layer, x, y, w, h, object);
4339
+ this._private_material = material;
4340
+ this._private_textureInternal = texture;
4341
+ }
4342
+ destroy() {
4343
+ this._private_material.dispose();
4344
+ super.destroy();
4345
+ }
4346
+ ["onBeforeRenderInternal"](renderer, deltaTime) {
4347
+ this._private_material.setQuadSize(this.width * this.micro.scaleX, this.height * this.micro.scaleY);
4348
+ }
4349
+ }
4350
+
4197
4351
  class UIProgressMaterial extends ShaderMaterial {
4198
4352
  constructor(foregroundTexture, backgroundTexture) {
4199
4353
  super({
@@ -5203,6 +5357,8 @@ class UIText extends UIElement {
5203
5357
  size.width + this._private_paddingInternal.left + this._private_paddingInternal.right;
5204
5358
  this._private_canvas.height =
5205
5359
  size.height + this._private_paddingInternal.top + this._private_paddingInternal.bottom;
5360
+ this.width = this._private_canvas.width;
5361
+ this.height = this._private_canvas.height;
5206
5362
  this._private_targetAspectRatio =
5207
5363
  (size.width + this._private_paddingInternal.left + this._private_paddingInternal.right) /
5208
5364
  (size.height + this._private_paddingInternal.top + this._private_paddingInternal.bottom);
@@ -5399,5 +5555,5 @@ function isUITextSpan(obj) {
5399
5555
  (!("style" in obj) || isUITextStyle(obj.style)));
5400
5556
  }
5401
5557
 
5402
- export { UIAnchor, UIAspectConstraint, UIConstraint, UIConstraint2DBuilder, UICustomConstraint, UIDummy, UIElement, UIElementEvent, UIExpression, UIFullscreenLayer, UIHeightConstraint, UIHorizontalDistanceConstraint, UIHorizontalProportionConstraint, UIImage, UILayer, UILayerEvent, UIMicro, UIMode, UIOrientation, UIPriority, UIProgress, UIRelation, UIScene, UISceneUpdateMode, UISingleParameterConstraint, UIText, UIVerticalDistanceConstraint, UIVerticalProportionConstraint, UIWidthConstraint, convertPriority, convertRelation, isUITextSpan, isUITextStyle, resolveOrientation, resolvePadding, resolvePriority, resolveRelation, resolveTextStyle };
5558
+ export { UIAnchor, 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 };
5403
5559
  //# sourceMappingURL=index.js.map