diff --git a/src/materials/Material.js b/src/materials/Material.js index 876d21fd21d8cb71976e5b9d5b2000d5e9a13aa3..04d0ae18ca70770a85d5177700c02f8fbd3e064d 100644 --- a/src/materials/Material.js +++ b/src/materials/Material.js @@ -115,6 +115,17 @@ THREE.Material.prototype = { toJSON: function ( meta ) { + var isRoot = meta === undefined; + + if ( isRoot ) { + + meta = { + textures: {}, + images: {} + }; + + } + var data = { metadata: { version: 4.4, @@ -176,6 +187,28 @@ THREE.Material.prototype = { if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest; if ( this.wireframe === true ) data.wireframe = this.wireframe; if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth; + + // TODO: Copied from Object3D.toJSON + + function extractFromCache ( cache ) { + var values = []; + for ( var key in cache ) { + var data = cache[ key ]; + delete data.metadata; + values.push( data ); + } + return values; + } + + if ( isRoot ) { + + var textures = extractFromCache( meta.textures ); + var images = extractFromCache( meta.images ); + + if ( textures.length > 0 ) data.textures = textures; + if ( images.length > 0 ) data.images = images; + + } return data;