Skip to content

Commit

Permalink
Make batch tables and CESIUM_RTC work with KHR_Materials_Common
Browse files Browse the repository at this point in the history
  • Loading branch information
JDepooter committed Nov 25, 2016
1 parent ceb6827 commit 4fe0b98
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
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 @@ -233,7 +233,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 @@ -333,6 +335,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 @@ -376,13 +379,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
26 changes: 22 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,16 @@ 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.UNSIGNED_SHORT
};
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 +693,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 +725,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 +739,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

0 comments on commit 4fe0b98

Please sign in to comment.