Skip to content

Commit dfaf9cc

Browse files
committed
Merge pull request #3413 from AnalyticalGraphicsInc/gltf-materials
glTF material improvements
2 parents 1cbc087 + f6361c7 commit dfaf9cc

File tree

17 files changed

+210
-310
lines changed

17 files changed

+210
-310
lines changed
-572 Bytes
Binary file not shown.

Apps/SampleData/models/CesiumMilkTruck/CesiumMilkTruck-kmc.gltf

Lines changed: 36 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -379,28 +379,19 @@
379379
"technique": "PHONG",
380380
"transparent": false,
381381
"values": {
382-
"diffuse": {
383-
"type": 35666,
384-
"value": [
385-
0,
386-
0.04050629958510399,
387-
0.021240700036287308,
388-
1
389-
]
390-
},
391-
"shininess": {
392-
"type": 5126,
393-
"value": 256
394-
},
395-
"specular": {
396-
"type": 35666,
397-
"value": [
398-
0.6499999761581421,
399-
0.6499999761581421,
400-
0.6499999761581421,
401-
1
402-
]
403-
}
382+
"diffuse": [
383+
0,
384+
0.04050629958510399,
385+
0.021240700036287308,
386+
1
387+
],
388+
"shininess": 256,
389+
"specular": [
390+
0.6499999761581421,
391+
0.6499999761581421,
392+
0.6499999761581421,
393+
1
394+
]
404395
}
405396
}
406397
},
@@ -414,58 +405,19 @@
414405
"technique": "PHONG",
415406
"transparent": false,
416407
"values": {
417-
"diffuse": {
418-
"type": 35678,
419-
"value": "texture_Image0001"
420-
},
421-
"shininess": {
422-
"type": 5126,
423-
"value": 256
424-
},
425-
"specular": {
426-
"type": 35666,
427-
"value": [
428-
0.03999999910593033,
429-
0.03999999910593033,
430-
0.03999999910593033,
431-
1
432-
]
433-
}
408+
"diffuse": "texture_Image0001",
409+
"shininess": 256,
410+
"specular": [
411+
0.03999999910593033,
412+
0.03999999910593033,
413+
0.03999999910593033,
414+
1
415+
]
434416
}
435417
}
436418
},
437419
"name": "truck"
438420
},
439-
"Effect-wheels": {
440-
"extensions": {
441-
"KHR_materials_common": {
442-
"doubleSided": false,
443-
"jointCount": 0,
444-
"technique": "PHONG",
445-
"transparent": false,
446-
"values": {
447-
"diffuse": {
448-
"type": 35678,
449-
"value": "texture_Image0001"
450-
},
451-
"shininess": {
452-
"type": 5126,
453-
"value": 256
454-
},
455-
"specular": {
456-
"type": 35666,
457-
"value": [
458-
0.03999999910593033,
459-
0.03999999910593033,
460-
0.03999999910593033,
461-
1
462-
]
463-
}
464-
}
465-
}
466-
},
467-
"name": "wheels"
468-
},
469421
"Effect-window_trim": {
470422
"extensions": {
471423
"KHR_materials_common": {
@@ -474,28 +426,19 @@
474426
"technique": "PHONG",
475427
"transparent": false,
476428
"values": {
477-
"diffuse": {
478-
"type": 35666,
479-
"value": [
480-
0.06400000303983688,
481-
0.06400000303983688,
482-
0.06400000303983688,
483-
1
484-
]
485-
},
486-
"shininess": {
487-
"type": 5126,
488-
"value": 256
489-
},
490-
"specular": {
491-
"type": 35666,
492-
"value": [
493-
0.03999999910593033,
494-
0.03999999910593033,
495-
0.03999999910593033,
496-
1
497-
]
498-
}
429+
"diffuse": [
430+
0.06400000303983688,
431+
0.06400000303983688,
432+
0.06400000303983688,
433+
1
434+
],
435+
"shininess": 256,
436+
"specular": [
437+
0.03999999910593033,
438+
0.03999999910593033,
439+
0.03999999910593033,
440+
1
441+
]
499442
}
500443
}
501444
},
@@ -513,7 +456,7 @@
513456
"TEXCOORD_0": "accessor_136"
514457
},
515458
"indices": "accessor_127",
516-
"material": "Effect-wheels",
459+
"material": "Effect-truck",
517460
"mode": 4
518461
},
519462
{
@@ -523,7 +466,7 @@
523466
"TEXCOORD_0": "accessor_136"
524467
},
525468
"indices": "accessor_130",
526-
"material": "Effect-wheels",
469+
"material": "Effect-truck",
527470
"mode": 4
528471
}
529472
]

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ Change Log
22
==========
33

