|
@@ -567,165 +567,89 @@ THREE.Object3D.prototype = {
|
|
|
|
|
|
},
|
|
|
|
|
|
- toJSON: function () {
|
|
|
+ toJSON: function( meta ) {
|
|
|
|
|
|
- var output = {
|
|
|
- metadata: {
|
|
|
- version: 4.3,
|
|
|
- type: 'Object',
|
|
|
- generator: 'ObjectExporter'
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- //
|
|
|
-
|
|
|
- var geometries = {};
|
|
|
-
|
|
|
- var parseGeometry = function ( geometry ) {
|
|
|
-
|
|
|
- if ( output.geometries === undefined ) {
|
|
|
-
|
|
|
- output.geometries = [];
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if ( geometries[ geometry.uuid ] === undefined ) {
|
|
|
-
|
|
|
- var json = geometry.toJSON();
|
|
|
-
|
|
|
- delete json.metadata;
|
|
|
-
|
|
|
- geometries[ geometry.uuid ] = json;
|
|
|
-
|
|
|
- output.geometries.push( json );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return geometry.uuid;
|
|
|
-
|
|
|
- };
|
|
|
-
|
|
|
- //
|
|
|
-
|
|
|
- var materials = {};
|
|
|
-
|
|
|
- var parseMaterial = function ( material ) {
|
|
|
-
|
|
|
- if ( output.materials === undefined ) {
|
|
|
-
|
|
|
- output.materials = [];
|
|
|
-
|
|
|
- }
|
|
|
+ var isRootObject = ( meta === undefined );
|
|
|
|
|
|
- if ( materials[ material.uuid ] === undefined ) {
|
|
|
+ // we will store all serialization data on 'data'
|
|
|
+ var data = {};
|
|
|
+ var metadata;
|
|
|
|
|
|
- var json = material.toJSON();
|
|
|
+ // meta is a hash used to collect geometries, materials.
|
|
|
+ // not providing it implies that this is the root object
|
|
|
+ // being serialized.
|
|
|
+ if ( isRootObject ) {
|
|
|
|
|
|
- delete json.metadata;
|
|
|
-
|
|
|
- materials[ material.uuid ] = json;
|
|
|
-
|
|
|
- output.materials.push( json );
|
|
|
+ // initialize meta obj
|
|
|
+ meta = {
|
|
|
+ geometries: {},
|
|
|
+ materials: {}
|
|
|
+ }
|
|
|
|
|
|
+ // add metadata
|
|
|
+ metadata = {
|
|
|
+ version: 4.4,
|
|
|
+ type: 'Object',
|
|
|
+ generator: 'Object3D.toJSON'
|
|
|
}
|
|
|
|
|
|
- return material.uuid;
|
|
|
-
|
|
|
- };
|
|
|
-
|
|
|
- //
|
|
|
-
|
|
|
- var parseObject = function ( object ) {
|
|
|
-
|
|
|
- var data = {};
|
|
|
-
|
|
|
- data.uuid = object.uuid;
|
|
|
- data.type = object.type;
|
|
|
-
|
|
|
- if ( object.name !== '' ) data.name = object.name;
|
|
|
- if ( JSON.stringify( object.userData ) !== '{}' ) data.userData = object.userData;
|
|
|
- if ( object.visible !== true ) data.visible = object.visible;
|
|
|
-
|
|
|
- if ( object instanceof THREE.PerspectiveCamera ) {
|
|
|
-
|
|
|
- data.fov = object.fov;
|
|
|
- data.aspect = object.aspect;
|
|
|
- data.near = object.near;
|
|
|
- data.far = object.far;
|
|
|
-
|
|
|
- } else if ( object instanceof THREE.OrthographicCamera ) {
|
|
|
+ }
|
|
|
|
|
|
- data.left = object.left;
|
|
|
- data.right = object.right;
|
|
|
- data.top = object.top;
|
|
|
- data.bottom = object.bottom;
|
|
|
- data.near = object.near;
|
|
|
- data.far = object.far;
|
|
|
+ // standard Object3D serialization
|
|
|
|
|
|
- } else if ( object instanceof THREE.AmbientLight ) {
|
|
|
+ data.type = this.type;
|
|
|
+ data.uuid = this.uuid;
|
|
|
+ if ( this.name !== '' ) data.name = this.name;
|
|
|
+ if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
|
|
|
+ if ( this.visible !== true ) data.visible = this.visible;
|
|
|
|
|
|
- data.color = object.color.getHex();
|
|
|
+ data.matrix = this.matrix.toArray();
|
|
|
|
|
|
- } else if ( object instanceof THREE.DirectionalLight ) {
|
|
|
+ if ( this.children.length > 0 ) {
|
|
|
|
|
|
- data.color = object.color.getHex();
|
|
|
- data.intensity = object.intensity;
|
|
|
+ data.children = [];
|
|
|
|
|
|
- } else if ( object instanceof THREE.PointLight ) {
|
|
|
+ for ( var i = 0; i < this.children.length; i ++ ) {
|
|
|
|
|
|
- data.color = object.color.getHex();
|
|
|
- data.intensity = object.intensity;
|
|
|
- data.distance = object.distance;
|
|
|
- data.decay = object.decay;
|
|
|
+ data.children.push( this.children[ i ].toJSON( meta ).object );
|
|
|
|
|
|
- } else if ( object instanceof THREE.SpotLight ) {
|
|
|
+ }
|
|
|
|
|
|
- data.color = object.color.getHex();
|
|
|
- data.intensity = object.intensity;
|
|
|
- data.distance = object.distance;
|
|
|
- data.angle = object.angle;
|
|
|
- data.exponent = object.exponent;
|
|
|
- data.decay = object.decay;
|
|
|
+ }
|
|
|
|
|
|
- } else if ( object instanceof THREE.HemisphereLight ) {
|
|
|
+ // wrap serialized object with additional data
|
|
|
|
|
|
- data.color = object.color.getHex();
|
|
|
- data.groundColor = object.groundColor.getHex();
|
|
|
+ var output;
|
|
|
|
|
|
- } else if ( object instanceof THREE.Mesh || object instanceof THREE.Line || object instanceof THREE.PointCloud ) {
|
|
|
+ if ( isRootObject ) {
|
|
|
|
|
|
- data.geometry = parseGeometry( object.geometry );
|
|
|
- data.material = parseMaterial( object.material );
|
|
|
+ output = {
|
|
|
+ metadata: metadata,
|
|
|
+ geometries: extractFromCache(meta.geometries),
|
|
|
+ materials: extractFromCache(meta.materials),
|
|
|
+ object: data
|
|
|
+ };
|
|
|
|
|
|
- if ( object instanceof THREE.Line ) data.mode = object.mode;
|
|
|
+ } else {
|
|
|
|
|
|
- } else if ( object instanceof THREE.Sprite ) {
|
|
|
+ output = { object: data };
|
|
|
|
|
|
- data.material = parseMaterial( object.material );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- data.matrix = object.matrix.toArray();
|
|
|
-
|
|
|
- if ( object.children.length > 0 ) {
|
|
|
-
|
|
|
- data.children = [];
|
|
|
-
|
|
|
- for ( var i = 0; i < object.children.length; i ++ ) {
|
|
|
-
|
|
|
- data.children.push( parseObject( object.children[ i ] ) );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return data;
|
|
|
-
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- output.object = parseObject( this );
|
|
|
+ return output;
|
|
|
|
|
|
- return output;
|
|
|
+ // extract data from the cache hash
|
|
|
+ // remove metadata on each item
|
|
|
+ // and return as array
|
|
|
+ function extractFromCache ( cache ) {
|
|
|
+ var values = [];
|
|
|
+ for ( var key in cache ) {
|
|
|
+ var data = cache[ key ];
|
|
|
+ delete data.metadata;
|
|
|
+ values.push( data );
|
|
|
+ }
|
|
|
+ return values;
|
|
|
+ }
|
|
|
|
|
|
},
|
|
|
|