Skip to content

Commit d088fc1

Browse files
authored
Sphere: Add toJSON and fromJSON functions (#31028)
* Add bounding sphere toJSON and fromJSON * Update object3d tojson * Add support for BufferGeometry * Remove unused import
1 parent f6fe356 commit d088fc1

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

src/core/BufferGeometry.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1299,10 +1299,7 @@ class BufferGeometry extends EventDispatcher {
12991299

13001300
if ( boundingSphere !== null ) {
13011301

1302-
data.data.boundingSphere = {
1303-
center: boundingSphere.center.toArray(),
1304-
radius: boundingSphere.radius
1305-
};
1302+
data.data.boundingSphere = boundingSphere.toJSON();
13061303

13071304
}
13081305

src/core/Object3D.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1297,10 +1297,7 @@ class Object3D extends EventDispatcher {
12971297
object.geometryInfo = this._geometryInfo.map( info => ( {
12981298
...info,
12991299
boundingBox: info.boundingBox ? info.boundingBox.toJSON() : undefined,
1300-
boundingSphere: info.boundingSphere ? {
1301-
radius: info.boundingSphere.radius,
1302-
center: info.boundingSphere.center.toArray()
1303-
} : undefined
1300+
boundingSphere: info.boundingSphere ? info.boundingSphere.toJSON() : undefined
13041301
} ) );
13051302
object.instanceInfo = this._instanceInfo.map( info => ( { ...info } ) );
13061303

@@ -1329,10 +1326,7 @@ class Object3D extends EventDispatcher {
13291326

13301327
if ( this.boundingSphere !== null ) {
13311328

1332-
object.boundingSphere = {
1333-
center: this.boundingSphere.center.toArray(),
1334-
radius: this.boundingSphere.radius
1335-
};
1329+
object.boundingSphere = this.boundingSphere.toJSON();
13361330

13371331
}
13381332

src/loaders/BufferGeometryLoader.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Sphere } from '../math/Sphere.js';
2-
import { Vector3 } from '../math/Vector3.js';
32
import { BufferAttribute } from '../core/BufferAttribute.js';
43
import { BufferGeometry } from '../core/BufferGeometry.js';
54
import { FileLoader } from './FileLoader.js';
@@ -227,15 +226,7 @@ class BufferGeometryLoader extends Loader {
227226

228227
if ( boundingSphere !== undefined ) {
229228

230-
const center = new Vector3();
231-
232-
if ( boundingSphere.center !== undefined ) {
233-
234-
center.fromArray( boundingSphere.center );
235-
236-
}
237-
238-
geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
229+
geometry.boundingSphere = new Sphere().fromJSON( boundingSphere );
239230

240231
}
241232

src/loaders/ObjectLoader.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -985,9 +985,7 @@ class ObjectLoader extends Loader {
985985

986986
if ( info.boundingSphere !== undefined ) {
987987

988-
sphere = new Sphere();
989-
sphere.radius = info.boundingSphere.radius;
990-
sphere.center.fromArray( info.boundingSphere.center );
988+
sphere = new Sphere().fromJSON( info.boundingSphere );
991989

992990
}
993991

@@ -1025,9 +1023,7 @@ class ObjectLoader extends Loader {
10251023

10261024
if ( data.boundingSphere !== undefined ) {
10271025

1028-
object.boundingSphere = new Sphere();
1029-
object.boundingSphere.center.fromArray( data.boundingSphere.center );
1030-
object.boundingSphere.radius = data.boundingSphere.radius;
1026+
object.boundingSphere = new Sphere().fromJSON( data.boundingSphere );
10311027

10321028
}
10331029

src/math/Sphere.js

+28
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,34 @@ class Sphere {
387387

388388
}
389389

390+
/**
391+
* Returns a serialized structure of the bounding sphere.
392+
*
393+
* @return {Object} Serialized structure with fields representing the object state.
394+
*/
395+
toJSON() {
396+
397+
return {
398+
radius: this.radius,
399+
center: this.center.toArray()
400+
};
401+
402+
}
403+
404+
/**
405+
* Returns a serialized structure of the bounding sphere.
406+
*
407+
* @param {Object} json - The serialized json to set the sphere from.
408+
* @return {Box3} A reference to this bounding sphere.
409+
*/
410+
fromJSON( json ) {
411+
412+
this.radius = json.radius;
413+
this.center.fromArray( json.center );
414+
return this;
415+
416+
}
417+
390418
}
391419

392420
export { Sphere };

0 commit comments

Comments
 (0)