This repository has been archived by the owner on May 24, 2022. It is now read-only.
/
load.js
313 lines (279 loc) · 12.9 KB
/
load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
var loadJSON = require('pex-io/loadJSON');
var loadBinary = require('pex-io/loadBinary');
var loadText = require('pex-io/loadText');
var loadImage = require('pex-io/loadImage');
var log = require('debug')('pex/gltf');
var info = require('debug')('pex/gltf/info');
var warn = require('debug')('pex/gltf/warn');
var path = require('path');
var async = require('async');
var iterateObject = require("iterate-object");
var WebGLConstants = {
34963: 'ELEMENT_ARRAY_BUFFER', //0x8893
34962: 'ARRAY_BUFFER', //0x8892
5123: 'UNSIGNED_SHORT', //0x1403
5126: 'FLOAT', //0x1406
4: 'TRIANGLES', //0x0004
35678: 'SAMPLER_2D', //0x8B5E
35664: 'FLOAT_VEC2', //0x8B50
35665: 'FLOAT_VEC3', //0x8B51
35666: 'FLOAT_VEC4', //0x8B52
35676: 'FLOAT_MAT4', //0x8B5C
5126: 'FLOAT' //0x1406
};
var SupportedAttributeSemantics = ['POSITION', 'NORMAL', 'TEXCOORD_0'];
function handleBuffer(ctx, json, basePath, bufferName, bufferInfo, callback) {
log('handleBuffer', bufferName, bufferInfo.uri);
if (bufferInfo.uri) {
loadBinary(basePath + '/' + bufferInfo.uri, function(err, data) {
bufferInfo.arrayBuffer = data; //TODO: obj addon use _
callback(err, data);
});
}
else {
throw new Error('gltf/handleBuffer missing uri in ' + JSON.stringify(bufferInfo));
}
}
function handleBufferView(ctx, json, basePath, bufferViewName, bufferViewInfo, callback) {
log('handleBufferView', bufferViewName);
var buffer = json.buffers[bufferViewInfo.buffer];
bufferViewInfo._typedArray = null;
if (bufferViewInfo.target == 34963) { //ELEMENT_ARRAY_BUFFER //TODO: ctx constant
//TODO: Slice or not to slice the buffer
bufferViewInfo.buffer = buffer.arrayBuffer.slice(bufferViewInfo.byteOffset, bufferViewInfo.byteOffset + bufferViewInfo.byteLength); //ADDON
info('slice', bufferViewName, bufferViewInfo.byteOffset, bufferViewInfo.byteOffset + bufferViewInfo.byteLength, '=', bufferViewInfo.buffer.byteLength);
bufferViewInfo._buffer = bufferViewInfo.buffer;
bufferViewInfo._typedArray = new Uint16Array(bufferViewInfo.buffer);
}
if (bufferViewInfo.target == 34962) { //ARRAY_BUFFER //TODO: ctx constant
//TODO: Slice or not to slice the buffer
bufferViewInfo.buffer = buffer.arrayBuffer.slice(bufferViewInfo.byteOffset, bufferViewInfo.byteOffset + bufferViewInfo.byteLength);
info('slice', bufferViewName, bufferViewInfo.byteOffset, bufferViewInfo.byteOffset + bufferViewInfo.byteLength, '=', bufferViewInfo.buffer.byteLength);
//bufferViewInfo.buffer = buffer.arrayBuffer;
bufferViewInfo._buffer = bufferViewInfo.buffer;
bufferViewInfo._typedArray = new Float32Array(bufferViewInfo.buffer);
}
log('handleBufferView', bufferViewName, WebGLConstants[bufferViewInfo.target], bufferViewInfo.byteOffset, '..', bufferViewInfo.byteLength, '/', buffer.arrayBuffer.byteLength)
callback(null, bufferViewInfo);
}
function handleAccessor(ctx, json, basePath, accessorName, accessorInfo, callback) {
log('handleAccessor', accessorName);
callback(null, accessorInfo);
}
function linkPrimitive(json, primitiveName, primitiveInfo) {
log('handlePrimitive', primitiveName)
primitiveInfo.indices.accessor = json.accessors[primitiveInfo.indices];
Object.keys(primitiveInfo.attributes).forEach(function(attribute) {
primitiveInfo.attributes[attribute].accessor = json.accessors[primitiveInfo.attributes[attribute]];
})
}
function handleMesh(ctx, json, basePath, meshName, meshInfo, callback) {
log('handleMesh', meshInfo.name);
meshInfo.primitives.forEach(function(primitiveInfo, primitiveIndex) {
linkPrimitive(json, primitiveIndex, primitiveInfo);
})
callback(null, meshInfo);
}
function buildMeshes(ctx, json, callback) {
log('buildMeshes');
var AttributeLocationMap = {
"POSITION": ctx.ATTRIB_POSITION,
"NORMAL": ctx.ATTRIB_NORMAL,
"TEXCOORD_0": ctx.ATTRIB_TEX_COORD_0
}
log('buildMesh AttributeNameMap');
var AttributeSizeMap = {
"SCALAR": 1,
"VEC3": 3,
"VEC2": 2
}
function buildBufferInfo(accessorName) {
var accessorInfo = json.accessors[accessorName];
var size = AttributeSizeMap[accessorInfo.type];
//TODO: any other way to limit attrib count?
var data = json.bufferViews[accessorInfo.bufferView]._typedArray;//.subarray(0, accessorInfo.count * size);
var buffer = json.bufferViews[accessorInfo.bufferView]._buffer;
if (buffer) {
if (json.bufferViews[accessorInfo.bufferView].target == 34963) { //TODO: use ctx
data = new Uint16Array(buffer.slice(accessorInfo.byteOffset, accessorInfo.byteOffset + accessorInfo.count * size * 2));
}
else {
data = new Float32Array(buffer.slice(accessorInfo.byteOffset, accessorInfo.byteOffset + accessorInfo.count * size * 4));
}
info('subarray', accessorName, accessorInfo.byteOffset, accessorInfo.byteOffset + accessorInfo.count * size * 4);
}
else {
info('subarray', accessorName, accessorInfo.byteOffset, accessorInfo.byteOffset + accessorInfo.count * size * 2);
}
var bufferInfo = {
data: data,
opts: {
offset: 0,
stride: accessorInfo.byteStride,
size: size
}
};
log(bufferInfo.opts);
return bufferInfo;
}
iterateObject(json.meshes, function(meshInfo, meshName, meshIndex) {
//if (meshIndex != 2) return; //FIXME: TEMP!
log('buildMesh', meshName);
meshInfo.primitives.forEach(function(primitiveInfo, primitiveIndex) {
log('buildPrimitive', primitiveIndex);
//var va = new VertexArray(ctx); //TODO: use ctx
var attributes = [];
iterateObject(primitiveInfo.attributes, function(accessorName, attributeSemantic) {
if (SupportedAttributeSemantics.indexOf(attributeSemantic) == -1) {
warn('buildAttribute unsupported attribute semantic', attributeSemantic);
return;
}
var attributeInfo = buildBufferInfo(accessorName);
var attributeLocation = AttributeLocationMap[attributeSemantic];
log('buildAttribute', attributeSemantic, attributeInfo.opts);
var vertexBuffer = ctx.createBuffer(ctx.ARRAY_BUFFER, attributeInfo.data, ctx.STATIC_DRAW);
attributes.push({
buffer: vertexBuffer,
location: attributeLocation,
size: attributeInfo.opts.size,
stride: attributeInfo.opts.stride,
offset: attributeInfo.opts.offset
})
});
var indexBufferInfo = buildBufferInfo(primitiveInfo.indices);
log('buildIndexBuffer', indexBufferInfo.opts, 'len:', indexBufferInfo.data.length);
//va.addIndexBuffer(indexBufferInfo.data, indexBufferInfo.opts); //TODO: use ctx
var indexBuffer = ctx.createBuffer(ctx.ELEMENT_ARRAY_BUFFER, indexBufferInfo.data, ctx.STATIC_DRAW);
var va = ctx.createVertexArray(attributes, indexBuffer);
primitiveInfo.vertexArray = va; //TODO: use ctx
//* var vertexArray = new VertexArray(ctx,[
//* {buffer : buffer0, location : ctx.ATTRIB_POSITION, size : 3, stride : 0, offset : 0 },
//* {buffer : buffer0, location : ctx.ATTRIB_NORMAL, size : 3, stride : 0, offset : 4 * 3 * 4},
//* {buffer : buffer1, location : ctx.ATTRIB_COLOR, size : 4},
//* ], indexBuffer);
})//
})
callback(null, json);
}
function handleShader(ctx, json, basePath, shaderName, shaderInfo, callback) {
log('handleShader', shaderName);
if (shaderInfo.uri) {
loadText(basePath + '/' + shaderInfo.uri, function(err, srcStr) {
//TODO: add error handling
log('handleShader');
//precision is already added in Program class
shaderInfo._src = srcStr.replace('precision highp float;', ''); //TODO: is it?
callback(err, shaderInfo);
});
}
else {
throw new Error('gltf/handleShader missing uri in ' + JSON.stringify(shaderInfo));
}
}
function handleImage(ctx, json, basePath, imageName, imageInfo, callback) {
log('handleImage', imageInfo.uri);
if (imageInfo.uri) {
var url = basePath + '/' + imageInfo.uri;
loadImage(url, function(err, img) {
if (err) {
log('handleImage', err.toString());
callback(err, null);
}
else {
imageInfo._img = img;
callback(null, img);
}
})
}
else {
throw new Error('gltf/handleImage missing uri in ' + JSON.stringify(imageInfo));
}
}
function handleTexture(ctx, json, basePath, textureName, textureInfo, callback) {
log('handleTexture', textureInfo.source)
if (textureInfo.source) {
var img = json.images[textureInfo.source]._img;
var opts = {
magFilter: ctx.NEAREST,
minFilter: ctx.NEAREST,
repeat: true,
flipY: false
}
textureInfo._texture = ctx.createTexture2D(img, img.width, img.height, opts);
callback(null, textureInfo);
}
else {
throw new Error('gltf/handleTexture missing uri in ' + JSON.stringify(textureInfo));
}
}
function handleProgram(ctx, json, basePath, programName, programInfo, callback) {
log('handleProgram', programName);
var vertSrc = json.shaders[programInfo.vertexShader]._src;
var fragSrc = json.shaders[programInfo.fragmentShader]._src;
//FIXME: hardcoded
vertSrc = vertSrc.replace(/a_position/g, 'position');
vertSrc = vertSrc.replace(/a_normal/g, 'normal');
vertSrc = vertSrc.replace(/a_texcoord0/g, 'texcoord');
fragSrc = '#ifdef GL_ES\nprecision highp float;\n#endif\n' + fragSrc;
programInfo._program = ctx.createProgram(vertSrc, fragSrc);
callback(null, programInfo);
}
function handleNode(ctx, json, basePath, nodeName, nodeInfo, callback) {
log('handleNode', nodeName);
//FIXME: solve that with Ramda partial
nodeInfo.children = nodeInfo.children.map(function(childNodeName) {
json.nodes[childNodeName].parent = nodeInfo;
return json.nodes[childNodeName];
})
callback(null, nodeInfo);
}
function handleScene(ctx, json, basePath, sceneName, sceneInfo, callback) {
log('handleScene', sceneName);
//FIXME: solve that with Ramda partial
sceneInfo.nodes = sceneInfo.nodes.map(function(childNodeName) {
return json.nodes[childNodeName];
})
callback(null, sceneInfo);
}
function handleAll(typeName, handler, ctx, json, basePath, callback) {
log('handleAll', typeName);
if (!json[typeName]) {
log('missing', typeName);
return callback(null, null);
}
async.map(
Object.keys(json[typeName]),
function(nodeName, callback) {
handler(ctx, json, basePath, nodeName, json[typeName][nodeName], callback);
},
callback
)
}
function load(ctx, file, callback) {
var basePath = path.dirname(file);
log('load ', file);
loadJSON(file, function(err, json) {
if (err) {
console.log('loadJSON', err)
return callback(err, null);
}
async.series([
function(callback) { handleAll('buffers' , handleBuffer , ctx, json, basePath, callback); },
function(callback) { handleAll('bufferViews', handleBufferView, ctx, json, basePath, callback); },
function(callback) { handleAll('accessors' , handleAccessor , ctx, json, basePath, callback); },
function(callback) { handleAll('meshes' , handleMesh , ctx, json, basePath, callback); },
function(callback) { handleAll('images' , handleImage , ctx, json, basePath, callback); },
function(callback) { handleAll('textures' , handleTexture , ctx, json, basePath, callback); },
function(callback) { handleAll('shaders' , handleShader , ctx, json, basePath, callback); },
function(callback) { handleAll('programs' , handleProgram , ctx, json, basePath, callback); },
function(callback) { handleAll('nodes' , handleNode , ctx, json, basePath, callback); },
function(callback) { handleAll('scenes' , handleScene , ctx, json, basePath, callback); },
function(callback) { buildMeshes(ctx, json, callback); },
], function(err, results) {
if (err) log('load done errors', err);
else log('load done');
callback(err, json);
})
})
}
module.exports = load;