44
### 1.18 - 2016-02-01
5-
65
* Breaking changes
76
* Removed support for `CESIUM_binary_glTF`. Use `KHR_binary_glTF` instead, which is the default for the online [COLLADA-to-glTF converter](http://cesiumjs.org/convertmodel.html).
87
* Deprecated
@@ -19,7 +18,9 @@ Change Log
1918
}
2019
```
2120
* `GroundPrimitive` now supports batching geometry for better performance.
21+
* Improved compatibility with glTF KHR_binary_glTF and KHR_materials_common extensions
2222
* Added `ImageryLayer.getViewableRectangle` to make it easy to get the effective bounds of an imagery layer.
23+
* Improved compatibility with glTF KHR_binary_glTF and KHR_materials_common extensions
2324
* Fixed a picking issue that sometimes prevented objects being selected. [#3386](https://github.com/AnalyticalGraphicsInc/cesium/issues/3386)
2425
* Fixed cracking between tiles in 2D. [#3486](https://github.com/AnalyticalGraphicsInc/cesium/pull/3486)
2526
* Fixed creating bounding volumes for `GroundPrimitive`s whose containing rectangle has a width greater than pi.

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,6 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
8181
* [Tucker Tibbetts](https://github.com/cttibbetts)
8282
* [Eric Putnam](https://github.com/eputnam)
8383
* [Dmitriy Pushkov](https://github.com/ezze)
84+
* [Max Limper](https://github.com/mlimper)
8485

8586
Also see [our contributors page](http://cesiumjs.org/contributors.html) for more information.

Source/Scene/Model.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ define([
956956
if (containsGltfMagic(array)) {
957957
// Load binary glTF
958958
var result = parseBinaryGltfHeader(array);
959-
// KHR_binary_glTF is from the beginning of the binary section
959+
// KHR_binary_glTF is from the beginning of the binary section
960960
if (result.binaryOffset !== 0) {
961961
array = array.subarray(result.binaryOffset);
962962
}
@@ -1129,7 +1129,10 @@ define([
11291129
if (buffers.hasOwnProperty(id)) {
11301130
var buffer = buffers[id];
11311131

1132-
if (id === 'KHR_binary_glTF') {
1132+
// The extension 'KHR_binary_glTF' uses a special buffer entitled just 'binary_glTF'.
1133+
// The 'KHR_binary_glTF' check is for backwards compatibility for the Cesium model converter
1134+
// circa Cesium 1.15-1.17 when the converter incorrectly used the buffer name 'KHR_binary_glTF'.
1135+
if ((id === 'binary_glTF') || (id === 'KHR_binary_glTF')) {
11331136
// Buffer is the binary glTF file itself that is already loaded
11341137
var loadResources = model._loadResources;
11351138
loadResources.buffers[id] = model._cachedGltf.bgltf;

Source/Scene/modelMaterialsCommon.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,16 @@ define([
223223
var lowerCase;
224224
var hasTexCoords = false;
225225
for(var name in parameterValues) {
226-
if (parameterValues.hasOwnProperty(name)) {
227-
var value = parameterValues[name];
226+
//generate shader parameters for KHR_materials_common attributes
227+
//(including a check, because some boolean flags should not be used as shader parameters)
228+
if (parameterValues.hasOwnProperty(name) && (name !== 'transparent') && (name !== 'doubleSided')) {
229+
var valType = getKHRMaterialsCommonValueType(name, parameterValues[name]);
228230
lowerCase = name.toLowerCase();
229-
if (!hasTexCoords && (value.type === WebGLConstants.SAMPLER_2D)) {
231+
if (!hasTexCoords && (valType === WebGLConstants.SAMPLER_2D)) {
230232
hasTexCoords = true;
231233
}
232234
techniqueParameters[lowerCase] = {
233-
type: value.type
235+
type: valType
234236
};
235237
}
236238
}
@@ -609,18 +611,54 @@ define([
609611
return techniqueId;
610612
}
611613

614+
function getKHRMaterialsCommonValueType(paramName, paramValue)
615+
{
616+
var value;
617+
618+
// Backwards compatibility for COLLADA2GLTF v1.0-draft when it encoding
619+
// materials using KHR_materials_common with explicit type/value members
620+
if (defined(paramValue.value)) {
621+
value = paramValue.value;
622+
} else {
623+
value = paramValue;
624+
}
625+
626+
switch (paramName) {
627+
case 'ambient':
628+
return (value instanceof String || typeof value === 'string') ? WebGLConstants.SAMPLER_2D : WebGLConstants.FLOAT_VEC4;
629+
case 'diffuse':
630+
return (value instanceof String || typeof value === 'string') ? WebGLConstants.SAMPLER_2D : WebGLConstants.FLOAT_VEC4;
631+
case 'emission':
632+
return (value instanceof String || typeof value === 'string') ? WebGLConstants.SAMPLER_2D : WebGLConstants.FLOAT_VEC4;
633+
case 'specular':
634+
return (value instanceof String || typeof value === 'string') ? WebGLConstants.SAMPLER_2D : WebGLConstants.FLOAT_VEC4;
635+
case 'shininess':
636+
return WebGLConstants.FLOAT;
637+
case 'transparency':
638+
return WebGLConstants.FLOAT;
639+
640+
// these two are usually not used directly within shaders,
641+
// they are just added here for completeness
642+
case 'transparent':
643+
return WebGLConstants.BOOL;
644+
case 'doubleSided':
645+
return WebGLConstants.BOOL;
646+
}
647+
}
648+
612649
function getTechniqueKey(khrMaterialsCommon) {
613650
var techniqueKey = '';
614-
techniqueKey += 'technique:' + khrMaterialsCommon.technique + ';';
651+
techniqueKey += 'technique:' + khrMaterialsCommon.technique + ';';
615652

616653
var values = khrMaterialsCommon.values;
617654
var keys = Object.keys(values).sort();
618655
var keysCount = keys.length;
619656
for (var i=0;i<keysCount;++i) {
620657
var name = keys[i];
621-
if (values.hasOwnProperty(name)) {
622-
var value = values[name];
623-
techniqueKey += name + ':' + value.type.toString();
658+
//generate first part of key using shader parameters for KHR_materials_common attributes
659+
//(including a check, because some boolean flags should not be used as shader parameters)
660+
if (values.hasOwnProperty(name) && (name !== 'transparent') && (name !== 'doubleSided')) {
661+
techniqueKey += name + ':' + getKHRMaterialsCommonValueType(name, values[name]);
624662
techniqueKey += ';';
625663
}
626664
}
@@ -692,7 +730,14 @@ define([
692730
for (var valueName in values) {
693731
if (values.hasOwnProperty(valueName)) {
694732
var value = values[valueName];
695-
material.values[valueName] = value.value;
733+
734+
// Backwards compatibility for COLLADA2GLTF v1.0-draft when it encoding
735+
// materials using KHR_materials_common with explicit type/value members
736+
if (defined(value.value)) {
737+
material.values[valueName] = value.value;
738+
} else {
739+
material.values[valueName] = value;
740+
}
696741
}
697742
}
698743

Specs/Data/Models/MaterialsCommon/BoxAmbientLight.gltf

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"api": "WebGL",
7070
"version": "1.0.2"
7171
},
72-
"version": 1
72+
"version": "1.0"
7373
},
7474
"bufferViews": {
7575
"bufferView_29": {
@@ -138,32 +138,20 @@
138138
"technique": "PHONG",
139139
"transparent": false,
140140
"values": {
141-
"ambient": {
142-
"type": 35678,
143-
"value": "texture_Image0001"
144-
},
145-
"diffuse": {
146-
"type": 35666,
147-
"value": [
148-
0,
149-
0,
150-
0,
151-
1
152-
]
153-
},
154-
"shininess": {
155-
"type": 5126,
156-
"value": 256
157-
},
158-
"specular": {
159-
"type": 35666,
160-
"value": [
161-
0.20000000298023224,
162-
0.20000000298023224,
163-
0.20000000298023224,
164-
1
165-
]
166-
}
141+
"ambient": "texture_Image0001",
142+
"diffuse": [
143+
0,
144+
0,
145+
0,
146+
1
147+
],
148+
"shininess": 256,
149+
"specular": [
150+
0.20000000298023224,
151+
0.20000000298023224,
152+
0.20000000298023224,
153+
1
154+
]
167155
}
168156
}
169157
},

0 commit comments

Comments
 (0)