@margoengine/voxel-edit 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +445 -2
- package/dist/animation.d.ts +50 -0
- package/dist/animation.js +88 -0
- package/dist/assist.d.ts +24 -0
- package/dist/assist.js +52 -0
- package/dist/brush-gpu.d.ts +62 -0
- package/dist/bytes.d.ts +55 -0
- package/dist/collab.d.ts +77 -0
- package/dist/collab.js +159 -0
- package/dist/edit.d.ts +30 -0
- package/dist/edit.js +361 -0
- package/dist/export.d.ts +58 -0
- package/dist/export.js +52 -0
- package/dist/file-colors-DHT5vrzW.js +55 -0
- package/dist/file-colors.d.ts +16 -0
- package/dist/format-plist.d.ts +4 -0
- package/dist/formats-extra.d.ts +20 -0
- package/dist/formats-extra.js +335 -0
- package/dist/formats.d.ts +96 -0
- package/dist/formats.js +924 -0
- package/dist/generate.d.ts +63 -0
- package/dist/generate.js +103 -0
- package/dist/gox.d.ts +60 -0
- package/dist/gpu.d.ts +74 -0
- package/dist/gpu.js +167 -0
- package/dist/grid-B2V0TGLV.js +120 -0
- package/dist/grid.d.ts +75 -0
- package/dist/history.d.ts +65 -0
- package/dist/history.js +75 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +631 -0
- package/dist/layout.d.ts +76 -0
- package/dist/library.d.ts +50 -0
- package/dist/library.js +89 -0
- package/dist/mesh.d.ts +128 -0
- package/dist/mesh.js +365 -0
- package/dist/model.d.ts +173 -0
- package/dist/pick.d.ts +34 -0
- package/dist/pick.js +122 -0
- package/dist/raster.d.ts +89 -0
- package/dist/rig.d.ts +88 -0
- package/dist/rig.js +216 -0
- package/dist/select.d.ts +50 -0
- package/dist/select.js +94 -0
- package/dist/shaders-BaY4gcm9.js +234 -0
- package/dist/shaders.d.ts +116 -0
- package/dist/shaders.js +1 -0
- package/dist/slices.d.ts +75 -0
- package/dist/smooth.d.ts +43 -0
- package/dist/smooth.js +212 -0
- package/dist/sparse.d.ts +239 -0
- package/dist/sparse.js +646 -0
- package/dist/stamp.d.ts +88 -0
- package/dist/vmax.d.ts +48 -0
- package/dist/vox-resolve.d.ts +64 -0
- package/dist/vox-scene.d.ts +51 -0
- package/dist/vox.d.ts +30 -0
- package/dist/vox.js +684 -0
- package/dist/voxelize.d.ts +70 -0
- package/dist/voxelize.js +249 -0
- package/dist/vxm.d.ts +54 -0
- package/package.json +130 -4
package/dist/raster.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Vec3 } from '@margoengine/math';
|
|
2
|
+
import { type GridLimits } from './grid.js';
|
|
3
|
+
/** Options every rasterizer takes. */
|
|
4
|
+
export interface RasterOptions extends GridLimits {
|
|
5
|
+
/** Cells outside the grid: `'refuse'` (default) throws a RangeError and returns nothing, `'clip'` drops them. */
|
|
6
|
+
outside?: 'refuse' | 'clip';
|
|
7
|
+
}
|
|
8
|
+
/** Options of {@link ellipsoidCells}. */
|
|
9
|
+
export interface EllipsoidOptions extends RasterOptions {
|
|
10
|
+
/** Centre, in cells (any finite point). */
|
|
11
|
+
center: Vec3;
|
|
12
|
+
/** Radii along x, y and z, each positive and finite. */
|
|
13
|
+
radii: Vec3;
|
|
14
|
+
}
|
|
15
|
+
/** Options of {@link cylinderCells}. */
|
|
16
|
+
export interface CylinderOptions extends RasterOptions {
|
|
17
|
+
/** The axis the cylinder runs along. */
|
|
18
|
+
axis: 'x' | 'y' | 'z';
|
|
19
|
+
/** Centre, in cells; each cell is rounded (`Math.round`) after its whole-cell offset is added. */
|
|
20
|
+
center: Vec3;
|
|
21
|
+
/** Radius across the axis, 0 or more. */
|
|
22
|
+
radius: number;
|
|
23
|
+
/** Length along the axis, 0 or more: offsets `ceil(-length / 2)` to `floor(length / 2)`. */
|
|
24
|
+
length: number;
|
|
25
|
+
/** Radius of a hole along the axis, 0 or more: cells with `b * b + d * d <= inner * inner` stay out. Default none. */
|
|
26
|
+
inner?: number;
|
|
27
|
+
}
|
|
28
|
+
/** Options of {@link ringCells}. */
|
|
29
|
+
export interface RingOptions extends RasterOptions {
|
|
30
|
+
/** Centre, in cells. */
|
|
31
|
+
center: Vec3;
|
|
32
|
+
/** Outer radius, 0 or more. */
|
|
33
|
+
outer: number;
|
|
34
|
+
/** Inner radius, 0 or more: the hole. */
|
|
35
|
+
inner: number;
|
|
36
|
+
/** Thickness along z, 0 or more, as a cylinder's length. */
|
|
37
|
+
thickness: number;
|
|
38
|
+
}
|
|
39
|
+
/** Options of {@link thickLineCells}. */
|
|
40
|
+
export interface ThickLineOptions extends RasterOptions {
|
|
41
|
+
from: Vec3;
|
|
42
|
+
to: Vec3;
|
|
43
|
+
/** Half width of the cube stamped at each step, 0 or more: cells from `ceil(p - halfWidth)` to `floor(p + halfWidth)`. Default 0. */
|
|
44
|
+
halfWidth?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Step count `n` from the longest axis span `s`. `'ceil'` (default): `n = ceil(s)`, steps 0 to n, so the line ends
|
|
47
|
+
* on `to`. `'span'`: `n = s` itself, steps 0 to `floor(s)`, so a fractional span stops short of `to`. Both put step
|
|
48
|
+
* `k` at `round(from + (to - from) * k / max(1, n))` per axis.
|
|
49
|
+
*/
|
|
50
|
+
steps?: 'ceil' | 'span';
|
|
51
|
+
}
|
|
52
|
+
/** Options of {@link taperedLineCells}. */
|
|
53
|
+
export interface TaperedLineOptions extends RasterOptions {
|
|
54
|
+
from: Vec3;
|
|
55
|
+
to: Vec3;
|
|
56
|
+
/** Radius at `from`, finite. Default 1. */
|
|
57
|
+
radius?: number;
|
|
58
|
+
/** Radius at `to`, finite. Default `radius`. */
|
|
59
|
+
endRadius?: number;
|
|
60
|
+
/** Smallest radius a sphere is stamped with, positive. Default 0.6. */
|
|
61
|
+
minRadius?: number;
|
|
62
|
+
/** Spheres per cell of length: `ceil(length * samplesPerCell)` steps. Positive. Default 1.4. */
|
|
63
|
+
samplesPerCell?: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The cells of a solid ellipsoid in a grid of `size`, as cell indexes: whole cells from `ceil(c - r)` to
|
|
67
|
+
* `floor(c + r)` per axis with `((x - cx) / rx)^2 + ((y - cy) / ry)^2 + ((z - cz) / rz)^2 <= 1` (cell coordinates,
|
|
68
|
+
* not centres), visited y, then z, then x.
|
|
69
|
+
*/
|
|
70
|
+
export declare function ellipsoidCells(size: Vec3, options: EllipsoidOptions): Float64Array;
|
|
71
|
+
/**
|
|
72
|
+
* The cells of a cylinder along `axis`: offset `a` along it from `ceil(-length / 2)` to `floor(length / 2)` and
|
|
73
|
+
* offsets `b`, `d` across it (the next two axes, x after z) from `ceil(-radius)` while within `radius`, kept when
|
|
74
|
+
* `b^2 + d^2 <= radius^2` and, with `inner`, `b^2 + d^2 > inner^2`; each cell is `round(center + offsets)`.
|
|
75
|
+
*/
|
|
76
|
+
export declare function cylinderCells(size: Vec3, options: CylinderOptions): Float64Array;
|
|
77
|
+
/** The cells of a flat ring around z: a z cylinder of `thickness` with radius `outer` and a hole of radius `inner`. */
|
|
78
|
+
export declare function ringCells(size: Vec3, options: RingOptions): Float64Array;
|
|
79
|
+
/**
|
|
80
|
+
* The cells of a straight line drawn as a cube of `halfWidth` at each step (see {@link ThickLineOptions.steps}), each
|
|
81
|
+
* cube's cells visited y, then z, then x.
|
|
82
|
+
*/
|
|
83
|
+
export declare function thickLineCells(size: Vec3, options: ThickLineOptions): Float64Array;
|
|
84
|
+
/**
|
|
85
|
+
* The cells of a tapered line: `ceil(length * samplesPerCell)` + 1 spheres (ellipsoids with three equal radii, the
|
|
86
|
+
* {@link ellipsoidCells} rule) at `from + (to - from) * t`, `t = i / max(1, steps)`, of radius
|
|
87
|
+
* `max(minRadius, radius + (endRadius - radius) * t)`.
|
|
88
|
+
*/
|
|
89
|
+
export declare function taperedLineCells(size: Vec3, options: TaperedLineOptions): Float64Array;
|
package/dist/rig.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Vec3 } from '@margoengine/math';
|
|
2
|
+
import { type GridLimits, type VoxelGrid } from './grid.js';
|
|
3
|
+
/** A rotation as a unit quaternion `[x, y, z, w]`. */
|
|
4
|
+
export type RigQuat = readonly [number, number, number, number];
|
|
5
|
+
/** One bone of a {@link VoxelRig}. */
|
|
6
|
+
export interface RigBone {
|
|
7
|
+
/** Unique name. */
|
|
8
|
+
name: string;
|
|
9
|
+
/** The parent bone's name; a bone without one is a root. Parents come before their children. */
|
|
10
|
+
parent?: string;
|
|
11
|
+
/** Where the bone turns, in model cells (corner coordinates) at rest. Default [0, 0, 0]. */
|
|
12
|
+
joint?: Vec3;
|
|
13
|
+
}
|
|
14
|
+
/** A voxel part carried by one bone. */
|
|
15
|
+
export interface RigPart {
|
|
16
|
+
/** The bone that carries it. */
|
|
17
|
+
bone: string;
|
|
18
|
+
/** Its cells. */
|
|
19
|
+
grid: VoxelGrid;
|
|
20
|
+
/** Where the grid's corner (cell 0, 0, 0) sits in the model at rest, in cells. Default [0, 0, 0]. */
|
|
21
|
+
offset?: Vec3;
|
|
22
|
+
}
|
|
23
|
+
/** A pose: bone rotations (about each joint, after the parent's) and a move of the whole model. */
|
|
24
|
+
export interface RigPose {
|
|
25
|
+
/** Rotation per bone name; a bone left out keeps its parent's turn. */
|
|
26
|
+
rotations?: Readonly<Record<string, RigQuat>>;
|
|
27
|
+
/** Moves the whole posed model, in cells. Default [0, 0, 0]. */
|
|
28
|
+
translation?: Vec3;
|
|
29
|
+
}
|
|
30
|
+
/** A key-framed clip: per bone, times in seconds (increasing) and a rotation at each. */
|
|
31
|
+
export interface RigClip {
|
|
32
|
+
/** Seconds; `sampleRigClip` wraps time into it when `loop` is true. */
|
|
33
|
+
duration: number;
|
|
34
|
+
loop?: boolean;
|
|
35
|
+
tracks: readonly {
|
|
36
|
+
bone: string;
|
|
37
|
+
times: readonly number[];
|
|
38
|
+
rotations: readonly RigQuat[];
|
|
39
|
+
}[];
|
|
40
|
+
}
|
|
41
|
+
/** A baked pose: the grid of the posed model and where its corner is in the model, in cells. */
|
|
42
|
+
export interface RigBake {
|
|
43
|
+
grid: VoxelGrid;
|
|
44
|
+
origin: Vec3;
|
|
45
|
+
}
|
|
46
|
+
/** The unit quaternion turning by `degrees` about `axis` (normalized here), right-handed. */
|
|
47
|
+
export declare function rigRotation(axis: Vec3, degrees: number): RigQuat;
|
|
48
|
+
/** Spherical interpolation between unit quaternions, the short way round. */
|
|
49
|
+
export declare function slerpRotation(a: RigQuat, b: RigQuat, t: number): RigQuat;
|
|
50
|
+
/**
|
|
51
|
+
* The bone rotations of `clip` at `time` seconds: each track's keys interpolated by {@link slerpRotation}, held before
|
|
52
|
+
* the first and after the last key; time wraps into `[0, duration)` when `clip.loop` is true.
|
|
53
|
+
* @throws TypeError or RangeError for a malformed clip.
|
|
54
|
+
*/
|
|
55
|
+
export declare function sampleRigClip(clip: RigClip, time: number): Record<string, RigQuat>;
|
|
56
|
+
/**
|
|
57
|
+
* A skeleton with voxel parts. {@link VoxelRig.matrices} gives each part's model matrix for a pose (draw the part's
|
|
58
|
+
* mesh, from `meshGrid`, with it); {@link VoxelRig.bake} gives the posed model as one grid, for frames, export or a
|
|
59
|
+
* world. Bones turn about their joints; a child turns with its parent.
|
|
60
|
+
*/
|
|
61
|
+
export declare class VoxelRig {
|
|
62
|
+
#private;
|
|
63
|
+
readonly bones: readonly Readonly<Required<Omit<RigBone, 'parent'>> & {
|
|
64
|
+
parent?: string;
|
|
65
|
+
}>[];
|
|
66
|
+
readonly parts: readonly Readonly<Required<RigPart>>[];
|
|
67
|
+
/**
|
|
68
|
+
* @throws TypeError for a malformed bone or part, a duplicate bone name, a parent listed after its child or not at
|
|
69
|
+
* all, or a part on an unknown bone.
|
|
70
|
+
*/
|
|
71
|
+
constructor(options: {
|
|
72
|
+
bones: readonly RigBone[];
|
|
73
|
+
parts: readonly RigPart[];
|
|
74
|
+
});
|
|
75
|
+
/**
|
|
76
|
+
* Each part's model matrix in `pose` (column-major 4 x 4, cells to model cells, its grid corner at the origin of its
|
|
77
|
+
* cells), in the order of `parts`. A rest pose gives a translation by each part's offset.
|
|
78
|
+
* @throws TypeError or RangeError for an unknown bone or a malformed rotation.
|
|
79
|
+
*/
|
|
80
|
+
matrices(pose?: RigPose): Float64Array[];
|
|
81
|
+
/**
|
|
82
|
+
* The posed model as one grid: every cell whose centre falls in a filled cell of a posed part takes that cell's
|
|
83
|
+
* index (a later part over an earlier one), so turned parts are resampled without holes. The grid covers the posed
|
|
84
|
+
* parts' boxes; `origin` is its corner in model cells.
|
|
85
|
+
* @throws RangeError when the posed box holds more than `maxCells` cells (default 16,777,216).
|
|
86
|
+
*/
|
|
87
|
+
bake(pose?: RigPose, limits?: GridLimits): RigBake;
|
|
88
|
+
}
|
package/dist/rig.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { c as e, l as t, o as n } from "./grid-B2V0TGLV.js";
|
|
2
|
+
//#region src/rig.ts
|
|
3
|
+
var r = () => Float64Array.of(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), i = (e, t) => {
|
|
4
|
+
let n = /* @__PURE__ */ new Float64Array(16);
|
|
5
|
+
for (let r = 0; r < 4; r++) for (let i = 0; i < 4; i++) {
|
|
6
|
+
let a = 0;
|
|
7
|
+
for (let n = 0; n < 4; n++) a += e[n * 4 + i] * t[r * 4 + n];
|
|
8
|
+
n[r * 4 + i] = a;
|
|
9
|
+
}
|
|
10
|
+
return n;
|
|
11
|
+
}, a = (e, t) => {
|
|
12
|
+
let [n, i, a, o] = e, s = r();
|
|
13
|
+
s[0] = 1 - 2 * (i * i + a * a), s[1] = 2 * (n * i + a * o), s[2] = 2 * (n * a - i * o), s[4] = 2 * (n * i - a * o), s[5] = 1 - 2 * (n * n + a * a), s[6] = 2 * (i * a + n * o), s[8] = 2 * (n * a + i * o), s[9] = 2 * (i * a - n * o), s[10] = 1 - 2 * (n * n + i * i);
|
|
14
|
+
for (let e = 0; e < 3; e++) s[12 + e] = t[e] - (s[e] * t[0] + s[4 + e] * t[1] + s[8 + e] * t[2]);
|
|
15
|
+
return s;
|
|
16
|
+
}, o = (e) => {
|
|
17
|
+
let t = r();
|
|
18
|
+
for (let n = 0; n < 3; n++) for (let r = 0; r < 3; r++) t[r * 4 + n] = e[n * 4 + r];
|
|
19
|
+
for (let n = 0; n < 3; n++) t[12 + n] = -(t[n] * e[12] + t[4 + n] * e[13] + t[8 + n] * e[14]);
|
|
20
|
+
return t;
|
|
21
|
+
}, s = (e, t) => {
|
|
22
|
+
if (e === void 0) return [
|
|
23
|
+
0,
|
|
24
|
+
0,
|
|
25
|
+
0
|
|
26
|
+
];
|
|
27
|
+
if (!Array.isArray(e) || e.length !== 3 || !e.every((e) => typeof e == "number" && Number.isFinite(e))) throw TypeError(`${t} must be three finite numbers; pass [x, y, z]`);
|
|
28
|
+
return [
|
|
29
|
+
e[0],
|
|
30
|
+
e[1],
|
|
31
|
+
e[2]
|
|
32
|
+
];
|
|
33
|
+
}, c = (e, t) => {
|
|
34
|
+
if (!Array.isArray(e) || e.length !== 4 || !e.every((e) => typeof e == "number" && Number.isFinite(e))) throw TypeError(`${t} must be a quaternion of four finite numbers; pass [x, y, z, w]`);
|
|
35
|
+
let n = Math.hypot(e[0], e[1], e[2], e[3]);
|
|
36
|
+
if (Math.abs(n - 1) > .001) throw RangeError(`${t} has length ${n}, not 1; pass a unit quaternion`);
|
|
37
|
+
return [
|
|
38
|
+
e[0] / n,
|
|
39
|
+
e[1] / n,
|
|
40
|
+
e[2] / n,
|
|
41
|
+
e[3] / n
|
|
42
|
+
];
|
|
43
|
+
};
|
|
44
|
+
function l(e, t) {
|
|
45
|
+
let [n, r, i] = s(e, "rigRotation axis"), a = Math.hypot(n, r, i);
|
|
46
|
+
if (!(a > 0)) throw RangeError("rigRotation axis is zero; pass a direction such as [0, 1, 0]");
|
|
47
|
+
if (typeof t != "number" || !Number.isFinite(t)) throw TypeError("rigRotation degrees must be a finite number; pass an angle such as 90");
|
|
48
|
+
let o = t * Math.PI / 360, c = Math.sin(o) / a;
|
|
49
|
+
return [
|
|
50
|
+
n * c,
|
|
51
|
+
r * c,
|
|
52
|
+
i * c,
|
|
53
|
+
Math.cos(o)
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
function u(e, t, n) {
|
|
57
|
+
let r = e[0] * t[0] + e[1] * t[1] + e[2] * t[2] + e[3] * t[3], i = r < 0 ? -1 : 1;
|
|
58
|
+
if (r *= i, r > .9995) {
|
|
59
|
+
let r = [
|
|
60
|
+
0,
|
|
61
|
+
1,
|
|
62
|
+
2,
|
|
63
|
+
3
|
|
64
|
+
].map((r) => e[r] + (i * t[r] - e[r]) * n), a = Math.hypot(...r);
|
|
65
|
+
return [
|
|
66
|
+
r[0] / a,
|
|
67
|
+
r[1] / a,
|
|
68
|
+
r[2] / a,
|
|
69
|
+
r[3] / a
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
let a = Math.acos(r), o = Math.sin((1 - n) * a) / Math.sin(a), s = i * Math.sin(n * a) / Math.sin(a);
|
|
73
|
+
return [
|
|
74
|
+
e[0] * o + t[0] * s,
|
|
75
|
+
e[1] * o + t[1] * s,
|
|
76
|
+
e[2] * o + t[2] * s,
|
|
77
|
+
e[3] * o + t[3] * s
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
function d(e, n) {
|
|
81
|
+
if (t(e, "sampleRigClip clip"), typeof e.duration != "number" || !(e.duration > 0) || e.duration === Infinity) throw RangeError(`sampleRigClip duration is ${e.duration}; pass seconds > 0`);
|
|
82
|
+
if (typeof n != "number" || !Number.isFinite(n)) throw TypeError("sampleRigClip time must be a finite number of seconds; pass one");
|
|
83
|
+
if (!Array.isArray(e.tracks)) throw TypeError("sampleRigClip tracks must be an array; pass [{bone, times, rotations}]");
|
|
84
|
+
let r = e.loop ? (n % e.duration + e.duration) % e.duration : n, i = {};
|
|
85
|
+
for (let t of e.tracks) {
|
|
86
|
+
let { bone: e, times: n, rotations: a } = t ?? {};
|
|
87
|
+
if (typeof e != "string" || !Array.isArray(n) || !Array.isArray(a) || !n.length || n.length !== a.length) throw TypeError("sampleRigClip track needs a bone name and as many rotations as times; pass {bone, times, rotations}");
|
|
88
|
+
for (let t = 1; t < n.length; t++) if (!(n[t] > n[t - 1])) throw RangeError(`sampleRigClip track ${e} times must increase; sort its keys`);
|
|
89
|
+
let o = a.map((t, n) => c(t, `sampleRigClip ${e} rotation ${n}`));
|
|
90
|
+
if (r <= n[0]) i[e] = o[0];
|
|
91
|
+
else if (r >= n[n.length - 1]) i[e] = o[o.length - 1];
|
|
92
|
+
else {
|
|
93
|
+
let t = 1;
|
|
94
|
+
for (; n[t] < r;) t++;
|
|
95
|
+
i[e] = u(o[t - 1], o[t], (r - n[t - 1]) / (n[t] - n[t - 1]));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return i;
|
|
99
|
+
}
|
|
100
|
+
var f = class {
|
|
101
|
+
#e = /* @__PURE__ */ new Map();
|
|
102
|
+
constructor(n) {
|
|
103
|
+
t(n, "VoxelRig");
|
|
104
|
+
let { bones: r, parts: i } = n;
|
|
105
|
+
if (!Array.isArray(r) || !r.length) throw TypeError("VoxelRig bones must be a non-empty array; pass [{name}]");
|
|
106
|
+
if (!Array.isArray(i)) throw TypeError("VoxelRig parts must be an array; pass [{bone, grid}]");
|
|
107
|
+
let a = [];
|
|
108
|
+
for (let e of r) {
|
|
109
|
+
if (typeof e?.name != "string" || !e.name) throw TypeError("VoxelRig bone name must be a non-empty string; pass {name}");
|
|
110
|
+
if (this.#e.has(e.name)) throw TypeError(`VoxelRig bone ${e.name} is listed twice; give each bone its own name`);
|
|
111
|
+
if (e.parent !== void 0 && !this.#e.has(e.parent)) throw TypeError(`VoxelRig bone ${e.name} has parent ${e.parent}, not listed before it; list parents first`);
|
|
112
|
+
this.#e.set(e.name, a.length), a.push({
|
|
113
|
+
name: e.name,
|
|
114
|
+
parent: e.parent,
|
|
115
|
+
joint: s(e.joint, `VoxelRig bone ${e.name} joint`)
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
this.bones = a, this.parts = i.map((t, n) => {
|
|
119
|
+
if (typeof t?.bone != "string" || !this.#e.has(t.bone)) throw TypeError(`VoxelRig part ${n} is on an unknown bone ${t?.bone}; name one of the bones`);
|
|
120
|
+
return e(t.grid, Infinity, `VoxelRig part ${n} grid`), {
|
|
121
|
+
bone: t.bone,
|
|
122
|
+
grid: t.grid,
|
|
123
|
+
offset: s(t.offset, `VoxelRig part ${n} offset`)
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
matrices(e = {}) {
|
|
128
|
+
t(e, "VoxelRig pose");
|
|
129
|
+
let n = e.rotations ?? {};
|
|
130
|
+
if (typeof n != "object") throw TypeError("VoxelRig pose rotations must be an object; pass {bone: [x, y, z, w]}");
|
|
131
|
+
for (let e of Object.keys(n)) if (!this.#e.has(e)) throw TypeError(`VoxelRig pose turns unknown bone ${e}; name one of the bones`);
|
|
132
|
+
let o = s(e.translation, "VoxelRig pose translation"), l = r(), u = [];
|
|
133
|
+
l.set(o, 12);
|
|
134
|
+
for (let e of this.bones) {
|
|
135
|
+
let t = e.parent === void 0 ? l : u[this.#e.get(e.parent)], r = Object.hasOwn(n, e.name) ? n[e.name] : void 0;
|
|
136
|
+
u.push(r === void 0 ? t : i(t, a(c(r, `VoxelRig pose ${e.name}`), e.joint)));
|
|
137
|
+
}
|
|
138
|
+
return this.parts.map((e) => {
|
|
139
|
+
let t = r();
|
|
140
|
+
return t.set(e.offset, 12), i(u[this.#e.get(e.bone)], t);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
bake(e = {}, r = {}) {
|
|
144
|
+
t(r, "VoxelRig.bake");
|
|
145
|
+
let i = n(r), a = this.matrices(e), s = [
|
|
146
|
+
Infinity,
|
|
147
|
+
Infinity,
|
|
148
|
+
Infinity
|
|
149
|
+
], c = [
|
|
150
|
+
-Infinity,
|
|
151
|
+
-Infinity,
|
|
152
|
+
-Infinity
|
|
153
|
+
], l = this.parts.map((e, t) => {
|
|
154
|
+
let n = a[t], [r, i, o] = e.grid.size, l = [
|
|
155
|
+
Infinity,
|
|
156
|
+
Infinity,
|
|
157
|
+
Infinity,
|
|
158
|
+
-Infinity,
|
|
159
|
+
-Infinity,
|
|
160
|
+
-Infinity
|
|
161
|
+
];
|
|
162
|
+
for (let e = 0; e < 8; e++) {
|
|
163
|
+
let t = e & 1 ? r : 0, a = e & 2 ? i : 0, s = e & 4 ? o : 0;
|
|
164
|
+
for (let e = 0; e < 3; e++) {
|
|
165
|
+
let r = n[e] * t + n[4 + e] * a + n[8 + e] * s + n[12 + e];
|
|
166
|
+
l[e] = Math.min(l[e], r), l[3 + e] = Math.max(l[3 + e], r);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
for (let e = 0; e < 3; e++) l[e] = Math.floor(l[e] + 1e-6), l[3 + e] = Math.ceil(l[3 + e] - 1e-6), s[e] = Math.min(s[e], l[e]), c[e] = Math.max(c[e], l[3 + e]);
|
|
170
|
+
return l;
|
|
171
|
+
});
|
|
172
|
+
if (!this.parts.length) return {
|
|
173
|
+
grid: {
|
|
174
|
+
size: [
|
|
175
|
+
1,
|
|
176
|
+
1,
|
|
177
|
+
1
|
|
178
|
+
],
|
|
179
|
+
voxels: /* @__PURE__ */ new Uint8Array(1)
|
|
180
|
+
},
|
|
181
|
+
origin: [
|
|
182
|
+
0,
|
|
183
|
+
0,
|
|
184
|
+
0
|
|
185
|
+
]
|
|
186
|
+
};
|
|
187
|
+
let u = [
|
|
188
|
+
c[0] - s[0],
|
|
189
|
+
c[1] - s[1],
|
|
190
|
+
c[2] - s[2]
|
|
191
|
+
], d = u[0] * u[1] * u[2];
|
|
192
|
+
if (d > i) throw RangeError(`VoxelRig.bake needs ${d} cells, over maxCells ${i}; raise maxCells`);
|
|
193
|
+
let f = new Uint8Array(d);
|
|
194
|
+
return this.parts.forEach((e, t) => {
|
|
195
|
+
let n = o(a[t]), r = l[t], [i, c, d] = e.grid.size, p = e.grid.voxels;
|
|
196
|
+
for (let e = r[1]; e < r[4]; e++) for (let t = r[2]; t < r[5]; t++) for (let a = r[0]; a < r[3]; a++) {
|
|
197
|
+
let r = a + .5, o = e + .5, l = t + .5, m = Math.floor(n[0] * r + n[4] * o + n[8] * l + n[12] + 1e-9), h = Math.floor(n[1] * r + n[5] * o + n[9] * l + n[13] + 1e-9), g = Math.floor(n[2] * r + n[6] * o + n[10] * l + n[14] + 1e-9);
|
|
198
|
+
if (m < 0 || h < 0 || g < 0 || m >= i || h >= c || g >= d) continue;
|
|
199
|
+
let _ = p[m + i * (g + d * h)];
|
|
200
|
+
_ && (f[a - s[0] + u[0] * (t - s[2] + u[2] * (e - s[1]))] = _);
|
|
201
|
+
}
|
|
202
|
+
}), {
|
|
203
|
+
grid: {
|
|
204
|
+
size: u,
|
|
205
|
+
voxels: f
|
|
206
|
+
},
|
|
207
|
+
origin: [
|
|
208
|
+
s[0],
|
|
209
|
+
s[1],
|
|
210
|
+
s[2]
|
|
211
|
+
]
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
//#endregion
|
|
216
|
+
export { f as VoxelRig, l as rigRotation, d as sampleRigClip, u as slerpRotation };
|
package/dist/select.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { GridSource } from './edit.js';
|
|
2
|
+
import { type GridLimits } from './grid.js';
|
|
3
|
+
/** Normalized device coordinates (-1 to 1, Y up). Convert pointer pixels before calling. */
|
|
4
|
+
export type ScreenPoint = readonly [number, number];
|
|
5
|
+
/** A closed screen shape, boundary included. Lasso uses the even-odd rule, including self-crossings. */
|
|
6
|
+
export type ScreenShape = {
|
|
7
|
+
type: 'rectangle';
|
|
8
|
+
min: ScreenPoint;
|
|
9
|
+
max: ScreenPoint;
|
|
10
|
+
} | {
|
|
11
|
+
type: 'lasso';
|
|
12
|
+
points: readonly ScreenPoint[];
|
|
13
|
+
};
|
|
14
|
+
/** Projection and visibility options for voxel selections. */
|
|
15
|
+
export interface ScreenSelectionOptions extends GridLimits {
|
|
16
|
+
/** Optional column-major grid-local to world matrix. Default identity. */
|
|
17
|
+
modelMatrix?: ArrayLike<number>;
|
|
18
|
+
/** Only cells whose projected centre ray hits that cell first. Default false (select through). */
|
|
19
|
+
visibleOnly?: boolean;
|
|
20
|
+
/** Cancellation checked before iteration and for each occupied cell. */
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
/**
|
|
23
|
+
* The backend the camera matrix was made for, `engine.backend`: `'webgpu'` (default, depth 0 to 1, reversed) or
|
|
24
|
+
* `'webgl2'` (OpenGL depth -1 to 1). The frustum's near and far tests and `visibleOnly` read depth by it.
|
|
25
|
+
*/
|
|
26
|
+
backend?: 'webgpu' | 'webgl2';
|
|
27
|
+
}
|
|
28
|
+
/** Colour similarity options. */
|
|
29
|
+
export interface ColorSelectionOptions extends ScreenSelectionOptions {
|
|
30
|
+
/** Euclidean RGBA8 distance divided by 510, in [0, 1]. Default 0 (exact RGBA). */
|
|
31
|
+
tolerance?: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Selects filled cells whose centres project into a closed shape and the camera frustum, as indexes like ./edit.
|
|
35
|
+
* Camera is the column-major view-projection (engine.camera.matrix), with optional modelMatrix for placement.
|
|
36
|
+
* Sparse results preserve indexes above 2^32; dense and sparse results are sorted in ascending index order.
|
|
37
|
+
* @throws RangeError for invalid lasso, budgets or overflowing transforms; pass bounded selection options.
|
|
38
|
+
* @throws TypeError for invalid matrices, points or options; pass finite NDC inputs.
|
|
39
|
+
*/
|
|
40
|
+
export declare function screenSelection(source: GridSource, camera: ArrayLike<number>, shape: ScreenShape, options?: ScreenSelectionOptions): Float64Array;
|
|
41
|
+
/** Rectangle selection; either drag direction, boundary included, select-through by default. */
|
|
42
|
+
export declare function rectangleSelection(source: GridSource, camera: ArrayLike<number>, a: ScreenPoint, b: ScreenPoint, options?: ScreenSelectionOptions): Float64Array;
|
|
43
|
+
/** Closed even-odd lasso selection, including concave shapes and boundary centres. */
|
|
44
|
+
export declare function lassoSelection(source: GridSource, camera: ArrayLike<number>, points: readonly ScreenPoint[], options?: ScreenSelectionOptions): Float64Array;
|
|
45
|
+
/**
|
|
46
|
+
* Selects cells within a camera/shape whose RGBA matches reference palette index (1 to 255) within tolerance.
|
|
47
|
+
* Equal RGBA entries match even with different palette indexes; this is global within the shape, not a flood.
|
|
48
|
+
* @throws RangeError for invalid reference, palette or tolerance; pass 1024 palette bytes and a tolerance in [0,1].
|
|
49
|
+
*/
|
|
50
|
+
export declare function colorSelection(source: GridSource, camera: ArrayLike<number>, shape: ScreenShape, palette: Uint8Array, reference: number, options?: ColorSelectionOptions): Float64Array;
|
package/dist/select.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { a as e, c as t, f as n, i as r, l as i, o as a } from "./grid-B2V0TGLV.js";
|
|
2
|
+
import { SparseGrid as o } from "./sparse.js";
|
|
3
|
+
import { pickRay as s, raycastGrid as c } from "./pick.js";
|
|
4
|
+
//#region src/select.ts
|
|
5
|
+
function l(e) {
|
|
6
|
+
if (!Array.isArray(e) || e.length !== 2 || !Number.isFinite(e[0]) || !Number.isFinite(e[1])) throw TypeError("screen point must have two finite coordinates; pass [ndcX, ndcY]");
|
|
7
|
+
}
|
|
8
|
+
function u(e) {
|
|
9
|
+
if (e?.length !== 16 || !Array.from(e).every(Number.isFinite)) throw TypeError("selection matrix must hold 16 finite numbers; pass the camera view-projection matrix");
|
|
10
|
+
return Array.from(e);
|
|
11
|
+
}
|
|
12
|
+
function d(e) {
|
|
13
|
+
if (i(e, "selection shape"), e.type === "rectangle") {
|
|
14
|
+
l(e.min), l(e.max);
|
|
15
|
+
let t = [Math.min(e.min[0], e.max[0]), Math.min(e.min[1], e.max[1])], n = [Math.max(e.min[0], e.max[0]), Math.max(e.min[1], e.max[1])];
|
|
16
|
+
return (e, r) => e >= t[0] && e <= n[0] && r >= t[1] && r <= n[1];
|
|
17
|
+
}
|
|
18
|
+
if (e.type !== "lasso" || !Array.isArray(e.points) || e.points.length < 3 || e.points.length > 65536) throw RangeError("selection lasso needs 3 to 65536 points; pass a closed polygon without repeating the endpoint");
|
|
19
|
+
e.points.forEach(l);
|
|
20
|
+
let t = e.points.map((e) => [...e]);
|
|
21
|
+
return (e, n) => {
|
|
22
|
+
let r = !1;
|
|
23
|
+
for (let i = 0, a = t.length - 1; i < t.length; a = i++) {
|
|
24
|
+
let [o, s] = t[a], [c, l] = t[i];
|
|
25
|
+
if ((e - o) * (l - s) - (n - s) * (c - o) === 0 && e >= Math.min(o, c) && e <= Math.max(o, c) && n >= Math.min(s, l) && n <= Math.max(s, l)) return !0;
|
|
26
|
+
s > n != l > n && e < o + (n - s) * (c - o) / (l - s) && (r = !r);
|
|
27
|
+
}
|
|
28
|
+
return r;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function f(l, f, p, m, h) {
|
|
32
|
+
i(m, "screen selection"), m.signal?.throwIfAborted();
|
|
33
|
+
let g = a(m), _ = l instanceof o;
|
|
34
|
+
if (!_) t(l, g);
|
|
35
|
+
else if (l.stats.filled > g) throw RangeError("screen selection exceeds maxCells; raise the occupied-cell budget");
|
|
36
|
+
let v = u(f);
|
|
37
|
+
if (m.modelMatrix !== void 0) {
|
|
38
|
+
let e = u(m.modelMatrix), t = Array(16).fill(0);
|
|
39
|
+
for (let n = 0; n < 4; n++) for (let r = 0; r < 4; r++) for (let i = 0; i < 4; i++) t[n * 4 + r] += v[i * 4 + r] * e[n * 4 + i];
|
|
40
|
+
if (v = t, !v.every(Number.isFinite)) throw RangeError("selection transform overflowed; rescale the model and camera");
|
|
41
|
+
}
|
|
42
|
+
let y = m.backend ?? "webgpu", b = n(y, "screen selection")[0] < 0, x = m.visibleOnly ?? !1;
|
|
43
|
+
if (typeof x != "boolean") throw TypeError("visibleOnly must be boolean; pass true or false");
|
|
44
|
+
let S = d(p), C = [];
|
|
45
|
+
function w(t, n, r, i) {
|
|
46
|
+
if (m.signal?.throwIfAborted(), !h(i)) return;
|
|
47
|
+
let a = t + .5, o = n + .5, u = r + .5, d = v[3] * a + v[7] * o + v[11] * u + v[15];
|
|
48
|
+
if (!(d > 0)) return;
|
|
49
|
+
let f = v[0] * a + v[4] * o + v[8] * u + v[12], p = v[1] * a + v[5] * o + v[9] * u + v[13], g = v[2] * a + v[6] * o + v[10] * u + v[14];
|
|
50
|
+
if (!Number.isFinite(f + p + g + d) || f < -d || f > d || p < -d || p > d || g < (b ? -d : 0) || g > d) return;
|
|
51
|
+
let _ = f / d, w = p / d;
|
|
52
|
+
if (S(_, w)) {
|
|
53
|
+
if (x) {
|
|
54
|
+
let e = s(v, _, w, { backend: y }), i = e && c(l, e);
|
|
55
|
+
if (!i || i.cell[0] !== t || i.cell[1] !== n || i.cell[2] !== r) return;
|
|
56
|
+
}
|
|
57
|
+
C.push(e(l.size, t, n, r));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (_) for (let [e, t, n, r] of l.filled()) w(e, t, n, r);
|
|
61
|
+
else for (let e = 0; e < l.voxels.length; e++) l.voxels[e] && w(...r(l.size, e), l.voxels[e]);
|
|
62
|
+
return Float64Array.from(C.sort((e, t) => e - t));
|
|
63
|
+
}
|
|
64
|
+
function p(e, t, n, r = {}) {
|
|
65
|
+
return f(e, t, n, r, () => !0);
|
|
66
|
+
}
|
|
67
|
+
function m(e, t, n, r, i = {}) {
|
|
68
|
+
return p(e, t, {
|
|
69
|
+
type: "rectangle",
|
|
70
|
+
min: n,
|
|
71
|
+
max: r
|
|
72
|
+
}, i);
|
|
73
|
+
}
|
|
74
|
+
function h(e, t, n, r = {}) {
|
|
75
|
+
return p(e, t, {
|
|
76
|
+
type: "lasso",
|
|
77
|
+
points: n
|
|
78
|
+
}, r);
|
|
79
|
+
}
|
|
80
|
+
function g(e, t, n, r, a, o = {}) {
|
|
81
|
+
i(o, "color selection");
|
|
82
|
+
let s = o.tolerance ?? 0;
|
|
83
|
+
if (!(r instanceof Uint8Array) || r.length !== 1024 || !Number.isInteger(a) || a < 1 || a > 255) throw RangeError("colour selection needs 1024 palette bytes and index 1 to 255; pass a valid palette and reference");
|
|
84
|
+
if (!Number.isFinite(s) || s < 0 || s > 1) throw RangeError("colour tolerance must be 0 to 1; pass a normalized RGBA distance");
|
|
85
|
+
let c = /* @__PURE__ */ new Uint8Array(256), l = (s * 510) ** 2;
|
|
86
|
+
for (let e = 1; e < 256; e++) {
|
|
87
|
+
let t = 0;
|
|
88
|
+
for (let n = 0; n < 4; n++) t += (r[4 * e + n] - r[4 * a + n]) ** 2;
|
|
89
|
+
t <= l && (c[e] = 1);
|
|
90
|
+
}
|
|
91
|
+
return f(e, t, n, o, (e) => !!c[e]);
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
export { g as colorSelection, h as lassoSelection, m as rectangleSelection, p as screenSelection };
|