Skip to content

Commit 41b6c87

Browse files
authored
Merge pull request #14015 from takahirox/GLTFExporterMaterialExtras
GLTFExporter: Serialize material.userData
2 parents d003192 + affb727 commit 41b6c87

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

examples/js/exporters/GLTFExporter.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,29 @@ THREE.GLTFExporter.prototype = {
325325

326326
}
327327

328+
/**
329+
* Serializes a userData.
330+
*
331+
* @param {THREE.Object3D|THREE.Material} object
332+
* @returns {Object}
333+
*/
334+
function serializeUserData( object ) {
335+
336+
try {
337+
338+
return JSON.parse( JSON.stringify( object.userData ) );
339+
340+
} catch ( error ) {
341+
342+
console.warn( 'THREE.GLTFExporter: userData of \'' + object.name + '\' ' +
343+
'won\'t be serialized because of JSON.stringify error - ' + error.message );
344+
345+
return {};
346+
347+
}
348+
349+
}
350+
328351
/**
329352
* Process a buffer to append to the default one.
330353
* @param {ArrayBuffer} buffer
@@ -936,6 +959,12 @@ THREE.GLTFExporter.prototype = {
936959

937960
}
938961

962+
if ( Object.keys( material.userData ).length > 0 ) {
963+
964+
gltfMaterial.extras = serializeUserData( material );
965+
966+
}
967+
939968
outputJSON.materials.push( gltfMaterial );
940969

941970
var index = outputJSON.materials.length - 1;
@@ -1512,15 +1541,7 @@ THREE.GLTFExporter.prototype = {
15121541

15131542
if ( object.userData && Object.keys( object.userData ).length > 0 ) {
15141543

1515-
try {
1516-
1517-
gltfNode.extras = JSON.parse( JSON.stringify( object.userData ) );
1518-
1519-
} catch ( e ) {
1520-
1521-
throw new Error( 'THREE.GLTFExporter: userData can\'t be serialized' );
1522-
1523-
}
1544+
gltfNode.extras = serializeUserData( object );
15241545

15251546
}
15261547

0 commit comments

Comments
 (0)