Skip to content

Commit

Permalink
Merge pull request #6421 from AnalyticalGraphicsInc/per-material-bools
Browse files Browse the repository at this point in the history
More careful splitting of glTF materials
  • Loading branch information
lilleyse authored Apr 4, 2018
2 parents d4ee00f + 602a81c commit 091200c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Change Log

##### Fixes :wrench:
* Fixed bugs in `TimeIntervalCollection.removeInterval`. [#6418](https://github.com/AnalyticalGraphicsInc/cesium/pull/6418).
* Fixed glTF support to handle meshes with and without tangent vectors, and with/without morph targets, sharing one material. [#6421](https://github.com/AnalyticalGraphicsInc/cesium/pull/6421)
* Fixed glTF support to handle skinned meshes when no skin is supplied. [#6061](https://github.com/AnalyticalGraphicsInc/cesium/issues/6061)

### 1.44 - 2018-04-02

Expand Down
55 changes: 37 additions & 18 deletions Source/ThirdParty/GltfPipeline/processPbrMetallicRoughness.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ define([
// Pre-processing to address incompatibilities between primitives using the same materials. Handles skinning and vertex color incompatibilities.
splitIncompatibleMaterials(gltf);

ForEach.material(gltf, function(material) {
ForEach.material(gltf, function(material, materialIndex) {
var pbrMetallicRoughness = material.pbrMetallicRoughness;
if (defined(pbrMetallicRoughness)) {
var technique = generateTechnique(gltf, material, options);
var technique = generateTechnique(gltf, material, materialIndex, options);

material.values = pbrMetallicRoughness;
material.technique = technique;
Expand All @@ -68,7 +68,7 @@ define([
return gltf;
}

function generateTechnique(gltf, material, options) {
function generateTechnique(gltf, material, materialIndex, options) {
var optimizeForCesium = defaultValue(options.optimizeForCesium, false);
var hasCesiumRTCExtension = defined(gltf.extensions) && defined(gltf.extensions.CESIUM_RTC);
var addBatchIdToGeneratedShaders = defaultValue(options.addBatchIdToGeneratedShaders, false);
Expand Down Expand Up @@ -101,7 +101,7 @@ define([

if (defined(primitiveInfo)) {
skinningInfo = primitiveInfo.skinning;
hasSkinning = skinningInfo.skinned;
hasSkinning = skinningInfo.skinned && (joints.length > 0);
hasVertexColors = primitiveInfo.hasVertexColors;
}

Expand All @@ -111,15 +111,17 @@ define([
var morphTargets;
ForEach.mesh(gltf, function(mesh) {
ForEach.meshPrimitive(mesh, function(primitive) {
var targets = primitive.targets;
if (!hasMorphTargets && defined(targets)) {
hasMorphTargets = true;
morphTargets = targets;
}
var attributes = primitive.attributes;
for (var attribute in attributes) {
if (attribute.indexOf('TANGENT') >= 0) {
hasTangents = true;
if (primitive.material === materialIndex) {
var targets = primitive.targets;
if (!hasMorphTargets && defined(targets)) {
hasMorphTargets = true;
morphTargets = targets;
}
var attributes = primitive.attributes;
for (var attribute in attributes) {
if (attribute.indexOf('TANGENT') >= 0) {
hasTangents = true;
}
}
}
});
Expand Down Expand Up @@ -264,7 +266,7 @@ define([
vertexShaderMain += ' weightedPosition += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n';
} else if (targetAttribute === 'NORMAL') {
vertexShaderMain += ' weightedNormal += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n';
} else if (targetAttribute === 'TANGENT') {
} else if (hasTangents && targetAttribute === 'TANGENT') {
vertexShaderMain += ' weightedTangent.xyz += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n';
}
}
Expand Down Expand Up @@ -802,6 +804,15 @@ define([
}
var isSkinned = defined(jointAccessorId);
var hasVertexColors = defined(primitive.attributes.COLOR_0);
var hasMorphTargets = defined(primitive.targets);

var hasTangents = false;
var attributes = primitive.attributes;
for (var attribute in attributes) {
if (attribute.indexOf('TANGENT') >= 0) {
hasTangents = true;
}
}

var primitiveInfo = material.extras._pipeline.primitive;
if (!defined(primitiveInfo)) {
Expand All @@ -811,21 +822,29 @@ define([
componentType : componentType,
type : type
},
hasVertexColors : hasVertexColors
hasVertexColors : hasVertexColors,
hasMorphTargets : hasMorphTargets,
hasTangents : hasTangents
};
} else if ((primitiveInfo.skinning.skinned !== isSkinned) || (primitiveInfo.skinning.type !== type) || (primitiveInfo.hasVertexColors !== hasVertexColors)) {
} else if ((primitiveInfo.skinning.skinned !== isSkinned) ||
(primitiveInfo.skinning.type !== type) ||
(primitiveInfo.hasVertexColors !== hasVertexColors) ||
(primitiveInfo.hasMorphTargets !== hasMorphTargets) ||
(primitiveInfo.hasTangents !== hasTangents)) {
// This primitive uses the same material as another one that either:
// * Isn't skinned
// * Uses a different type to store joints and weights
// * Doesn't have vertex colors
// * Doesn't have vertex colors, tangents, or morph targets
var clonedMaterial = clone(material, true);
clonedMaterial.extras._pipeline.skinning = {
skinning : {
skinned : isSkinned,
componentType : componentType,
type : type
},
hasVertexColors : hasVertexColors
hasVertexColors : hasVertexColors,
hasMorphTargets : hasMorphTargets,
hasTangents : hasTangents
};
// Split this off as a separate material
materialId = addToArray(materials, clonedMaterial);
Expand Down

0 comments on commit 091200c

Please sign in to comment.