Skip to content

Commit 8021449

Browse files
committed
toJSON - move class specific code onto individual classes
1 parent 9bce7c9 commit 8021449

24 files changed

Lines changed: 404 additions & 262 deletions

src/cameras/OrthographicCamera.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,18 @@ THREE.OrthographicCamera.prototype.clone = function () {
5656

5757
return camera;
5858
};
59+
60+
THREE.OrthographicCamera.prototype.toJSON = function ( meta ) {
61+
62+
var data = THREE.Object3D.prototype.toJSON.call( this, meta );
63+
64+
data.object.left = this.left;
65+
data.object.right = this.right;
66+
data.object.top = this.top;
67+
data.object.bottom = this.bottom;
68+
data.object.near = this.near;
69+
data.object.far = this.far;
70+
71+
return data;
72+
73+
};

src/cameras/PerspectiveCamera.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,16 @@ THREE.PerspectiveCamera.prototype.clone = function () {
140140
return camera;
141141

142142
};
143+
144+
THREE.PerspectiveCamera.prototype.toJSON = function ( meta ) {
145+
146+
var data = THREE.Object3D.prototype.toJSON.call( this, meta );
147+
148+
data.object.fov = this.fov;
149+
data.object.aspect = this.aspect;
150+
data.object.near = this.near;
151+
data.object.far = this.far;
152+
153+
return data;
154+
155+
};

src/core/BufferGeometry.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -874,22 +874,27 @@ THREE.BufferGeometry.prototype = {
874874
}
875875
},
876876

877-
toJSON: function () {
878-
879-
var output = {
880-
metadata: {
881-
version: 4.0,
882-
type: 'BufferGeometry',
883-
generator: 'BufferGeometryExporter'
884-
},
885-
uuid: this.uuid,
886-
type: this.type,
887-
data: {
888-
attributes: {}
889-
}
890-
};
877+
toJSON: function() {
891878

892-
var attributes = this.attributes;
879+
// we will store all serialization data on 'data'
880+
var data = {};
881+
882+
// add metadata
883+
data.metadata = {
884+
version: 4.4,
885+
type: 'BufferGeometry',
886+
generator: 'BufferGeometry.toJSON'
887+
}
888+
889+
// standard BufferGeometry serialization
890+
891+
data.type = this.type;
892+
data.uuid = this.uuid;
893+
if ( this.name !== '' ) data.name = this.name;
894+
data.data = {};
895+
data.data.attributes = {};
896+
897+
var attributes = this.attributes;
893898
var offsets = this.offsets;
894899
var boundingSphere = this.boundingSphere;
895900

@@ -899,7 +904,7 @@ THREE.BufferGeometry.prototype = {
899904

900905
var array = Array.prototype.slice.call( attribute.array );
901906

902-
output.data.attributes[ key ] = {
907+
data.data.attributes[ key ] = {
903908
itemSize: attribute.itemSize,
904909
type: attribute.array.constructor.name,
905910
array: array
@@ -909,20 +914,20 @@ THREE.BufferGeometry.prototype = {
909914

910915
if ( offsets.length > 0 ) {
911916

912-
output.data.offsets = JSON.parse( JSON.stringify( offsets ) );
917+
data.data.offsets = JSON.parse( JSON.stringify( offsets ) );
913918

914919
}
915920

916921
if ( boundingSphere !== null ) {
917922

918-
output.data.boundingSphere = {
923+
data.data.boundingSphere = {
919924
center: boundingSphere.center.toArray(),
920925
radius: boundingSphere.radius
921926
}
922927

923928
}
924929

925-
return output;
930+
return data;
926931

927932
},
928933

src/core/Geometry.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -797,31 +797,35 @@ THREE.Geometry.prototype = {
797797

798798
},
799799

800-
toJSON: function () {
801-
802-
var output = {
803-
metadata: {
804-
version: 4.0,
805-
type: 'BufferGeometry',
806-
generator: 'BufferGeometryExporter'
807-
},
808-
uuid: this.uuid,
809-
type: this.type
800+
toJSON: function() {
801+
802+
// we will store all serialization data on 'data'
803+
var data = {};
804+
805+
// add metadata
806+
data.metadata = {
807+
version: 4.4,
808+
type: 'Geometry',
809+
generator: 'Geometry.toJSON'
810810
};
811811

812-
if ( this.name !== "" ) output.name = this.name;
812+
// standard Geometry serialization
813813

814-
if ( this.parameters !== undefined ) {
814+
data.type = this.type;
815+
data.uuid = this.uuid;
816+
if ( this.name !== '' ) data.name = this.name;
817+
818+
if ( this.parameters !== undefined ) {
815819

816820
var parameters = this.parameters;
817821

818822
for ( var key in parameters ) {
819823

820-
if ( parameters[ key ] !== undefined ) output[ key ] = parameters[ key ];
824+
if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
821825

822826
}
823827

824-
return output;
828+
return data;
825829

826830
}
827831

@@ -984,17 +988,15 @@ THREE.Geometry.prototype = {
984988

985989
}
986990

987-
output.data = {};
988-
989-
output.data.vertices = vertices;
990-
output.data.normals = normals;
991-
if ( colors.length > 0 ) output.data.colors = colors;
992-
if ( uvs.length > 0 ) output.data.uvs = [ uvs ]; // temporal backward compatibility
993-
output.data.faces = faces;
991+
data.data = {};
994992

995-
//
993+
data.data.vertices = vertices;
994+
data.data.normals = normals;
995+
if ( colors.length > 0 ) data.data.colors = colors;
996+
if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility
997+
data.data.faces = faces;
996998

997-
return output;
999+
return data;
9981000

9991001
},
10001002

0 commit comments

Comments
 (0)