@yali-engine/gltf 0.0.0-stage → 0.4.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.
@@ -0,0 +1,230 @@
1
+ /** One drawable piece of a mesh: triangles with one material. Arrays are tightly packed. */
2
+ export interface GltfPrimitive {
3
+ /** xyz per vertex. */
4
+ readonly positions: Float32Array;
5
+ /** xyz per vertex (computed, smooth, when the file has none). */
6
+ readonly normals: Float32Array;
7
+ /** uv per vertex, or null. */
8
+ readonly uvs: Float32Array | null;
9
+ /** Triangle list (generated 0..n-1 when the file has no indices). */
10
+ readonly indices: Uint32Array;
11
+ /** Index into `materials`, or -1 for the default material. */
12
+ readonly material: number;
13
+ readonly min: readonly [number, number, number];
14
+ readonly max: readonly [number, number, number];
15
+ /**
16
+ * Coarser levels of detail from `MSFT_lod`, finest first: index lists over this
17
+ * primitive's own vertices, with their error in mesh units (`extras.yaliLodError`).
18
+ * Empty when the file has none.
19
+ */
20
+ readonly lods: readonly GltfLod[];
21
+ /**
22
+ * Skinning (`JOINTS_0` / `WEIGHTS_0`): 4 joint slots and 4 weights per vertex, weights
23
+ * normalized to sum to 1; a file's `JOINTS_1` / `WEIGHTS_1` are folded into the 4 largest
24
+ * (ADR-0083). Null for a rigid primitive.
25
+ */
26
+ readonly skin: {
27
+ readonly joints: Uint16Array;
28
+ readonly weights: Float32Array;
29
+ } | null;
30
+ /** Morph targets: per target, position deltas (xyz per vertex) and normal deltas (or null). Empty without any. */
31
+ readonly targets: readonly GltfMorphTarget[];
32
+ /**
33
+ * The primitive's clusters, packed ahead of time (`YALI_clusters`, written by `yali convert
34
+ * --clusters`; ADR-0121): `packClusters`' layout over this primitive's own vertices. Null without.
35
+ */
36
+ readonly clusters: GltfClusters | null;
37
+ /**
38
+ * The primitive's cluster hierarchy in streaming pages (`YALI_cluster_pages`, written by `yali convert
39
+ * --clusters --stream`; ADR-0134): the primitive's own vertices and indices are then its root pages'
40
+ * mesh. Null without.
41
+ */
42
+ readonly pages: GltfClusterPages | null;
43
+ }
44
+ /**
45
+ * A primitive's cluster hierarchy in pages (`YALI_cluster_pages`, ADR-0134), in `@yali-engine/render`'s
46
+ * `ClusterPages` layout: every cluster record, the page table, the root pages' bytes (in the file),
47
+ * and where the other pages are.
48
+ */
49
+ export interface GltfClusterPages {
50
+ readonly words: Uint32Array;
51
+ readonly pages: Uint32Array;
52
+ readonly roots: number;
53
+ readonly localIds: number;
54
+ readonly triangleBytes: number;
55
+ readonly corners: number;
56
+ readonly slotVertices: number;
57
+ /** The root pages' bytes: the page data's first `rootData.length` bytes. */
58
+ readonly rootData: Uint8Array;
59
+ /** The other pages' file, relative to the GLB: a page at table offset `o` is at `base + o - rootData.length` in it (one file holds every primitive's). */
60
+ readonly uri: string;
61
+ readonly base: number;
62
+ }
63
+ /**
64
+ * A primitive's clusters as `yali convert --clusters` stores them (`YALI_clusters`, ADR-0121): the
65
+ * records, the clusters' local vertex ids and their triangle bytes, in `@yali-engine/render`'s
66
+ * `PackedClusters` layout, and whether they form a cluster LOD hierarchy.
67
+ */
68
+ export interface GltfClusters {
69
+ readonly lod: boolean;
70
+ readonly words: Uint32Array;
71
+ readonly vertices: Uint32Array;
72
+ readonly triangles: Uint8Array;
73
+ readonly corners: number;
74
+ }
75
+ /** One morph target of a primitive: offsets added in proportion to its weight. */
76
+ export interface GltfMorphTarget {
77
+ readonly positions: Float32Array;
78
+ readonly normals: Float32Array | null;
79
+ }
80
+ /** One level of detail of a primitive. */
81
+ export interface GltfLod {
82
+ readonly indices: Uint32Array;
83
+ /** How far, in mesh units, this level's surface may be from the full primitive's. */
84
+ readonly error: number;
85
+ }
86
+ export interface GltfMesh {
87
+ readonly name: string;
88
+ readonly primitives: readonly GltfPrimitive[];
89
+ /** Morph target weights when nothing animates them (`mesh.weights`, else zeros). */
90
+ readonly weights: readonly number[];
91
+ }
92
+ /** A node: a local transform (TRS), an optional mesh, children. */
93
+ export interface GltfNode {
94
+ readonly name: string;
95
+ /** Index into `meshes`, or -1. */
96
+ readonly mesh: number;
97
+ /** Index into `skins` (its mesh is skinned), or -1. */
98
+ readonly skin: number;
99
+ readonly children: readonly number[];
100
+ readonly t: readonly [number, number, number];
101
+ readonly r: readonly [number, number, number, number];
102
+ readonly s: readonly [number, number, number];
103
+ }
104
+ /** A material's texture: the image it samples (index into `GltfScene.images`) and its UV set. */
105
+ export interface GltfTextureRef {
106
+ readonly image: number;
107
+ readonly texCoord: number;
108
+ }
109
+ /** A glTF metallic-roughness material (M2 increment 5, ADR-0066). */
110
+ export interface GltfMaterial {
111
+ readonly name: string;
112
+ readonly baseColor: readonly [number, number, number, number];
113
+ readonly metallic: number;
114
+ readonly roughness: number;
115
+ /** Linear emissive colour times `KHR_materials_emissive_strength`. */
116
+ readonly emissive: readonly [number, number, number];
117
+ readonly normalScale: number;
118
+ readonly occlusionStrength: number;
119
+ readonly alphaMode: 'OPAQUE' | 'MASK' | 'BLEND';
120
+ readonly alphaCutoff: number;
121
+ readonly doubleSided: boolean;
122
+ readonly baseColorTexture: GltfTextureRef | null;
123
+ readonly metallicRoughnessTexture: GltfTextureRef | null;
124
+ readonly normalTexture: GltfTextureRef | null;
125
+ readonly occlusionTexture: GltfTextureRef | null;
126
+ readonly emissiveTexture: GltfTextureRef | null;
127
+ /** KHR_materials_clearcoat (ADR-0093): the coat's strength and roughness, 0 without it. */
128
+ readonly clearcoat: number;
129
+ readonly clearcoatRoughness: number;
130
+ readonly clearcoatTexture: GltfTextureRef | null;
131
+ readonly clearcoatRoughnessTexture: GltfTextureRef | null;
132
+ /** KHR_materials_transmission: 0 without it. */
133
+ readonly transmission: number;
134
+ readonly transmissionTexture: GltfTextureRef | null;
135
+ /** KHR_materials_sheen: linear colour (black without it) and roughness. */
136
+ readonly sheenColor: readonly [number, number, number];
137
+ readonly sheenRoughness: number;
138
+ readonly sheenColorTexture: GltfTextureRef | null;
139
+ /** KHR_materials_ior (default 1.5). */
140
+ readonly ior: number;
141
+ /** KHR_materials_volume (ADR-0099): thickness (0: thin-walled), attenuation colour and distance (Infinity: none). */
142
+ readonly thickness: number;
143
+ /** The volume's thickness map (its G channel times `thickness`, ADR-0125), or null. */
144
+ readonly thicknessTexture: GltfTextureRef | null;
145
+ /** KHR_materials_dispersion (ADR-0125): how far the colours part when refracted (20 / the Abbe number; 0: none). */
146
+ readonly dispersion: number;
147
+ readonly attenuationColor: readonly [number, number, number];
148
+ readonly attenuationDistance: number;
149
+ }
150
+ /** An image a texture uses: its bytes when the file embeds it (a buffer view or data URI), else its URL. */
151
+ export interface GltfImage {
152
+ readonly mimeType: string;
153
+ readonly bytes: Uint8Array | null;
154
+ readonly url: string | null;
155
+ /**
156
+ * The sizes `yali convert` wrote of an external KTX2 (`extras.yaliSizes`, ascending, the last
157
+ * the full one): `<name>.<variant>.<size>.ktx2` beside `<name>.<variant>.ktx2`.
158
+ */
159
+ readonly sizes?: readonly number[];
160
+ }
161
+ /** A skin: the nodes its joint slots follow, and each slot's inverse bind matrix. */
162
+ export interface GltfSkin {
163
+ readonly name: string;
164
+ /** Node of each joint slot (a vertex's `JOINTS_0` values are slots). */
165
+ readonly joints: readonly number[];
166
+ /** 16 floats (column-major) per slot; identities when the file has none. */
167
+ readonly inverseBindMatrices: Float32Array;
168
+ }
169
+ /** One animated property of one node. */
170
+ export interface GltfChannel {
171
+ readonly node: number;
172
+ readonly path: 'translation' | 'rotation' | 'scale' | 'weights';
173
+ /** LINEAR or STEP (a CUBICSPLINE sampler is resampled to LINEAR at 60 Hz when parsed). */
174
+ readonly interpolation: 'LINEAR' | 'STEP';
175
+ /** Key times (seconds), increasing. */
176
+ readonly times: Float32Array;
177
+ /** Values per key: 3 (translation, scale), 4 (rotation, xyzw) or one per morph target. */
178
+ readonly values: Float32Array;
179
+ }
180
+ /** An animation: channels over nodes. */
181
+ export interface GltfAnimation {
182
+ readonly name: string;
183
+ readonly channels: readonly GltfChannel[];
184
+ }
185
+ /** A parsed glTF: plain data and typed arrays, so it crosses from a decode worker by transfer. */
186
+ export interface GltfScene {
187
+ readonly nodes: readonly GltfNode[];
188
+ readonly skins: readonly GltfSkin[];
189
+ readonly animations: readonly GltfAnimation[];
190
+ /** The default scene's root nodes. */
191
+ readonly roots: readonly number[];
192
+ readonly meshes: readonly GltfMesh[];
193
+ readonly materials: readonly GltfMaterial[];
194
+ readonly images: readonly GltfImage[];
195
+ readonly stats: {
196
+ readonly vertices: number;
197
+ readonly triangles: number;
198
+ readonly bytes: number;
199
+ };
200
+ /** Things skipped (non-triangle primitives, unsupported optional extensions). */
201
+ readonly warnings: readonly string[];
202
+ }
203
+ /** What `parseGltf` needs for a `.gltf` with external buffers. */
204
+ export interface ParseOptions {
205
+ /** Base URL that relative buffer URIs resolve against (the `.gltf` file's URL). */
206
+ readonly baseUrl?: string;
207
+ /** Fetches an external buffer (default: `fetch`). */
208
+ readonly fetchBuffer?: (url: string) => Promise<Uint8Array>;
209
+ }
210
+ /**
211
+ * Parses a glTF 2.0 asset (`.glb`, or `.gltf` with embedded or external buffers) into
212
+ * plain arrays (DESIGN.md §9; ADR-0047). Pure and allocation-heavy by nature: it runs in a
213
+ * decode worker.
214
+ *
215
+ * @example
216
+ * const scene = await parseGltf(new Uint8Array(await (await fetch('/temple.glb')).arrayBuffer()));
217
+ * scene.meshes[0].primitives[0].positions; // Float32Array
218
+ */
219
+ export declare function parseGltf(bytes: Uint8Array, options?: ParseOptions): Promise<GltfScene>;
220
+ /** Rate (keys per second) a CUBICSPLINE sampler is resampled to LINEAR at (ADR-0083). */
221
+ export declare const CUBIC_RESAMPLE_HZ = 60;
222
+ /**
223
+ * A glTF CUBICSPLINE sampler (per key: in-tangent, value, out-tangent) as LINEAR keys at
224
+ * `CUBIC_RESAMPLE_HZ` plus every original key, evaluated with the spec's Hermite form;
225
+ * rotations are normalized.
226
+ */
227
+ export declare function resampleCubic(times: Float32Array, data: Float32Array, width: number, rotation: boolean): {
228
+ times: Float32Array;
229
+ values: Float32Array;
230
+ };