Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KHR_materials_common and KHR_binary_glTF compatibility #3294

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Change Log
==========

### 1.18 - 2016-02-01

* Improved compatibility with glTF KHR_binary_glTF and KHR_materials_common extensions
* Reduced the amount of CPU memory used by terrain by ~25% in Chrome.
* Fixed a picking problem ([#3386](https://github.com/AnalyticalGraphicsInc/cesium/issues/3386)) that sometimes prevented objects being selected.
* Added `Scene.useDepthPicking` to enable or disable picking using the depth buffer. [#3390](https://github.com/AnalyticalGraphicsInc/cesium/pull/3390)
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
* [Tucker Tibbetts](https://github.com/cttibbetts)
* [Eric Putnam](https://github.com/eputnam)
* [Dmitriy Pushkov](https://github.com/ezze)
* [Max Limper](https://github.com/mlimper)

Also see [our contributors page](http://cesiumjs.org/contributors.html) for more information.
8 changes: 5 additions & 3 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ define([
// Binary glTF
var result = parseBinaryGltfHeader(gltf);

// CESIUM_binary_glTF is from the beginning of the file but
// CESIUM_binary_glTF is from the beginning of the file but the
// KHR_binary_glTF is from the beginning of the binary section
if (result.binaryOffset !== 0) {
gltf = gltf.subarray(result.binaryOffset);
Expand Down Expand Up @@ -975,7 +975,7 @@ define([
// Load binary glTF
var result = parseBinaryGltfHeader(array);
// CESIUM_binary_glTF is from the beginning of the file but
// KHR_binary_glTF is from the beginning of the binary section
// KHR_binary_glTF is from the beginning of the binary section
if (result.binaryOffset !== 0) {
array = array.subarray(result.binaryOffset);
}
Expand Down Expand Up @@ -1148,7 +1148,9 @@ define([
if (buffers.hasOwnProperty(id)) {
var buffer = buffers[id];

if (id === 'CESIUM_binary_glTF' || id === 'KHR_binary_glTF') {
// The extension 'KHR_binary_glTF' uses a special buffer entitled just 'binary_glTF'.
// The 'KHR_binary_glTF' check is for backwards compatibility with Cesium 1.15.
if (id === 'CESIUM_binary_glTF' || id === 'binary_glTF' || id === 'KHR_binary_glTF') {
// Buffer is the binary glTF file itself that is already loaded
var loadResources = model._loadResources;
loadResources.buffers[id] = model._cachedGltf.bgltf;
Expand Down
77 changes: 68 additions & 9 deletions Source/Scene/modelMaterialsCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,17 @@ define([
var lowerCase;
var hasTexCoords = false;
for(var name in parameterValues) {
if (parameterValues.hasOwnProperty(name)) {
var value = parameterValues[name];
//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 && (value.type === WebGLConstants.SAMPLER_2D)) {
if (!hasTexCoords && (valType === WebGLConstants.SAMPLER_2D)) {
hasTexCoords = true;
}
techniqueParameters[lowerCase] = {
type: value.type
type: valType
};
}
}
Expand Down Expand Up @@ -609,18 +612,64 @@ define([
return techniqueId;
}

function getKHRMaterialsCommonValueType(paramName, paramValue)
{
var value;

// for compatibility with old glb files, 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 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 + ';';
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];
if (values.hasOwnProperty(name)) {
var value = values[name];
techniqueKey += name + ':' + value.type.toString();
//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 += ';';
}
}
Expand Down Expand Up @@ -692,7 +741,17 @@ define([
for (var valueName in values) {
if (values.hasOwnProperty(valueName)) {
var value = values[valueName];
material.values[valueName] = value.value;

// for compatibility with old glb files, 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;
}
}
}

Expand Down