@margoengine/loaders 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 +1665 -2
- 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/crease.d.ts +7 -0
- package/dist/crease.js +67 -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-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -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 +166 -0
- package/dist/dae.js +644 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +122 -0
- package/dist/dxf.js +985 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.js +120 -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 +92 -0
- package/dist/gcode.js +369 -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 +125 -0
- package/dist/mtl.js +550 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +70 -0
- package/dist/nrrd.js +785 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +145 -0
- package/dist/obj.js +462 -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 +32 -0
- package/dist/render.js +181 -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 +45 -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-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +379 -0
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +162 -0
- package/dist/threemf-materials.js +497 -0
- package/dist/threemf-mesh.d.ts +75 -0
- package/dist/threemf-mesh.js +422 -0
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +170 -0
- package/dist/threemf.js +609 -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 +108 -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 +289 -4
package/dist/md2.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type LoadedGeometry, type ParseLimits } from './types.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
/** Options of {@link parseMD2}: the shared size limits. `maxVertices` counts vertices of all frames together. */
|
|
4
|
+
export interface MD2Options extends ParseLimits {
|
|
5
|
+
}
|
|
6
|
+
/** One frame of {@link MD2Model.frames}, with the same vertices and triangles as the first. */
|
|
7
|
+
export interface MD2Frame {
|
|
8
|
+
name: string;
|
|
9
|
+
positions: Float32Array;
|
|
10
|
+
normals: Float32Array;
|
|
11
|
+
}
|
|
12
|
+
/** One run of frames that share a name, such as `run1` to `run6`. */
|
|
13
|
+
export interface MD2Clip {
|
|
14
|
+
name: string;
|
|
15
|
+
/** First frame. */
|
|
16
|
+
start: number;
|
|
17
|
+
/** Frames in the clip. */
|
|
18
|
+
count: number;
|
|
19
|
+
}
|
|
20
|
+
/** What {@link parseMD2} returns: the first frame as a mesh, and every frame beside it. Coordinates are the file's, Z up. */
|
|
21
|
+
export interface MD2Model extends LoadedGeometry {
|
|
22
|
+
kind: 'mesh';
|
|
23
|
+
normals: Float32Array;
|
|
24
|
+
uvs: Float32Array;
|
|
25
|
+
indices: Uint32Array;
|
|
26
|
+
frames: MD2Frame[];
|
|
27
|
+
clips: MD2Clip[];
|
|
28
|
+
/** The skin paths the file names; `loadTextures` from `@margoengine/loaders/textures` reads them. */
|
|
29
|
+
skins: string[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Reads a Quake II `.md2` model. Corners that share a vertex but not a texture coordinate become separate vertices, so
|
|
33
|
+
* every frame has the same layout as the mesh and frames can be blended as morph targets. Frames with one name and a
|
|
34
|
+
* running number form a clip. Normals are computed from each frame's triangles. The GL command list is not needed.
|
|
35
|
+
* `loadTextures` from `@margoengine/loaders/textures` reads the skins, and `parsePCX` from `@margoengine/decoders/pcx`
|
|
36
|
+
* decodes the PCX files Quake II uses.
|
|
37
|
+
*
|
|
38
|
+
* @param input The file's bytes.
|
|
39
|
+
* @param options The shared size limits.
|
|
40
|
+
* @returns The first frame as a mesh, with `frames`, `clips` and `skins`.
|
|
41
|
+
* @throws LoaderError with code `header`, `truncated`, `range` or `limit`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { parseMD2 } from '@margoengine/loaders/md2';
|
|
46
|
+
*
|
|
47
|
+
* const bytes = await (await fetch('/models/knight.md2')).arrayBuffer();
|
|
48
|
+
* const { frames, clips, skins } = parseMD2(bytes);
|
|
49
|
+
* console.log(frames.length, clips[0]?.name, skins[0]);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function parseMD2(input: BinaryInput, options?: MD2Options): MD2Model;
|
package/dist/md2.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Quake II .md2 models: skins, texture coordinates, triangles and compressed vertex frames.
|
|
2
|
+
import { computeVertexNormals } from './normals.js';
|
|
3
|
+
import { LoaderError } from './types.js';
|
|
4
|
+
import { CornerWelder, latin1, limits, toBytes } from './util.js';
|
|
5
|
+
const F = 'MD2';
|
|
6
|
+
const HEADER = 68;
|
|
7
|
+
function text(bytes, start, size) {
|
|
8
|
+
let end = start;
|
|
9
|
+
while (end < start + size && bytes[end])
|
|
10
|
+
end++;
|
|
11
|
+
return latin1(bytes, start, end);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reads a Quake II `.md2` model. Corners that share a vertex but not a texture coordinate become separate vertices, so
|
|
15
|
+
* every frame has the same layout as the mesh and frames can be blended as morph targets. Frames with one name and a
|
|
16
|
+
* running number form a clip. Normals are computed from each frame's triangles. The GL command list is not needed.
|
|
17
|
+
* `loadTextures` from `@margoengine/loaders/textures` reads the skins, and `parsePCX` from `@margoengine/decoders/pcx`
|
|
18
|
+
* decodes the PCX files Quake II uses.
|
|
19
|
+
*
|
|
20
|
+
* @param input The file's bytes.
|
|
21
|
+
* @param options The shared size limits.
|
|
22
|
+
* @returns The first frame as a mesh, with `frames`, `clips` and `skins`.
|
|
23
|
+
* @throws LoaderError with code `header`, `truncated`, `range` or `limit`.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { parseMD2 } from '@margoengine/loaders/md2';
|
|
28
|
+
*
|
|
29
|
+
* const bytes = await (await fetch('/models/knight.md2')).arrayBuffer();
|
|
30
|
+
* const { frames, clips, skins } = parseMD2(bytes);
|
|
31
|
+
* console.log(frames.length, clips[0]?.name, skins[0]);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function parseMD2(input, options = {}) {
|
|
35
|
+
const bytes = toBytes(input, F), { maxVertices, maxTriangles } = limits(options, F);
|
|
36
|
+
if (bytes.length < HEADER)
|
|
37
|
+
throw new LoaderError(F, 'header', 'the file is too small');
|
|
38
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), int = (index) => view.getInt32(index * 4, true);
|
|
39
|
+
if (latin1(bytes, 0, 4) !== 'IDP2')
|
|
40
|
+
throw new LoaderError(F, 'header', 'missing the IDP2 magic');
|
|
41
|
+
if (int(1) !== 8)
|
|
42
|
+
throw new LoaderError(F, 'header', `version ${int(1)} is not 8`);
|
|
43
|
+
const skinWidth = int(2), skinHeight = int(3), frameSize = int(4), skinCount = int(5), vertexCount = int(6), coordCount = int(7), triangleCount = int(8), frameCount = int(10), skinsAt = int(11), coordsAt = int(12), trianglesAt = int(13), framesAt = int(14);
|
|
44
|
+
for (const count of [skinCount, vertexCount, coordCount, triangleCount, frameCount])
|
|
45
|
+
if (count < 0)
|
|
46
|
+
throw new LoaderError(F, 'header', `negative count ${count}`);
|
|
47
|
+
if (skinWidth <= 0 || skinHeight <= 0)
|
|
48
|
+
throw new LoaderError(F, 'range', 'the skin size must be positive');
|
|
49
|
+
if (!frameCount)
|
|
50
|
+
throw new LoaderError(F, 'range', 'the model has no frames');
|
|
51
|
+
if (frameSize < 40 + vertexCount * 4)
|
|
52
|
+
throw new LoaderError(F, 'range', `frame size ${frameSize} is too small`);
|
|
53
|
+
if (triangleCount > maxTriangles)
|
|
54
|
+
throw new LoaderError(F, 'limit', `triangle count ${triangleCount} exceeds the limit of ${maxTriangles}`);
|
|
55
|
+
const within = (at, size, what) => {
|
|
56
|
+
if (at < HEADER || at + size > bytes.length)
|
|
57
|
+
throw new LoaderError(F, 'truncated', `${what} run past the file`);
|
|
58
|
+
};
|
|
59
|
+
within(skinsAt, skinCount * 64, 'skins');
|
|
60
|
+
within(coordsAt, coordCount * 4, 'texture coordinates');
|
|
61
|
+
within(trianglesAt, triangleCount * 12, 'triangles');
|
|
62
|
+
within(framesAt, frameCount * frameSize, 'frames');
|
|
63
|
+
const skins = [];
|
|
64
|
+
for (let i = 0; i < skinCount; i++)
|
|
65
|
+
skins.push(text(bytes, skinsAt + i * 64, 64));
|
|
66
|
+
const welder = new CornerWelder(vertexCount), indices = new Uint32Array(triangleCount * 3);
|
|
67
|
+
for (let t = 0; t < triangleCount; t++)
|
|
68
|
+
for (let k = 0; k < 3; k++) {
|
|
69
|
+
const at = trianglesAt + t * 12, vertex = view.getUint16(at + k * 2, true), coord = view.getUint16(at + 6 + k * 2, true);
|
|
70
|
+
if (vertex >= vertexCount)
|
|
71
|
+
throw new LoaderError(F, 'range', `vertex ${vertex} is outside 0..${vertexCount - 1}`);
|
|
72
|
+
if (coord >= coordCount)
|
|
73
|
+
throw new LoaderError(F, 'range', `coordinate ${coord} is outside 0..${coordCount - 1}`);
|
|
74
|
+
// MD2 front faces wind clockwise: 0, 2, 1 makes them counter-clockwise.
|
|
75
|
+
indices[t * 3 + ((3 - k) % 3)] = welder.weld(vertex, coord, 0, 0);
|
|
76
|
+
}
|
|
77
|
+
const count = welder.count, keys = welder.keys.array;
|
|
78
|
+
if (count * frameCount > maxVertices)
|
|
79
|
+
throw new LoaderError(F, 'limit', `${count * frameCount} frame vertices exceed the limit of ${maxVertices}`);
|
|
80
|
+
const uvs = new Float32Array(count * 2);
|
|
81
|
+
for (let v = 0; v < count; v++) {
|
|
82
|
+
const at = coordsAt + keys[v * 4 + 1] * 4;
|
|
83
|
+
uvs[v * 2] = view.getInt16(at, true) / skinWidth;
|
|
84
|
+
uvs[v * 2 + 1] = view.getInt16(at + 2, true) / skinHeight;
|
|
85
|
+
}
|
|
86
|
+
const frames = [], clips = [];
|
|
87
|
+
for (let f = 0; f < frameCount; f++) {
|
|
88
|
+
const at = framesAt + f * frameSize, scale = [0, 1, 2].map((i) => view.getFloat32(at + i * 4, true)), move = [0, 1, 2].map((i) => view.getFloat32(at + 12 + i * 4, true)), name = text(bytes, at + 24, 16), positions = new Float32Array(count * 3);
|
|
89
|
+
for (let v = 0; v < count; v++) {
|
|
90
|
+
const from = at + 40 + keys[v * 4] * 4;
|
|
91
|
+
for (let c = 0; c < 3; c++)
|
|
92
|
+
positions[v * 3 + c] = bytes[from + c] * scale[c] + move[c];
|
|
93
|
+
}
|
|
94
|
+
frames.push({ name, positions, normals: computeVertexNormals(positions, indices) });
|
|
95
|
+
const stem = name.replace(/\d+$/, ''), last = clips[clips.length - 1];
|
|
96
|
+
if (last?.name === stem)
|
|
97
|
+
last.count++;
|
|
98
|
+
else
|
|
99
|
+
clips.push({ name: stem, start: f, count: 1 });
|
|
100
|
+
}
|
|
101
|
+
const [first] = frames;
|
|
102
|
+
return {
|
|
103
|
+
kind: 'mesh',
|
|
104
|
+
positions: first.positions,
|
|
105
|
+
normals: first.normals,
|
|
106
|
+
uvs,
|
|
107
|
+
indices,
|
|
108
|
+
frames,
|
|
109
|
+
clips,
|
|
110
|
+
skins,
|
|
111
|
+
};
|
|
112
|
+
}
|
package/dist/mmcif.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type BinaryInput } from './util.js';
|
|
2
|
+
import type { PDBMolecule, PDBOptions } from './pdb.js';
|
|
3
|
+
/** Coordinate loader options. The first data block is read by default. */
|
|
4
|
+
export interface MMCIFOptions extends PDBOptions {
|
|
5
|
+
/** Data block name without data_. Case insensitive. */
|
|
6
|
+
block?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Parse the first coordinate model of a PDBx/mmCIF data block in angstroms. Supports CIF 1.1 quoted and
|
|
10
|
+
* semicolon text fields, loops, author/label identifiers, first alternate locations, and identity-symmetry
|
|
11
|
+
* covalent/disulfide struct_conn records. Optional distance bonds use the PDB loader's covalent radii.
|
|
12
|
+
* Returns the same structure as parsePDB; crystal/biological assembly expansion is a separate operation.
|
|
13
|
+
*/
|
|
14
|
+
export declare function parseMMCIF(input: BinaryInput | string, options?: MMCIFOptions): PDBMolecule;
|
package/dist/mmcif.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// PDBx/mmCIF coordinate structures, written from the IUCr CIF 1.1 syntax and wwPDB dictionary.
|
|
2
|
+
import { LoaderError } from './types.js';
|
|
3
|
+
import { limits, toText } from './util.js';
|
|
4
|
+
import { elementProperties, nearbyPairs } from './molecule-common.js';
|
|
5
|
+
const F = 'mmCIF';
|
|
6
|
+
function fail(message) {
|
|
7
|
+
throw new LoaderError(F, 'syntax', message);
|
|
8
|
+
}
|
|
9
|
+
const present = (v) => (v === '.' || v === '?' ? '' : (v ?? ''));
|
|
10
|
+
const control = (t) => t.bare && /^(?:_|data_|save_|loop_$|stop_$|global_$)/i.test(t.value);
|
|
11
|
+
/** One token of lookahead, retaining whether a reserved-looking value was quoted. */
|
|
12
|
+
class Scanner {
|
|
13
|
+
#at = 0;
|
|
14
|
+
#next;
|
|
15
|
+
constructor(text) {
|
|
16
|
+
this.text = text;
|
|
17
|
+
}
|
|
18
|
+
peek() {
|
|
19
|
+
return (this.#next ??= this.#read());
|
|
20
|
+
}
|
|
21
|
+
take() {
|
|
22
|
+
const t = this.peek();
|
|
23
|
+
this.#next = undefined;
|
|
24
|
+
return t;
|
|
25
|
+
}
|
|
26
|
+
#read() {
|
|
27
|
+
const s = this.text;
|
|
28
|
+
while (this.#at < s.length) {
|
|
29
|
+
if (/\s/.test(s[this.#at])) {
|
|
30
|
+
this.#at++;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (s[this.#at] === '#') {
|
|
34
|
+
while (this.#at < s.length && s[this.#at] !== '\n')
|
|
35
|
+
this.#at++;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
if (this.#at >= s.length)
|
|
41
|
+
return undefined;
|
|
42
|
+
const start = this.#at++, first = s[start];
|
|
43
|
+
if (first === ';' && (start === 0 || s[start - 1] === '\n')) {
|
|
44
|
+
const end = s.indexOf('\n;', this.#at);
|
|
45
|
+
if (end < 0)
|
|
46
|
+
fail('unterminated semicolon text field');
|
|
47
|
+
this.#at = end + 2;
|
|
48
|
+
if (this.#at < s.length && !/\s/.test(s[this.#at]))
|
|
49
|
+
fail('text-field delimiter must end its line');
|
|
50
|
+
return {
|
|
51
|
+
value: s
|
|
52
|
+
.slice(start + 1, end)
|
|
53
|
+
.replace(/\r\n/g, '\n')
|
|
54
|
+
.replace(/\r$/, ''),
|
|
55
|
+
bare: false,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (first === "'" || first === '"') {
|
|
59
|
+
while (this.#at < s.length) {
|
|
60
|
+
const at = this.#at++;
|
|
61
|
+
if (s[at] === '\n' || s[at] === '\r')
|
|
62
|
+
fail('quoted fields cannot contain line breaks');
|
|
63
|
+
if (s[at] === first && (this.#at === s.length || /\s/.test(s[this.#at])))
|
|
64
|
+
return { value: s.slice(start + 1, at), bare: false };
|
|
65
|
+
}
|
|
66
|
+
fail('unterminated quoted field');
|
|
67
|
+
}
|
|
68
|
+
while (this.#at < s.length && !/\s/.test(s[this.#at]))
|
|
69
|
+
this.#at++;
|
|
70
|
+
return { value: s.slice(start, this.#at), bare: true };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const get = (row, ...names) => {
|
|
74
|
+
for (const n of names) {
|
|
75
|
+
const value = row.get(n.toLowerCase()) ?? '';
|
|
76
|
+
if (value)
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
return '';
|
|
80
|
+
};
|
|
81
|
+
const key = (...v) => JSON.stringify(v);
|
|
82
|
+
/**
|
|
83
|
+
* Parse the first coordinate model of a PDBx/mmCIF data block in angstroms. Supports CIF 1.1 quoted and
|
|
84
|
+
* semicolon text fields, loops, author/label identifiers, first alternate locations, and identity-symmetry
|
|
85
|
+
* covalent/disulfide struct_conn records. Optional distance bonds use the PDB loader's covalent radii.
|
|
86
|
+
* Returns the same structure as parsePDB; crystal/biological assembly expansion is a separate operation.
|
|
87
|
+
*/
|
|
88
|
+
export function parseMMCIF(input, options = {}) {
|
|
89
|
+
const scanner = new Scanner(toText(input, F)), { maxVertices } = limits(options, F), atoms = [], xyz = [], colors = [], radii = [], seen = new Set(), warnings = new Set(), connections = [], labels = new Map(), authors = new Map();
|
|
90
|
+
let selected = false, found = false, model = '', title = '';
|
|
91
|
+
function row(category, r) {
|
|
92
|
+
if (category === '_struct_conn') {
|
|
93
|
+
connections.push(r);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (category !== '_atom_site')
|
|
97
|
+
return;
|
|
98
|
+
const current = get(r, '_atom_site.pdbx_pdb_model_num') || '1';
|
|
99
|
+
if (!model)
|
|
100
|
+
model = current;
|
|
101
|
+
if (model !== current) {
|
|
102
|
+
warnings.add('only the first MODEL is read');
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const chain = get(r, '_atom_site.auth_asym_id', '_atom_site.label_asym_id'), residueId = get(r, '_atom_site.auth_seq_id', '_atom_site.label_seq_id'), name = get(r, '_atom_site.auth_atom_id', '_atom_site.label_atom_id'), residue = get(r, '_atom_site.auth_comp_id', '_atom_site.label_comp_id'), insertionCode = get(r, '_atom_site.pdbx_pdb_ins_code'), alternateLocation = get(r, '_atom_site.label_alt_id'), identity = key(chain, residueId, insertionCode, residue, name);
|
|
106
|
+
if (!name || !residue || !residueId)
|
|
107
|
+
fail('atom_site is missing atom or residue identifiers');
|
|
108
|
+
if (seen.has(identity))
|
|
109
|
+
return;
|
|
110
|
+
seen.add(identity);
|
|
111
|
+
if (atoms.length >= maxVertices)
|
|
112
|
+
throw new LoaderError(F, 'limit', `atom count exceeds ${maxVertices}`);
|
|
113
|
+
const values = ['x', 'y', 'z'].map((k) => {
|
|
114
|
+
const text = get(r, '_atom_site.cartn_' + k), v = Number(text);
|
|
115
|
+
if (!text || !Number.isFinite(v) || !Number.isFinite(Math.fround(v)))
|
|
116
|
+
fail('invalid Cartesian coordinate');
|
|
117
|
+
return v;
|
|
118
|
+
});
|
|
119
|
+
const element = get(r, '_atom_site.type_symbol').toUpperCase();
|
|
120
|
+
if (!/^[A-Z]{1,2}$/.test(element))
|
|
121
|
+
fail('atom_site requires a chemical element symbol');
|
|
122
|
+
const [rgb, radius] = elementProperties(element), id = get(r, '_atom_site.id'), serial = Number(id), seq = Number(residueId), labelSeq = Number(get(r, '_atom_site.label_seq_id'));
|
|
123
|
+
const atom = {
|
|
124
|
+
serial: id && Number.isSafeInteger(serial) ? serial : atoms.length + 1,
|
|
125
|
+
id,
|
|
126
|
+
name,
|
|
127
|
+
element,
|
|
128
|
+
residue,
|
|
129
|
+
residueId,
|
|
130
|
+
insertionCode,
|
|
131
|
+
alternateLocation,
|
|
132
|
+
sequence: Number.isSafeInteger(seq) ? seq : Number.isSafeInteger(labelSeq) ? labelSeq : 0,
|
|
133
|
+
chain,
|
|
134
|
+
hetero: get(r, '_atom_site.group_pdb').toUpperCase() === 'HETATM',
|
|
135
|
+
};
|
|
136
|
+
authors.set(key(chain, residueId, insertionCode, name), atoms.length);
|
|
137
|
+
labels.set(key(get(r, '_atom_site.label_asym_id'), get(r, '_atom_site.label_seq_id'), get(r, '_atom_site.label_comp_id'), get(r, '_atom_site.label_atom_id')), atoms.length);
|
|
138
|
+
atoms.push(atom);
|
|
139
|
+
xyz.push(...values);
|
|
140
|
+
radii.push(radius);
|
|
141
|
+
colors.push((rgb >> 16) / 255, ((rgb >> 8) & 255) / 255, (rgb & 255) / 255);
|
|
142
|
+
}
|
|
143
|
+
const single = new Map();
|
|
144
|
+
const flush = () => {
|
|
145
|
+
for (const [cat, r] of single)
|
|
146
|
+
row(cat, r);
|
|
147
|
+
single.clear();
|
|
148
|
+
};
|
|
149
|
+
while (scanner.peek()) {
|
|
150
|
+
const t = scanner.take();
|
|
151
|
+
const low = t.value.toLowerCase();
|
|
152
|
+
if (t.bare && low.startsWith('data_')) {
|
|
153
|
+
if (selected) {
|
|
154
|
+
flush();
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
selected = options.block === undefined ? !found : low.slice(5) === options.block.toLowerCase();
|
|
158
|
+
if (selected)
|
|
159
|
+
found = true;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (!selected)
|
|
163
|
+
continue;
|
|
164
|
+
if (t.bare && low === 'loop_') {
|
|
165
|
+
flush();
|
|
166
|
+
const tags = [];
|
|
167
|
+
while (scanner.peek()?.bare && scanner.peek().value.startsWith('_'))
|
|
168
|
+
tags.push(scanner.take().value.toLowerCase());
|
|
169
|
+
if (!tags.length || new Set(tags).size !== tags.length)
|
|
170
|
+
fail('loop requires unique data names');
|
|
171
|
+
const category = tags[0].split('.')[0];
|
|
172
|
+
if (tags.some((tag) => tag.split('.')[0] !== category))
|
|
173
|
+
fail('loop mixes data categories');
|
|
174
|
+
while (scanner.peek() && !control(scanner.peek())) {
|
|
175
|
+
const r = new Map();
|
|
176
|
+
for (const tag of tags) {
|
|
177
|
+
const value = scanner.take();
|
|
178
|
+
if (!value || control(value))
|
|
179
|
+
fail('incomplete loop row');
|
|
180
|
+
r.set(tag, value.bare ? present(value.value) : value.value);
|
|
181
|
+
}
|
|
182
|
+
row(category, r);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else if (t.bare && low.startsWith('_')) {
|
|
186
|
+
const value = scanner.take();
|
|
187
|
+
if (!value || control(value))
|
|
188
|
+
fail('data name has no value');
|
|
189
|
+
const cat = low.split('.')[0], r = single.get(cat) ?? new Map();
|
|
190
|
+
if (r.has(low))
|
|
191
|
+
fail('duplicate data name');
|
|
192
|
+
r.set(low, value.bare ? present(value.value) : value.value);
|
|
193
|
+
single.set(cat, r);
|
|
194
|
+
if (low === '_struct.title')
|
|
195
|
+
title = value.bare ? present(value.value) : value.value;
|
|
196
|
+
}
|
|
197
|
+
else if (t.bare && low === 'stop_')
|
|
198
|
+
continue;
|
|
199
|
+
else
|
|
200
|
+
fail('unexpected token or unsupported save frame in coordinate data block');
|
|
201
|
+
}
|
|
202
|
+
flush();
|
|
203
|
+
if (!atoms.length)
|
|
204
|
+
fail('selected data block has no atom_site coordinates');
|
|
205
|
+
const bonds = [], known = new Set(), add = (a, b) => {
|
|
206
|
+
const low = Math.min(a, b), high = Math.max(a, b), id = `${low}:${high}`;
|
|
207
|
+
if (low === high || known.has(id))
|
|
208
|
+
return;
|
|
209
|
+
known.add(id);
|
|
210
|
+
bonds.push(low, high);
|
|
211
|
+
};
|
|
212
|
+
for (const r of connections) {
|
|
213
|
+
const type = get(r, '_struct_conn.conn_type_id').toLowerCase();
|
|
214
|
+
if (!type.startsWith('covale') && type !== 'disulf')
|
|
215
|
+
continue;
|
|
216
|
+
const partner = (n) => {
|
|
217
|
+
const p = `_struct_conn.ptnr${n}_`, extra = `_struct_conn.pdbx_ptnr${n}_`, symmetry = get(r, p + 'symmetry');
|
|
218
|
+
if (symmetry && symmetry !== '1_555') {
|
|
219
|
+
warnings.add('symmetry-related struct_conn bonds require crystal expansion');
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
const alt = get(r, extra + 'label_alt_id');
|
|
223
|
+
const index = authors.get(key(get(r, p + 'auth_asym_id'), get(r, p + 'auth_seq_id'), get(r, extra + 'pdb_ins_code'), get(r, p + 'auth_atom_id', p + 'label_atom_id'))) ??
|
|
224
|
+
labels.get(key(get(r, p + 'label_asym_id'), get(r, p + 'label_seq_id'), get(r, p + 'label_comp_id'), get(r, p + 'label_atom_id')));
|
|
225
|
+
return index !== undefined && (!alt || atoms[index].alternateLocation === alt) ? index : undefined;
|
|
226
|
+
};
|
|
227
|
+
const a = partner(1), b = partner(2);
|
|
228
|
+
if (a !== undefined && b !== undefined)
|
|
229
|
+
add(a, b);
|
|
230
|
+
else
|
|
231
|
+
warnings.add('struct_conn names atoms not present in the selected model/conformer');
|
|
232
|
+
}
|
|
233
|
+
const positions = Float32Array.from(xyz), sizes = Float32Array.from(radii);
|
|
234
|
+
if (options.guessBonds ?? true)
|
|
235
|
+
nearbyPairs(positions, sizes, add);
|
|
236
|
+
return {
|
|
237
|
+
kind: 'points',
|
|
238
|
+
positions,
|
|
239
|
+
atoms,
|
|
240
|
+
radii: sizes,
|
|
241
|
+
colors: Float32Array.from(colors),
|
|
242
|
+
bonds: Uint32Array.from(bonds),
|
|
243
|
+
warnings: [...warnings],
|
|
244
|
+
...(title ? { title } : {}),
|
|
245
|
+
};
|
|
246
|
+
}
|
package/dist/mmd.d.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { type LoadedGeometry, type ParseLimits } from './types.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
/** Options of the MMD parsers: the shared size limits, the space conversion and the bounds below. */
|
|
4
|
+
export interface MMDOptions extends ParseLimits {
|
|
5
|
+
/** Metres per MMD unit. Default 0.08. */
|
|
6
|
+
scale?: number;
|
|
7
|
+
/** Mirror MMD's left-handed Z into the engine's space and keep the winding. Default true. */
|
|
8
|
+
rightHanded?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** A material of a PMX or PMD model, with the run of triangles it draws. */
|
|
11
|
+
export interface MMDMaterial {
|
|
12
|
+
name: string;
|
|
13
|
+
nameEnglish: string;
|
|
14
|
+
diffuse: [number, number, number, number];
|
|
15
|
+
specular: [number, number, number];
|
|
16
|
+
specularPower: number;
|
|
17
|
+
ambient: [number, number, number];
|
|
18
|
+
/** Both faces are drawn (PMX flag 1; PMD: a diffuse alpha under 1 does not imply it). */
|
|
19
|
+
doubleSided: boolean;
|
|
20
|
+
/** Outline drawing: PMX flag 16 or PMD `edge_flag`. */
|
|
21
|
+
edge: boolean;
|
|
22
|
+
edgeColor: [number, number, number, number];
|
|
23
|
+
edgeSize: number;
|
|
24
|
+
/** Texture path (PMX: from the texture table; PMD: the part before `*`), or undefined. */
|
|
25
|
+
texture?: string;
|
|
26
|
+
/** Sphere map path and mode (0 none, 1 multiply, 2 add, 3 sub-texture). */
|
|
27
|
+
sphere?: string;
|
|
28
|
+
sphereMode: number;
|
|
29
|
+
/** A shared toon number 0 to 9 (`toon<n+1>.bmp`), or a texture path. */
|
|
30
|
+
toon?: number | string;
|
|
31
|
+
/** Number of triangles; the material's group of `geometry` has this many. */
|
|
32
|
+
triangles: number;
|
|
33
|
+
}
|
|
34
|
+
/** A bone of a PMX or PMD model: its rest position, parent, flags, grant and IK chain. */
|
|
35
|
+
export interface MMDBone {
|
|
36
|
+
name: string;
|
|
37
|
+
nameEnglish: string;
|
|
38
|
+
/** Rest position in the engine's space. */
|
|
39
|
+
position: [number, number, number];
|
|
40
|
+
/** -1 for a root. */
|
|
41
|
+
parent: number;
|
|
42
|
+
/** PMX deform layer (0 for PMD). */
|
|
43
|
+
layer: number;
|
|
44
|
+
/** PMX flags (see the specification); PMD bones set `visible`, `rotatable` and `ik`. */
|
|
45
|
+
rotatable: boolean;
|
|
46
|
+
translatable: boolean;
|
|
47
|
+
visible: boolean;
|
|
48
|
+
/** PMD bone type 0 to 9, or -1 for PMX. */
|
|
49
|
+
type: number;
|
|
50
|
+
/** Tail bone index, or -1 when the tail is an offset. */
|
|
51
|
+
tail: number;
|
|
52
|
+
tailOffset?: [number, number, number];
|
|
53
|
+
/** Inherit rotation or translation from this bone (PMX grant), at `inheritRate`. */
|
|
54
|
+
inheritRotation: boolean;
|
|
55
|
+
inheritTranslation: boolean;
|
|
56
|
+
inherit: number;
|
|
57
|
+
inheritRate: number;
|
|
58
|
+
afterPhysics: boolean;
|
|
59
|
+
ik?: {
|
|
60
|
+
target: number;
|
|
61
|
+
iterations: number;
|
|
62
|
+
limitAngle: number;
|
|
63
|
+
links: {
|
|
64
|
+
bone: number;
|
|
65
|
+
limit?: {
|
|
66
|
+
min: number[];
|
|
67
|
+
max: number[];
|
|
68
|
+
};
|
|
69
|
+
}[];
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/** A morph of a PMX or PMD model: its panel, type and raw offsets. */
|
|
73
|
+
export interface MMDMorph {
|
|
74
|
+
name: string;
|
|
75
|
+
nameEnglish: string;
|
|
76
|
+
/** Panel 0 system, 1 eyebrows, 2 eyes, 3 mouth, 4 other. */
|
|
77
|
+
panel: number;
|
|
78
|
+
type: 'group' | 'vertex' | 'bone' | 'uv' | 'uv1' | 'uv2' | 'uv3' | 'uv4' | 'material' | 'flip' | 'impulse';
|
|
79
|
+
/** The offsets, as the type reads them: `{morph, rate}`, `{vertex, offset}`, `{bone, translation, rotation}`, and so on. */
|
|
80
|
+
offsets: Record<string, unknown>[];
|
|
81
|
+
}
|
|
82
|
+
/** A rigid body, as data only: this package never simulates it. Positions and sizes are in the engine's units. */
|
|
83
|
+
export interface MMDRigidBody {
|
|
84
|
+
name: string;
|
|
85
|
+
nameEnglish: string;
|
|
86
|
+
bone: number;
|
|
87
|
+
group: number;
|
|
88
|
+
/** Bit mask of groups it does not collide with. */
|
|
89
|
+
noCollide: number;
|
|
90
|
+
shape: 'sphere' | 'box' | 'capsule';
|
|
91
|
+
size: [number, number, number];
|
|
92
|
+
position: [number, number, number];
|
|
93
|
+
/** Euler angles in radians. */
|
|
94
|
+
rotation: [number, number, number];
|
|
95
|
+
mass: number;
|
|
96
|
+
linearDamping: number;
|
|
97
|
+
angularDamping: number;
|
|
98
|
+
restitution: number;
|
|
99
|
+
friction: number;
|
|
100
|
+
/** 'bone' follows the bone, 'physics' is driven, 'physicsBone' is driven and aligned to the bone. */
|
|
101
|
+
mode: 'bone' | 'physics' | 'physicsBone';
|
|
102
|
+
}
|
|
103
|
+
/** A joint between two rigid bodies, as data only. */
|
|
104
|
+
export interface MMDJoint {
|
|
105
|
+
name: string;
|
|
106
|
+
nameEnglish: string;
|
|
107
|
+
type: number;
|
|
108
|
+
bodyA: number;
|
|
109
|
+
bodyB: number;
|
|
110
|
+
position: [number, number, number];
|
|
111
|
+
rotation: [number, number, number];
|
|
112
|
+
limitPositionMin: [number, number, number];
|
|
113
|
+
limitPositionMax: [number, number, number];
|
|
114
|
+
limitRotationMin: [number, number, number];
|
|
115
|
+
limitRotationMax: [number, number, number];
|
|
116
|
+
springPosition: [number, number, number];
|
|
117
|
+
springRotation: [number, number, number];
|
|
118
|
+
}
|
|
119
|
+
/** A parsed PMD or PMX model. */
|
|
120
|
+
export interface LoadedMMD {
|
|
121
|
+
format: 'PMD' | 'PMX';
|
|
122
|
+
version: number;
|
|
123
|
+
name: string;
|
|
124
|
+
nameEnglish: string;
|
|
125
|
+
comment: string;
|
|
126
|
+
commentEnglish: string;
|
|
127
|
+
/** Triangles; one group per material (`properties.materialIndex`). `attributes` may hold `uv1` to `uv4` (4 each) and `edgeScale`. */
|
|
128
|
+
geometry: LoadedGeometry;
|
|
129
|
+
/** Four bone indices per vertex and their weights; `skinning` names the PMX weight type used. */
|
|
130
|
+
joints: Uint16Array;
|
|
131
|
+
weights: Float32Array;
|
|
132
|
+
/** SDEF vertices: `vertices` index them, `data` holds C, R0 and R1 (9 values each) in the engine's space. */
|
|
133
|
+
sdef?: {
|
|
134
|
+
vertices: Uint32Array;
|
|
135
|
+
data: Float32Array;
|
|
136
|
+
};
|
|
137
|
+
materials: MMDMaterial[];
|
|
138
|
+
/** PMX texture table (PMD has none: textures are on the materials). */
|
|
139
|
+
textures: string[];
|
|
140
|
+
bones: MMDBone[];
|
|
141
|
+
morphs: MMDMorph[];
|
|
142
|
+
rigidBodies: MMDRigidBody[];
|
|
143
|
+
joints_: MMDJoint[];
|
|
144
|
+
warnings: string[];
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Parses a PMD model (Polygon Model Data 1.0): vertices with two-bone weights, triangles by material, bones and IK,
|
|
148
|
+
* morphs, the optional English names, and rigid bodies and joints as data. Shift-JIS names are decoded with
|
|
149
|
+
* `TextDecoder('shift_jis')`.
|
|
150
|
+
*/
|
|
151
|
+
export declare function parsePMD(input: BinaryInput, options?: MMDOptions): LoadedMMD;
|
|
152
|
+
/**
|
|
153
|
+
* Parses a PMX 2.0 or 2.1 model: vertices with BDEF1, BDEF2, BDEF4, SDEF and QDEF weights and up to four extra UV sets,
|
|
154
|
+
* the texture table, materials with edge, sphere and toon settings, bones with IK, grants and axes, every morph kind,
|
|
155
|
+
* and rigid bodies and joints as data. Soft bodies (2.1) are not read. Index sizes of 1, 2 and 4 bytes are honoured.
|
|
156
|
+
*/
|
|
157
|
+
export declare function parsePMX(input: BinaryInput, options?: MMDOptions): LoadedMMD;
|
|
158
|
+
/** Reads a PMD or a PMX file, by its signature. */
|
|
159
|
+
export declare function parseMMD(input: BinaryInput, options?: MMDOptions): LoadedMMD;
|
|
160
|
+
/** Options of {@link parseVMD}: the space conversion and the bounds on counts. */
|
|
161
|
+
export interface VMDOptions {
|
|
162
|
+
/** Metres per MMD unit. Default 0.08. */
|
|
163
|
+
scale?: number;
|
|
164
|
+
/** Mirror MMD's left-handed Z into the engine's space. Default true. */
|
|
165
|
+
rightHanded?: boolean;
|
|
166
|
+
/** Most keys of one kind. Default 2,000,000. */
|
|
167
|
+
maxKeys?: number;
|
|
168
|
+
}
|
|
169
|
+
/** The keys of one named bone, in frame order. `interpolation` holds 64 bytes a key (see {@link sampleVMDBone}). */
|
|
170
|
+
export interface VMDBoneTrack {
|
|
171
|
+
name: string;
|
|
172
|
+
frames: Uint32Array;
|
|
173
|
+
/** xyz a key, in metres. */
|
|
174
|
+
positions: Float32Array;
|
|
175
|
+
/** Quaternion x, y, z, w a key. */
|
|
176
|
+
rotations: Float32Array;
|
|
177
|
+
interpolation: Uint8Array;
|
|
178
|
+
}
|
|
179
|
+
/** The weights of one named morph, in frame order. */
|
|
180
|
+
export interface VMDMorphTrack {
|
|
181
|
+
name: string;
|
|
182
|
+
frames: Uint32Array;
|
|
183
|
+
weights: Float32Array;
|
|
184
|
+
}
|
|
185
|
+
/** Camera keys, in frame order. `rotation` is MMD's Euler angles (radians); `distance` is from `position` back to the eye. */
|
|
186
|
+
export interface VMDCameraTrack {
|
|
187
|
+
frames: Uint32Array;
|
|
188
|
+
distance: Float32Array;
|
|
189
|
+
/** The point looked at, xyz a key, in metres. */
|
|
190
|
+
position: Float32Array;
|
|
191
|
+
/** Euler angles xyz a key, mirrored with the space. */
|
|
192
|
+
rotation: Float32Array;
|
|
193
|
+
/** Vertical field of view in radians. */
|
|
194
|
+
fov: Float32Array;
|
|
195
|
+
/** false when the key turns perspective off. */
|
|
196
|
+
perspective: Uint8Array;
|
|
197
|
+
/** 24 bytes a key. */
|
|
198
|
+
interpolation: Uint8Array;
|
|
199
|
+
}
|
|
200
|
+
/** A parsed VMD motion. Frames run at 30 a second. */
|
|
201
|
+
export interface LoadedVMD {
|
|
202
|
+
/** The model the motion was made for. */
|
|
203
|
+
modelName: string;
|
|
204
|
+
fps: 30;
|
|
205
|
+
bones: VMDBoneTrack[];
|
|
206
|
+
morphs: VMDMorphTrack[];
|
|
207
|
+
/** Empty for a motion with no camera keys. */
|
|
208
|
+
camera: VMDCameraTrack | undefined;
|
|
209
|
+
lights: {
|
|
210
|
+
frame: number;
|
|
211
|
+
color: [number, number, number];
|
|
212
|
+
position: [number, number, number];
|
|
213
|
+
}[];
|
|
214
|
+
shadows: {
|
|
215
|
+
frame: number;
|
|
216
|
+
mode: number;
|
|
217
|
+
distance: number;
|
|
218
|
+
}[];
|
|
219
|
+
/** IK on/off switches by frame. */
|
|
220
|
+
ik: {
|
|
221
|
+
frame: number;
|
|
222
|
+
show: boolean;
|
|
223
|
+
states: {
|
|
224
|
+
name: string;
|
|
225
|
+
enabled: boolean;
|
|
226
|
+
}[];
|
|
227
|
+
}[];
|
|
228
|
+
/** The last frame of any track. */
|
|
229
|
+
duration: number;
|
|
230
|
+
}
|
|
231
|
+
/** Parses a VMD motion or camera clip: bone, morph, camera, light, shadow and IK keys. Tracks are grouped by name. */
|
|
232
|
+
export declare function parseVMD(input: BinaryInput, options?: VMDOptions): LoadedVMD;
|
|
233
|
+
/** A cubic Bezier easing through (0,0), (x1,y1), (x2,y2) and (1,1), with the control values 0 to 127 as VMD stores them. */
|
|
234
|
+
export declare function vmdBezier(x1: number, y1: number, x2: number, y2: number, t: number): number;
|
|
235
|
+
/**
|
|
236
|
+
* Samples a bone track at `frame` (fractional frames allowed): position and rotation between the two keys around it,
|
|
237
|
+
* each eased by the later key's Bezier curves (the X, Y, Z and rotation curves are the interleaved first 16 bytes of its
|
|
238
|
+
* `interpolation`: x1, y1, x2, y2 of curve k are at bytes k, k + 4, k + 8, k + 12). Before the first key and after the last
|
|
239
|
+
* it holds that key. Returns `{position, rotation}` in the track's space, or undefined for an empty track.
|
|
240
|
+
*/
|
|
241
|
+
export declare function sampleVMDBone(track: VMDBoneTrack, frame: number): {
|
|
242
|
+
position: [number, number, number];
|
|
243
|
+
rotation: [number, number, number, number];
|
|
244
|
+
} | undefined;
|