@@ -49,10 +49,10 @@ define([
49
49
// Pre-processing to address incompatibilities between primitives using the same materials. Handles skinning and vertex color incompatibilities.
50
50
splitIncompatibleMaterials ( gltf ) ;
51
51
52
- ForEach . material ( gltf , function ( material ) {
52
+ ForEach . material ( gltf , function ( material , materialIndex ) {
53
53
var pbrMetallicRoughness = material . pbrMetallicRoughness ;
54
54
if ( defined ( pbrMetallicRoughness ) ) {
55
- var technique = generateTechnique ( gltf , material , options ) ;
55
+ var technique = generateTechnique ( gltf , material , materialIndex , options ) ;
56
56
57
57
material . values = pbrMetallicRoughness ;
58
58
material . technique = technique ;
@@ -68,7 +68,7 @@ define([
68
68
return gltf ;
69
69
}
70
70
71
- function generateTechnique ( gltf , material , options ) {
71
+ function generateTechnique ( gltf , material , materialIndex , options ) {
72
72
var optimizeForCesium = defaultValue ( options . optimizeForCesium , false ) ;
73
73
var hasCesiumRTCExtension = defined ( gltf . extensions ) && defined ( gltf . extensions . CESIUM_RTC ) ;
74
74
var addBatchIdToGeneratedShaders = defaultValue ( options . addBatchIdToGeneratedShaders , false ) ;
@@ -101,7 +101,7 @@ define([
101
101
102
102
if ( defined ( primitiveInfo ) ) {
103
103
skinningInfo = primitiveInfo . skinning ;
104
- hasSkinning = skinningInfo . skinned ;
104
+ hasSkinning = skinningInfo . skinned && ( joints . length > 0 ) ;
105
105
hasVertexColors = primitiveInfo . hasVertexColors ;
106
106
}
107
107
@@ -111,15 +111,17 @@ define([
111
111
var morphTargets ;
112
112
ForEach . mesh ( gltf , function ( mesh ) {
113
113
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
+ }
123
125
}
124
126
}
125
127
} ) ;
@@ -264,7 +266,7 @@ define([
264
266
vertexShaderMain += ' weightedPosition += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n' ;
265
267
} else if ( targetAttribute === 'NORMAL' ) {
266
268
vertexShaderMain += ' weightedNormal += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n' ;
267
- } else if ( targetAttribute === 'TANGENT' ) {
269
+ } else if ( hasTangents && targetAttribute === 'TANGENT' ) {
268
270
vertexShaderMain += ' weightedTangent.xyz += u_morphWeights[' + k + '] * a_' + attributeLower + ';\n' ;
269
271
}
270
272
}
@@ -802,6 +804,15 @@ define([
802
804
}
803
805
var isSkinned = defined ( jointAccessorId ) ;
804
806
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
+ }
805
816
806
817
var primitiveInfo = material . extras . _pipeline . primitive ;
807
818
if ( ! defined ( primitiveInfo ) ) {
@@ -811,21 +822,29 @@ define([
811
822
componentType : componentType ,
812
823
type : type
813
824
} ,
814
- hasVertexColors : hasVertexColors
825
+ hasVertexColors : hasVertexColors ,
826
+ hasMorphTargets : hasMorphTargets ,
827
+ hasTangents : hasTangents
815
828
} ;
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 ) ) {
817
834
// This primitive uses the same material as another one that either:
818
835
// * Isn't skinned
819
836
// * 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
821
838
var clonedMaterial = clone ( material , true ) ;
822
839
clonedMaterial . extras . _pipeline . skinning = {
823
840
skinning : {
824
841
skinned : isSkinned ,
825
842
componentType : componentType ,
826
843
type : type
827
844
} ,
828
- hasVertexColors : hasVertexColors
845
+ hasVertexColors : hasVertexColors ,
846
+ hasMorphTargets : hasMorphTargets ,
847
+ hasTangents : hasTangents
829
848
} ;
830
849
// Split this off as a separate material
831
850
materialId = addToArray ( materials , clonedMaterial ) ;
0 commit comments