Skip to content

Commit

Permalink
Merge pull request #4684 from JDepooter/khr_materials_common_with_bat…
Browse files Browse the repository at this point in the history
…ch_id

3D Tiles: Make KHR_Materials_Common work with batch tables and CESIUM_RTC extensions
  • Loading branch information
lilleyse authored Dec 6, 2016
2 parents f744170 + 3de6aa2 commit b40394b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
* [Camptocamp SA](https://www.camptocamp.com/)
* [Frédéric Junod](https://github.com/fredj)
* [Guillaume Beraudo](https://github.com/gberaudo)
* [Safe Software](https://www.safe.com)
* [Joel Depooter](https://github.com/JDepooter)


## [Individual CLA](http://www.agi.com/licenses/individual-cla-agi-v1.0.txt)
* [Victor Berchet](https://github.com/vicb)
Expand Down
3 changes: 2 additions & 1 deletion Source/Scene/Batched3DModel3DTileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ define([
uniformMapLoaded : batchTable.getUniformMapCallback(),
pickVertexShaderLoaded : getPickVertexShaderCallback(this),
pickFragmentShaderLoaded : batchTable.getPickFragmentShaderCallback(),
pickUniformMapLoaded : batchTable.getPickUniformMapCallback()
pickUniformMapLoaded : batchTable.getPickUniformMapCallback(),
addBatchIdToGeneratedShaders : (batchLength > 0) // If the batch table has values in it, generated shaders will need a batchId attribute
});

this._model = model;
Expand Down
11 changes: 8 additions & 3 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ define([
// Note that this is a global cache, compared to renderer resources, which
// are cached per context.
function CachedGltf(options) {
this._gltf = modelMaterialsCommon(gltfDefaults(options.gltf));
this._gltf = modelMaterialsCommon(gltfDefaults(options.gltf), {
addBatchIdToGeneratedShaders : options.addBatchIdToGeneratedShaders
});
this._bgltf = options.bgltf;
this.ready = options.ready;
this.modelsToLoad = [];
Expand Down Expand Up @@ -331,6 +333,7 @@ define([
* @param {HeightReference} [options.heightReference] Determines how the model is drawn relative to terrain.
* @param {Scene} [options.scene] Must be passed in for models that use the height reference property.
* @param {DistanceDisplayCondition} [options.istanceDisplayCondition] The condition specifying at what distance from the camera that this model will be displayed.
* @param {Boolean} [options.addBatchIdToGeneratedShaders=false] Determines if shaders generated for materials using the KHR_materials_common extension should include a batchId attribute. For models contained in B3DM tiles.
*
* @exception {DeveloperError} bgltf is not a valid Binary glTF file.
* @exception {DeveloperError} Only glTF Binary version 1 is supported.
Expand Down Expand Up @@ -374,13 +377,15 @@ define([
cachedGltf = new CachedGltf({
gltf : result.glTF,
bgltf : gltf,
ready : true
ready : true,
addBatchIdToGeneratedShaders : options.addBatchIdToGeneratedShaders
});
} else {
// Normal glTF (JSON)
cachedGltf = new CachedGltf({
gltf : options.gltf,
ready : true
ready : true,
addBatchIdToGeneratedShaders : options.addBatchIdToGeneratedShaders
});
}

Expand Down
25 changes: 21 additions & 4 deletions Source/Scene/modelMaterialsCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ define([
var vertexShaderCount = 0;
var fragmentShaderCount = 0;
var programCount = 0;
function generateTechnique(gltf, khrMaterialsCommon, lightParameters) {
function generateTechnique(gltf, khrMaterialsCommon, lightParameters, options) {
var techniques = gltf.techniques;
var shaders = gltf.shaders;
var programs = gltf.programs;
Expand Down Expand Up @@ -195,7 +195,7 @@ define([
var techniqueParameters = {
// Add matrices
modelViewMatrix: {
semantic: 'MODELVIEW',
semantic: options.useCesiumRTCMatrixInShaders ? 'CESIUM_RTC_MODELVIEW' : 'MODELVIEW',
type: WebGLConstants.FLOAT_MAT4
},
projectionMatrix: {
Expand Down Expand Up @@ -349,6 +349,15 @@ define([
vertexShader += 'attribute vec4 a_joint;\n';
vertexShader += 'attribute vec4 a_weight;\n';
}

if (options.addBatchIdToGeneratedShaders) {
techniqueAttributes.a_batchId = 'batchId';
techniqueParameters.batchId = {
semantic: 'BATCHID',
type: WebGLConstants.FLOAT
};
vertexShader += 'attribute float a_batchId;\n';
}

var hasSpecular = hasNormals && ((lightingModel === 'BLINN') || (lightingModel === 'PHONG')) &&
defined(techniqueParameters.specular) && defined(techniqueParameters.shininess);
Expand Down Expand Up @@ -683,10 +692,12 @@ define([
*
* @private
*/
function modelMaterialsCommon(gltf) {
function modelMaterialsCommon(gltf, options) {
if (!defined(gltf)) {
return undefined;
}

options = defaultValue(options, defaultValue.EMPTY_OBJECT);

var hasExtension = false;
var extensionsUsed = gltf.extensionsUsed;
Expand All @@ -713,6 +724,9 @@ define([
}

var lightParameters = generateLightParameters(gltf);

var hasCesiumRTCExtension = defined(gltf.extensions) && defined(gltf.extensions.CESIUM_RTC);
var addBatchIdToGeneratedShaders = defaultValue(options.addBatchIdToGeneratedShaders, false);

var techniques = {};
var materials = gltf.materials;
Expand All @@ -724,7 +738,10 @@ define([
var techniqueKey = getTechniqueKey(khrMaterialsCommon);
var technique = techniques[techniqueKey];
if (!defined(technique)) {
technique = generateTechnique(gltf, khrMaterialsCommon, lightParameters);
technique = generateTechnique(gltf, khrMaterialsCommon, lightParameters, {
addBatchIdToGeneratedShaders : addBatchIdToGeneratedShaders,
useCesiumRTCMatrixInShaders : hasCesiumRTCExtension
});
techniques[techniqueKey] = technique;
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"asset": {
"version": "0.0"
},
"properties": {
"Longitude": {
"minimum": -1.3196972173766555,
"maximum": -1.3196718547473905
},
"Latitude": {
"minimum": 0.6988624606923348,
"maximum": 0.6988888301460953
},
"Height": {
"minimum": 6.2074098233133554,
"maximum": 12.83180232718587
}
},
"geometricError": 70,
"root": {
"refine": "add",
"boundingVolume": {
"region": [
-1.3197004795898053,
0.6988582109,
-1.3196595204101946,
0.6988897891,
0,
20
]
},
"geometricError": 0,
"content": {
"url": "batchedWithKHRMaterialsCommon.b3dm"
}
}
}
8 changes: 8 additions & 0 deletions Specs/Scene/Batched3DModel3DTileContentSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defineSuite([
var withoutBatchTableUrl = './Data/Cesium3DTiles/Batched/BatchedWithoutBatchTable/';
var translucentUrl = './Data/Cesium3DTiles/Batched/BatchedTranslucent/';
var translucentOpaqueMixUrl = './Data/Cesium3DTiles/Batched/BatchedTranslucentOpaqueMix/';
var withKHRMaterialsCommonUrl = './Data/Cesium3DTiles/Batched/BatchedWithKHRMaterialsCommon/';
var withTransformBoxUrl = './Data/Cesium3DTiles/Batched/BatchedWithTransformBox/';
var withTransformSphereUrl = './Data/Cesium3DTiles/Batched/BatchedWithTransformSphere/';
var withTransformRegionUrl = './Data/Cesium3DTiles/Batched/BatchedWithTransformRegion/';
Expand Down Expand Up @@ -143,6 +144,13 @@ defineSuite([
});
});

it('renders with KHR_materials_common extension', function() {
// Tests that the batchId attribute and CESIUM_RTC extension are handled correctly
return Cesium3DTilesTester.loadTileset(scene, withKHRMaterialsCommonUrl).then(function(tileset) {
Cesium3DTilesTester.expectRenderTileset(scene, tileset);
});
});

function expectRenderWithTransform(url) {
return Cesium3DTilesTester.loadTileset(scene, url).then(function(tileset) {
Cesium3DTilesTester.expectRenderTileset(scene, tileset);
Expand Down

0 comments on commit b40394b

Please sign in to comment.