Skip to content

Box3: Add toJSON, fromJSON #31021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -1296,10 +1296,7 @@ class Object3D extends EventDispatcher {

object.geometryInfo = this._geometryInfo.map( info => ( {
...info,
boundingBox: info.boundingBox ? {
min: info.boundingBox.min.toArray(),
max: info.boundingBox.max.toArray()
} : undefined,
boundingBox: info.boundingBox ? info.boundingBox.toJSON() : undefined,
boundingSphere: info.boundingSphere ? {
radius: info.boundingSphere.radius,
center: info.boundingSphere.center.toArray()
Expand Down Expand Up @@ -1341,10 +1338,7 @@ class Object3D extends EventDispatcher {

if ( this.boundingBox !== null ) {

object.boundingBox = {
min: this.boundingBox.min.toArray(),
max: this.boundingBox.max.toArray()
};
object.boundingBox = this.boundingBox.toJSON();

}

Expand Down
8 changes: 2 additions & 6 deletions src/loaders/ObjectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,7 @@ class ObjectLoader extends Loader {
let sphere = null;
if ( info.boundingBox !== undefined ) {

box = new Box3();
box.min.fromArray( info.boundingBox.min );
box.max.fromArray( info.boundingBox.max );
box = new Box3().fromJSON( info.boundingBox );

}

Expand Down Expand Up @@ -1035,9 +1033,7 @@ class ObjectLoader extends Loader {

if ( data.boundingBox !== undefined ) {

object.boundingBox = new Box3();
object.boundingBox.min.fromArray( data.boundingBox.min );
object.boundingBox.max.fromArray( data.boundingBox.max );
object.boundingBox = new Box3().fromJSON( data.boundingBox );

}

Expand Down
28 changes: 28 additions & 0 deletions src/math/Box3.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,34 @@ class Box3 {

}

/**
* Returns a serialized structure of the bounding box.
*
* @return {Object} Serialized structure with fields representing the object state.
*/
toJSON() {

return {
min: this.min.toArray(),
max: this.max.toArray()
};

}

/**
* Returns a serialized structure of the bounding box.
*
* @param {Object} json - The serialized json to set the box from.
* @return {Box3} A reference to this bounding box.
*/
fromJSON( json ) {

this.min.fromArray( json.min );
this.max.fromArray( json.max );
return this;

}

}

const _points = [
Expand Down