2
0
Эх сурвалжийг харах

GLTFExporter: Serialize material.userData

Takahiro 7 жил өмнө
parent
commit
cb9dc432ac

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

@@ -325,6 +325,26 @@ THREE.GLTFExporter.prototype = {
 
 		}
 
+		/**
+		 * Serializes a userData.
+		 *
+		 * @param {Object} userData
+		 * @returns {Object}
+		 */
+		function serializeUserData( userData ) {
+
+			try {
+
+				return JSON.parse( JSON.stringify( userData ) );
+
+			} catch {
+
+				throw new Error( 'THREE.GLTFExporter: userData can\'t be serialized' );
+
+			}
+
+		}
+
 		/**
 		 * Process a buffer to append to the default one.
 		 * @param  {ArrayBuffer} buffer
@@ -936,6 +956,12 @@ THREE.GLTFExporter.prototype = {
 
 			}
 
+			if ( Object.keys( material.userData ).length > 0 ) {
+
+				gltfMaterial.extras = serializeUserData( material.userData );
+
+			}
+
 			outputJSON.materials.push( gltfMaterial );
 
 			var index = outputJSON.materials.length - 1;
@@ -1512,15 +1538,7 @@ THREE.GLTFExporter.prototype = {
 
 			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.userData );
 
 			}