Skip to content

Commit

Permalink
Merge pull request #13136 from robertlong/feature/gltf-KHR_materials_…
Browse files Browse the repository at this point in the history
…unlit

GLTFLoader KHR_materials_unlit extension support.
  • Loading branch information
mrdoob authored Feb 21, 2018
2 parents 91146ab + eb9b990 commit fc102a3
Show file tree
Hide file tree
Showing 6 changed files with 2,348 additions and 24 deletions.
92 changes: 69 additions & 23 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ THREE.GLTFLoader = ( function () {

}

if ( json.extensionsUsed.indexOf( EXTENSIONS.KHR_MATERIALS_UNLIT ) >= 0 ) {

extensions[ EXTENSIONS.KHR_MATERIALS_UNLIT ] = new GLTFMaterialsUnlitExtension( json );

}

if ( json.extensionsUsed.indexOf( EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ) >= 0 ) {

extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] = new GLTFMaterialsPbrSpecularGlossinessExtension();
Expand Down Expand Up @@ -216,7 +222,8 @@ THREE.GLTFLoader = ( function () {
KHR_BINARY_GLTF: 'KHR_binary_glTF',
KHR_DRACO_MESH_COMPRESSION: 'KHR_draco_mesh_compression',
KHR_LIGHTS: 'KHR_lights',
KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness'
KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness',
KHR_MATERIALS_UNLIT: 'KHR_materials_unlit'
};

/**
Expand Down Expand Up @@ -303,6 +310,55 @@ THREE.GLTFLoader = ( function () {

}

/**
* Unlit Materials Extension (pending)
*
* PR: https://github.com/KhronosGroup/glTF/pull/1163
*/
function GLTFMaterialsUnlitExtension( json ) {

this.name = EXTENSIONS.KHR_MATERIALS_UNLIT;

}

GLTFMaterialsUnlitExtension.prototype.getMaterialType = function ( material ) {

return THREE.MeshBasicMaterial;

};

GLTFMaterialsUnlitExtension.prototype.extendParams = function ( materialParams, material, parser ) {

var pending = [];

materialParams.color = new THREE.Color( 1.0, 1.0, 1.0 );
materialParams.opacity = 1.0;

var metallicRoughness = material.pbrMetallicRoughness;

if ( metallicRoughness ) {

if ( Array.isArray( metallicRoughness.baseColorFactor ) ) {

var array = metallicRoughness.baseColorFactor;

materialParams.color.fromArray( array );
materialParams.opacity = array[ 3 ];

}

if ( metallicRoughness.baseColorTexture !== undefined ) {

pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture.index ) );

}

}

return Promise.all( pending );

};

/* BINARY EXTENSION */

var BINARY_EXTENSION_BUFFER_NAME = 'binary_glTF';
Expand Down Expand Up @@ -1808,6 +1864,12 @@ THREE.GLTFLoader = ( function () {
materialType = sgExtension.getMaterialType( materialDef );
pending.push( sgExtension.extendParams( materialParams, materialDef, parser ) );

} else if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_UNLIT ] ) {

var kmuExtension = extensions[ EXTENSIONS.KHR_MATERIALS_UNLIT ];
materialType = kmuExtension.getMaterialType( materialDef );
pending.push( kmuExtension.extendParams( materialParams, materialDef, parser ) );

} else if ( materialDef.pbrMetallicRoughness !== undefined ) {

// Specification:
Expand Down Expand Up @@ -1876,7 +1938,7 @@ THREE.GLTFLoader = ( function () {

}

if ( materialDef.normalTexture !== undefined ) {
if ( materialDef.normalTexture !== undefined && materialType !== THREE.MeshBasicMaterial) {

pending.push( parser.assignTexture( materialParams, 'normalMap', materialDef.normalTexture.index ) );

Expand All @@ -1890,7 +1952,7 @@ THREE.GLTFLoader = ( function () {

}

if ( materialDef.occlusionTexture !== undefined ) {
if ( materialDef.occlusionTexture !== undefined && materialType !== THREE.MeshBasicMaterial) {

pending.push( parser.assignTexture( materialParams, 'aoMap', materialDef.occlusionTexture.index ) );

Expand All @@ -1902,31 +1964,15 @@ THREE.GLTFLoader = ( function () {

}

if ( materialDef.emissiveFactor !== undefined ) {

if ( materialType === THREE.MeshBasicMaterial ) {
if ( materialDef.emissiveFactor !== undefined && materialType !== THREE.MeshBasicMaterial) {

materialParams.color = new THREE.Color().fromArray( materialDef.emissiveFactor );

} else {

materialParams.emissive = new THREE.Color().fromArray( materialDef.emissiveFactor );

}
materialParams.emissive = new THREE.Color().fromArray( materialDef.emissiveFactor );

}

if ( materialDef.emissiveTexture !== undefined ) {
if ( materialDef.emissiveTexture !== undefined && materialType !== THREE.MeshBasicMaterial) {

if ( materialType === THREE.MeshBasicMaterial ) {

pending.push( parser.assignTexture( materialParams, 'map', materialDef.emissiveTexture.index ) );

} else {

pending.push( parser.assignTexture( materialParams, 'emissiveMap', materialDef.emissiveTexture.index ) );

}
pending.push( parser.assignTexture( materialParams, 'emissiveMap', materialDef.emissiveTexture.index ) );

}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fc102a3

Please sign in to comment.