@margoengine/loaders 0.0.0-stage → 0.1.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 +1198 -3
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +161 -0
- package/dist/dae.js +633 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf.d.ts +81 -0
- package/dist/dxf.js +774 -0
- package/dist/engine.d.ts +36 -0
- package/dist/engine.js +117 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +55 -0
- package/dist/gcode.js +278 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +120 -0
- package/dist/mtl.js +469 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +63 -0
- package/dist/nrrd.js +743 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +143 -0
- package/dist/obj.js +456 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +27 -0
- package/dist/render.js +153 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +42 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +514 -0
- package/dist/threemf-materials.d.ts +75 -0
- package/dist/threemf-materials.js +314 -0
- package/dist/threemf-mesh.d.ts +60 -0
- package/dist/threemf-mesh.js +201 -0
- package/dist/threemf.d.ts +101 -0
- package/dist/threemf.js +415 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +246 -6
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type ParseLimits } from './types.js';
|
|
2
|
+
export declare const DEFAULT_MAX_VERTICES = 8000000;
|
|
3
|
+
export declare const DEFAULT_MAX_TRIANGLES = 16000000;
|
|
4
|
+
/** Input accepted by every parser. */
|
|
5
|
+
export type BinaryInput = ArrayBuffer | ArrayBufferView;
|
|
6
|
+
export declare function toBytes(input: BinaryInput, format: string): Uint8Array;
|
|
7
|
+
export interface Limits {
|
|
8
|
+
maxVertices: number;
|
|
9
|
+
maxTriangles: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function limits(options: ParseLimits | undefined, format: string): Limits;
|
|
12
|
+
export declare function checkCount(value: number, max: number, format: string, label: string): number;
|
|
13
|
+
/** Decodes bytes one-to-one into UTF-16 code units, so string offsets equal byte offsets. */
|
|
14
|
+
export declare function latin1(bytes: Uint8Array, start?: number, end?: number): string;
|
|
15
|
+
/** Returns the offset of the next line feed at or after `from`, searching up to `limit`, or -1. */
|
|
16
|
+
export declare function findLineEnd(bytes: Uint8Array, from: number, limit: number): number;
|
|
17
|
+
/** Whitespace token scanner over text decoded with `latin1`. */
|
|
18
|
+
export declare class TextScanner {
|
|
19
|
+
readonly text: string;
|
|
20
|
+
readonly format: string;
|
|
21
|
+
pos: number;
|
|
22
|
+
constructor(text: string, format: string, start?: number);
|
|
23
|
+
skipSpace(): void;
|
|
24
|
+
get done(): boolean;
|
|
25
|
+
token(): string | null;
|
|
26
|
+
number(what: string): number;
|
|
27
|
+
integer(what: string): number;
|
|
28
|
+
/** Returns the rest of the current line (without the line break) and moves past it. */
|
|
29
|
+
line(): string | null;
|
|
30
|
+
}
|
|
31
|
+
/** Splits a line into tokens on whitespace, commas and semicolons. */
|
|
32
|
+
export declare function splitFields(line: string): string[];
|
|
33
|
+
/** Parses a numeric field, rejecting garbage but allowing nan/inf spellings. */
|
|
34
|
+
export declare function parseNumber(token: string, format: string, where: string): number;
|
|
35
|
+
/** Append-only typed array. */
|
|
36
|
+
export declare class Growable<T extends Float32Array | Uint32Array> {
|
|
37
|
+
private readonly make;
|
|
38
|
+
array: T;
|
|
39
|
+
length: number;
|
|
40
|
+
constructor(make: (size: number) => T, capacity?: number);
|
|
41
|
+
reserve(extra: number): void;
|
|
42
|
+
push(value: number): void;
|
|
43
|
+
push3(a: number, b: number, c: number): void;
|
|
44
|
+
result(): T;
|
|
45
|
+
}
|
|
46
|
+
export declare const floats: (capacity?: number) => Growable<Float32Array>;
|
|
47
|
+
export declare const uints: (capacity?: number) => Growable<Uint32Array>;
|
|
48
|
+
/**
|
|
49
|
+
* Fan-triangulates a polygon of `corners` indices read through `at`, validating each index.
|
|
50
|
+
* Returns the number of triangles written.
|
|
51
|
+
*/
|
|
52
|
+
export declare function triangulate(indices: Growable<Uint32Array>, corners: number, at: (corner: number) => number, vertexCount: number, format: string, triangleLimit: number): number;
|
|
53
|
+
/** Normalizes 0..255 integer colors to [0,1] when any component exceeds 1. */
|
|
54
|
+
export declare function normalizeColors(colors: Float32Array): void;
|
|
55
|
+
export declare function throwIfAborted(signal: AbortSignal | undefined, format: string): void;
|
|
56
|
+
/** Decodes text input: strings pass through, bytes decode as UTF-8 without a byte order mark. */
|
|
57
|
+
export declare function toText(input: BinaryInput | string, format: string): string;
|
|
58
|
+
/**
|
|
59
|
+
* Resolves `path` against the folder of `from` (both `/` or `\` separated, relative to the root file) and removes
|
|
60
|
+
* `.` and `..` segments. Absolute paths and URLs (`/x`, `C:/x`, `https://x`) come back with forward slashes only.
|
|
61
|
+
*/
|
|
62
|
+
export declare function resolvePath(from: string, path: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Gives every distinct (position, a, b, c) index tuple one output vertex, so corners that share a position but differ
|
|
65
|
+
* in UV, normal or color index stay separate (seams are kept). Pass 0xffffffff for an absent index.
|
|
66
|
+
*/
|
|
67
|
+
export declare class CornerWelder {
|
|
68
|
+
private readonly heads;
|
|
69
|
+
private readonly next;
|
|
70
|
+
/** Four indices per output vertex: position, a, b, c. */
|
|
71
|
+
readonly keys: Growable<Uint32Array<ArrayBufferLike>>;
|
|
72
|
+
constructor(positionCount: number);
|
|
73
|
+
get count(): number;
|
|
74
|
+
weld(p: number, a: number, b: number, c: number): number;
|
|
75
|
+
}
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
export const DEFAULT_MAX_VERTICES = 8_000_000;
|
|
3
|
+
export const DEFAULT_MAX_TRIANGLES = 16_000_000;
|
|
4
|
+
export function toBytes(input, format) {
|
|
5
|
+
if (input instanceof Uint8Array)
|
|
6
|
+
return input;
|
|
7
|
+
if (input instanceof ArrayBuffer)
|
|
8
|
+
return new Uint8Array(input);
|
|
9
|
+
if (ArrayBuffer.isView(input))
|
|
10
|
+
return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
11
|
+
throw new LoaderError(format, 'range', 'expected an ArrayBuffer or Uint8Array');
|
|
12
|
+
}
|
|
13
|
+
export function limits(options, format) {
|
|
14
|
+
const check = (value, fallback, label) => {
|
|
15
|
+
if (value === undefined)
|
|
16
|
+
return fallback;
|
|
17
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
18
|
+
throw new LoaderError(format, 'range', `${label} must be a non-negative integer`);
|
|
19
|
+
return value;
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
maxVertices: check(options?.maxVertices, DEFAULT_MAX_VERTICES, 'maxVertices'),
|
|
23
|
+
maxTriangles: check(options?.maxTriangles, DEFAULT_MAX_TRIANGLES, 'maxTriangles'),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function checkCount(value, max, format, label) {
|
|
27
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
28
|
+
throw new LoaderError(format, 'header', `invalid ${label} count ${value}`);
|
|
29
|
+
if (value > max)
|
|
30
|
+
throw new LoaderError(format, 'limit', `${label} count ${value} exceeds the limit of ${max}`);
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
/** Decodes bytes one-to-one into UTF-16 code units, so string offsets equal byte offsets. */
|
|
34
|
+
export function latin1(bytes, start = 0, end = bytes.length) {
|
|
35
|
+
let text = '';
|
|
36
|
+
for (let i = start; i < end; i += 0x8000)
|
|
37
|
+
text += String.fromCharCode.apply(null, bytes.subarray(i, Math.min(end, i + 0x8000)));
|
|
38
|
+
return text;
|
|
39
|
+
}
|
|
40
|
+
/** Returns the offset of the next line feed at or after `from`, searching up to `limit`, or -1. */
|
|
41
|
+
export function findLineEnd(bytes, from, limit) {
|
|
42
|
+
const end = Math.min(bytes.length, limit);
|
|
43
|
+
for (let i = from; i < end; i++)
|
|
44
|
+
if (bytes[i] === 10)
|
|
45
|
+
return i;
|
|
46
|
+
return -1;
|
|
47
|
+
}
|
|
48
|
+
const isSpace = (code) => code === 32 || (code >= 9 && code <= 13);
|
|
49
|
+
/** Whitespace token scanner over text decoded with `latin1`. */
|
|
50
|
+
export class TextScanner {
|
|
51
|
+
constructor(text, format, start = 0) {
|
|
52
|
+
this.text = text;
|
|
53
|
+
this.format = format;
|
|
54
|
+
this.pos = start;
|
|
55
|
+
}
|
|
56
|
+
skipSpace() {
|
|
57
|
+
const text = this.text;
|
|
58
|
+
let pos = this.pos;
|
|
59
|
+
while (pos < text.length && isSpace(text.charCodeAt(pos)))
|
|
60
|
+
pos++;
|
|
61
|
+
this.pos = pos;
|
|
62
|
+
}
|
|
63
|
+
get done() {
|
|
64
|
+
this.skipSpace();
|
|
65
|
+
return this.pos >= this.text.length;
|
|
66
|
+
}
|
|
67
|
+
token() {
|
|
68
|
+
this.skipSpace();
|
|
69
|
+
const text = this.text, start = this.pos;
|
|
70
|
+
if (start >= text.length)
|
|
71
|
+
return null;
|
|
72
|
+
let pos = start;
|
|
73
|
+
while (pos < text.length && !isSpace(text.charCodeAt(pos)))
|
|
74
|
+
pos++;
|
|
75
|
+
this.pos = pos;
|
|
76
|
+
return text.slice(start, pos);
|
|
77
|
+
}
|
|
78
|
+
number(what) {
|
|
79
|
+
const token = this.token();
|
|
80
|
+
if (token === null)
|
|
81
|
+
throw new LoaderError(this.format, 'truncated', `unexpected end of data while reading ${what}`);
|
|
82
|
+
const value = Number(token);
|
|
83
|
+
if (Number.isNaN(value) && !/^[+-]?nan$/i.test(token))
|
|
84
|
+
throw new LoaderError(this.format, 'syntax', `invalid number "${token.slice(0, 32)}" in ${what}`);
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
integer(what) {
|
|
88
|
+
const value = this.number(what);
|
|
89
|
+
if (!Number.isSafeInteger(value))
|
|
90
|
+
throw new LoaderError(this.format, 'syntax', `expected an integer in ${what}, got ${value}`);
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
/** Returns the rest of the current line (without the line break) and moves past it. */
|
|
94
|
+
line() {
|
|
95
|
+
const text = this.text;
|
|
96
|
+
if (this.pos >= text.length)
|
|
97
|
+
return null;
|
|
98
|
+
let end = text.indexOf('\n', this.pos);
|
|
99
|
+
if (end < 0)
|
|
100
|
+
end = text.length;
|
|
101
|
+
const line = text.slice(this.pos, end);
|
|
102
|
+
this.pos = end + 1;
|
|
103
|
+
return line.endsWith('\r') ? line.slice(0, -1) : line;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Splits a line into tokens on whitespace, commas and semicolons. */
|
|
107
|
+
export function splitFields(line) {
|
|
108
|
+
const fields = line.trim().split(/[\s,;]+/);
|
|
109
|
+
return fields.length === 1 && fields[0] === '' ? [] : fields;
|
|
110
|
+
}
|
|
111
|
+
/** Parses a numeric field, rejecting garbage but allowing nan/inf spellings. */
|
|
112
|
+
export function parseNumber(token, format, where) {
|
|
113
|
+
const value = Number(token);
|
|
114
|
+
if (Number.isNaN(value) && !/^[+-]?nan$/i.test(token))
|
|
115
|
+
throw new LoaderError(format, 'syntax', `invalid number "${token.slice(0, 32)}" ${where}`);
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
/** Append-only typed array. */
|
|
119
|
+
export class Growable {
|
|
120
|
+
constructor(make, capacity = 1024) {
|
|
121
|
+
this.make = make;
|
|
122
|
+
this.length = 0;
|
|
123
|
+
this.array = make(Math.max(16, capacity));
|
|
124
|
+
}
|
|
125
|
+
reserve(extra) {
|
|
126
|
+
const needed = this.length + extra;
|
|
127
|
+
if (needed <= this.array.length)
|
|
128
|
+
return;
|
|
129
|
+
let size = this.array.length * 2;
|
|
130
|
+
while (size < needed)
|
|
131
|
+
size *= 2;
|
|
132
|
+
const next = this.make(size);
|
|
133
|
+
next.set(this.array.subarray(0, this.length));
|
|
134
|
+
this.array = next;
|
|
135
|
+
}
|
|
136
|
+
push(value) {
|
|
137
|
+
if (this.length === this.array.length)
|
|
138
|
+
this.reserve(1);
|
|
139
|
+
this.array[this.length++] = value;
|
|
140
|
+
}
|
|
141
|
+
push3(a, b, c) {
|
|
142
|
+
if (this.length + 3 > this.array.length)
|
|
143
|
+
this.reserve(3);
|
|
144
|
+
const array = this.array, length = this.length;
|
|
145
|
+
array[length] = a;
|
|
146
|
+
array[length + 1] = b;
|
|
147
|
+
array[length + 2] = c;
|
|
148
|
+
this.length = length + 3;
|
|
149
|
+
}
|
|
150
|
+
result() {
|
|
151
|
+
return this.array.slice(0, this.length);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
export const floats = (capacity) => new Growable((size) => new Float32Array(size), capacity);
|
|
155
|
+
export const uints = (capacity) => new Growable((size) => new Uint32Array(size), capacity);
|
|
156
|
+
/**
|
|
157
|
+
* Fan-triangulates a polygon of `corners` indices read through `at`, validating each index.
|
|
158
|
+
* Returns the number of triangles written.
|
|
159
|
+
*/
|
|
160
|
+
export function triangulate(indices, corners, at, vertexCount, format, triangleLimit) {
|
|
161
|
+
if (corners < 3)
|
|
162
|
+
return 0;
|
|
163
|
+
const first = at(0);
|
|
164
|
+
if (!(first >= 0 && first < vertexCount && Number.isInteger(first)))
|
|
165
|
+
throw new LoaderError(format, 'range', `face index ${first} is outside 0..${vertexCount - 1}`);
|
|
166
|
+
let previous = at(1);
|
|
167
|
+
if (!(previous >= 0 && previous < vertexCount && Number.isInteger(previous)))
|
|
168
|
+
throw new LoaderError(format, 'range', `face index ${previous} is outside 0..${vertexCount - 1}`);
|
|
169
|
+
if (indices.length / 3 + corners - 2 > triangleLimit)
|
|
170
|
+
throw new LoaderError(format, 'limit', `triangle count exceeds the limit of ${triangleLimit}`);
|
|
171
|
+
for (let corner = 2; corner < corners; corner++) {
|
|
172
|
+
const current = at(corner);
|
|
173
|
+
if (!(current >= 0 && current < vertexCount && Number.isInteger(current)))
|
|
174
|
+
throw new LoaderError(format, 'range', `face index ${current} is outside 0..${vertexCount - 1}`);
|
|
175
|
+
indices.push3(first, previous, current);
|
|
176
|
+
previous = current;
|
|
177
|
+
}
|
|
178
|
+
return corners - 2;
|
|
179
|
+
}
|
|
180
|
+
/** Normalizes 0..255 integer colors to [0,1] when any component exceeds 1. */
|
|
181
|
+
export function normalizeColors(colors) {
|
|
182
|
+
let max = 0;
|
|
183
|
+
for (let i = 0; i < colors.length; i++)
|
|
184
|
+
if (colors[i] > max)
|
|
185
|
+
max = colors[i];
|
|
186
|
+
if (max > 1)
|
|
187
|
+
for (let i = 0; i < colors.length; i++)
|
|
188
|
+
colors[i] /= 255;
|
|
189
|
+
}
|
|
190
|
+
export function throwIfAborted(signal, format) {
|
|
191
|
+
if (signal?.aborted)
|
|
192
|
+
throw new LoaderError(format, 'aborted', 'aborted');
|
|
193
|
+
}
|
|
194
|
+
/** Decodes text input: strings pass through, bytes decode as UTF-8 without a byte order mark. */
|
|
195
|
+
export function toText(input, format) {
|
|
196
|
+
return typeof input === 'string' ? input : new TextDecoder().decode(toBytes(input, format));
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Resolves `path` against the folder of `from` (both `/` or `\` separated, relative to the root file) and removes
|
|
200
|
+
* `.` and `..` segments. Absolute paths and URLs (`/x`, `C:/x`, `https://x`) come back with forward slashes only.
|
|
201
|
+
*/
|
|
202
|
+
export function resolvePath(from, path) {
|
|
203
|
+
path = path.replaceAll('\\', '/');
|
|
204
|
+
if (/^(\/|[a-z][a-z\d+.-]*:)/i.test(path))
|
|
205
|
+
return path;
|
|
206
|
+
const parts = from.replaceAll('\\', '/').split('/').slice(0, -1);
|
|
207
|
+
for (const part of path.split('/'))
|
|
208
|
+
if (part === '..' && parts.length && parts[parts.length - 1] !== '..')
|
|
209
|
+
parts.pop();
|
|
210
|
+
else if (part && part !== '.')
|
|
211
|
+
parts.push(part);
|
|
212
|
+
return parts.join('/');
|
|
213
|
+
}
|
|
214
|
+
const NONE = 0xffffffff;
|
|
215
|
+
/**
|
|
216
|
+
* Gives every distinct (position, a, b, c) index tuple one output vertex, so corners that share a position but differ
|
|
217
|
+
* in UV, normal or color index stay separate (seams are kept). Pass 0xffffffff for an absent index.
|
|
218
|
+
*/
|
|
219
|
+
export class CornerWelder {
|
|
220
|
+
constructor(positionCount) {
|
|
221
|
+
this.next = uints();
|
|
222
|
+
/** Four indices per output vertex: position, a, b, c. */
|
|
223
|
+
this.keys = uints();
|
|
224
|
+
this.heads = new Uint32Array(positionCount).fill(NONE);
|
|
225
|
+
}
|
|
226
|
+
get count() {
|
|
227
|
+
return this.next.length;
|
|
228
|
+
}
|
|
229
|
+
weld(p, a, b, c) {
|
|
230
|
+
const keys = this.keys.array, next = this.next.array;
|
|
231
|
+
for (let v = this.heads[p]; v !== NONE; v = next[v])
|
|
232
|
+
if (keys[v * 4 + 1] === a && keys[v * 4 + 2] === b && keys[v * 4 + 3] === c)
|
|
233
|
+
return v;
|
|
234
|
+
const v = this.next.length;
|
|
235
|
+
this.next.push(this.heads[p]);
|
|
236
|
+
this.heads[p] = v;
|
|
237
|
+
this.keys.push3(p, a, b);
|
|
238
|
+
this.keys.push(c);
|
|
239
|
+
return v;
|
|
240
|
+
}
|
|
241
|
+
}
|
package/dist/vdb.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type BinaryInput } from './util.js';
|
|
2
|
+
/** A value type of a grid this reader decodes. `half` grids arrive as `float`, with `half` set on the grid. */
|
|
3
|
+
export type VDBValueType = 'float' | 'double' | 'int32' | 'int64' | 'vec3i' | 'vec3s' | 'vec3d';
|
|
4
|
+
/** Values in the grid's own precision; 64-bit integers arrive as `Float64Array`, exact up to 2^53. */
|
|
5
|
+
export type VDBData = Float32Array<ArrayBuffer> | Float64Array<ArrayBuffer> | Int32Array<ArrayBuffer>;
|
|
6
|
+
/** A value of the grid: a number, or `[x, y, z]` for a vector grid. */
|
|
7
|
+
export type VDBValue = number | [number, number, number];
|
|
8
|
+
/** Options of {@link parseVDB}. */
|
|
9
|
+
export interface VDBOptions {
|
|
10
|
+
/** Read only the grids with these names, such as `['density']`. The others go to `skipped`. Default every grid. */
|
|
11
|
+
grids?: readonly string[];
|
|
12
|
+
/** Maximum values kept in all leaves of the file together, counting 512 per leaf and vector component. Default 536,870,912. */
|
|
13
|
+
maxValues?: number;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
/** Options of {@link VDBGrid.dense}. */
|
|
17
|
+
export interface VDBDenseOptions {
|
|
18
|
+
/** Inclusive index box `[minX, minY, minZ, maxX, maxY, maxZ]` to fill. Default the grid's `box`. */
|
|
19
|
+
box?: readonly [number, number, number, number, number, number];
|
|
20
|
+
/** Maximum values in the array. Default 536,870,912. */
|
|
21
|
+
maxValues?: number;
|
|
22
|
+
}
|
|
23
|
+
/** One 8 by 8 by 8 block of voxels (in a default tree) with stored values. */
|
|
24
|
+
export interface VDBLeaf {
|
|
25
|
+
/** Index of the block's lowest corner. */
|
|
26
|
+
origin: [number, number, number];
|
|
27
|
+
/** Active voxels, one bit each, z fastest then y then x. */
|
|
28
|
+
mask: Uint8Array<ArrayBuffer>;
|
|
29
|
+
/** Every voxel, in the order of `mask`, inactive ones too; vector components sit together. */
|
|
30
|
+
values: VDBData;
|
|
31
|
+
}
|
|
32
|
+
/** A cube of voxels that all share one value, stored in place of a subtree. Inactive tiles equal to the background are dropped. */
|
|
33
|
+
export interface VDBTile {
|
|
34
|
+
origin: [number, number, number];
|
|
35
|
+
/** Edge length in voxels. */
|
|
36
|
+
size: number;
|
|
37
|
+
value: VDBValue;
|
|
38
|
+
active: boolean;
|
|
39
|
+
}
|
|
40
|
+
/** What {@link VDBGrid.dense} returns. Pass `data`, `dims` and `bounds` to `fig.volume()` of `@margoengine/sciviz`. */
|
|
41
|
+
export interface VDBDense {
|
|
42
|
+
/** Every voxel of the box, x fastest, then y, then z; vector components sit together. */
|
|
43
|
+
data: VDBData;
|
|
44
|
+
/** Voxels along each axis. */
|
|
45
|
+
dims: [number, number, number];
|
|
46
|
+
/** Values per voxel: 1, or 3 for a vector grid. */
|
|
47
|
+
components: number;
|
|
48
|
+
/** The inclusive index box that was filled. */
|
|
49
|
+
box: [number, number, number, number, number, number];
|
|
50
|
+
/** `[minX, maxX, minY, maxY, minZ, maxZ]` around the voxel centres in world units. */
|
|
51
|
+
bounds: [number, number, number, number, number, number];
|
|
52
|
+
}
|
|
53
|
+
/** One grid of a file. */
|
|
54
|
+
export interface VDBGrid {
|
|
55
|
+
name: string;
|
|
56
|
+
/** The tree type as written, such as `Tree_float_5_4_3`. */
|
|
57
|
+
type: string;
|
|
58
|
+
valueType: VDBValueType;
|
|
59
|
+
/** True when the file stored 16-bit values. They arrive widened to `valueType`. */
|
|
60
|
+
half: boolean;
|
|
61
|
+
/** 1, or 3 for a vector grid. */
|
|
62
|
+
components: number;
|
|
63
|
+
/** The `class` metadata, such as `fog volume` or `level set`. `unknown` when absent. */
|
|
64
|
+
gridClass: string;
|
|
65
|
+
/** The value of every voxel with nothing stored. */
|
|
66
|
+
background: VDBValue;
|
|
67
|
+
/** Column-major 4x4 from voxel index to world units, from the grid's linear transform. */
|
|
68
|
+
matrix: Float64Array<ArrayBuffer>;
|
|
69
|
+
/** World size of one voxel along each index axis. */
|
|
70
|
+
voxelSize: [number, number, number];
|
|
71
|
+
/** Inclusive index box around every active voxel, or null for a grid with none. */
|
|
72
|
+
box: [number, number, number, number, number, number] | null;
|
|
73
|
+
/** Active voxels, tiles counted by their volume. */
|
|
74
|
+
activeVoxels: number;
|
|
75
|
+
leaves: VDBLeaf[];
|
|
76
|
+
tiles: VDBTile[];
|
|
77
|
+
/** Grid metadata by name. Null prototype. */
|
|
78
|
+
meta: Record<string, unknown>;
|
|
79
|
+
/** The value at a voxel index: its leaf's value, else its tile's, else the background. */
|
|
80
|
+
at(x: number, y: number, z: number): VDBValue;
|
|
81
|
+
/** Whether the voxel at an index is active. */
|
|
82
|
+
isActive(x: number, y: number, z: number): boolean;
|
|
83
|
+
/** Fills a dense array over a box. Voxels with nothing stored hold the background. */
|
|
84
|
+
dense(options?: VDBDenseOptions): VDBDense;
|
|
85
|
+
}
|
|
86
|
+
/** What {@link parseVDB} returns. */
|
|
87
|
+
export interface VDBFile {
|
|
88
|
+
/** The file format version, such as 224. */
|
|
89
|
+
version: number;
|
|
90
|
+
/** The OpenVDB version that wrote the file, `[major, minor]`. */
|
|
91
|
+
library: [number, number];
|
|
92
|
+
uuid: string;
|
|
93
|
+
/** File metadata by name. Null prototype. */
|
|
94
|
+
meta: Record<string, unknown>;
|
|
95
|
+
grids: VDBGrid[];
|
|
96
|
+
/** Grids left out, with why: a value type this reader does not decode, a transform with no matrix, or `options.grids`. */
|
|
97
|
+
skipped: {
|
|
98
|
+
name: string;
|
|
99
|
+
type: string;
|
|
100
|
+
reason: string;
|
|
101
|
+
}[];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Reads an OpenVDB `.vdb` file into sparse grids: leaves and tiles as stored, with `at()` for lookups and `dense()` for
|
|
105
|
+
* the arrays a volume renderer takes. Reads file versions 222 and later (OpenVDB 2.0 onward).
|
|
106
|
+
*
|
|
107
|
+
* @throws LoaderError code `header` when the bytes are not a VDB file, `unsupported` for an older version or a Blosc
|
|
108
|
+
* codec other than LZ4, `truncated`, `range` or `syntax` for a damaged file, `limit` over `maxValues`, `zip` for bad
|
|
109
|
+
* compressed data, `aborted` when `signal` fires.
|
|
110
|
+
*/
|
|
111
|
+
export declare function parseVDB(input: BinaryInput, options?: VDBOptions): Promise<VDBFile>;
|