Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTFLoader KHR_materials_unlit extension support. #13136

Merged
merged 5 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 69 additions & 23 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,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 @@ -202,7 +208,8 @@ THREE.GLTFLoader = ( function () {
var EXTENSIONS = {
KHR_BINARY_GLTF: 'KHR_binary_glTF',
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 @@ -289,6 +296,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 @@ -1715,6 +1771,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 @@ -1783,7 +1845,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 @@ -1797,7 +1859,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 @@ -1809,31 +1871,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