Skip to content

Commit f6fe356

Browse files
authored
Box3: Add toJSON, fromJSON (#31021)
* Box3: Add toJSON, fromJSON * Object3D: use toJSON * ObjectLoader: use toJSON
1 parent 4a02e57 commit f6fe356

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/core/Object3D.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,10 +1296,7 @@ class Object3D extends EventDispatcher {
12961296

12971297
object.geometryInfo = this._geometryInfo.map( info => ( {
12981298
...info,
1299-
boundingBox: info.boundingBox ? {
1300-
min: info.boundingBox.min.toArray(),
1301-
max: info.boundingBox.max.toArray()
1302-
} : undefined,
1299+
boundingBox: info.boundingBox ? info.boundingBox.toJSON() : undefined,
13031300
boundingSphere: info.boundingSphere ? {
13041301
radius: info.boundingSphere.radius,
13051302
center: info.boundingSphere.center.toArray()
@@ -1341,10 +1338,7 @@ class Object3D extends EventDispatcher {
13411338

13421339
if ( this.boundingBox !== null ) {
13431340

1344-
object.boundingBox = {
1345-
min: this.boundingBox.min.toArray(),
1346-
max: this.boundingBox.max.toArray()
1347-
};
1341+
object.boundingBox = this.boundingBox.toJSON();
13481342

13491343
}
13501344

src/loaders/ObjectLoader.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,7 @@ class ObjectLoader extends Loader {
979979
let sphere = null;
980980
if ( info.boundingBox !== undefined ) {
981981

982-
box = new Box3();
983-
box.min.fromArray( info.boundingBox.min );
984-
box.max.fromArray( info.boundingBox.max );
982+
box = new Box3().fromJSON( info.boundingBox );
985983

986984
}
987985

@@ -1035,9 +1033,7 @@ class ObjectLoader extends Loader {
10351033

10361034
if ( data.boundingBox !== undefined ) {
10371035

1038-
object.boundingBox = new Box3();
1039-
object.boundingBox.min.fromArray( data.boundingBox.min );
1040-
object.boundingBox.max.fromArray( data.boundingBox.max );
1036+
object.boundingBox = new Box3().fromJSON( data.boundingBox );
10411037

10421038
}
10431039

src/math/Box3.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,34 @@ class Box3 {
714714

715715
}
716716

717+
/**
718+
* Returns a serialized structure of the bounding box.
719+
*
720+
* @return {Object} Serialized structure with fields representing the object state.
721+
*/
722+
toJSON() {
723+
724+
return {
725+
min: this.min.toArray(),
726+
max: this.max.toArray()
727+
};
728+
729+
}
730+
731+
/**
732+
* Returns a serialized structure of the bounding box.
733+
*
734+
* @param {Object} json - The serialized json to set the box from.
735+
* @return {Box3} A reference to this bounding box.
736+
*/
737+
fromJSON( json ) {
738+
739+
this.min.fromArray( json.min );
740+
this.max.fromArray( json.max );
741+
return this;
742+
743+
}
744+
717745
}
718746

719747
const _points = [

0 commit comments

Comments
 (0)