@@ -95,6 +95,8 @@ THREE.OBJLoader.prototype = {
95
95
96
96
}
97
97
98
+ var previousMaterial = ( this . object && typeof this . object . currentMaterial === 'function' ? this . object . currentMaterial ( ) : undefined ) ;
99
+
98
100
this . object = {
99
101
name : name || '' ,
100
102
fromDeclaration : ( fromDeclaration !== false ) ,
@@ -111,14 +113,36 @@ THREE.OBJLoader.prototype = {
111
113
112
114
var previous = this . _finalize ( false ) ;
113
115
116
+ // New usemtl declaration overwrites an inherited material, except if faces were declared
117
+ // after the material, then it must be preserved for proper MultiMaterial continuation.
118
+ if ( previous && ( previous . inherited || previous . groupCount <= 0 ) ) {
119
+
120
+ this . materials . splice ( previous . index , 1 ) ;
121
+
122
+ }
123
+
114
124
var material = {
115
125
index : this . materials . length ,
116
126
name : name || '' ,
117
127
mtllib : ( Array . isArray ( libraries ) && libraries . length > 0 ? libraries [ libraries . length - 1 ] : '' ) ,
118
128
smooth : ( previous !== undefined ? previous . smooth : this . smooth ) ,
119
129
groupStart : ( previous !== undefined ? previous . groupEnd : 0 ) ,
120
130
groupEnd : - 1 ,
121
- groupCount : - 1
131
+ groupCount : - 1 ,
132
+ inherited : false ,
133
+
134
+ clone : function ( index ) {
135
+ return {
136
+ index : ( typeof index === 'number' ? index : this . index ) ,
137
+ name : this . name ,
138
+ mtllib : this . mtllib ,
139
+ smooth : this . smooth ,
140
+ groupStart : this . groupEnd ,
141
+ groupEnd : - 1 ,
142
+ groupCount : - 1 ,
143
+ inherited : false
144
+ } ;
145
+ }
122
146
} ;
123
147
124
148
this . materials . push ( material ) ;
@@ -144,6 +168,7 @@ THREE.OBJLoader.prototype = {
144
168
145
169
lastMultiMaterial . groupEnd = this . geometry . vertices . length / 3 ;
146
170
lastMultiMaterial . groupCount = lastMultiMaterial . groupEnd - lastMultiMaterial . groupStart ;
171
+ lastMultiMaterial . inherited = false ;
147
172
148
173
}
149
174
@@ -160,6 +185,20 @@ THREE.OBJLoader.prototype = {
160
185
}
161
186
} ;
162
187
188
+ // Inherit previous objects material.
189
+ // Spec tells us that a declared material must be set to all objects until a new material is declared.
190
+ // If a usemtl declaration is encountered while this new object is being parsed, it will
191
+ // overwrite the inherited material. Exception being that there was already face declarations
192
+ // to the inherited material, then it will be preserved for proper MultiMaterial continuation.
193
+
194
+ if ( previousMaterial && previousMaterial . name && typeof previousMaterial . clone === "function" ) {
195
+
196
+ var declared = previousMaterial . clone ( 0 ) ;
197
+ declared . inherited = true ;
198
+ this . object . materials . push ( declared ) ;
199
+
200
+ }
201
+
163
202
this . objects . push ( this . object ) ;
164
203
165
204
} ,
0 commit comments