Skip to content

Commit

Permalink
Merge pull request #13566 from donmccurdy/feat-gltfexporter-unlit
Browse files Browse the repository at this point in the history
GLTFExporter: Support KHR_materials_unlit.
  • Loading branch information
mrdoob committed Mar 13, 2018
2 parents b88a2bf + 73c9056 commit 6d5e6fc
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions examples/js/exporters/GLTFExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ THREE.GLTFExporter.prototype = {
var pending = [];
var nodeMap = {};
var skins = [];
var extensionsUsed = {};
var cachedData = {

materials: new Map(),
Expand Down Expand Up @@ -640,27 +641,32 @@ THREE.GLTFExporter.prototype = {

}

if ( material instanceof THREE.ShaderMaterial ) {
if ( material.isShaderMaterial ) {

console.warn( 'GLTFExporter: THREE.ShaderMaterial not supported.' );
return null;

}


if ( ! ( material instanceof THREE.MeshStandardMaterial ) ) {

console.warn( 'GLTFExporter: Currently just THREE.MeshStandardMaterial is supported. Material conversion may lose information.' );

}

// @QUESTION Should we avoid including any attribute that has the default value?
var gltfMaterial = {

pbrMetallicRoughness: {}

};

if ( material.isMeshBasicMaterial ) {

gltfMaterial.extensions = { KHR_materials_unlit: {} };

extensionsUsed[ 'KHR_materials_unlit' ] = true;

} else if ( ! material.isMeshStandardMaterial ) {

console.warn( 'GLTFExporter: Use MeshStandardMaterial or MeshBasicMaterial for best results.' );

}

// pbrMetallicRoughness.baseColorFactor
var color = material.color.toArray().concat( [ material.opacity ] );

Expand All @@ -670,11 +676,16 @@ THREE.GLTFExporter.prototype = {

}

if ( material instanceof THREE.MeshStandardMaterial ) {
if ( material.isMeshStandardMaterial ) {

gltfMaterial.pbrMetallicRoughness.metallicFactor = material.metalness;
gltfMaterial.pbrMetallicRoughness.roughnessFactor = material.roughness;

} else if ( material.isMeshBasicMaterial ) {

gltfMaterial.pbrMetallicRoughness.metallicFactor = 0.0;
gltfMaterial.pbrMetallicRoughness.roughnessFactor = 0.9;

} else {

gltfMaterial.pbrMetallicRoughness.metallicFactor = 0.5;
Expand Down Expand Up @@ -712,9 +723,9 @@ THREE.GLTFExporter.prototype = {

}

if ( material instanceof THREE.MeshBasicMaterial ||
material instanceof THREE.LineBasicMaterial ||
material instanceof THREE.PointsMaterial ) {
if ( material.isMeshBasicMaterial ||
material.isLineBasicMaterial ||
material.isPointsMaterial ) {

} else {

Expand Down Expand Up @@ -834,19 +845,19 @@ THREE.GLTFExporter.prototype = {
var mode;

// Use the correct mode
if ( mesh instanceof THREE.LineSegments ) {
if ( mesh.isLineSegments ) {

mode = WEBGL_CONSTANTS.LINES;

} else if ( mesh instanceof THREE.LineLoop ) {
} else if ( mesh.isLineLoop ) {

mode = WEBGL_CONSTANTS.LINE_LOOP;

} else if ( mesh instanceof THREE.Line ) {
} else if ( mesh.isLine ) {

mode = WEBGL_CONSTANTS.LINE_STRIP;

} else if ( mesh instanceof THREE.Points ) {
} else if ( mesh.isPoints ) {

mode = WEBGL_CONSTANTS.POINTS;

Expand Down Expand Up @@ -1047,7 +1058,7 @@ THREE.GLTFExporter.prototype = {

}

var isOrtho = camera instanceof THREE.OrthographicCamera;
var isOrtho = camera.isOrthographicCamera;

var gltfCamera = {

Expand Down Expand Up @@ -1254,7 +1265,7 @@ THREE.GLTFExporter.prototype = {
*/
function processNode( object ) {

if ( object instanceof THREE.Light ) {
if ( object.isLight ) {

console.warn( 'GLTFExporter: Unsupported node type:', object.constructor.name );
return null;
Expand Down Expand Up @@ -1325,19 +1336,17 @@ THREE.GLTFExporter.prototype = {

}

if ( object instanceof THREE.Mesh ||
object instanceof THREE.Line ||
object instanceof THREE.Points ) {
if ( object.isMesh || object.isLine || object.isPoints ) {

gltfNode.mesh = processMesh( object );

} else if ( object instanceof THREE.Camera ) {
} else if ( object.isCamera ) {

gltfNode.camera = processCamera( object );

}

if ( object instanceof THREE.SkinnedMesh ) {
if ( object.isSkinnedMesh ) {

skins.push( object );

Expand Down Expand Up @@ -1505,6 +1514,10 @@ THREE.GLTFExporter.prototype = {
// Merge buffers.
var blob = new Blob( buffers, { type: 'application/octet-stream' } );

// Declare extensions.
var extensionsUsedList = Object.keys( extensionsUsed );
if ( extensionsUsedList.length > 0 ) outputJSON.extensionsUsed = extensionsUsedList;

if ( outputJSON.buffers && outputJSON.buffers.length > 0 ) {

// Update bytelength of the single buffer.
Expand Down

0 comments on commit 6d5e6fc

Please sign in to comment.