Bladeren bron

Sphere: Add toJSON and fromJSON functions (#31028)

* Add bounding sphere toJSON and fromJSON

* Update object3d tojson

* Add support for BufferGeometry

* Remove unused import
Garrett Johnson 8 maanden geleden
bovenliggende
commit
d088fc1bf1
5 gewijzigde bestanden met toevoegingen van 34 en 28 verwijderingen
  1. 1 4
      src/core/BufferGeometry.js
  2. 2 8
      src/core/Object3D.js
  3. 1 10
      src/loaders/BufferGeometryLoader.js
  4. 2 6
      src/loaders/ObjectLoader.js
  5. 28 0
      src/math/Sphere.js

+ 1 - 4
src/core/BufferGeometry.js

@@ -1299,10 +1299,7 @@ class BufferGeometry extends EventDispatcher {
 
 		if ( boundingSphere !== null ) {
 
-			data.data.boundingSphere = {
-				center: boundingSphere.center.toArray(),
-				radius: boundingSphere.radius
-			};
+			data.data.boundingSphere = boundingSphere.toJSON();
 
 		}
 

+ 2 - 8
src/core/Object3D.js

@@ -1297,10 +1297,7 @@ class Object3D extends EventDispatcher {
 			object.geometryInfo = this._geometryInfo.map( info => ( {
 				...info,
 				boundingBox: info.boundingBox ? info.boundingBox.toJSON() : undefined,
-				boundingSphere: info.boundingSphere ? {
-					radius: info.boundingSphere.radius,
-					center: info.boundingSphere.center.toArray()
-				} : undefined
+				boundingSphere: info.boundingSphere ? info.boundingSphere.toJSON() : undefined
 			} ) );
 			object.instanceInfo = this._instanceInfo.map( info => ( { ...info } ) );
 
@@ -1329,10 +1326,7 @@ class Object3D extends EventDispatcher {
 
 			if ( this.boundingSphere !== null ) {
 
-				object.boundingSphere = {
-					center: this.boundingSphere.center.toArray(),
-					radius: this.boundingSphere.radius
-				};
+				object.boundingSphere = this.boundingSphere.toJSON();
 
 			}
 

+ 1 - 10
src/loaders/BufferGeometryLoader.js

@@ -1,5 +1,4 @@
 import { Sphere } from '../math/Sphere.js';
-import { Vector3 } from '../math/Vector3.js';
 import { BufferAttribute } from '../core/BufferAttribute.js';
 import { BufferGeometry } from '../core/BufferGeometry.js';
 import { FileLoader } from './FileLoader.js';
@@ -227,15 +226,7 @@ class BufferGeometryLoader extends Loader {
 
 		if ( boundingSphere !== undefined ) {
 
-			const center = new Vector3();
-
-			if ( boundingSphere.center !== undefined ) {
-
-				center.fromArray( boundingSphere.center );
-
-			}
-
-			geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
+			geometry.boundingSphere = new Sphere().fromJSON( boundingSphere );
 
 		}
 

+ 2 - 6
src/loaders/ObjectLoader.js

@@ -985,9 +985,7 @@ class ObjectLoader extends Loader {
 
 					if ( info.boundingSphere !== undefined ) {
 
-						sphere = new Sphere();
-						sphere.radius = info.boundingSphere.radius;
-						sphere.center.fromArray( info.boundingSphere.center );
+						sphere = new Sphere().fromJSON( info.boundingSphere );
 
 					}
 
@@ -1025,9 +1023,7 @@ class ObjectLoader extends Loader {
 
 				if ( data.boundingSphere !== undefined ) {
 
-					object.boundingSphere = new Sphere();
-					object.boundingSphere.center.fromArray( data.boundingSphere.center );
-					object.boundingSphere.radius = data.boundingSphere.radius;
+					object.boundingSphere = new Sphere().fromJSON( data.boundingSphere );
 
 				}
 

+ 28 - 0
src/math/Sphere.js

@@ -387,6 +387,34 @@ class Sphere {
 
 	}
 
+	/**
+	 * Returns a serialized structure of the bounding sphere.
+	 *
+	 * @return {Object} Serialized structure with fields representing the object state.
+	 */
+	toJSON() {
+
+		return {
+			radius: this.radius,
+			center: this.center.toArray()
+		};
+
+	}
+
+	/**
+	 * Returns a serialized structure of the bounding sphere.
+	 *
+	 * @param {Object} json - The serialized json to set the sphere from.
+	 * @return {Box3} A reference to this bounding sphere.
+	 */
+	fromJSON( json ) {
+
+		this.radius = json.radius;
+		this.center.fromArray( json.center );
+		return this;
+
+	}
+
 }
 
 export { Sphere };

粤ICP备19079148号