-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathaddDefaults.js
516 lines (488 loc) · 16.3 KB
/
addDefaults.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
'use strict';
var Cesium = require('cesium');
var addToArray = require('./addToArray');
var ForEach = require('./ForEach');
var clone = Cesium.clone;
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
var WebGLConstants = Cesium.WebGLConstants;
module.exports = addDefaults;
var gltfTemplate = {
accessors: [],
animations : [
{
channels : [],
samplers : [
{
interpolation : 'LINEAR'
}
]
}
],
asset : {},
buffers : [
{
byteLength: 0,
type: 'arraybuffer'
}
],
bufferViews: [
{
byteLength: 0
}
],
cameras: [],
images: [],
materials: [
{
values: function(material) {
var extensions = defaultValue(material.extensions, {});
var materialsCommon = extensions.KHR_materials_common;
if (!defined(materialsCommon)) {
return {};
}
},
extensions: function(material) {
var extensions = defaultValue(material.extensions, {});
var materialsCommon = extensions.KHR_materials_common;
if (defined(materialsCommon)) {
var technique = materialsCommon.technique;
var defaults = {
ambient: [0.0, 0.0, 0.0, 1.0],
emission: [0.0, 0.0, 0.0, 1.0],
transparency: 1.0
};
if (technique !== 'CONSTANT') {
defaults.diffuse = [0.0, 0.0, 0.0, 1.0];
if (technique !== 'LAMBERT') {
defaults.specular = [0.0, 0.0, 0.0, 1.0];
defaults.shininess = 0.0;
}
}
return {
KHR_materials_common: {
doubleSided: false,
transparent: false,
values: defaults
}
};
}
}
}
],
meshes : [
{
primitives : [
{
attributes : {},
mode : WebGLConstants.TRIANGLES
}
]
}
],
nodes : [
{
children : [],
matrix : function(node) {
if (!defined(node.translation) && !defined(node.rotation) && !defined(node.scale)) {
return [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
];
}
},
rotation : function(node) {
if (defined(node.translation) || defined(node.scale)) {
return [0.0, 0.0, 0.0, 1.0];
}
},
scale : function(node) {
if (defined(node.translation) || defined(node.rotation)) {
return [1.0, 1.0, 1.0];
}
},
translation : function(node) {
if (defined(node.rotation) || defined(node.scale)) {
return [0.0, 0.0, 0.0];
}
}
}
],
programs : [
{
attributes : []
}
],
samplers : [
{
magFilter: WebGLConstants.LINEAR,
minFilter : WebGLConstants.NEAREST_MIPMAP_LINEAR,
wrapS : WebGLConstants.REPEAT,
wrapT : WebGLConstants.REPEAT
}
],
scenes : [
{
nodes : []
}
],
shaders : [],
skins : [
{
bindShapeMatrix : [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
]
}
],
techniques : [
{
parameters: {},
attributes: {},
uniforms: {},
states: {
enable: []
}
}
],
textures : [
{
format: WebGLConstants.RGBA,
internalFormat: WebGLConstants.RGBA,
target: WebGLConstants.TEXTURE_2D,
type: WebGLConstants.UNSIGNED_BYTE
}
],
extensionsUsed : [],
extensionsRequired : []
};
function addDefaultsFromTemplate(object, template) {
for (var id in template) {
if (template.hasOwnProperty(id)) {
var templateValue = template[id];
if (typeof templateValue === 'function') {
templateValue = templateValue(object);
}
if (defined(templateValue)) {
if (typeof templateValue === 'object') {
if (Array.isArray(templateValue)) {
var arrayValue = defaultValue(object[id], []);
if (templateValue.length > 0) {
var arrayTemplate = templateValue[0];
if (typeof arrayTemplate === 'object') {
var arrayValueLength = arrayValue.length;
for (var i = 0; i < arrayValueLength; i++) {
addDefaultsFromTemplate(arrayValue[i], arrayTemplate);
}
} else {
arrayValue = defaultValue(object[id], templateValue);
}
}
object[id] = arrayValue;
} else {
var applyTemplate = templateValue['*'];
object[id] = defaultValue(object[id], {});
var objectValue = object[id];
if (defined(applyTemplate)) {
for (var subId in objectValue) {
if (objectValue.hasOwnProperty(subId) && subId !== 'extras') {
var subObject = objectValue[subId];
addDefaultsFromTemplate(subObject, applyTemplate);
}
}
} else {
addDefaultsFromTemplate(objectValue, templateValue);
}
}
} else {
object[id] = defaultValue(object[id], templateValue);
}
}
}
}
}
function getAnimatedNodes(gltf) {
var nodes = {};
ForEach.animation(gltf, function(animation) {
ForEach.animationChannel(animation, function(channel) {
var target = channel.target;
var nodeId = target.node;
var path = target.path;
// Ignore animations that target 'weights'
if (path === 'translation' || path === 'rotation' || path === 'scale') {
nodes[nodeId] = true;
}
});
});
return nodes;
}
function addDefaultTransformToAnimatedNodes(gltf) {
var animatedNodes = getAnimatedNodes(gltf);
ForEach.node(gltf, function(node, id) {
if (defined(animatedNodes[id])) {
delete node.matrix;
node.translation = defaultValue(node.translation, [0.0, 0.0, 0.0]);
node.rotation = defaultValue(node.rotation, [0.0, 0.0, 0.0, 1.0]);
node.scale = defaultValue(node.scale, [1.0, 1.0, 1.0]);
}
});
}
var defaultMaterial = {
values : {
emission : [
0.5, 0.5, 0.5, 1.0
]
},
extras : {
_pipeline: {}
}
};
var defaultTechnique = {
attributes : {
a_position : 'position'
},
parameters : {
modelViewMatrix : {
semantic : 'MODELVIEW',
type : WebGLConstants.FLOAT_MAT4
},
projectionMatrix : {
semantic : 'PROJECTION',
type : WebGLConstants.FLOAT_MAT4
},
emission : {
type : WebGLConstants.FLOAT_VEC4,
value : [
0.5, 0.5, 0.5, 1.0
]
},
position : {
semantic: 'POSITION',
type: WebGLConstants.FLOAT_VEC3
}
},
states : {
enable : [
WebGLConstants.CULL_FACE,
WebGLConstants.DEPTH_TEST
]
},
uniforms : {
u_modelViewMatrix : 'modelViewMatrix',
u_projectionMatrix : 'projectionMatrix',
u_emission : 'emission'
}
};
var defaultProgram = {
attributes : [
'a_position'
]
};
var defaultVertexShader = {
type : WebGLConstants.VERTEX_SHADER,
extras : {
_pipeline : {
extension : '.vert',
source : '' +
'precision highp float;\n' +
'\n' +
'uniform mat4 u_modelViewMatrix;\n' +
'uniform mat4 u_projectionMatrix;\n' +
'\n' +
'attribute vec3 a_position;\n' +
'\n' +
'void main (void)\n' +
'{\n' +
' gl_Position = u_projectionMatrix * u_modelViewMatrix * vec4(a_position, 1.0);\n' +
'}\n'
}
}
};
var defaultFragmentShader = {
type : WebGLConstants.FRAGMENT_SHADER,
extras : {
_pipeline : {
extension: '.frag',
source : '' +
'precision highp float;\n' +
'\n' +
'uniform vec4 u_emission;\n' +
'\n' +
'void main(void)\n' +
'{\n' +
' gl_FragColor = u_emission;\n' +
'}\n'
}
}
};
function addDefaultMaterial(gltf) {
var materials = gltf.materials;
var meshes = gltf.meshes;
var defaultMaterialId;
var meshesLength = meshes.length;
for (var meshId = 0; meshId < meshesLength; meshId++) {
var mesh = meshes[meshId];
var primitives = mesh.primitives;
var primitivesLength = primitives.length;
for (var j = 0; j < primitivesLength; j++) {
var primitive = primitives[j];
if (!defined(primitive.material)) {
if (!defined(defaultMaterialId)) {
defaultMaterialId = addToArray(materials, clone(defaultMaterial, true));
}
primitive.material = defaultMaterialId;
}
}
}
}
function addDefaultTechnique(gltf) {
var materials = gltf.materials;
var techniques = gltf.techniques;
var programs = gltf.programs;
var shaders = gltf.shaders;
var defaultTechniqueId;
var materialsLength = materials.length;
for (var materialId = 0; materialId < materialsLength; materialId++) {
var material = materials[materialId];
var techniqueId = material.technique;
var extensions = defaultValue(material.extensions, {});
var materialsCommon = extensions.KHR_materials_common;
if (!defined(techniqueId)) {
if (!defined(defaultTechniqueId) && !defined(materialsCommon)) {
var technique = clone(defaultTechnique, true);
defaultTechniqueId = addToArray(techniques, technique);
var program = clone(defaultProgram, true);
technique.program = addToArray(programs, program);
var vertexShader = clone(defaultVertexShader, true);
program.vertexShader = addToArray(shaders, vertexShader);
var fragmentShader = clone(defaultFragmentShader, true);
program.fragmentShader = addToArray(shaders, fragmentShader);
}
material.technique = defaultTechniqueId;
}
}
}
function addDefaultByteOffsets(gltf) {
var accessors = gltf.accessors;
var accessorLength = accessors.length;
for (var i = 0; i < accessorLength; i++) {
var accessor = accessors[i];
if (!defined(accessor.byteOffset)) {
accessor.byteOffset = 0;
}
}
var bufferViews = gltf.bufferViews;
var bufferViewsLength = bufferViews.length;
for (var j = 0; j < bufferViewsLength; j++) {
var bufferView = bufferViews[j];
if (!defined(bufferView.byteOffset)) {
bufferView.byteOffset = 0;
}
}
}
function selectDefaultScene(gltf) {
if (defined(gltf.scenes) && !defined(gltf.scene)) {
gltf.scene = 0;
}
}
function optimizeForCesium(gltf) {
// Give the diffuse uniform a semantic to support color replacement in 3D Tiles
var techniques = gltf.techniques;
var techniquesLength = techniques.length;
for (var techniqueId = 0; techniqueId < techniquesLength; techniqueId++) {
var technique = techniques[techniqueId];
var parameters = technique.parameters;
if (defined(parameters.diffuse)) {
parameters.diffuse.semantic = '_3DTILESDIFFUSE';
}
}
}
function inferBufferViewTargets(gltf) {
// If bufferView elements are missing targets, we can infer their type from their use
var needsTarget = {};
var shouldTraverse = 0;
ForEach.bufferView(gltf, function(bufferView, bufferViewId) {
if (!defined(bufferView.target)) {
needsTarget[bufferViewId] = true;
shouldTraverse++;
}
});
if (shouldTraverse > 0) {
var accessors = gltf.accessors;
var bufferViews = gltf.bufferViews;
ForEach.mesh(gltf, function (mesh) {
ForEach.meshPrimitive(mesh, function (primitive) {
var indices = primitive.indices;
if (defined(indices)) {
var accessor = accessors[indices];
var bufferViewId = accessor.bufferView;
if (needsTarget[bufferViewId]) {
var bufferView = bufferViews[bufferViewId];
if (defined(bufferView)) {
bufferView.target = WebGLConstants.ELEMENT_ARRAY_BUFFER;
needsTarget[bufferViewId] = false;
shouldTraverse--;
}
}
}
ForEach.meshPrimitiveAttribute(primitive, function (accessorId) {
var accessor = accessors[accessorId];
var bufferViewId = accessor.bufferView;
if (needsTarget[bufferViewId]) {
var bufferView = bufferViews[bufferViewId];
if (defined(bufferView)) {
bufferView.target = WebGLConstants.ARRAY_BUFFER;
needsTarget[bufferViewId] = false;
shouldTraverse--;
}
}
});
ForEach.meshPrimitiveTargetAttribute(primitive, function (targetAttribute) {
var bufferViewId = accessors[targetAttribute].bufferView;
if (needsTarget[bufferViewId]) {
var bufferView = bufferViews[bufferViewId];
if (defined(bufferView)) {
bufferView.target = WebGLConstants.ARRAY_BUFFER;
needsTarget[bufferViewId] = false;
shouldTraverse--;
}
}
});
});
if (shouldTraverse === 0) {
return true;
}
});
}
}
/**
* Adds default glTF values if they don't exist.
*
* The glTF asset must be initialized for the pipeline.
*
* @param {Object} gltf A javascript object containing a glTF asset.
* @param {Object} [options] An object with the following properties:
* @param {Boolean} [options.optimizeForCesium] Optimize the defaults for Cesium. Uses the Cesium sun as the default light source.
* @returns {Object} The modified glTF.
*
* @see addPipelineExtras
* @see loadGltfUris
*/
function addDefaults(gltf, options) {
options = defaultValue(options, {});
addDefaultsFromTemplate(gltf, gltfTemplate);
addDefaultTransformToAnimatedNodes(gltf);
addDefaultMaterial(gltf);
addDefaultTechnique(gltf);
addDefaultByteOffsets(gltf);
selectDefaultScene(gltf);
inferBufferViewTargets(gltf);
if (options.optimizeForCesium) {
optimizeForCesium(gltf);
}
return gltf;
}