Skip to content

Commit 091200c

Browse files
authored
Merge pull request #6421 from AnalyticalGraphicsInc/per-material-bools
More careful splitting of glTF materials
2 parents d4ee00f + 602a81c commit 091200c

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Change Log
55

66
##### Fixes :wrench:
77
* Fixed bugs in `TimeIntervalCollection.removeInterval`. [#6418](https://github.com/AnalyticalGraphicsInc/cesium/pull/6418).
8+
* 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)
9+
* Fixed glTF support to handle skinned meshes when no skin is supplied. [#6061](https://github.com/AnalyticalGraphicsInc/cesium/issues/6061)
810

911
### 1.44 - 2018-04-02
1012

Source/ThirdParty/GltfPipeline/processPbrMetallicRoughness.js

+37-18
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ define([
4949
// Pre-processing to address incompatibilities between primitives using the same materials. Handles skinning and vertex color incompatibilities.
5050
splitIncompatibleMaterials(gltf);
5151

52-
ForEach.material(gltf, function(material) {
52+
ForEach.material(gltf, function(material, materialIndex) {
5353
var pbrMetallicRoughness = material.pbrMetallicRoughness;
5454
if (defined(pbrMetallicRoughness)) {
55-
var technique = generateTechnique(gltf, material, options);
55+
var technique = generateTechnique(gltf, material, materialIndex, options);
5656

5757
material.values = pbrMetallicRoughness;
5858
material.technique = technique;
@@ -68,7 +68,7 @@ define([
6868
return gltf;
6969
}
7070

71-
function generateTechnique(gltf, material, options) {
71+
function generateTechnique(gltf, material, materialIndex, options) {
7272
var optimizeForCesium = defaultValue(options.optimizeForCesium, false);
7373
var hasCesiumRTCExtension = defined(gltf.extensions) && defined(gltf.extensions.CESIUM_RTC);
7474
var addBatchIdToGeneratedShaders = defaultValue(options.addBatchIdToGeneratedShaders, false);
@@ -101,7 +101,7 @@ define([
101101

102102
if (defined(primitiveInfo)) {
103103
skinningInfo = primitiveInfo.skinning;
104-
hasSkinning = skinningInfo.skinned;
104+
hasSkinning = skinningInfo.skinned && (joints.length > 0);
105105
hasVertexColors = primitiveInfo.hasVertexColors;
106106
}
107107

@@ -111,15 +111,17 @@ define([
111111
var morphTargets;
112112
ForEach.mesh(gltf, function(mesh) {
113113
ForEach.meshPrimitive(mesh, function(primitive) {
114-
var targets = primitive.targets;
115-
if (!hasMorphTargets && defined(targets)) {
116-
hasMorphTargets = true;
117-
morphTargets = targets;
118-
}
119-
var attributes = primitive.attributes;
120-
for (var attribute in attributes) {
121-
if (attribute.indexOf('TANGENT') >= 0) {
122-
hasTangents = true;
114+
if (primitive.material === materialIndex) {
115+
var targets = primitive.targets;
116+
if (!hasMorphTargets && defined(targets)) {
117+
hasMorphTargets = true;
118+
morphTargets = targets;
119+
}
120+
var attributes = primitive.attributes;
121+
for (var attribute in attributes) {
122+
if (attribute.indexOf('TANGENT') >= 0) {
123+
hasTangents = true;
124+
}
123125
}
124126
}
125127
});
@@ -264,7 +266,7 @@ define([
264266
vertexShaderMain += ' weightedPosition += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n';
265267
} else if (targetAttribute === 'NORMAL') {
266268
vertexShaderMain += ' weightedNormal += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n';
267-
} else if (targetAttribute === 'TANGENT') {
269+
} else if (hasTangents && targetAttribute === 'TANGENT') {
268270
vertexShaderMain += ' weightedTangent.xyz += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n';
269271
}
270272
}
@@ -802,6 +804,15 @@ define([
802804
}
803805
var isSkinned = defined(jointAccessorId);
804806
var hasVertexColors = defined(primitive.attributes.COLOR_0);
807+
var hasMorphTargets = defined(primitive.targets);
808+
809+
var hasTangents = false;
810+
var attributes = primitive.attributes;
811+
for (var attribute in attributes) {
812+
if (attribute.indexOf('TANGENT') >= 0) {
813+
hasTangents = true;
814+
}
815+
}
805816

806817
var primitiveInfo = material.extras._pipeline.primitive;
807818
if (!defined(primitiveInfo)) {
@@ -811,21 +822,29 @@ define([
811822
componentType : componentType,
812823
type : type
813824
},
814-
hasVertexColors : hasVertexColors
825+
hasVertexColors : hasVertexColors,
826+
hasMorphTargets : hasMorphTargets,
827+
hasTangents : hasTangents
815828
};
816-
} else if ((primitiveInfo.skinning.skinned !== isSkinned) || (primitiveInfo.skinning.type !== type) || (primitiveInfo.hasVertexColors !== hasVertexColors)) {
829+
} else if ((primitiveInfo.skinning.skinned !== isSkinned) ||
830+
(primitiveInfo.skinning.type !== type) ||
831+
(primitiveInfo.hasVertexColors !== hasVertexColors) ||
832+
(primitiveInfo.hasMorphTargets !== hasMorphTargets) ||
833+
(primitiveInfo.hasTangents !== hasTangents)) {
817834
// This primitive uses the same material as another one that either:
818835
// * Isn't skinned
819836
// * Uses a different type to store joints and weights
820-
// * Doesn't have vertex colors
837+
// * Doesn't have vertex colors, tangents, or morph targets
821838
var clonedMaterial = clone(material, true);
822839
clonedMaterial.extras._pipeline.skinning = {
823840
skinning : {
824841
skinned : isSkinned,
825842
componentType : componentType,
826843
type : type
827844
},
828-
hasVertexColors : hasVertexColors
845+
hasVertexColors : hasVertexColors,
846+
hasMorphTargets : hasMorphTargets,
847+
hasTangents : hasTangents
829848
};
830849
// Split this off as a separate material
831850
materialId = addToArray(materials, clonedMaterial);

0 commit comments

Comments
 (0)