@domgell/gltf-parser 1.0.1 → 1.0.2
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/.idea/copilot.data.migration.agent.xml +6 -0
- package/.idea/copilot.data.migration.ask2agent.xml +6 -0
- package/.idea/copilot.data.migration.edit.xml +6 -0
- package/.idea/gltf-parser.iml +12 -0
- package/.idea/modules.xml +8 -0
- package/package.json +2 -2
- package/src/Parser.ts +13 -12
- package/src/types.ts +44 -27
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="WEB_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager">
|
|
4
|
+
<content url="file://$MODULE_DIR$">
|
|
5
|
+
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
|
6
|
+
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
|
7
|
+
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
|
8
|
+
</content>
|
|
9
|
+
<orderEntry type="inheritedJdk" />
|
|
10
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
11
|
+
</component>
|
|
12
|
+
</module>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/gltf-parser.iml" filepath="$PROJECT_DIR$/.idea/gltf-parser.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domgell/gltf-parser",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"exports": "./src/index.ts",
|
|
6
6
|
"author": "domgell",
|
|
7
7
|
"license": "ISC",
|
|
8
8
|
"description": "Parse .GLTF/.GLF files into JS objects",
|
|
9
|
-
"keywords": ["gltf", "glb", "mesh", "parser"],
|
|
9
|
+
"keywords": ["gltf", "glb", "mesh", "parser", "model"],
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@domgell/gltf-types": "*",
|
|
12
12
|
"@domgell/ts-util": "*",
|
package/src/Parser.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {mat4, TransformOrder} from "dom-game-math";
|
|
|
10
10
|
import {Matrix4x4, TextureFilterMap, TextureWrapModeMap} from "./types.ts";
|
|
11
11
|
import {assert, fail} from "@domgell/ts-util";
|
|
12
12
|
|
|
13
|
-
export
|
|
13
|
+
export interface ParserOptions {
|
|
14
14
|
transformOrder: TransformOrder,
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -51,7 +51,10 @@ export class Parser {
|
|
|
51
51
|
|
|
52
52
|
// ----------------------------------- Accessor ------------------------------------
|
|
53
53
|
|
|
54
|
-
accessor<
|
|
54
|
+
accessor<
|
|
55
|
+
T extends AccessorArrayType,
|
|
56
|
+
I extends number | undefined
|
|
57
|
+
>(index: I, type: T): I extends number ? AccessorConstructorType<T> : AccessorConstructorType<T> | undefined {
|
|
55
58
|
if (index === undefined) return undefined as any;
|
|
56
59
|
|
|
57
60
|
assert(this.header.accessors !== undefined, "Accessors are undefined");
|
|
@@ -132,7 +135,7 @@ export class Parser {
|
|
|
132
135
|
skin(gltfSkin: GLTF.Skin) {
|
|
133
136
|
assert(this.header.nodes !== undefined, "`Nodes` is undefined");
|
|
134
137
|
|
|
135
|
-
// Default
|
|
138
|
+
// Default to array of identity matrices with length = number of joints
|
|
136
139
|
const inverseBindMatrices = gltfSkin.inverseBindMatrices
|
|
137
140
|
? chunk(this.accessor(gltfSkin.inverseBindMatrices, Float32Array), 16)
|
|
138
141
|
: gltfSkin.joints.map(() => Array.from(mat4.idt) as Matrix4x4);
|
|
@@ -229,25 +232,22 @@ export class Parser {
|
|
|
229
232
|
|
|
230
233
|
let normal: Parsed.MaterialNormalInfo | undefined;
|
|
231
234
|
if (gltfMaterial.normalTexture !== undefined) {
|
|
232
|
-
fail("TODO");
|
|
235
|
+
fail("TODO: Normal Texture");
|
|
233
236
|
}
|
|
234
237
|
|
|
235
238
|
let emissive: Parsed.MaterialEmissiveInfo | undefined;
|
|
236
239
|
if (gltfMaterial.emissiveTexture !== undefined) {
|
|
237
|
-
fail("TODO");
|
|
240
|
+
fail("TODO: Emissive Texture");
|
|
238
241
|
}
|
|
239
242
|
|
|
240
243
|
let ambientOcclusion: Parsed.MaterialAmbientOcclusionInfo | undefined;
|
|
241
244
|
if (gltfMaterial.occlusionTexture !== undefined) {
|
|
242
|
-
fail("TODO");
|
|
245
|
+
fail("TODO: Occlusion Texture");
|
|
243
246
|
}
|
|
244
247
|
|
|
245
248
|
const alpha: Parsed.MaterialAlphaInfo = {
|
|
246
|
-
|
|
247
249
|
mode: gltfMaterial.alphaMode ?? "OPAQUE",
|
|
248
|
-
|
|
249
250
|
cutoff: gltfMaterial.alphaCutoff ?? 0.5,
|
|
250
|
-
|
|
251
251
|
};
|
|
252
252
|
|
|
253
253
|
const doubleSided = gltfMaterial.doubleSided ?? false;
|
|
@@ -302,11 +302,12 @@ export class Parser {
|
|
|
302
302
|
const buffer = this.header.buffers[bufferView.buffer];
|
|
303
303
|
|
|
304
304
|
if (buffer.uri !== undefined) {
|
|
305
|
-
fail("TODO");
|
|
305
|
+
fail("TODO: Buffer URI");
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
-
|
|
309
|
-
const
|
|
308
|
+
const dataBegin = bufferView.byteOffset ?? 0;
|
|
309
|
+
const dataEnd = dataBegin + bufferView.byteLength;
|
|
310
|
+
const data = this.binary.slice(dataBegin, dataEnd);
|
|
310
311
|
const blob = new Blob([new Uint8Array(data)]);
|
|
311
312
|
|
|
312
313
|
return await createImageBitmap(blob);
|
package/src/types.ts
CHANGED
|
@@ -7,7 +7,7 @@ export type Vector4 = Tuple<number, 4>
|
|
|
7
7
|
|
|
8
8
|
// ------------------------------- Scene -------------------------------
|
|
9
9
|
|
|
10
|
-
export
|
|
10
|
+
export interface Scene {
|
|
11
11
|
/**
|
|
12
12
|
* All nodes in scene
|
|
13
13
|
*/
|
|
@@ -24,15 +24,29 @@ export type Node = {
|
|
|
24
24
|
name?: string,
|
|
25
25
|
children: Node[],
|
|
26
26
|
parent?: Node,
|
|
27
|
-
} & (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
} & (MeshNode | SkinNode | CameraNode)
|
|
28
|
+
|
|
29
|
+
interface MeshNode {
|
|
30
|
+
mesh: Mesh,
|
|
31
|
+
skin?: undefined,
|
|
32
|
+
camera?: undefined
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface SkinNode {
|
|
36
|
+
skin: Skin,
|
|
37
|
+
mesh?: undefined,
|
|
38
|
+
camera?: undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface CameraNode {
|
|
42
|
+
camera: Camera,
|
|
43
|
+
mesh?: undefined,
|
|
44
|
+
skin?: undefined
|
|
45
|
+
}
|
|
32
46
|
|
|
33
47
|
// ------------------------------ Camera -------------------------------
|
|
34
48
|
|
|
35
|
-
export
|
|
49
|
+
export interface OrthographicCamera {
|
|
36
50
|
type: "Orthographic"
|
|
37
51
|
xmag: number,
|
|
38
52
|
ymag: number,
|
|
@@ -42,7 +56,7 @@ export type OrthographicCamera = {
|
|
|
42
56
|
projection: Matrix4x4,
|
|
43
57
|
}
|
|
44
58
|
|
|
45
|
-
export
|
|
59
|
+
export interface PerspectiveCamera {
|
|
46
60
|
type: "Perspective"
|
|
47
61
|
aspectRatio: number,
|
|
48
62
|
yfov: number,
|
|
@@ -56,7 +70,7 @@ export type Camera = OrthographicCamera | PerspectiveCamera
|
|
|
56
70
|
|
|
57
71
|
// ------------------------------- Mesh --------------------------------
|
|
58
72
|
|
|
59
|
-
export
|
|
73
|
+
export interface MeshPrimitiveAttributes {
|
|
60
74
|
positions: Vector3[],
|
|
61
75
|
uvs?: Vector2[],
|
|
62
76
|
normals?: Vector3[],
|
|
@@ -66,7 +80,7 @@ export type MeshPrimitiveAttributes = {
|
|
|
66
80
|
tangents?: Vector4[], // TODO
|
|
67
81
|
}
|
|
68
82
|
|
|
69
|
-
export
|
|
83
|
+
export interface MeshPrimitive {
|
|
70
84
|
/**
|
|
71
85
|
* Vertex attributes of the mesh primitive
|
|
72
86
|
*/
|
|
@@ -75,14 +89,14 @@ export type MeshPrimitive = {
|
|
|
75
89
|
material?: Material,
|
|
76
90
|
}
|
|
77
91
|
|
|
78
|
-
export
|
|
92
|
+
export interface Mesh {
|
|
79
93
|
primitives: MeshPrimitive[],
|
|
80
94
|
name?: string,
|
|
81
95
|
}
|
|
82
96
|
|
|
83
97
|
// -------------------------------- Skin -------------------------------
|
|
84
98
|
|
|
85
|
-
export
|
|
99
|
+
export interface Joint {
|
|
86
100
|
name: string,
|
|
87
101
|
transform: Matrix4x4,
|
|
88
102
|
inverseBindTransform: Matrix4x4,
|
|
@@ -90,7 +104,7 @@ export type Joint = {
|
|
|
90
104
|
parent?: string,
|
|
91
105
|
}
|
|
92
106
|
|
|
93
|
-
export
|
|
107
|
+
export interface Skin {
|
|
94
108
|
root: Joint,
|
|
95
109
|
joints: Record<string, Joint>,
|
|
96
110
|
animations: Animation[],
|
|
@@ -100,31 +114,34 @@ export type Skin = {
|
|
|
100
114
|
|
|
101
115
|
// ----------------------------- Animation -----------------------------
|
|
102
116
|
|
|
103
|
-
export
|
|
117
|
+
export interface Animation {
|
|
104
118
|
duration: number,
|
|
105
119
|
name?: string,
|
|
106
120
|
channels: Record<string, AnimationChannel>,
|
|
107
121
|
}
|
|
108
122
|
|
|
109
|
-
export
|
|
110
|
-
translation: AnimationKeyframe<Vector3>[]
|
|
111
|
-
rotation: AnimationKeyframe<Vector4>[]
|
|
112
|
-
scale: AnimationKeyframe<Vector3>[]
|
|
123
|
+
export interface AnimationChannel {
|
|
124
|
+
translation: AnimationKeyframe<Vector3>[];
|
|
125
|
+
rotation: AnimationKeyframe<Vector4>[];
|
|
126
|
+
scale: AnimationKeyframe<Vector3>[];
|
|
113
127
|
}
|
|
114
128
|
|
|
115
|
-
export
|
|
129
|
+
export interface AnimationKeyframe<T> {
|
|
130
|
+
time: number,
|
|
131
|
+
value: T
|
|
132
|
+
}
|
|
116
133
|
|
|
117
134
|
// ----------------------------- Texture -------------------------------
|
|
118
135
|
|
|
119
136
|
|
|
120
137
|
// TODO: If source is undefined, don't create texture, if sampler is undefined create default sampler
|
|
121
|
-
export
|
|
138
|
+
export interface Texture {
|
|
122
139
|
source: ImageBitmap,
|
|
123
140
|
sampler: Sampler,
|
|
124
141
|
name?: string,
|
|
125
142
|
}
|
|
126
143
|
|
|
127
|
-
export
|
|
144
|
+
export interface Sampler {
|
|
128
145
|
wrap: { s: TextureWrapMode, t: TextureWrapMode },
|
|
129
146
|
filter: { min: TextureMinFilter, mag: TextureMagFilter },
|
|
130
147
|
}
|
|
@@ -161,7 +178,7 @@ export const TextureFilterMap = {
|
|
|
161
178
|
|
|
162
179
|
// ----------------------------- Material ------------------------------
|
|
163
180
|
|
|
164
|
-
export
|
|
181
|
+
export interface Material {
|
|
165
182
|
metallicRoughness: MaterialMetallicRoughnessInfo,
|
|
166
183
|
normal?: MaterialNormalInfo,
|
|
167
184
|
emissive?: MaterialEmissiveInfo,
|
|
@@ -173,7 +190,7 @@ export type Material = {
|
|
|
173
190
|
doubleSided: boolean,
|
|
174
191
|
}
|
|
175
192
|
|
|
176
|
-
export
|
|
193
|
+
export interface MaterialAlphaInfo {
|
|
177
194
|
/**
|
|
178
195
|
* The alpha rendering mode of the material. Default is `"OPAQUE"`.
|
|
179
196
|
*/
|
|
@@ -184,7 +201,7 @@ export type MaterialAlphaInfo = {
|
|
|
184
201
|
cutoff: number,
|
|
185
202
|
}
|
|
186
203
|
|
|
187
|
-
export
|
|
204
|
+
export interface MaterialMetallicRoughnessInfo {
|
|
188
205
|
/**
|
|
189
206
|
* The base color factor of the material. Default is `[1, 1, 1, 1]`.
|
|
190
207
|
*/
|
|
@@ -207,7 +224,7 @@ export type MaterialMetallicRoughnessInfo = {
|
|
|
207
224
|
metallicRoughnessTexture?: Texture,
|
|
208
225
|
}
|
|
209
226
|
|
|
210
|
-
export
|
|
227
|
+
export interface MaterialNormalInfo {
|
|
211
228
|
/**
|
|
212
229
|
* The normal texture of the material.
|
|
213
230
|
*/
|
|
@@ -218,7 +235,7 @@ export type MaterialNormalInfo = {
|
|
|
218
235
|
scale: number,
|
|
219
236
|
}
|
|
220
237
|
|
|
221
|
-
export
|
|
238
|
+
export interface MaterialEmissiveInfo {
|
|
222
239
|
/**
|
|
223
240
|
* The emissive texture of the material.
|
|
224
241
|
*/
|
|
@@ -230,7 +247,7 @@ export type MaterialEmissiveInfo = {
|
|
|
230
247
|
factor: Vector3,
|
|
231
248
|
}
|
|
232
249
|
|
|
233
|
-
export
|
|
250
|
+
export interface MaterialAmbientOcclusionInfo {
|
|
234
251
|
/**
|
|
235
252
|
* The occlusion texture of the material.
|
|
236
253
|
*/
|