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
38 changes: 36 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,35 @@ 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.promise;
}

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);
}

var image = new Image();
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,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA';

return supportsWebpPromise.promise;
}

var typedArrayTypes = [];
if (typeof ArrayBuffer !== 'undefined') {
typedArrayTypes.push(Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array);
Expand Down Expand Up @@ -235,6 +268,7 @@ define([
hardwareConcurrency : defaultValue(theNavigator.hardwareConcurrency, 3),
supportsPointerEvents : supportsPointerEvents,
supportsImageRenderingPixelated: supportsImageRenderingPixelated,
supportsWebp: supportsWebp,
imageRenderingValue: imageRenderingValue,
typedArrayTypes: typedArrayTypes
};
Expand Down
14 changes: 13 additions & 1 deletion Source/Scene/ClassificationModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ define([
this._rtcCenterEye = undefined; // in eye coordinates
this._rtcCenter3D = undefined; // in world coordinates
this._rtcCenter2D = undefined; // in projected world coordinates

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(ClassificationModel.prototype, {
Expand Down Expand Up @@ -958,6 +966,10 @@ define([
return;
}

if (!defined(this._supportsWebp)) {
return;
}

if ((this._state === ModelState.NEEDS_LOAD) && defined(this.gltf)) {
this._state = ModelState.LOADING;
if (this._state !== ModelState.FAILED) {
Expand Down Expand Up @@ -991,7 +1003,7 @@ define([
// Transition from LOADING -> LOADED once resources are downloaded and created.
// Textures may continue to stream in while in the LOADED state.
if (loadResources.pendingBufferLoads === 0) {
ModelUtility.checkSupportedExtensions(this.extensionsRequired);
ModelUtility.checkSupportedExtensions(this.extensionsRequired, this._supportsWebp);

addBuffersToLoadResources(this);
parseBufferViews(this);
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)) {
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
7 changes: 7 additions & 0 deletions Specs/Core/FeatureDetectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,11 @@ defineSuite([
expect(FeatureDetection.imageRenderingValue()).not.toBeDefined();
}
});

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