-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GLTFLoader: Export KHR_materials_variants extension handler from the …
…loader
- Loading branch information
Showing
4 changed files
with
111 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
examples/jsm/loaders/gltf_plugins/KHR_materials_variants.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Materials variants extension | ||
* | ||
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_variants | ||
*/ | ||
export default function GLTFMaterialsVariantsExtension( parser ) { | ||
|
||
this.parser = parser; | ||
this.name = 'KHR_materials_variants'; | ||
this.addExtensionsToUserData = true; | ||
|
||
} | ||
|
||
GLTFMaterialsVariantsExtension.prototype.afterRoot = function ( gltf ) { | ||
|
||
var parser = this.parser; | ||
var json = parser.json; | ||
var name = this.name; | ||
|
||
if ( ! json.extensions || ! json.extensions[ name ] ) return null; | ||
|
||
var extensionDef = json.extensions[ name ]; | ||
var variantsDef = extensionDef.variants || []; | ||
var variants = []; | ||
|
||
for ( var i = 0, il = variantsDef.length; i < il; i ++ ) { | ||
|
||
variants.push( variantsDef[ i ].name ); | ||
|
||
} | ||
|
||
for ( var i = 0, il = gltf.scenes.length; i < il; i ++ ) { | ||
|
||
gltf.scenes[ i ].userData.variants = variants; | ||
|
||
} | ||
|
||
gltf.userData.variants = variants; | ||
|
||
gltf.userData.selectVariant = function selectVariant ( variantName ) { | ||
|
||
var scenes = gltf.scenes; | ||
var pending = []; | ||
|
||
for ( var i = 0, il = scenes.length; i < il; i ++ ) { | ||
|
||
var scene = scenes[ i ]; | ||
|
||
var variantIndex = variants.indexOf( variantName ); | ||
|
||
if ( variantIndex === - 1 ) continue; | ||
|
||
scene.traverse( function ( object ) { | ||
|
||
if ( ! object.isMesh || ! object.userData.gltfExtensions || | ||
! object.userData.gltfExtensions[ name ] ) return; | ||
|
||
var meshVariantDef = object.userData.gltfExtensions[ name ]; | ||
var mappings = meshVariantDef.mappings || []; | ||
var materialIndex = - 1; | ||
|
||
for ( var j = 0, jl = mappings.length; j < jl; j ++ ) { | ||
|
||
var mapping = mappings[ j ]; | ||
|
||
if ( mapping.variants.indexOf( variantIndex ) !== - 1 ) { | ||
|
||
materialIndex = mapping.material; | ||
break; | ||
|
||
} | ||
|
||
} | ||
|
||
if ( materialIndex >= 0 ) { | ||
|
||
if ( ! object.userData.originalMaterial ) { | ||
|
||
object.userData.originalMaterial = object.material; | ||
|
||
} | ||
|
||
pending.push( parser.getDependency( 'material', materialIndex ).then( function ( material ) { | ||
|
||
object.material = material; | ||
|
||
} ) ); | ||
|
||
} else if ( object.userData.originalMaterial ) { | ||
|
||
object.material = object.userData.originalMaterial; | ||
|
||
} | ||
|
||
} ); | ||
|
||
} | ||
|
||
return Promise.all( pending ); | ||
|
||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters