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

Add support for glTF EXT_texture_webp #7486

Merged
merged 14 commits into from
Jan 31, 2019
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Change Log
* Added `EllipsoidRhumbLine` class as a rhumb line counterpart to `EllipsoidGeodesic`. [#7484](https://github.com/AnalyticalGraphicsInc/cesium/pull/7484)
* Added rhumb line support to `PolygonGeometry`, `PolygonOutlineGeometry`, `PolylineGeometry`, `GroundPolylineGeometry`, and `SimplePolylineGeometry`. [#7492](https://github.com/AnalyticalGraphicsInc/cesium/pull/7492)
* Improved the performance of `QuantizedMeshTerrainData.interpolateHeight`. [#7508](https://github.com/AnalyticalGraphicsInc/cesium/pull/7508)
* Added support for glTF models with WebP textures using the `EXT_texture_webp` extension. [#7486](https://github.com/AnalyticalGraphicsInc/cesium/pull/7486)

##### Fixes :wrench:
* Fixed 3D Tiles performance regression. [#7482](https://github.com/AnalyticalGraphicsInc/cesium/pull/7482)
Expand Down
40 changes: 38 additions & 2 deletions Source/Core/FeatureDetection.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
define([
'./defaultValue',
'./defined',
'./Fullscreen'
'./Fullscreen',
'./RuntimeError',
'../ThirdParty/when'
], function(
defaultValue,
defined,
Fullscreen) {
Fullscreen,
RuntimeError,
when) {
'use strict';
/*global CanvasPixelArray*/

Expand Down Expand Up @@ -199,6 +203,37 @@ define([
return supportsImageRenderingPixelated() ? imageRenderingValueResult : undefined;
}

var supportsWebpPromise;
function supportsWebp() {
// From https://developers.google.com/speed/webp/faq#how_can_i_detect_browser_support_for_webp
if (defined(supportsWebpPromise)) {
return supportsWebpPromise;
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
}

var webpDataUri = 'UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA';
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
var image = new Image();
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
supportsWebpPromise = when.defer();

if (isEdge()) {
// Edge's WebP support with WebGL is incomplete.
// See bug report: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/19221241/
supportsWebpPromise.resolve(false);
}

image.onload = function () {
var success = (image.width > 0) && (image.height > 0);
supportsWebpPromise.resolve(success);
};

image.onerror = function () {
supportsWebpPromise.resolve(false);
};

image.src = 'data:image/webp;base64,' + webpDataUri;

return supportsWebpPromise;
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
}

var typedArrayTypes = [];
if (typeof ArrayBuffer !== 'undefined') {
typedArrayTypes.push(Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array);
Expand Down Expand Up @@ -235,6 +270,7 @@ define([
hardwareConcurrency : defaultValue(theNavigator.hardwareConcurrency, 3),
supportsPointerEvents : supportsPointerEvents,
supportsImageRenderingPixelated: supportsImageRenderingPixelated,
supportsWebp: supportsWebp,
imageRenderingValue: imageRenderingValue,
typedArrayTypes: typedArrayTypes
};
Expand Down
18 changes: 17 additions & 1 deletion Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,13 @@ define([
this._useDefaultSpecularMaps = false;

this._shouldRegenerateShaders = false;
this._supportsWebp = undefined;

var that = this;
FeatureDetection.supportsWebp()
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
.then(function(result) {
that._supportsWebp = result;
});
}

defineProperties(Model.prototype, {
Expand Down Expand Up @@ -1652,6 +1659,11 @@ define([
var uri;
ForEach.texture(gltf, function(texture, id) {
var imageId = texture.source;

if (defined(texture.extensions) && defined(texture.extensions.EXT_texture_webp) && model._supportsWebp) {
imageId = texture.extensions.EXT_texture_webp.source;
}

var gltfImage = images[imageId];
var extras = gltfImage.extras;

Expand Down Expand Up @@ -4296,6 +4308,10 @@ define([
return;
}

if(!defined(this._supportsWebp)) {
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
return;
}

var context = frameState.context;
this._defaultTexture = context.defaultTexture;

Expand Down Expand Up @@ -4370,7 +4386,7 @@ define([
if (!loadResources.initialized) {
frameState.brdfLutGenerator.update(frameState);

ModelUtility.checkSupportedExtensions(this.extensionsRequired);
ModelUtility.checkSupportedExtensions(this.extensionsRequired, this._supportsWebp);
ModelUtility.updateForwardAxis(this);

// glTF pipeline updates, not needed if loading from cache
Expand Down
11 changes: 9 additions & 2 deletions Source/Scene/ModelUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define([
'../Core/Matrix3',
'../Core/Matrix4',
'../Core/Quaternion',
'../Core/FeatureDetection',
'../Core/RuntimeError',
'../Core/WebGLConstants',
'../Renderer/ShaderSource',
Expand All @@ -30,6 +31,7 @@ define([
Matrix3,
Matrix4,
Quaternion,
FeatureDetection,
RuntimeError,
WebGLConstants,
ShaderSource,
Expand Down Expand Up @@ -491,15 +493,20 @@ define([
'KHR_techniques_webgl' : true,
'KHR_materials_unlit' : true,
'KHR_materials_pbrSpecularGlossiness' : true,
'WEB3D_quantized_attributes' : true
'WEB3D_quantized_attributes' : true,
'EXT_texture_webp' : true
};

ModelUtility.checkSupportedExtensions = function(extensionsRequired) {
ModelUtility.checkSupportedExtensions = function(extensionsRequired, browserSupportsWebp) {
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
for (var extension in extensionsRequired) {
if (extensionsRequired.hasOwnProperty(extension)) {
if (!ModelUtility.supportedExtensions[extension]) {
throw new RuntimeError('Unsupported glTF Extension: ' + extension);
}

if (extension === 'EXT_texture_webp' && browserSupportsWebp === false) {
throw new RuntimeError('Loaded model requires WebP but browser does not support it.');
}
}
}
};
Expand Down
8 changes: 8 additions & 0 deletions Specs/Core/FeatureDetectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,12 @@ defineSuite([
expect(FeatureDetection.imageRenderingValue()).not.toBeDefined();
}
});

it('detects WebP support', function(done) {
FeatureDetection.supportsWebp()
.then(function(supportsWebp) {
expect(typeof supportsWebp).toEqual('boolean');
done();
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
});
});
OmarShehata marked this conversation as resolved.
Show resolved Hide resolved
});
Loading