/
processModelMaterialsCommon.js
882 lines (788 loc) · 34.5 KB
/
processModelMaterialsCommon.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
'use strict';
var Cesium = require('cesium');
var WebGLConstants = Cesium.WebGLConstants;
var defined = Cesium.defined;
var defaultValue = Cesium.defaultValue;
var techniqueParameterForSemantic = require('./techniqueParameterForSemantic');
module.exports = modelMaterialsCommon;
function webGLConstantToGlslType(webGLValue) {
switch (webGLValue) {
case WebGLConstants.FLOAT:
return 'float';
case WebGLConstants.FLOAT_VEC2:
return 'vec2';
case WebGLConstants.FLOAT_VEC3:
return 'vec3';
case WebGLConstants.FLOAT_VEC4:
return 'vec4';
case WebGLConstants.FLOAT_MAT2:
return 'mat2';
case WebGLConstants.FLOAT_MAT3:
return 'mat3';
case WebGLConstants.FLOAT_MAT4:
return 'mat4';
case WebGLConstants.SAMPLER_2D:
return 'sampler2D';
}
}
function generateLightParameters(gltf) {
var result = {};
var lights;
if (defined(gltf.extensions) && defined(gltf.extensions.KHR_materials_common)) {
lights = gltf.extensions.KHR_materials_common.lights;
}
if (defined(lights)) {
// Figure out which node references the light
var nodes = gltf.nodes;
for (var nodeName in nodes) {
if (nodes.hasOwnProperty(nodeName)) {
var node = nodes[nodeName];
if (defined(node.extensions) && defined(node.extensions.KHR_materials_common)) {
var nodeLightId = node.extensions.KHR_materials_common.light;
if (defined(nodeLightId) && defined(lights[nodeLightId])) {
lights[nodeLightId].node = nodeName;
}
delete node.extensions.KHR_materials_common;
}
}
}
// Add light parameters to result
var lightCount = 0;
for (var lightName in lights) {
if (lights.hasOwnProperty(lightName)) {
var light = lights[lightName];
var lightType = light.type;
if ((lightType !== 'ambient') && !defined(light.node)) {
delete lights[lightName];
continue;
}
var lightBaseName = 'light' + lightCount.toString();
light.baseName = lightBaseName;
switch (lightType) {
case 'ambient':
var ambient = light.ambient;
result[lightBaseName + 'Color'] = {
type: WebGLConstants.FLOAT_VEC3,
value: ambient.color
};
break;
case 'directional':
var directional = light.directional;
result[lightBaseName + 'Color'] = {
type: WebGLConstants.FLOAT_VEC3,
value: directional.color
};
if (defined(light.node)) {
result[lightBaseName + 'Transform'] = {
node: light.node,
semantic: 'MODELVIEW',
type: WebGLConstants.FLOAT_MAT4
};
}
break;
case 'point':
var point = light.point;
result[lightBaseName + 'Color'] = {
type: WebGLConstants.FLOAT_VEC3,
value: point.color
};
if (defined(light.node)) {
result[lightBaseName + 'Transform'] = {
node: light.node,
semantic: 'MODELVIEW',
type: WebGLConstants.FLOAT_MAT4
};
}
result[lightBaseName + 'Attenuation'] = {
type: WebGLConstants.FLOAT_VEC3,
value: [point.constantAttenuation, point.linearAttenuation, point.quadraticAttenuation]
};
break;
case 'spot':
var spot = light.spot;
result[lightBaseName + 'Color'] = {
type: WebGLConstants.FLOAT_VEC3,
value: spot.color
};
if (defined(light.node)) {
result[lightBaseName + 'Transform'] = {
node: light.node,
semantic: 'MODELVIEW',
type: WebGLConstants.FLOAT_MAT4
};
result[lightBaseName + 'InverseTransform'] = {
node: light.node,
semantic: 'MODELVIEWINVERSE',
type: WebGLConstants.FLOAT_MAT4,
useInFragment: true
};
}
result[lightBaseName + 'Attenuation'] = {
type: WebGLConstants.FLOAT_VEC3,
value: [spot.constantAttenuation, spot.linearAttenuation, spot.quadraticAttenuation]
};
result[lightBaseName + 'FallOff'] = {
type: WebGLConstants.FLOAT_VEC2,
value: [spot.fallOffAngle, spot.fallOffExponent]
};
break;
}
++lightCount;
}
}
}
return result;
}
function getNextId(dictionary, baseName, startingCount) {
var count = defaultValue(startingCount, 0);
var nextId;
do {
nextId = baseName + (count++).toString();
} while (defined(dictionary[nextId]));
return nextId;
}
var techniqueCount = 0;
var vertexShaderCount = 0;
var fragmentShaderCount = 0;
var programCount = 0;
function generateTechnique(gltf, khrMaterialsCommon, lightParameters, optimizeForCesium) {
var techniques = gltf.techniques;
var shaders = gltf.shaders;
var programs = gltf.programs;
var lightingModel = khrMaterialsCommon.technique.toUpperCase();
var lights;
if (defined(gltf.extensions) && defined(gltf.extensions.KHR_materials_common)) {
lights = gltf.extensions.KHR_materials_common.lights;
}
var parameterValues = khrMaterialsCommon.values;
var jointCount = defaultValue(parameterValues.jointCount, 0);
var hasSkinning = (jointCount > 0);
var vertexShader = 'precision highp float;\n';
var fragmentShader = 'precision highp float;\n';
// Generate IDs for our new objects
var techniqueId = getNextId(techniques, 'technique', techniqueCount);
var vertexShaderId = getNextId(shaders, 'vertexShader', vertexShaderCount);
var fragmentShaderId = getNextId(shaders, 'fragmentShader', fragmentShaderCount);
var programId = getNextId(programs, 'program', programCount);
var hasNormals = (lightingModel !== 'CONSTANT');
// Add techniques
var techniqueParameters = {
// Add matrices
modelViewMatrix: {
semantic: 'MODELVIEW',
type: WebGLConstants.FLOAT_MAT4
},
projectionMatrix: {
semantic: 'PROJECTION',
type: WebGLConstants.FLOAT_MAT4
}
};
if (hasNormals) {
techniqueParameters.normalMatrix = {
semantic: 'MODELVIEWINVERSETRANSPOSE',
type: WebGLConstants.FLOAT_MAT3
};
}
if (hasSkinning) {
techniqueParameters.jointMatrix = {
count: jointCount,
semantic: 'JOINTMATRIX',
type: WebGLConstants.FLOAT_MAT4
};
}
// Add material parameters
var lowerCase;
var hasTexCoords = false;
for (var name in parameterValues) {
//generate shader parameters for KHR_materials_common attributes
//(including a check, because some boolean flags should not be used as shader parameters)
if (parameterValues.hasOwnProperty(name) && (name !== 'transparent') && (name !== 'doubleSided')) {
var valType = getKHRMaterialsCommonValueType(name, parameterValues[name]);
lowerCase = name.toLowerCase();
if (!hasTexCoords && (valType === WebGLConstants.SAMPLER_2D)) {
hasTexCoords = true;
}
techniqueParameters[lowerCase] = {
type: valType
};
}
}
// Give the diffuse uniform a semantic to support color replacement in 3D Tiles
if (defined(techniqueParameters.diffuse) && optimizeForCesium) {
techniqueParameters.diffuse.semantic = '_3DTILESDIFFUSE';
}
// Copy light parameters into technique parameters
if (defined(lightParameters)) {
for (var lightParamName in lightParameters) {
if (lightParameters.hasOwnProperty(lightParamName)) {
techniqueParameters[lightParamName] = lightParameters[lightParamName];
}
}
}
// Generate uniforms object before attributes are added
var techniqueUniforms = {};
for (var paramName in techniqueParameters) {
if (techniqueParameters.hasOwnProperty(paramName) && paramName !== 'extras') {
var param = techniqueParameters[paramName];
techniqueUniforms['u_' + paramName] = paramName;
var arraySize = defined(param.count) ? '[' + param.count + ']' : '';
if (((param.type !== WebGLConstants.FLOAT_MAT3) && (param.type !== WebGLConstants.FLOAT_MAT4)) ||
param.useInFragment) {
fragmentShader += 'uniform ' + webGLConstantToGlslType(param.type) + ' u_' + paramName + arraySize + ';\n';
delete param.useInFragment;
} else {
vertexShader += 'uniform ' + webGLConstantToGlslType(param.type) + ' u_' + paramName + arraySize + ';\n';
}
}
}
// Add attributes with semantics
var vertexShaderMain = '';
if (hasSkinning) {
vertexShaderMain += ' mat4 skinMat = a_weight.x * u_jointMatrix[int(a_joint.x)];\n';
vertexShaderMain += ' skinMat += a_weight.y * u_jointMatrix[int(a_joint.y)];\n';
vertexShaderMain += ' skinMat += a_weight.z * u_jointMatrix[int(a_joint.z)];\n';
vertexShaderMain += ' skinMat += a_weight.w * u_jointMatrix[int(a_joint.w)];\n';
}
// Add position always
var techniqueAttributes = {
a_position: 'position'
};
techniqueParameters.position = {
semantic: 'POSITION',
type: WebGLConstants.FLOAT_VEC3
};
vertexShader += 'attribute vec3 a_position;\n';
vertexShader += 'varying vec3 v_positionEC;\n';
if (hasSkinning) {
vertexShaderMain += ' vec4 pos = u_modelViewMatrix * skinMat * vec4(a_position,1.0);\n';
} else {
vertexShaderMain += ' vec4 pos = u_modelViewMatrix * vec4(a_position,1.0);\n';
}
vertexShaderMain += ' v_positionEC = pos.xyz;\n';
vertexShaderMain += ' gl_Position = u_projectionMatrix * pos;\n';
fragmentShader += 'varying vec3 v_positionEC;\n';
// Add normal if we don't have constant lighting
if (hasNormals) {
techniqueAttributes.a_normal = 'normal';
techniqueParameters.normal = {
semantic: 'NORMAL',
type: WebGLConstants.FLOAT_VEC3
};
vertexShader += 'attribute vec3 a_normal;\n';
vertexShader += 'varying vec3 v_normal;\n';
if (hasSkinning) {
vertexShaderMain += ' v_normal = u_normalMatrix * mat3(skinMat) * a_normal;\n';
} else {
vertexShaderMain += ' v_normal = u_normalMatrix * a_normal;\n';
}
fragmentShader += 'varying vec3 v_normal;\n';
}
// Add texture coordinates if the material uses them
var v_texcoord;
if (hasTexCoords) {
techniqueAttributes.a_texcoord_0 = 'texcoord_0';
techniqueParameters.texcoord_0 = {
semantic: 'TEXCOORD_0',
type: WebGLConstants.FLOAT_VEC2
};
v_texcoord = 'v_texcoord_0';
vertexShader += 'attribute vec2 a_texcoord_0;\n';
vertexShader += 'varying vec2 ' + v_texcoord + ';\n';
vertexShaderMain += ' ' + v_texcoord + ' = a_texcoord_0;\n';
fragmentShader += 'varying vec2 ' + v_texcoord + ';\n';
}
if (hasSkinning) {
techniqueAttributes.a_joint = 'joint';
techniqueParameters.joint = {
semantic: 'JOINT',
type: WebGLConstants.FLOAT_VEC4
};
techniqueAttributes.a_weight = 'weight';
techniqueParameters.weight = {
semantic: 'WEIGHT',
type: WebGLConstants.FLOAT_VEC4
};
vertexShader += 'attribute vec4 a_joint;\n';
vertexShader += 'attribute vec4 a_weight;\n';
}
var hasSpecular = hasNormals && ((lightingModel === 'BLINN') || (lightingModel === 'PHONG')) &&
defined(techniqueParameters.specular) && defined(techniqueParameters.shininess);
// Generate lighting code blocks
var hasNonAmbientLights = false;
var hasAmbientLights = false;
var fragmentLightingBlock = '';
for (var lightName in lights) {
if (lights.hasOwnProperty(lightName)) {
var light = lights[lightName];
var lightType = light.type.toLowerCase();
var lightBaseName = light.baseName;
fragmentLightingBlock += ' {\n';
var lightColorName = 'u_' + lightBaseName + 'Color';
var varyingDirectionName;
var varyingPositionName;
if (lightType === 'ambient') {
hasAmbientLights = true;
fragmentLightingBlock += ' ambientLight += ' + lightColorName + ';\n';
} else if (hasNormals) {
hasNonAmbientLights = true;
varyingDirectionName = 'v_' + lightBaseName + 'Direction';
varyingPositionName = 'v_' + lightBaseName + 'Position';
if (lightType !== 'point') {
vertexShader += 'varying vec3 ' + varyingDirectionName + ';\n';
fragmentShader += 'varying vec3 ' + varyingDirectionName + ';\n';
vertexShaderMain += ' ' + varyingDirectionName + ' = mat3(u_' + lightBaseName + 'Transform) * vec3(0.,0.,1.);\n';
if (lightType === 'directional') {
fragmentLightingBlock += ' vec3 l = normalize(' + varyingDirectionName + ');\n';
}
}
if (lightType !== 'directional') {
vertexShader += 'varying vec3 ' + varyingPositionName + ';\n';
fragmentShader += 'varying vec3 ' + varyingPositionName + ';\n';
vertexShaderMain += ' ' + varyingPositionName + ' = u_' + lightBaseName + 'Transform[3].xyz;\n';
fragmentLightingBlock += ' vec3 VP = ' + varyingPositionName + ' - v_positionEC;\n';
fragmentLightingBlock += ' vec3 l = normalize(VP);\n';
fragmentLightingBlock += ' float range = length(VP);\n';
fragmentLightingBlock += ' float attenuation = 1.0 / (u_' + lightBaseName + 'Attenuation.x + ';
fragmentLightingBlock += '(u_' + lightBaseName + 'Attenuation.y * range) + ';
fragmentLightingBlock += '(u_' + lightBaseName + 'Attenuation.z * range * range));\n';
} else {
fragmentLightingBlock += ' float attenuation = 1.0;\n';
}
if (lightType === 'spot') {
fragmentLightingBlock += ' float spotDot = dot(l, normalize(' + varyingDirectionName + '));\n';
fragmentLightingBlock += ' if (spotDot < cos(u_' + lightBaseName + 'FallOff.x * 0.5))\n';
fragmentLightingBlock += ' {\n';
fragmentLightingBlock += ' attenuation = 0.0;\n';
fragmentLightingBlock += ' }\n';
fragmentLightingBlock += ' else\n';
fragmentLightingBlock += ' {\n';
fragmentLightingBlock += ' attenuation *= max(0.0, pow(spotDot, u_' + lightBaseName + 'FallOff.y));\n';
fragmentLightingBlock += ' }\n';
}
fragmentLightingBlock += ' diffuseLight += ' + lightColorName + '* max(dot(normal,l), 0.) * attenuation;\n';
if (hasSpecular) {
if (lightingModel === 'BLINN') {
fragmentLightingBlock += ' vec3 h = normalize(l + viewDir);\n';
fragmentLightingBlock += ' float specularIntensity = max(0., pow(max(dot(normal, h), 0.), u_shininess)) * attenuation;\n';
} else { // PHONG
fragmentLightingBlock += ' vec3 reflectDir = reflect(-l, normal);\n';
fragmentLightingBlock += ' float specularIntensity = max(0., pow(max(dot(reflectDir, viewDir), 0.), u_shininess)) * attenuation;\n';
}
fragmentLightingBlock += ' specularLight += ' + lightColorName + ' * specularIntensity;\n';
}
}
fragmentLightingBlock += ' }\n';
}
}
if (!hasAmbientLights) {
// Add an ambient light if we don't have one
fragmentLightingBlock += ' ambientLight += vec3(0.2, 0.2, 0.2);\n';
}
if (!hasNonAmbientLights && (lightingModel !== 'CONSTANT')) {
if (optimizeForCesium) {
fragmentLightingBlock += ' vec3 l = normalize(czm_sunDirectionEC);\n';
} else {
fragmentLightingBlock += ' vec3 l = vec3(0.0, 0.0, 1.0);\n';
}
fragmentLightingBlock += ' diffuseLight += vec3(1.0, 1.0, 1.0) * max(dot(normal,l), 0.);\n';
if (hasSpecular) {
if (lightingModel === 'BLINN') {
fragmentLightingBlock += ' vec3 h = normalize(l + viewDir);\n';
fragmentLightingBlock += ' float specularIntensity = max(0., pow(max(dot(normal, h), 0.), u_shininess));\n';
} else { // PHONG
fragmentLightingBlock += ' vec3 reflectDir = reflect(-l, normal);\n';
fragmentLightingBlock += ' float specularIntensity = max(0., pow(max(dot(reflectDir, viewDir), 0.), u_shininess));\n';
}
fragmentLightingBlock += ' specularLight += vec3(1.0, 1.0, 1.0) * specularIntensity;\n';
}
}
vertexShader += 'void main(void) {\n';
vertexShader += vertexShaderMain;
vertexShader += '}\n';
fragmentShader += 'void main(void) {\n';
var colorCreationBlock = ' vec3 color = vec3(0.0, 0.0, 0.0);\n';
if (hasNormals) {
fragmentShader += ' vec3 normal = normalize(v_normal);\n';
if (parameterValues.doubleSided) {
fragmentShader += ' if (gl_FrontFacing == false)\n';
fragmentShader += ' {\n';
fragmentShader += ' normal = -normal;\n';
fragmentShader += ' }\n';
}
}
var finalColorComputation;
if (lightingModel !== 'CONSTANT') {
if (defined(techniqueParameters.diffuse)) {
if (techniqueParameters.diffuse.type === WebGLConstants.SAMPLER_2D) {
fragmentShader += ' vec4 diffuse = texture2D(u_diffuse, ' + v_texcoord + ');\n';
} else {
fragmentShader += ' vec4 diffuse = u_diffuse;\n';
}
fragmentShader += ' vec3 diffuseLight = vec3(0.0, 0.0, 0.0);\n';
colorCreationBlock += ' color += diffuse.rgb * diffuseLight;\n';
}
if (hasSpecular) {
if (techniqueParameters.specular.type === WebGLConstants.SAMPLER_2D) {
fragmentShader += ' vec3 specular = texture2D(u_specular, ' + v_texcoord + ').rgb;\n';
} else {
fragmentShader += ' vec3 specular = u_specular.rgb;\n';
}
fragmentShader += ' vec3 specularLight = vec3(0.0, 0.0, 0.0);\n';
colorCreationBlock += ' color += specular * specularLight;\n';
}
if (defined(techniqueParameters.transparency)) {
finalColorComputation = ' gl_FragColor = vec4(color * diffuse.a * u_transparency, diffuse.a * u_transparency);\n';
} else {
finalColorComputation = ' gl_FragColor = vec4(color * diffuse.a, diffuse.a);\n';
}
} else {
if (defined(techniqueParameters.transparency)) {
finalColorComputation = ' gl_FragColor = vec4(color * u_transparency, u_transparency);\n';
} else {
finalColorComputation = ' gl_FragColor = vec4(color, 1.0);\n';
}
}
if (defined(techniqueParameters.emission)) {
if (techniqueParameters.emission.type === WebGLConstants.SAMPLER_2D) {
fragmentShader += ' vec3 emission = texture2D(u_emission, ' + v_texcoord + ').rgb;\n';
} else {
fragmentShader += ' vec3 emission = u_emission.rgb;\n';
}
colorCreationBlock += ' color += emission;\n';
}
if (defined(techniqueParameters.ambient) || (lightingModel !== 'CONSTANT')) {
if (defined(techniqueParameters.ambient)) {
if (techniqueParameters.ambient.type === WebGLConstants.SAMPLER_2D) {
fragmentShader += ' vec3 ambient = texture2D(u_ambient, ' + v_texcoord + ').rgb;\n';
} else {
fragmentShader += ' vec3 ambient = u_ambient.rgb;\n';
}
} else {
fragmentShader += ' vec3 ambient = diffuse.rgb;\n';
}
colorCreationBlock += ' color += ambient * ambientLight;\n';
}
fragmentShader += ' vec3 viewDir = -normalize(v_positionEC);\n';
fragmentShader += ' vec3 ambientLight = vec3(0.0, 0.0, 0.0);\n';
// Add in light computations
fragmentShader += fragmentLightingBlock;
fragmentShader += colorCreationBlock;
fragmentShader += finalColorComputation;
fragmentShader += '}\n';
var techniqueStates;
if (parameterValues.transparent) {
techniqueStates = {
enable: [
WebGLConstants.DEPTH_TEST,
WebGLConstants.BLEND
],
depthMask: false,
functions: {
blendEquationSeparate: [
WebGLConstants.FUNC_ADD,
WebGLConstants.FUNC_ADD
],
blendFuncSeparate: [
WebGLConstants.ONE,
WebGLConstants.ONE_MINUS_SRC_ALPHA,
WebGLConstants.ONE,
WebGLConstants.ONE_MINUS_SRC_ALPHA
]
}
};
} else if (parameterValues.doubleSided) {
techniqueStates = {
enable: [
WebGLConstants.DEPTH_TEST
]
};
} else { // Not transparent or double sided
techniqueStates = {
enable: [
WebGLConstants.CULL_FACE,
WebGLConstants.DEPTH_TEST
]
};
}
techniques[techniqueId] = {
attributes: techniqueAttributes,
parameters: techniqueParameters,
program: programId,
states: techniqueStates,
uniforms: techniqueUniforms
};
// Add shaders
shaders[vertexShaderId] = {
type: WebGLConstants.VERTEX_SHADER,
extras: {
_pipeline: {
source: vertexShader,
extension: '.glsl'
}
}
};
shaders[fragmentShaderId] = {
type: WebGLConstants.FRAGMENT_SHADER,
extras: {
_pipeline: {
source: fragmentShader,
extension: '.glsl'
}
}
};
// Add program
var programAttributes = Object.keys(techniqueAttributes);
programs[programId] = {
attributes: programAttributes,
fragmentShader: fragmentShaderId,
vertexShader: vertexShaderId
};
return techniqueId;
}
function getKHRMaterialsCommonValueType(paramName, paramValue) {
var value;
// Backwards compatibility for COLLADA2GLTF v1.0-draft when it encoding
// materials using KHR_materials_common with explicit type/value members
if (defined(paramValue.value)) {
value = paramValue.value;
} else {
value = paramValue;
}
switch (paramName) {
case 'ambient':
return (value instanceof String || typeof value === 'string') ? WebGLConstants.SAMPLER_2D : WebGLConstants.FLOAT_VEC4;
case 'diffuse':
return (value instanceof String || typeof value === 'string') ? WebGLConstants.SAMPLER_2D : WebGLConstants.FLOAT_VEC4;
case 'emission':
return (value instanceof String || typeof value === 'string') ? WebGLConstants.SAMPLER_2D : WebGLConstants.FLOAT_VEC4;
case 'specular':
return (value instanceof String || typeof value === 'string') ? WebGLConstants.SAMPLER_2D : WebGLConstants.FLOAT_VEC4;
case 'shininess':
return WebGLConstants.FLOAT;
case 'transparency':
return WebGLConstants.FLOAT;
// these two are usually not used directly within shaders,
// they are just added here for completeness
case 'transparent':
return WebGLConstants.BOOL;
case 'doubleSided':
return WebGLConstants.BOOL;
}
}
function getTechniqueKey(khrMaterialsCommon) {
var techniqueKey = '';
techniqueKey += 'technique:' + khrMaterialsCommon.technique + ';';
var values = khrMaterialsCommon.values;
var keys = Object.keys(values).sort();
var keysCount = keys.length;
for (var i = 0; i < keysCount; ++i) {
var name = keys[i];
//generate first part of key using shader parameters for KHR_materials_common attributes
//(including a check, because some boolean flags should not be used as shader parameters)
if (values.hasOwnProperty(name) && (name !== 'transparent') && (name !== 'doubleSided')) {
techniqueKey += name + ':' + getKHRMaterialsCommonValueType(name, values[name]);
techniqueKey += ';';
}
}
var doubleSided = defaultValue(values.doubleSided, false);
techniqueKey += doubleSided.toString() + ';';
var transparent = defaultValue(values.transparent, false);
techniqueKey += transparent.toString() + ';';
var jointCount = defaultValue(values.jointCount, 0);
techniqueKey += jointCount.toString() + ';';
return techniqueKey;
}
function lightDefaults(gltf) {
if (!defined(gltf.extensions)) {
gltf.extensions = {};
}
var extensions = gltf.extensions;
if (!defined(extensions.KHR_materials_common)) {
extensions.KHR_materials_common = {};
}
var khrMaterialsCommon = extensions.KHR_materials_common;
if (!defined(khrMaterialsCommon.lights)) {
khrMaterialsCommon.lights = {};
}
var lights = khrMaterialsCommon.lights;
for (var name in lights) {
if (lights.hasOwnProperty(name)) {
var light = lights[name];
if (light.type === 'ambient') {
if (!defined(light.ambient)) {
light.ambient = {};
}
var ambientLight = light.ambient;
if (!defined(ambientLight.color)) {
ambientLight.color = [1.0, 1.0, 1.0];
}
} else if (light.type === 'directional') {
if (!defined(light.directional)) {
light.directional = {};
}
var directionalLight = light.directional;
if (!defined(directionalLight.color)) {
directionalLight.color = [1.0, 1.0, 1.0];
}
} else if (light.type === 'point') {
if (!defined(light.point)) {
light.point = {};
}
var pointLight = light.point;
if (!defined(pointLight.color)) {
pointLight.color = [1.0, 1.0, 1.0];
}
pointLight.constantAttenuation = defaultValue(pointLight.constantAttenuation, 1.0);
pointLight.linearAttenuation = defaultValue(pointLight.linearAttenuation, 0.0);
pointLight.quadraticAttenuation = defaultValue(pointLight.quadraticAttenuation, 0.0);
} else if (light.type === 'spot') {
if (!defined(light.spot)) {
light.spot = {};
}
var spotLight = light.spot;
if (!defined(spotLight.color)) {
spotLight.color = [1.0, 1.0, 1.0];
}
spotLight.constantAttenuation = defaultValue(spotLight.constantAttenuation, 1.0);
spotLight.fallOffAngle = defaultValue(spotLight.fallOffAngle, 3.14159265);
spotLight.fallOffExponent = defaultValue(spotLight.fallOffExponent, 0.0);
spotLight.linearAttenuation = defaultValue(spotLight.linearAttenuation, 0.0);
spotLight.quadraticAttenuation = defaultValue(spotLight.quadraticAttenuation, 0.0);
}
}
}
}
function getShaderVariable(accessor) {
var type = accessor.type;
if (type === 'SCALAR') {
return 'float';
}
return type.toLowerCase();
}
function ensureSemanticExistenceForPrimitive(gltf, primitive) {
var accessors = gltf.accessors;
var materials = gltf.materials;
var techniques = gltf.techniques;
var programs = gltf.programs;
var shaders = gltf.shaders;
var attributes = primitive.attributes;
var material = materials[primitive.material];
var technique = techniques[material.technique];
var program = programs[technique.program];
var vertexShader = shaders[program.vertexShader];
for (var semantic in attributes) {
if (attributes.hasOwnProperty(semantic)) {
if (!defined(techniqueParameterForSemantic(technique, semantic))) {
var accessorId = attributes[semantic];
var accessor = accessors[accessorId];
var lowerCase = semantic.toLowerCase();
var attributeName = 'a_' + lowerCase;
technique.parameters[lowerCase] = {
semantic: semantic,
type: accessor.componentType
};
technique.attributes[attributeName] = lowerCase;
program.attributes.push(attributeName);
var pipelineExtras = vertexShader.extras._pipeline;
var shaderText = pipelineExtras.source;
shaderText = 'attribute ' + getShaderVariable(accessor) + ' ' + attributeName + ';\n' + shaderText;
pipelineExtras.source = shaderText;
}
}
}
}
function ensureSemanticExistence(gltf) {
var meshes = gltf.meshes;
for (var meshId in meshes) {
if (meshes.hasOwnProperty(meshId)) {
var mesh = meshes[meshId];
var primitives = mesh.primitives;
var primitivesLength = primitives.length;
for (var i = 0; i < primitivesLength; i++) {
ensureSemanticExistenceForPrimitive(gltf, primitives[i]);
}
}
}
}
/**
* @private
*/
function modelMaterialsCommon(gltf, options) {
options = defaultValue(options, {});
if (!defined(gltf)) {
return undefined;
}
var hasExtension = false;
var extensionsUsed = gltf.extensionsUsed;
if (defined(extensionsUsed)) {
var extensionsUsedCount = extensionsUsed.length;
for (var i = 0; i < extensionsUsedCount; ++i) {
if (extensionsUsed[i] === 'KHR_materials_common') {
hasExtension = true;
extensionsUsed.splice(i, 1);
break;
}
}
}
if (hasExtension) {
gltf.asset.premultipliedAlpha = true;
if (!defined(gltf.programs)) {
gltf.programs = {};
}
if (!defined(gltf.shaders)) {
gltf.shaders = {};
}
if (!defined(gltf.techniques)) {
gltf.techniques = {};
}
lightDefaults(gltf);
var lightParameters = generateLightParameters(gltf);
var techniques = {};
var materials = gltf.materials;
for (var name in materials) {
if (materials.hasOwnProperty(name)) {
var material = materials[name];
if (defined(material.extensions) && defined(material.extensions.KHR_materials_common)) {
var khrMaterialsCommon = material.extensions.KHR_materials_common;
var techniqueKey = getTechniqueKey(khrMaterialsCommon);
var technique = techniques[techniqueKey];
if (!defined(technique)) {
technique = generateTechnique(gltf, khrMaterialsCommon, lightParameters, options.optimizeForCesium);
techniques[techniqueKey] = technique;
}
// Take advantage of the fact that we generate techniques that use the
// same parameter names as the extension values.
material.values = {};
var values = khrMaterialsCommon.values;
for (var valueName in values) {
if (values.hasOwnProperty(valueName) && (valueName !== 'transparent') && (valueName !== 'doubleSided')) {
var value = values[valueName];
// Backwards compatibility for COLLADA2GLTF v1.0-draft when it encoding
// materials using KHR_materials_common with explicit type/value members
if (defined(value.value)) {
material.values[valueName] = value.value;
} else {
material.values[valueName] = value;
}
}
}
material.technique = technique;
delete material.extensions.KHR_materials_common;
if (Object.keys(material.extensions).length === 0) {
delete material.extensions;
}
}
}
}
if (defined(gltf.extensions)) {
delete gltf.extensions.KHR_materials_common;
if (Object.keys(gltf.extensions).length === 0) {
delete gltf.extensions;
}
}
// If any primitives have semantics that aren't declared in the generated
// shaders, we want to preserve them.
ensureSemanticExistence(gltf);
}
return gltf;
}