Skip to content

Commit

Permalink
Merge pull request #5753 from AnalyticalGraphicsInc/crn-ktx-fix
Browse files Browse the repository at this point in the history
Fix for binary glTFs storing CRN and KTX textures
  • Loading branch information
pjcozzi authored Aug 21, 2017
2 parents 59dc98e + 2dd77a9 commit 675840f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Change Log
* Added small improvements to the atmosphere. [#5741](https://github.com/AnalyticalGraphicsInc/cesium/pull/5741)
* Fixed a bug that caused imagery splitting to work incorrectly when CSS pixels were not equivalent to WebGL drawing buffer pixels, such as on high DPI displays in Microsoft Edge and Internet Explorer.
* Added `Cesium3DTileset.loadJson` to support overriding the default tileset loading behavior. [#5685](https://github.com/AnalyticalGraphicsInc/cesium/pull/5685)
* Fixed loading of binary glTFs containing CRN or KTX textures. [#5753](https://github.com/AnalyticalGraphicsInc/cesium/pull/5753)

### 1.36 - 2017-08-01

Expand Down
12 changes: 9 additions & 3 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,7 @@ define([
var extras = gltfImage.extras;

var bufferViewId = gltfImage.bufferView;
var mimeType = gltfImage.mimeType;
uri = gltfImage.uri;

// First check for a compressed texture
Expand All @@ -1542,24 +1543,28 @@ define([
var etc1 = extras.compressedImage3DTiles.etc1;

if (context.s3tc && defined(crunch)) {
mimeType = crunch.mimeType;
if (defined(crunch.bufferView)) {
bufferViewId = crunch.bufferView;
} else {
uri = crunch.uri;
}
} else if (context.s3tc && defined(s3tc)) {
mimeType = s3tc.mimeType;
if (defined(s3tc.bufferView)) {
bufferViewId = s3tc.bufferView;
} else {
uri = s3tc.uri;
}
} else if (context.pvrtc && defined(pvrtc)) {
mimeType = pvrtc.mimeType;
if (defined(pvrtc.bufferView)) {
bufferViewId = pvrtc.bufferView;
} else {
uri = pvrtc.uri;
}
} else if (context.etc1 && defined(etc1)) {
mimeType = etc1.mimeType;
if (defined(etc1.bufferView)) {
bufferViewId = etc1.bufferView;
} else {
Expand All @@ -1574,7 +1579,7 @@ define([
id : id,
image : undefined,
bufferView : bufferViewId,
mimeType : gltfImage.mimeType
mimeType : mimeType
});
} else {
++model._loadResources.pendingTextureLoads;
Expand Down Expand Up @@ -2193,14 +2198,15 @@ define([

var gltf = model.gltf;
var bufferView = gltf.bufferViews[gltfTexture.bufferView];
var imageId = gltf.textures[gltfTexture.id].source;

var onerror = getFailedLoadFunction(model, 'image', 'id: ' + gltfTexture.id + ', bufferView: ' + gltfTexture.bufferView);

if (gltfTexture.mimeType === 'image/ktx') {
loadKTX(loadResources.getBuffer(bufferView)).then(imageLoad(model, gltfTexture.id)).otherwise(onerror);
loadKTX(loadResources.getBuffer(bufferView)).then(imageLoad(model, gltfTexture.id, imageId)).otherwise(onerror);
++model._loadResources.pendingTextureLoads;
} else if (gltfTexture.mimeType === 'image/crn') {
loadCRN(loadResources.getBuffer(bufferView)).then(imageLoad(model, gltfTexture.id)).otherwise(onerror);
loadCRN(loadResources.getBuffer(bufferView)).then(imageLoad(model, gltfTexture.id, imageId)).otherwise(onerror);
++model._loadResources.pendingTextureLoads;
} else {
var onload = getOnImageCreatedFromTypedArray(loadResources, gltfTexture);
Expand Down
32 changes: 32 additions & 0 deletions Source/ThirdParty/GltfPipeline/updateVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,26 @@ define([
delete image.extensions;
}
}
if (defined(image.extras)) {
var compressedImages = image.extras.compressedImage3DTiles;
for (var type in compressedImages) {
if (compressedImages.hasOwnProperty(type)) {
var compressedImage = compressedImages[type];
var compressedExtensions = compressedImage.extensions;
if (defined(compressedExtensions)) {
var compressedBinaryGltf = compressedExtensions.KHR_binary_glTF;
if (defined(compressedBinaryGltf)) {
compressedImage.bufferView = globalMapping.bufferViews[compressedBinaryGltf.bufferView];
compressedImage.mimeType = compressedBinaryGltf.mimeType;
delete compressedExtensions.KHR_binary_glTF;
}
if (Object.keys(extensions).length === 0) {
delete compressedImage.extensions;
}
}
}
}
}
});
ForEach.texture(gltf, function(texture) {
if (defined(texture.sampler)) {
Expand Down Expand Up @@ -769,6 +789,18 @@ define([
if (defined(imageBufferView)) {
image.bufferView = bufferViewShiftMap[imageBufferView];
}
if (defined(image.extras)) {
var compressedImages = image.extras.compressedImage3DTiles;
for (var type in compressedImages) {
if (compressedImages.hasOwnProperty(type)) {
var compressedImage = compressedImages[type];
var compressedImageBufferView = compressedImage.bufferView;
if (defined(compressedImageBufferView)) {
compressedImage.bufferView = bufferViewShiftMap[compressedImageBufferView];
}
}
}
}
});
}

Expand Down
30 changes: 24 additions & 6 deletions Specs/Scene/ModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,22 +888,31 @@ defineSuite([
});

it('renders textured box with external KTX texture', function() {
return loadModel(texturedBoxKTXUrl).then(function(m) {
return loadModel(texturedBoxKTXUrl, {
incrementallyLoadTextures : false
}).then(function(m) {
verifyRender(m);
expect(Object.keys(m._rendererResources.textures).length).toBe(1);
primitives.remove(m);
});
});

it('renders textured box with embedded binary KTX texture', function() {
return loadModel(texturedBoxKTXBinaryUrl).then(function(m) {
return loadModel(texturedBoxKTXBinaryUrl, {
incrementallyLoadTextures : false
}).then(function(m) {
verifyRender(m);
expect(Object.keys(m._rendererResources.textures).length).toBe(1);
primitives.remove(m);
});
});

it('renders textured box with embedded base64 encoded KTX texture', function() {
return loadModel(texturedBoxKTXEmbeddedUrl).then(function(m) {
return loadModel(texturedBoxKTXEmbeddedUrl, {
incrementallyLoadTextures : false
}).then(function(m) {
verifyRender(m);
expect(Object.keys(m._rendererResources.textures).length).toBe(1);
primitives.remove(m);
});
});
Expand All @@ -912,8 +921,11 @@ defineSuite([
if (!scene.context.s3tc) {
return;
}
return loadModel(texturedBoxCRNUrl).then(function(m) {
return loadModel(texturedBoxCRNUrl, {
incrementallyLoadTextures : false
}).then(function(m) {
verifyRender(m);
expect(Object.keys(m._rendererResources.textures).length).toBe(1);
primitives.remove(m);
});
});
Expand All @@ -922,8 +934,11 @@ defineSuite([
if (!scene.context.s3tc) {
return;
}
return loadModel(texturedBoxCRNBinaryUrl).then(function(m) {
return loadModel(texturedBoxCRNBinaryUrl, {
incrementallyLoadTextures : false
}).then(function(m) {
verifyRender(m);
expect(Object.keys(m._rendererResources.textures).length).toBe(1);
primitives.remove(m);
});
});
Expand All @@ -932,8 +947,11 @@ defineSuite([
if (!scene.context.s3tc) {
return;
}
return loadModel(texturedBoxCRNEmbeddedUrl).then(function(m) {
return loadModel(texturedBoxCRNEmbeddedUrl, {
incrementallyLoadTextures : false
}).then(function(m) {
verifyRender(m);
expect(Object.keys(m._rendererResources.textures).length).toBe(1);
primitives.remove(m);
});
});
Expand Down

0 comments on commit 675840f

Please sign in to comment.