Browse Source

Export userData as extra attribute

Fernando Serrano 8 years ago
parent
commit
767ba0e720
2 changed files with 30 additions and 17 deletions
  1. 16 16
      examples/gltf_exporter.html
  2. 14 1
      examples/js/exporters/GLTFExporter.js

+ 16 - 16
examples/gltf_exporter.html

@@ -71,11 +71,11 @@
 
 				var light, object;
 
-				scene1.add( new THREE.AmbientLight( 0xffffff ) );
+				// scene1.add( new THREE.AmbientLight( 0xffffff ) );
 
 				light = new THREE.DirectionalLight( 0xffffff );
 				light.position.set( 0, 1, 0 );
-				scene1.add( light );
+				// scene1.add( light );
 
 				var map = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
 				map.wrapS = map.wrapT = THREE.RepeatWrapping;
@@ -84,7 +84,6 @@
 				// var material = new THREE.MeshLambertMaterial( { map: map, side: THREE.DoubleSide } );
 
 				//
-/*
 
 				object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 1 ), material );
 				object.position.set( -200, 0, 200 );
@@ -92,18 +91,18 @@
 
 				object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 2 ), material );
 				object.position.set( 0, 0, 200 );
-				scene1.add( object );
+				// scene1.add( object );
 
 				object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
 				object.position.set( 200, 0, 200 );
-				scene1.add( object );
+				// scene1.add( object );
 
 				//
 
 				object = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100, 4, 4 ), material );
 				object.position.set( -400, 0, 0 );
-				scene1.add( object );
-*/
+				// scene1.add( object );
+
 
 				var map2 = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
 
@@ -116,7 +115,7 @@
 				object = new THREE.Mesh( new THREE.SphereBufferGeometry( 100, 32, 32 ), material );
 				object.position.set( 0, 0, 0 );
 				object.name = "Sphere";
-				scene1.add(object);
+				// scene1.add(object);
 /*
 				var material = new THREE.MeshStandardMaterial( {
 					color: 0xff0000,
@@ -132,14 +131,14 @@
 				object3 = new THREE.Mesh( new THREE.CylinderBufferGeometry( 100, 100, 100 ), material );
 				object3.position.set( 200, 0, 0 );
 				object3.name = "Cube";
-				scene1.add(object3);
+				// scene1.add(object3);
 */
 
 /*
 				object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
 				object.position.set( -200, 0, 0 );
 				object.name = "Cube";
-				scene1.add( object );
+				// scene1.add( object );
 
 				object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 10, 80, 2, 2, 2 ), material );
 				object2.position.set( 0, 0, 50 );
@@ -150,7 +149,7 @@
 
 				group1 = new THREE.Group();
 				group1.name = "Group";
-				// scene1.add( group1 );
+				// // scene1.add( group1 );
 
 				group2 = new THREE.Group();
 				group2.name = "subGroup";
@@ -196,9 +195,10 @@
 				object = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial( { color: 0xff00ff, side: THREE.DoubleSide } ) );
 				object.position.x = 150;
 				object.setDrawMode( THREE.TriangleStripDrawMode );
-				console.log(object);
-				console.log('>>>>', JSON.stringify(object));
-				scene1.add( object );
+
+				object.userData = { data: 'customdata', list: [ 1,2,3,4 ] };
+
+				// scene1.add( object );
 
 
 				// ---------------------------------
@@ -218,7 +218,7 @@
 				object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
 				object.position.x = -150;
 
-				scene1.add( object );
+				// scene1.add( object );
 
 
 				// ---------------------------------
@@ -240,7 +240,7 @@
 				object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
 				object.position.x = -100;
 
-				scene1.add( object );
+				// scene1.add( object );
 
 
 				// ---------------------------------

+ 14 - 1
examples/js/exporters/GLTFExporter.js

@@ -430,7 +430,6 @@ THREE.GLTFExporter.prototype = {
 				mode = gl.POINTS;
 
 			} else {
-				console.log(mesh.drawMode, THREE.TrianglesDrawMode, THREE.TriangleStripDrawMode, THREE.TriangleFanDrawMode,gl.TRIANGLE_STRIP);
 
 				// @QUESTION Set mode = gl.LINES if material.wireframe = true ?
 				if ( mesh.drawMode === THREE.TriangleFanDrawMode ) {
@@ -581,6 +580,20 @@ THREE.GLTFExporter.prototype = {
 				gltfNode.name = object.name;
 			}
 
+			if ( object.userData && Object.keys( object.userData ).length > 0 ) {
+
+				try {
+
+					gltfNode.extras = JSON.parse( JSON.stringify( object.userData ) );
+
+				} catch (e) {
+
+					throw new Error( 'GLTFExporter: userData can\'t be serialized' );
+
+				}
+
+			}
+
 			if ( object instanceof THREE.Mesh
 				|| object instanceof THREE.Line
 				|| object instanceof THREE.Points ) {