提交 3adf4783 编写于 作者: J Jonne Nauha 提交者: Mr.doob

OBJLoader: When a new o/g starts, inherit the currently parsed material. (#9013)

Inherited material is overwritten if 'usemtl' declaration is encountered after the object starts but prior to next object start.
Exception to this is when faces have been declared to the inherited material, to keep MultiMaterial in sync.

Fixes #8792 and #8871.
上级 df810c13
...@@ -95,6 +95,8 @@ THREE.OBJLoader.prototype = { ...@@ -95,6 +95,8 @@ THREE.OBJLoader.prototype = {
} }
var previousMaterial = ( this.object && typeof this.object.currentMaterial === 'function' ? this.object.currentMaterial() : undefined );
this.object = { this.object = {
name : name || '', name : name || '',
fromDeclaration : ( fromDeclaration !== false ), fromDeclaration : ( fromDeclaration !== false ),
...@@ -111,6 +113,14 @@ THREE.OBJLoader.prototype = { ...@@ -111,6 +113,14 @@ THREE.OBJLoader.prototype = {
var previous = this._finalize( false ); var previous = this._finalize( false );
// New usemtl declaration overwrites an inherited material, except if faces were declared
// after the material, then it must be preserved for proper MultiMaterial continuation.
if ( previous && ( previous.inherited || previous.groupCount <= 0 ) ) {
this.materials.splice( previous.index, 1 );
}
var material = { var material = {
index : this.materials.length, index : this.materials.length,
name : name || '', name : name || '',
...@@ -118,7 +128,21 @@ THREE.OBJLoader.prototype = { ...@@ -118,7 +128,21 @@ THREE.OBJLoader.prototype = {
smooth : ( previous !== undefined ? previous.smooth : this.smooth ), smooth : ( previous !== undefined ? previous.smooth : this.smooth ),
groupStart : ( previous !== undefined ? previous.groupEnd : 0 ), groupStart : ( previous !== undefined ? previous.groupEnd : 0 ),
groupEnd : -1, groupEnd : -1,
groupCount : -1 groupCount : -1,
inherited : false,
clone : function( index ) {
return {
index : ( typeof index === 'number' ? index : this.index ),
name : this.name,
mtllib : this.mtllib,
smooth : this.smooth,
groupStart : this.groupEnd,
groupEnd : -1,
groupCount : -1,
inherited : false
};
}
}; };
this.materials.push( material ); this.materials.push( material );
...@@ -144,6 +168,7 @@ THREE.OBJLoader.prototype = { ...@@ -144,6 +168,7 @@ THREE.OBJLoader.prototype = {
lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3; lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart; lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
lastMultiMaterial.inherited = false;
} }
...@@ -160,6 +185,20 @@ THREE.OBJLoader.prototype = { ...@@ -160,6 +185,20 @@ THREE.OBJLoader.prototype = {
} }
}; };
// Inherit previous objects material.
// Spec tells us that a declared material must be set to all objects until a new material is declared.
// If a usemtl declaration is encountered while this new object is being parsed, it will
// overwrite the inherited material. Exception being that there was already face declarations
// to the inherited material, then it will be preserved for proper MultiMaterial continuation.
if ( previousMaterial && previousMaterial.name && typeof previousMaterial.clone === "function" ) {
var declared = previousMaterial.clone( 0 );
declared.inherited = true;
this.object.materials.push( declared );
}
this.objects.push( this.object ); this.objects.push( this.object );
}, },
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册