Browse Source

Merge pull request #14015 from takahirox/GLTFExporterMaterialExtras

GLTFExporter: Serialize material.userData
Mr.doob 7 years ago
parent
commit
41b6c87788
1 changed files with 30 additions and 9 deletions
  1. 30 9
      examples/js/exporters/GLTFExporter.js

+ 30 - 9
examples/js/exporters/GLTFExporter.js

@@ -325,6 +325,29 @@ THREE.GLTFExporter.prototype = {
 
 
 		}
 		}
 
 
+		/**
+		 * Serializes a userData.
+		 *
+		 * @param {THREE.Object3D|THREE.Material} object
+		 * @returns {Object}
+		 */
+		function serializeUserData( object ) {
+
+			try {
+
+				return JSON.parse( JSON.stringify( object.userData ) );
+
+			} catch ( error ) {
+
+				console.warn( 'THREE.GLTFExporter: userData of \'' + object.name + '\' ' +
+					'won\'t be serialized because of JSON.stringify error - ' + error.message );
+
+				return {};
+
+			}
+
+		}
+
 		/**
 		/**
 		 * Process a buffer to append to the default one.
 		 * Process a buffer to append to the default one.
 		 * @param  {ArrayBuffer} buffer
 		 * @param  {ArrayBuffer} buffer
@@ -936,6 +959,12 @@ THREE.GLTFExporter.prototype = {
 
 
 			}
 			}
 
 
+			if ( Object.keys( material.userData ).length > 0 ) {
+
+				gltfMaterial.extras = serializeUserData( material );
+
+			}
+
 			outputJSON.materials.push( gltfMaterial );
 			outputJSON.materials.push( gltfMaterial );
 
 
 			var index = outputJSON.materials.length - 1;
 			var index = outputJSON.materials.length - 1;
@@ -1512,15 +1541,7 @@ THREE.GLTFExporter.prototype = {
 
 
 			if ( object.userData && Object.keys( object.userData ).length > 0 ) {
 			if ( object.userData && Object.keys( object.userData ).length > 0 ) {
 
 
-				try {
-
-					gltfNode.extras = JSON.parse( JSON.stringify( object.userData ) );
-
-				} catch ( e ) {
-
-					throw new Error( 'THREE.GLTFExporter: userData can\'t be serialized' );
-
-				}
+				gltfNode.extras = serializeUserData( object );
 
 
 			}
 			}