laymur 0.25.2 → 0.27.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/README.md +114 -34
- package/dist/core/constraints/UICustomConstraint.d.ts +1 -1
- package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +1 -1
- package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +1 -1
- package/dist/core/constraints/builders/UIConstraint2DBuilder.d.ts +2 -3
- package/dist/core/elements/UIAnchor/UIAnchor.d.ts +2 -2
- package/dist/core/elements/UIAnchor/UIAnchor.js +3 -2
- package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.d.ts +2 -2
- package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.js +24 -12
- package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +13 -0
- package/dist/core/elements/UIDummy/UIDummy.d.ts +74 -5
- package/dist/core/elements/UIDummy/UIDummy.js +222 -29
- package/dist/core/elements/UIElement/UIElement.Internal.d.ts +1 -1
- package/dist/core/elements/UIElement/UIElement.d.ts +37 -1
- package/dist/core/elements/UIElement/UIElement.js +84 -6
- package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +1 -1
- package/dist/core/elements/UIGraphics/UIGraphics.d.ts +1 -1
- package/dist/core/elements/UIGraphics/UIGraphics.js +4 -4
- package/dist/core/elements/UIImage/UIImage.d.ts +2 -2
- package/dist/core/elements/UIImage/UIImage.js +10 -8
- package/dist/core/elements/UINineSlice/UINineSlice.d.ts +4 -4
- package/dist/core/elements/UINineSlice/UINineSlice.js +11 -9
- package/dist/core/elements/UIProgress/UIProgress.d.ts +2 -2
- package/dist/core/elements/UIProgress/UIProgress.js +17 -15
- package/dist/core/elements/UIScene/UIScene.Internal.d.ts +1 -1
- package/dist/core/elements/UIScene/UIScene.d.ts +3 -2
- package/dist/core/elements/UIScene/UIScene.js +21 -14
- package/dist/core/elements/UIText/UIText.Measuring.d.ts +1 -1
- package/dist/core/elements/UIText/UIText.d.ts +3 -3
- package/dist/core/elements/UIText/UIText.js +8 -8
- package/dist/core/index.d.ts +60 -11
- package/dist/core/index.js +22 -7
- package/dist/core/layers/UILayer/UILayer.d.ts +45 -5
- package/dist/core/layers/UILayer/UILayer.js +103 -10
- package/dist/core/layers/UIViewportLayer/UIViewportLayer.Internal.d.ts +27 -0
- package/dist/core/layers/{UIFullscreenLayer/UIFullscreenLayer.Internal.js → UIViewportLayer/UIViewportLayer.Internal.js} +1 -1
- package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +98 -0
- package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +251 -0
- package/dist/core/miscellaneous/UIAffineMatrix3.d.ts +13 -0
- package/dist/core/miscellaneous/UIAffineMatrix3.js +13 -0
- package/dist/core/miscellaneous/area/UIArea.d.ts +10 -1
- package/dist/core/miscellaneous/area/UIArea.js +4 -1
- package/dist/core/miscellaneous/area/UIAreaCircle.d.ts +1 -0
- package/dist/core/miscellaneous/area/UIAreaCircle.js +0 -1
- package/dist/core/miscellaneous/area/UIAreaPolygon.d.ts +1 -0
- package/dist/core/miscellaneous/area/UIAreaPolygon.js +0 -1
- package/dist/core/miscellaneous/area/UIAreaRectangle.d.ts +1 -0
- package/dist/core/miscellaneous/area/UIAreaRectangle.js +0 -1
- package/dist/core/miscellaneous/area/UIAreaRoundedRectangle.d.ts +1 -0
- package/dist/core/miscellaneous/area/UIAreaRoundedRectangle.js +0 -1
- package/dist/core/miscellaneous/asserts.d.ts +23 -0
- package/dist/core/miscellaneous/asserts.js +44 -0
- package/dist/core/miscellaneous/canvas.js +7 -2
- package/dist/core/miscellaneous/color/UIColor.js +5 -2
- package/dist/core/miscellaneous/computeTransform.d.ts +12 -0
- package/dist/core/miscellaneous/computeTransform.js +20 -0
- package/dist/core/miscellaneous/context-monitor/UIContextMonitor.d.ts +1 -0
- package/dist/core/miscellaneous/context-monitor/UIContextMonitor.js +60 -0
- package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.d.ts +17 -3
- package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.js +88 -26
- package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Layout.d.ts +9 -0
- package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Layout.js +29 -6
- package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.js +51 -11
- package/dist/core/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.Internal.d.ts +2 -0
- package/dist/core/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.js +47 -6
- package/dist/core/miscellaneous/generic-plane/shared.d.ts +15 -3
- package/dist/core/miscellaneous/generic-plane/shared.js +38 -7
- package/dist/core/miscellaneous/generic-plane/varyingPacking.d.ts +46 -0
- package/dist/core/miscellaneous/generic-plane/varyingPacking.js +170 -0
- package/dist/core/miscellaneous/mask/UIMask.Internal.d.ts +61 -0
- package/dist/core/miscellaneous/mask/UIMask.Internal.js +56 -0
- package/dist/core/miscellaneous/mask/UIMask.d.ts +84 -0
- package/dist/core/miscellaneous/mask/UIMask.js +265 -0
- package/dist/core/miscellaneous/mask/UIMaskMaterialPatch.d.ts +50 -0
- package/dist/core/miscellaneous/mask/UIMaskMaterialPatch.js +233 -0
- package/dist/core/miscellaneous/mask-function/UIProgressMaskFunction.d.ts +10 -1
- package/dist/core/miscellaneous/mask-function/UIProgressMaskFunction.js +7 -1
- package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.d.ts +2 -0
- package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.js +0 -1
- package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.d.ts +2 -0
- package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.js +0 -1
- package/dist/core/miscellaneous/micro/UIMicro.Internal.d.ts +3 -0
- package/dist/core/miscellaneous/micro/UIMicro.Internal.js +1 -0
- package/dist/core/miscellaneous/micro/UIMicro.d.ts +18 -1
- package/dist/core/miscellaneous/micro/UIMicro.js +23 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicy.d.ts +19 -1
- package/dist/core/miscellaneous/resize-policy/UIResizePolicy.js +16 -3
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyCover.js +2 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyCross.js +2 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyCrossInverted.js +2 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyFit.js +2 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedHeight.js +2 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedWidth.js +2 -2
- package/dist/core/miscellaneous/text-style/UITextStyle.js +2 -2
- package/dist/core/miscellaneous/texture/UITextureView.d.ts +6 -2
- package/dist/core/miscellaneous/texture/UITextureView.js +13 -9
- package/dist/core/miscellaneous/viewport/UIElementViewport.d.ts +31 -0
- package/dist/core/miscellaneous/viewport/UIElementViewport.js +54 -0
- package/dist/core/miscellaneous/viewport/UIViewport.d.ts +37 -0
- package/dist/core/miscellaneous/viewport/UIViewport.js +31 -0
- package/dist/core/miscellaneous/viewport/UIWindowViewport.d.ts +13 -0
- package/dist/core/miscellaneous/viewport/UIWindowViewport.js +25 -0
- package/dist/core/miscellaneous/webglCapabilities.js +61 -55
- package/dist/core/shaders/UIMask.d.ts +15 -0
- package/dist/core/shaders/UIMask.js +45 -0
- package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +36 -5
- package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +23 -2
- package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +153 -15
- package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.Internal.d.ts +3 -1
- package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.d.ts +1 -0
- package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.js +3 -0
- package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +2 -0
- package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +3 -0
- package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +36 -16
- package/dist/debug/UIAspectDebug.js +4 -0
- package/dist/debug/UIHeightDebug.js +4 -0
- package/dist/debug/UIHorizontalDistanceDebug.js +5 -0
- package/dist/debug/UIHorizontalInterpolationDebug.js +5 -0
- package/dist/debug/UILayerDebug.d.ts +7 -0
- package/dist/debug/UILayerDebug.js +21 -0
- package/dist/debug/UIVerticalDistanceDebug.js +5 -0
- package/dist/debug/UIVerticalInterpolationDebug.js +5 -0
- package/dist/debug/UIWidthDebug.js +4 -0
- package/dist/debug/index.d.ts +2 -0
- package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.d.ts +8 -1
- package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.js +0 -2
- package/dist/particles/behaviorModules/UIBehaviorModule.d.ts +12 -2
- package/dist/particles/behaviorModules/UIBehaviorModule.js +2 -1
- package/dist/particles/behaviorModules/UIBehaviorPointGravity.d.ts +8 -1
- package/dist/particles/behaviorModules/UIBehaviorPointGravity.js +13 -12
- package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.d.ts +7 -0
- package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.js +6 -6
- package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.d.ts +7 -0
- package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.js +4 -5
- package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.d.ts +8 -1
- package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.js +8 -10
- package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.d.ts +10 -2
- package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.js +8 -7
- package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.d.ts +7 -0
- package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.js +8 -10
- package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.d.ts +13 -4
- package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.js +12 -10
- package/dist/particles/elements/UIEmitter.Internal.d.ts +21 -1
- package/dist/particles/elements/UIEmitter.Internal.js +6 -0
- package/dist/particles/elements/UIEmitter.d.ts +20 -11
- package/dist/particles/elements/UIEmitter.js +100 -54
- package/dist/particles/index.d.ts +7 -1
- package/dist/particles/index.js +4 -0
- package/dist/particles/instancedParticle/UIInstancedParticle.Internal.d.ts +19 -4
- package/dist/particles/instancedParticle/UIInstancedParticle.Internal.js +117 -83
- package/dist/particles/instancedParticle/UIInstancedParticle.d.ts +8 -0
- package/dist/particles/instancedParticle/UIInstancedParticle.js +41 -4
- package/dist/particles/miscellaneous/miscellaneous.d.ts +34 -0
- package/dist/particles/miscellaneous/miscellaneous.js +72 -0
- package/dist/particles/renderingModules/UIRenderingColorOverLife.d.ts +4 -0
- package/dist/particles/renderingModules/UIRenderingColorOverLife.js +2 -4
- package/dist/particles/renderingModules/UIRenderingColorOverVelocity.d.ts +4 -0
- package/dist/particles/renderingModules/UIRenderingColorOverVelocity.js +2 -5
- package/dist/particles/renderingModules/UIRenderingModule.d.ts +11 -1
- package/dist/particles/renderingModules/UIRenderingModule.js +4 -1
- package/dist/particles/renderingModules/UIRenderingTexture.d.ts +4 -0
- package/dist/particles/renderingModules/UIRenderingTexture.js +2 -1
- package/dist/particles/shaders/UIRenderingColorOverLife.d.ts +1 -2
- package/dist/particles/shaders/UIRenderingColorOverLife.js +5 -3
- package/dist/particles/shaders/UIRenderingColorOverVelocity.d.ts +1 -9
- package/dist/particles/shaders/UIRenderingColorOverVelocity.js +6 -13
- package/dist/particles/spawnModules/UISpawnModule.d.ts +16 -1
- package/dist/particles/spawnModules/UISpawnModule.js +3 -1
- package/dist/particles/spawnModules/UISpawnOffset.d.ts +7 -0
- package/dist/particles/spawnModules/UISpawnOffset.js +0 -2
- package/dist/particles/spawnModules/UISpawnRandomLifetime.d.ts +8 -1
- package/dist/particles/spawnModules/UISpawnRandomLifetime.js +5 -7
- package/dist/particles/spawnModules/UISpawnRandomRotation.d.ts +7 -0
- package/dist/particles/spawnModules/UISpawnRandomRotation.js +5 -5
- package/dist/particles/spawnModules/UISpawnRandomScale.d.ts +7 -0
- package/dist/particles/spawnModules/UISpawnRandomScale.js +4 -5
- package/dist/particles/spawnModules/UISpawnRandomTorque.d.ts +7 -0
- package/dist/particles/spawnModules/UISpawnRandomTorque.js +5 -5
- package/dist/particles/spawnModules/UISpawnRandomVelocity.d.ts +7 -0
- package/dist/particles/spawnModules/UISpawnRandomVelocity.js +6 -6
- package/dist/particles/spawnModules/UISpawnRectangle.d.ts +7 -0
- package/dist/particles/spawnModules/UISpawnRectangle.js +6 -6
- package/package.json +13 -4
- package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.Internal.d.ts +0 -21
- package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.d.ts +0 -63
- package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.js +0 -144
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Packing of varyings into as few vec4 rows as possible.
|
|
3
|
+
*
|
|
4
|
+
* The varying budget (MAX_VARYING_VECTORS: at least 8 on WebGL1, 15 on WebGL2) is counted in
|
|
5
|
+
* rows of four components, and drivers lay varyings out with the GLSL ES reference algorithm:
|
|
6
|
+
* a vec3 or a mat3 column leaves its fourth component to floats only, so vec3 + vec2 already
|
|
7
|
+
* takes two rows. Instead every field is cut into scalars and placed into packed vec4 varyings
|
|
8
|
+
* by the shader generator itself, which takes exactly ceil(components / 4) rows. Fields are
|
|
9
|
+
* kept contiguous where they fit, so most reads stay plain swizzles.
|
|
10
|
+
*
|
|
11
|
+
* Packing changes nothing for interpolation: varyings are interpolated per component. The
|
|
12
|
+
* fragment shader reassembles each field once, which costs a few register moves.
|
|
13
|
+
*
|
|
14
|
+
* The row count is exact, so the budget can be checked before the program fails to link.
|
|
15
|
+
*/
|
|
16
|
+
const ROW_CAPACITY = 4;
|
|
17
|
+
const ROW_TYPES = ["float", "vec2", "vec3", "vec4"];
|
|
18
|
+
const SWIZZLE_COMPONENTS = "xyzw";
|
|
19
|
+
/** Number of components and column size of the GLSL types a varying field may have */
|
|
20
|
+
const TYPE_SHAPES = {
|
|
21
|
+
float: { size: 1, columnSize: 1 },
|
|
22
|
+
vec2: { size: 2, columnSize: 2 },
|
|
23
|
+
vec3: { size: 3, columnSize: 3 },
|
|
24
|
+
vec4: { size: 4, columnSize: 4 },
|
|
25
|
+
mat2: { size: 4, columnSize: 2 },
|
|
26
|
+
mat3: { size: 9, columnSize: 3 },
|
|
27
|
+
mat4: { size: 16, columnSize: 4 },
|
|
28
|
+
};
|
|
29
|
+
const AFFINE_SHAPE = { size: 6, columnSize: 2 };
|
|
30
|
+
export function packedVaryingName(row) {
|
|
31
|
+
return `v_packed${row}`;
|
|
32
|
+
}
|
|
33
|
+
/** Whether the code mentions the identifier as a whole word */
|
|
34
|
+
export function isIdentifierReferenced(code, identifier) {
|
|
35
|
+
return new RegExp(`\\b${identifier}\\b`).test(code);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Splits `base.xyz`-style scalar expressions into runs that can be read with one swizzle.
|
|
39
|
+
* Expressions that are not a swizzle of a single component stay on their own.
|
|
40
|
+
*/
|
|
41
|
+
function groupSwizzles(expressions, fullSizes) {
|
|
42
|
+
const groups = [];
|
|
43
|
+
let base;
|
|
44
|
+
let letters = "";
|
|
45
|
+
const flush = () => {
|
|
46
|
+
if (base !== undefined) {
|
|
47
|
+
const isWhole = fullSizes.get(base) === letters.length && SWIZZLE_COMPONENTS.startsWith(letters);
|
|
48
|
+
groups.push(isWhole ? base : `${base}.${letters}`);
|
|
49
|
+
}
|
|
50
|
+
base = undefined;
|
|
51
|
+
letters = "";
|
|
52
|
+
};
|
|
53
|
+
for (const expression of expressions) {
|
|
54
|
+
const match = /^([A-Za-z_]\w*(?:\[\d+\])*)\.([xyzw])$/.exec(expression);
|
|
55
|
+
if (match === null) {
|
|
56
|
+
flush();
|
|
57
|
+
groups.push(expression);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const [, expressionBase, letter] = match;
|
|
61
|
+
const expected = base === undefined
|
|
62
|
+
? undefined
|
|
63
|
+
: SWIZZLE_COMPONENTS[SWIZZLE_COMPONENTS.indexOf(letters[letters.length - 1]) + 1];
|
|
64
|
+
if (expressionBase === base && letter === expected) {
|
|
65
|
+
letters += letter;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
flush();
|
|
69
|
+
base = expressionBase;
|
|
70
|
+
letters = letter;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
flush();
|
|
74
|
+
return groups;
|
|
75
|
+
}
|
|
76
|
+
function buildConstructor(glslTypeName, groups, size) {
|
|
77
|
+
// A single group of the right size already has the right type, unless a matrix is built
|
|
78
|
+
if (groups.length === 1 && !glslTypeName.startsWith("mat")) {
|
|
79
|
+
return groups[0];
|
|
80
|
+
}
|
|
81
|
+
return size === 1 ? groups[0] : `${glslTypeName}(${groups.join(", ")})`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Packs the fields into vec4 varyings. The layout depends on the fields and their order only.
|
|
85
|
+
* @throws If a field has an unsupported type or a wrong number of components
|
|
86
|
+
*/
|
|
87
|
+
export function packVaryings(fields) {
|
|
88
|
+
const chunks = [];
|
|
89
|
+
let total = 0;
|
|
90
|
+
fields.forEach((field, fieldIndex) => {
|
|
91
|
+
const shape = field.affine === true && field.glslTypeName === "mat3"
|
|
92
|
+
? AFFINE_SHAPE
|
|
93
|
+
: TYPE_SHAPES[field.glslTypeName];
|
|
94
|
+
if (shape === undefined) {
|
|
95
|
+
throw new Error(`packVaryings.fields: unsupported type ${field.glslTypeName} of "${field.name}"`);
|
|
96
|
+
}
|
|
97
|
+
if (field.components.length !== shape.size) {
|
|
98
|
+
throw new Error(`packVaryings.fields: "${field.name}" needs ${shape.size} components, got ${field.components.length}`);
|
|
99
|
+
}
|
|
100
|
+
for (let offset = 0; offset < shape.size; offset += shape.columnSize) {
|
|
101
|
+
chunks.push({ fieldIndex, offset, size: shape.columnSize });
|
|
102
|
+
}
|
|
103
|
+
total += shape.size;
|
|
104
|
+
});
|
|
105
|
+
// Largest chunks first, ties in declaration order, so the layout is deterministic
|
|
106
|
+
const order = chunks
|
|
107
|
+
.map((chunk, index) => ({ chunk, index }))
|
|
108
|
+
.sort((a, b) => b.chunk.size - a.chunk.size || a.index - b.index);
|
|
109
|
+
const rowCount = Math.ceil(total / ROW_CAPACITY);
|
|
110
|
+
const used = new Array(rowCount).fill(0);
|
|
111
|
+
const slots = fields.map((field) => new Array(field.components.length));
|
|
112
|
+
for (const { chunk } of order) {
|
|
113
|
+
const fieldSlots = slots[chunk.fieldIndex];
|
|
114
|
+
// Best fit keeps a chunk whole and leaves the larger gaps for the larger chunks
|
|
115
|
+
let bestRow = -1;
|
|
116
|
+
for (let row = 0; row < rowCount; row++) {
|
|
117
|
+
const free = ROW_CAPACITY - used[row];
|
|
118
|
+
if (free >= chunk.size && (bestRow === -1 || free < ROW_CAPACITY - used[bestRow])) {
|
|
119
|
+
bestRow = row;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (bestRow !== -1) {
|
|
123
|
+
for (let i = 0; i < chunk.size; i++) {
|
|
124
|
+
fieldSlots[chunk.offset + i] = { row: bestRow, component: used[bestRow] + i };
|
|
125
|
+
}
|
|
126
|
+
used[bestRow] += chunk.size;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
// No room for the whole chunk: spread it over the remaining components; rowCount rows
|
|
130
|
+
// always have room for all of them
|
|
131
|
+
let i = 0;
|
|
132
|
+
for (let row = 0; row < rowCount && i < chunk.size; row++) {
|
|
133
|
+
while (used[row] < ROW_CAPACITY && i < chunk.size) {
|
|
134
|
+
fieldSlots[chunk.offset + i] = { row, component: used[row] };
|
|
135
|
+
used[row]++;
|
|
136
|
+
i++;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const rowSources = used.map((size) => new Array(size));
|
|
141
|
+
fields.forEach((field, fieldIndex) => {
|
|
142
|
+
slots[fieldIndex].forEach(({ row, component }, componentIndex) => {
|
|
143
|
+
rowSources[row][component] = field.components[componentIndex];
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
const declarations = [];
|
|
147
|
+
const vertexAssignments = [];
|
|
148
|
+
const rowSizes = new Map();
|
|
149
|
+
for (let row = 0; row < rowCount; row++) {
|
|
150
|
+
const name = packedVaryingName(row);
|
|
151
|
+
const size = used[row];
|
|
152
|
+
const type = ROW_TYPES[size - 1];
|
|
153
|
+
rowSizes.set(name, size);
|
|
154
|
+
declarations.push(`varying ${type} ${name};`);
|
|
155
|
+
vertexAssignments.push(`${name} = ${buildConstructor(type, groupSwizzles(rowSources[row], new Map()), size)};`);
|
|
156
|
+
}
|
|
157
|
+
const fragmentExpressions = new Map();
|
|
158
|
+
fields.forEach((field, fieldIndex) => {
|
|
159
|
+
const references = slots[fieldIndex].map(({ row, component }) => rowSizes.get(packedVaryingName(row)) === 1
|
|
160
|
+
? packedVaryingName(row)
|
|
161
|
+
: `${packedVaryingName(row)}.${SWIZZLE_COMPONENTS[component]}`);
|
|
162
|
+
if (field.affine === true) {
|
|
163
|
+
const columns = [0, 2, 4].map((start) => groupSwizzles(references.slice(start, start + 2), rowSizes).join(", "));
|
|
164
|
+
fragmentExpressions.set(field.name, `mat3(vec3(${columns[0]}, 0.0), vec3(${columns[1]}, 0.0), vec3(${columns[2]}, 1.0))`);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
fragmentExpressions.set(field.name, buildConstructor(field.glslTypeName, groupSwizzles(references, rowSizes), field.components.length));
|
|
168
|
+
});
|
|
169
|
+
return { rowCount, declarations, vertexAssignments, fragmentExpressions };
|
|
170
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { UIInsetsConfig } from "../insets/UIInsets.Internal.js";
|
|
2
|
+
/** Shape a mask cuts out of the rectangle of its source */
|
|
3
|
+
export declare enum UIMaskShape {
|
|
4
|
+
/** Rectangle, optionally with rounded corners (see `radius`) */
|
|
5
|
+
RECTANGLE = 0,
|
|
6
|
+
/** Ellipse inscribed into the rectangle; a circle when the rectangle is a square */
|
|
7
|
+
ELLIPSE = 1
|
|
8
|
+
}
|
|
9
|
+
/** Configuration options for UIMask */
|
|
10
|
+
export interface UIMaskOptions {
|
|
11
|
+
/** Shape cut out of the source rectangle */
|
|
12
|
+
shape: UIMaskShape;
|
|
13
|
+
/** Corner radius of RECTANGLE in world units, clamped to half of the smaller side */
|
|
14
|
+
radius: number;
|
|
15
|
+
/**
|
|
16
|
+
* Width of the soft edge in world units, laid inside the shape so nothing leaks out of it.
|
|
17
|
+
* 0 keeps a hard edge, still antialiased over about a pixel.
|
|
18
|
+
*/
|
|
19
|
+
softness: number;
|
|
20
|
+
/** Insets shrinking the source rectangle, in world units */
|
|
21
|
+
padding: UIInsetsConfig;
|
|
22
|
+
/** Whether the mask keeps the outside of the shape instead of the inside */
|
|
23
|
+
inverted: boolean;
|
|
24
|
+
/** Whether the micro transform of the source moves the mask, when the source has one */
|
|
25
|
+
useMicro: boolean;
|
|
26
|
+
/** Whether masked elements also stop catching input outside the visible part */
|
|
27
|
+
clipInput: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare const MASK_DEFAULT_SHAPE = UIMaskShape.RECTANGLE;
|
|
30
|
+
export declare const MASK_DEFAULT_RADIUS = 0;
|
|
31
|
+
export declare const MASK_DEFAULT_SOFTNESS = 0;
|
|
32
|
+
export declare const MASK_DEFAULT_INVERTED = false;
|
|
33
|
+
export declare const MASK_DEFAULT_USE_MICRO = true;
|
|
34
|
+
export declare const MASK_DEFAULT_CLIP_INPUT = true;
|
|
35
|
+
/**
|
|
36
|
+
* Number of floats in the packed mask data:
|
|
37
|
+
* - [0..2] first row of the layer -> mask-local affine map
|
|
38
|
+
* - [3..5] second row of that map
|
|
39
|
+
* - [6..9] half width, half height, corner radius, softness (world units)
|
|
40
|
+
* - [10] mode: shape + 2 when inverted
|
|
41
|
+
*
|
|
42
|
+
* Mask-local space is centered on the mask, its axes follow the source rotation and its units
|
|
43
|
+
* are world units, so distances and softness stay isotropic.
|
|
44
|
+
*/
|
|
45
|
+
export declare const MASK_DATA_SIZE = 11;
|
|
46
|
+
export declare const MASK_DATA_ROW0_OFFSET = 0;
|
|
47
|
+
export declare const MASK_DATA_ROW1_OFFSET = 3;
|
|
48
|
+
export declare const MASK_DATA_SHAPE_OFFSET = 6;
|
|
49
|
+
export declare const MASK_DATA_MODE_OFFSET = 10;
|
|
50
|
+
/**
|
|
51
|
+
* Translation that puts every point of an empty mask (a source collapsed to no area) far
|
|
52
|
+
* outside its zero-sized shape, in world units
|
|
53
|
+
*/
|
|
54
|
+
export declare const MASK_EMPTY_OFFSET = 1000000;
|
|
55
|
+
/** Mode value of an external material patch with no mask applied */
|
|
56
|
+
export declare const MASK_MODE_DISABLED = -1;
|
|
57
|
+
/**
|
|
58
|
+
* Signed distance from a mask-local point to the shape boundary, negative inside. Matches the
|
|
59
|
+
* GLSL laymurMaskDistance in sign; for the ellipse neither is the exact distance.
|
|
60
|
+
*/
|
|
61
|
+
export declare function computeMaskDistance(localX: number, localY: number, halfWidth: number, halfHeight: number, radius: number, shape: UIMaskShape): number;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** Shape a mask cuts out of the rectangle of its source */
|
|
2
|
+
export var UIMaskShape;
|
|
3
|
+
(function (UIMaskShape) {
|
|
4
|
+
/** Rectangle, optionally with rounded corners (see `radius`) */
|
|
5
|
+
UIMaskShape[UIMaskShape["RECTANGLE"] = 0] = "RECTANGLE";
|
|
6
|
+
/** Ellipse inscribed into the rectangle; a circle when the rectangle is a square */
|
|
7
|
+
UIMaskShape[UIMaskShape["ELLIPSE"] = 1] = "ELLIPSE";
|
|
8
|
+
})(UIMaskShape || (UIMaskShape = {}));
|
|
9
|
+
export const MASK_DEFAULT_SHAPE = UIMaskShape.RECTANGLE;
|
|
10
|
+
export const MASK_DEFAULT_RADIUS = 0;
|
|
11
|
+
export const MASK_DEFAULT_SOFTNESS = 0;
|
|
12
|
+
export const MASK_DEFAULT_INVERTED = false;
|
|
13
|
+
export const MASK_DEFAULT_USE_MICRO = true;
|
|
14
|
+
export const MASK_DEFAULT_CLIP_INPUT = true;
|
|
15
|
+
/**
|
|
16
|
+
* Number of floats in the packed mask data:
|
|
17
|
+
* - [0..2] first row of the layer -> mask-local affine map
|
|
18
|
+
* - [3..5] second row of that map
|
|
19
|
+
* - [6..9] half width, half height, corner radius, softness (world units)
|
|
20
|
+
* - [10] mode: shape + 2 when inverted
|
|
21
|
+
*
|
|
22
|
+
* Mask-local space is centered on the mask, its axes follow the source rotation and its units
|
|
23
|
+
* are world units, so distances and softness stay isotropic.
|
|
24
|
+
*/
|
|
25
|
+
export const MASK_DATA_SIZE = 11;
|
|
26
|
+
export const MASK_DATA_ROW0_OFFSET = 0;
|
|
27
|
+
export const MASK_DATA_ROW1_OFFSET = 3;
|
|
28
|
+
export const MASK_DATA_SHAPE_OFFSET = 6;
|
|
29
|
+
export const MASK_DATA_MODE_OFFSET = 10;
|
|
30
|
+
/**
|
|
31
|
+
* Translation that puts every point of an empty mask (a source collapsed to no area) far
|
|
32
|
+
* outside its zero-sized shape, in world units
|
|
33
|
+
*/
|
|
34
|
+
export const MASK_EMPTY_OFFSET = 1e6;
|
|
35
|
+
/** Mode value of an external material patch with no mask applied */
|
|
36
|
+
export const MASK_MODE_DISABLED = -1;
|
|
37
|
+
/**
|
|
38
|
+
* Signed distance from a mask-local point to the shape boundary, negative inside. Matches the
|
|
39
|
+
* GLSL laymurMaskDistance in sign; for the ellipse neither is the exact distance.
|
|
40
|
+
*/
|
|
41
|
+
export function computeMaskDistance(localX, localY, halfWidth, halfHeight, radius, shape) {
|
|
42
|
+
if (shape === UIMaskShape.ELLIPSE) {
|
|
43
|
+
if (halfWidth <= 0 || halfHeight <= 0) {
|
|
44
|
+
return Infinity;
|
|
45
|
+
}
|
|
46
|
+
const nx = localX / halfWidth;
|
|
47
|
+
const ny = localY / halfHeight;
|
|
48
|
+
// The input test only needs the sign; the smaller axis keeps the units sensible
|
|
49
|
+
return (Math.sqrt(nx * nx + ny * ny) - 1) * Math.min(halfWidth, halfHeight);
|
|
50
|
+
}
|
|
51
|
+
const qx = Math.abs(localX) - halfWidth + radius;
|
|
52
|
+
const qy = Math.abs(localY) - halfHeight + radius;
|
|
53
|
+
const outsideX = Math.max(qx, 0);
|
|
54
|
+
const outsideY = Math.max(qy, 0);
|
|
55
|
+
return (Math.sqrt(outsideX * outsideX + outsideY * outsideY) + Math.min(Math.max(qx, qy), 0) - radius);
|
|
56
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { UIDummy } from "../../elements/UIDummy/UIDummy.js";
|
|
2
|
+
import type { UILayer } from "../../layers/UILayer/UILayer.js";
|
|
3
|
+
import { UIInsets } from "../insets/UIInsets.js";
|
|
4
|
+
import type { UIMaskOptions } from "./UIMask.Internal.js";
|
|
5
|
+
import { UIMaskShape } from "./UIMask.Internal.js";
|
|
6
|
+
/**
|
|
7
|
+
* Analytic mask that follows the rectangle of a source element or layer.
|
|
8
|
+
*
|
|
9
|
+
* Elements are masked by assigning the mask to their `mask` property; one mask can serve any
|
|
10
|
+
* number of elements of the same layer. The mask is computed in the shader, so edges can be
|
|
11
|
+
* soft, masked elements keep batching together and no stencil buffer is needed. With
|
|
12
|
+
* `clipInput` the masked part of an element stops catching input as well.
|
|
13
|
+
*
|
|
14
|
+
* Lifetime follows the rules of constraints: destroying the source destroys the mask, and a
|
|
15
|
+
* destroyed mask is taken off every element it masked.
|
|
16
|
+
*/
|
|
17
|
+
export declare class UIMask {
|
|
18
|
+
readonly source: UIDummy | UILayer;
|
|
19
|
+
/** Insets shrinking the source rectangle, in world units */
|
|
20
|
+
readonly padding: UIInsets;
|
|
21
|
+
/** Layer of the source; only elements of this layer can use the mask */
|
|
22
|
+
readonly layer: UILayer;
|
|
23
|
+
private shapeInternal;
|
|
24
|
+
private radiusInternal;
|
|
25
|
+
private softnessInternal;
|
|
26
|
+
private invertedInternal;
|
|
27
|
+
private useMicroInternal;
|
|
28
|
+
private clipInputInternal;
|
|
29
|
+
private readonly consumers;
|
|
30
|
+
private readonly dataInternal;
|
|
31
|
+
private versionInternal;
|
|
32
|
+
private destroyedInternal;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new UIMask instance.
|
|
35
|
+
*
|
|
36
|
+
* @param source - Element or layer whose rectangle the mask follows
|
|
37
|
+
* @param options - Configuration options
|
|
38
|
+
* @throws If the source is destroyed
|
|
39
|
+
*/
|
|
40
|
+
constructor(source: UIDummy | UILayer, options?: Partial<UIMaskOptions>);
|
|
41
|
+
/** Whether the mask is destroyed, by its own `destroy` or together with its source */
|
|
42
|
+
get destroyed(): boolean;
|
|
43
|
+
/** Shape cut out of the source rectangle */
|
|
44
|
+
get shape(): UIMaskShape;
|
|
45
|
+
/** Corner radius of RECTANGLE in world units */
|
|
46
|
+
get radius(): number;
|
|
47
|
+
/** Width of the soft edge in world units, laid inside the shape */
|
|
48
|
+
get softness(): number;
|
|
49
|
+
/** Whether the mask keeps the outside of the shape instead of the inside */
|
|
50
|
+
get inverted(): boolean;
|
|
51
|
+
/** Whether the micro transform of the source moves the mask */
|
|
52
|
+
get useMicro(): boolean;
|
|
53
|
+
/** Whether masked elements also stop catching input outside the visible part */
|
|
54
|
+
get clipInput(): boolean;
|
|
55
|
+
/** Elements masked by this mask */
|
|
56
|
+
get elements(): readonly UIDummy[];
|
|
57
|
+
/** Shape cut out of the source rectangle */
|
|
58
|
+
set shape(value: UIMaskShape);
|
|
59
|
+
/** Corner radius of RECTANGLE in world units */
|
|
60
|
+
set radius(value: number);
|
|
61
|
+
/** Width of the soft edge in world units, laid inside the shape */
|
|
62
|
+
set softness(value: number);
|
|
63
|
+
/** Whether the mask keeps the outside of the shape instead of the inside */
|
|
64
|
+
set inverted(value: boolean);
|
|
65
|
+
/** Whether the micro transform of the source moves the mask */
|
|
66
|
+
set useMicro(value: boolean);
|
|
67
|
+
/** Whether masked elements also stop catching input outside the visible part */
|
|
68
|
+
set clipInput(value: boolean);
|
|
69
|
+
/**
|
|
70
|
+
* Whether a point is in the visible part of the mask; the boundary is the shape itself,
|
|
71
|
+
* without the soft edge.
|
|
72
|
+
* @param x X in layer coordinates
|
|
73
|
+
* @param y Y in layer coordinates
|
|
74
|
+
* @throws If the mask is destroyed
|
|
75
|
+
*/
|
|
76
|
+
containsPoint(x: number, y: number): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Takes the mask off every element and removes it from its layer.
|
|
79
|
+
* Does nothing if the mask is already destroyed.
|
|
80
|
+
*/
|
|
81
|
+
destroy(): void;
|
|
82
|
+
private computeData;
|
|
83
|
+
private assertNotDestroyed;
|
|
84
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { Matrix4 } from "three";
|
|
2
|
+
import { assertValidNonNegativeNumber, assertValidNumber } from "../asserts.js";
|
|
3
|
+
import { UIInsets } from "../insets/UIInsets.js";
|
|
4
|
+
import { computeMaskDistance, MASK_DATA_MODE_OFFSET, MASK_DATA_ROW0_OFFSET, MASK_DATA_ROW1_OFFSET, MASK_DATA_SHAPE_OFFSET, MASK_DATA_SIZE, MASK_DEFAULT_CLIP_INPUT, MASK_DEFAULT_INVERTED, MASK_DEFAULT_RADIUS, MASK_DEFAULT_SHAPE, MASK_DEFAULT_SOFTNESS, MASK_DEFAULT_USE_MICRO, MASK_EMPTY_OFFSET, UIMaskShape, } from "./UIMask.Internal.js";
|
|
5
|
+
const TEMP_TRANSFORM = new Matrix4();
|
|
6
|
+
const TEMP_DATA = new Float32Array(MASK_DATA_SIZE);
|
|
7
|
+
/**
|
|
8
|
+
* Tells an element from a layer without importing UIDummy as a value, which would also fail
|
|
9
|
+
* for an element of another copy of laymur
|
|
10
|
+
*/
|
|
11
|
+
function isElementSource(source) {
|
|
12
|
+
return "computeVisualTransform" in source;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Analytic mask that follows the rectangle of a source element or layer.
|
|
16
|
+
*
|
|
17
|
+
* Elements are masked by assigning the mask to their `mask` property; one mask can serve any
|
|
18
|
+
* number of elements of the same layer. The mask is computed in the shader, so edges can be
|
|
19
|
+
* soft, masked elements keep batching together and no stencil buffer is needed. With
|
|
20
|
+
* `clipInput` the masked part of an element stops catching input as well.
|
|
21
|
+
*
|
|
22
|
+
* Lifetime follows the rules of constraints: destroying the source destroys the mask, and a
|
|
23
|
+
* destroyed mask is taken off every element it masked.
|
|
24
|
+
*/
|
|
25
|
+
export class UIMask {
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new UIMask instance.
|
|
28
|
+
*
|
|
29
|
+
* @param source - Element or layer whose rectangle the mask follows
|
|
30
|
+
* @param options - Configuration options
|
|
31
|
+
* @throws If the source is destroyed
|
|
32
|
+
*/
|
|
33
|
+
constructor(source, options = {}) {
|
|
34
|
+
this.source = source;
|
|
35
|
+
this.consumers = new Set();
|
|
36
|
+
this.dataInternal = new Float32Array(MASK_DATA_SIZE);
|
|
37
|
+
this.versionInternal = 0;
|
|
38
|
+
this.destroyedInternal = false;
|
|
39
|
+
if (source.destroyed) {
|
|
40
|
+
throw new Error(`UIMask.constructor.source: source is destroyed`);
|
|
41
|
+
}
|
|
42
|
+
const radius = options.radius ?? MASK_DEFAULT_RADIUS;
|
|
43
|
+
const softness = options.softness ?? MASK_DEFAULT_SOFTNESS;
|
|
44
|
+
assertValidNonNegativeNumber(radius, "UIMask.constructor.options.radius");
|
|
45
|
+
assertValidNonNegativeNumber(softness, "UIMask.constructor.options.softness");
|
|
46
|
+
this.layer = isElementSource(source) ? source.layer : source;
|
|
47
|
+
this.padding = new UIInsets(options.padding);
|
|
48
|
+
this.shapeInternal = options.shape ?? MASK_DEFAULT_SHAPE;
|
|
49
|
+
this.radiusInternal = radius;
|
|
50
|
+
this.softnessInternal = softness;
|
|
51
|
+
this.invertedInternal = options.inverted ?? MASK_DEFAULT_INVERTED;
|
|
52
|
+
this.useMicroInternal = options.useMicro ?? MASK_DEFAULT_USE_MICRO;
|
|
53
|
+
this.clipInputInternal = options.clipInput ?? MASK_DEFAULT_CLIP_INPUT;
|
|
54
|
+
this.layer.addMask(this);
|
|
55
|
+
}
|
|
56
|
+
/** Whether the mask is destroyed, by its own `destroy` or together with its source */
|
|
57
|
+
get destroyed() {
|
|
58
|
+
return this.destroyedInternal;
|
|
59
|
+
}
|
|
60
|
+
/** Shape cut out of the source rectangle */
|
|
61
|
+
get shape() {
|
|
62
|
+
return this.shapeInternal;
|
|
63
|
+
}
|
|
64
|
+
/** Corner radius of RECTANGLE in world units */
|
|
65
|
+
get radius() {
|
|
66
|
+
return this.radiusInternal;
|
|
67
|
+
}
|
|
68
|
+
/** Width of the soft edge in world units, laid inside the shape */
|
|
69
|
+
get softness() {
|
|
70
|
+
return this.softnessInternal;
|
|
71
|
+
}
|
|
72
|
+
/** Whether the mask keeps the outside of the shape instead of the inside */
|
|
73
|
+
get inverted() {
|
|
74
|
+
return this.invertedInternal;
|
|
75
|
+
}
|
|
76
|
+
/** Whether the micro transform of the source moves the mask */
|
|
77
|
+
get useMicro() {
|
|
78
|
+
return this.useMicroInternal;
|
|
79
|
+
}
|
|
80
|
+
/** Whether masked elements also stop catching input outside the visible part */
|
|
81
|
+
get clipInput() {
|
|
82
|
+
return this.clipInputInternal;
|
|
83
|
+
}
|
|
84
|
+
/** Elements masked by this mask */
|
|
85
|
+
get elements() {
|
|
86
|
+
return [...this.consumers];
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Incremented whenever the packed data changes.
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
get version() {
|
|
93
|
+
return this.versionInternal;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Packed mask data, see MASK_DATA_SIZE; valid after {@link resolve}.
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
get data() {
|
|
100
|
+
return this.dataInternal;
|
|
101
|
+
}
|
|
102
|
+
/** Shape cut out of the source rectangle */
|
|
103
|
+
set shape(value) {
|
|
104
|
+
this.assertNotDestroyed("UIMask.shape");
|
|
105
|
+
this.shapeInternal = value;
|
|
106
|
+
}
|
|
107
|
+
/** Corner radius of RECTANGLE in world units */
|
|
108
|
+
set radius(value) {
|
|
109
|
+
this.assertNotDestroyed("UIMask.radius");
|
|
110
|
+
assertValidNonNegativeNumber(value, "UIMask.radius");
|
|
111
|
+
this.radiusInternal = value;
|
|
112
|
+
}
|
|
113
|
+
/** Width of the soft edge in world units, laid inside the shape */
|
|
114
|
+
set softness(value) {
|
|
115
|
+
this.assertNotDestroyed("UIMask.softness");
|
|
116
|
+
assertValidNonNegativeNumber(value, "UIMask.softness");
|
|
117
|
+
this.softnessInternal = value;
|
|
118
|
+
}
|
|
119
|
+
/** Whether the mask keeps the outside of the shape instead of the inside */
|
|
120
|
+
set inverted(value) {
|
|
121
|
+
this.assertNotDestroyed("UIMask.inverted");
|
|
122
|
+
this.invertedInternal = value;
|
|
123
|
+
}
|
|
124
|
+
/** Whether the micro transform of the source moves the mask */
|
|
125
|
+
set useMicro(value) {
|
|
126
|
+
this.assertNotDestroyed("UIMask.useMicro");
|
|
127
|
+
this.useMicroInternal = value;
|
|
128
|
+
}
|
|
129
|
+
/** Whether masked elements also stop catching input outside the visible part */
|
|
130
|
+
set clipInput(value) {
|
|
131
|
+
this.assertNotDestroyed("UIMask.clipInput");
|
|
132
|
+
this.clipInputInternal = value;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Whether a point is in the visible part of the mask; the boundary is the shape itself,
|
|
136
|
+
* without the soft edge.
|
|
137
|
+
* @param x X in layer coordinates
|
|
138
|
+
* @param y Y in layer coordinates
|
|
139
|
+
* @throws If the mask is destroyed
|
|
140
|
+
*/
|
|
141
|
+
containsPoint(x, y) {
|
|
142
|
+
this.assertNotDestroyed("UIMask.containsPoint");
|
|
143
|
+
assertValidNumber(x, "UIMask.containsPoint.x");
|
|
144
|
+
assertValidNumber(y, "UIMask.containsPoint.y");
|
|
145
|
+
this.resolve();
|
|
146
|
+
const data = this.dataInternal;
|
|
147
|
+
const localX = data[MASK_DATA_ROW0_OFFSET] * x +
|
|
148
|
+
data[MASK_DATA_ROW0_OFFSET + 1] * y +
|
|
149
|
+
data[MASK_DATA_ROW0_OFFSET + 2];
|
|
150
|
+
const localY = data[MASK_DATA_ROW1_OFFSET] * x +
|
|
151
|
+
data[MASK_DATA_ROW1_OFFSET + 1] * y +
|
|
152
|
+
data[MASK_DATA_ROW1_OFFSET + 2];
|
|
153
|
+
const inside = computeMaskDistance(localX, localY, data[MASK_DATA_SHAPE_OFFSET], data[MASK_DATA_SHAPE_OFFSET + 1], data[MASK_DATA_SHAPE_OFFSET + 2], this.shapeInternal) <= 0;
|
|
154
|
+
return inside !== this.invertedInternal;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Takes the mask off every element and removes it from its layer.
|
|
158
|
+
* Does nothing if the mask is already destroyed.
|
|
159
|
+
*/
|
|
160
|
+
destroy() {
|
|
161
|
+
if (this.destroyedInternal) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
this.destroyedInternal = true;
|
|
165
|
+
for (const consumer of [...this.consumers]) {
|
|
166
|
+
consumer.releaseMask(this);
|
|
167
|
+
}
|
|
168
|
+
this.consumers.clear();
|
|
169
|
+
this.layer.removeMask(this);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Brings the packed data up to date with the source, bumping the version on a change. Cheap
|
|
173
|
+
* enough to be done by every masked element every frame, so it is never a frame late.
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
resolve() {
|
|
177
|
+
const data = TEMP_DATA;
|
|
178
|
+
this.computeData(data);
|
|
179
|
+
for (let i = 0; i < MASK_DATA_SIZE; i++) {
|
|
180
|
+
if (!Object.is(this.dataInternal[i], data[i])) {
|
|
181
|
+
this.dataInternal.set(data);
|
|
182
|
+
this.versionInternal++;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** @internal */
|
|
188
|
+
attach(consumer) {
|
|
189
|
+
this.consumers.add(consumer);
|
|
190
|
+
}
|
|
191
|
+
/** @internal */
|
|
192
|
+
detach(consumer) {
|
|
193
|
+
this.consumers.delete(consumer);
|
|
194
|
+
}
|
|
195
|
+
computeData(data) {
|
|
196
|
+
const source = this.source;
|
|
197
|
+
const width = source.width;
|
|
198
|
+
const height = source.height;
|
|
199
|
+
data[MASK_DATA_SHAPE_OFFSET + 3] = this.softnessInternal;
|
|
200
|
+
data[MASK_DATA_MODE_OFFSET] =
|
|
201
|
+
(this.shapeInternal === UIMaskShape.ELLIPSE ? 1 : 0) + (this.invertedInternal ? 2 : 0);
|
|
202
|
+
// A source collapsed by the layout has no area: nothing is inside, everything is outside
|
|
203
|
+
if (!(width > 0 && height > 0)) {
|
|
204
|
+
data.fill(0, 0, MASK_DATA_SHAPE_OFFSET + 3);
|
|
205
|
+
data[MASK_DATA_ROW0_OFFSET + 2] = MASK_EMPTY_OFFSET;
|
|
206
|
+
data[MASK_DATA_ROW1_OFFSET + 2] = MASK_EMPTY_OFFSET;
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
let originX;
|
|
210
|
+
let originY;
|
|
211
|
+
let axisXx;
|
|
212
|
+
let axisXy;
|
|
213
|
+
let axisYx;
|
|
214
|
+
let axisYy;
|
|
215
|
+
if (isElementSource(source)) {
|
|
216
|
+
const e = source.computeVisualTransform(this.useMicroInternal, TEMP_TRANSFORM).elements;
|
|
217
|
+
axisXx = e[0];
|
|
218
|
+
axisXy = e[1];
|
|
219
|
+
axisYx = e[4];
|
|
220
|
+
axisYy = e[5];
|
|
221
|
+
originX = e[12];
|
|
222
|
+
originY = e[13];
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
axisXx = width;
|
|
226
|
+
axisXy = 0;
|
|
227
|
+
axisYx = 0;
|
|
228
|
+
axisYy = height;
|
|
229
|
+
originX = source.x;
|
|
230
|
+
originY = source.y;
|
|
231
|
+
}
|
|
232
|
+
// The padded rectangle in the unit space of the source, so that micro scale applies to it
|
|
233
|
+
const u0 = this.padding.left / width;
|
|
234
|
+
const u1 = 1 - this.padding.right / width;
|
|
235
|
+
const v0 = this.padding.bottom / height;
|
|
236
|
+
const v1 = 1 - this.padding.top / height;
|
|
237
|
+
const centerU = (u0 + u1) / 2;
|
|
238
|
+
const centerV = (v0 + v1) / 2;
|
|
239
|
+
const lengthX = Math.hypot(axisXx, axisXy);
|
|
240
|
+
const lengthY = Math.hypot(axisYx, axisYy);
|
|
241
|
+
const halfWidth = Math.max(0, (u1 - u0) / 2) * lengthX;
|
|
242
|
+
const halfHeight = Math.max(0, (v1 - v0) / 2) * lengthY;
|
|
243
|
+
const centerX = originX + centerU * axisXx + centerV * axisYx;
|
|
244
|
+
const centerY = originY + centerU * axisXy + centerV * axisYy;
|
|
245
|
+
// Micro only rotates and scales, so the axes stay perpendicular; a zero axis is degenerate
|
|
246
|
+
const unitXx = lengthX > 0 ? axisXx / lengthX : 1;
|
|
247
|
+
const unitXy = lengthX > 0 ? axisXy / lengthX : 0;
|
|
248
|
+
const unitYx = lengthY > 0 ? axisYx / lengthY : 0;
|
|
249
|
+
const unitYy = lengthY > 0 ? axisYy / lengthY : 1;
|
|
250
|
+
data[MASK_DATA_ROW0_OFFSET] = unitXx;
|
|
251
|
+
data[MASK_DATA_ROW0_OFFSET + 1] = unitXy;
|
|
252
|
+
data[MASK_DATA_ROW0_OFFSET + 2] = -(centerX * unitXx + centerY * unitXy);
|
|
253
|
+
data[MASK_DATA_ROW1_OFFSET] = unitYx;
|
|
254
|
+
data[MASK_DATA_ROW1_OFFSET + 1] = unitYy;
|
|
255
|
+
data[MASK_DATA_ROW1_OFFSET + 2] = -(centerX * unitYx + centerY * unitYy);
|
|
256
|
+
data[MASK_DATA_SHAPE_OFFSET] = halfWidth;
|
|
257
|
+
data[MASK_DATA_SHAPE_OFFSET + 1] = halfHeight;
|
|
258
|
+
data[MASK_DATA_SHAPE_OFFSET + 2] = Math.min(this.radiusInternal, halfWidth, halfHeight);
|
|
259
|
+
}
|
|
260
|
+
assertNotDestroyed(subject) {
|
|
261
|
+
if (this.destroyedInternal) {
|
|
262
|
+
throw new Error(`${subject}: mask is destroyed`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|