Просмотр исходного кода

Box3: Add toJSON, fromJSON (#31021)

* Box3: Add toJSON, fromJSON

* Object3D: use toJSON

* ObjectLoader: use toJSON
Garrett Johnson 10 месяцев назад
Родитель
Сommit
f6fe356cbb
3 измененных файлов с 32 добавлено и 14 удалено
  1. 2 8
      src/core/Object3D.js
  2. 2 6
      src/loaders/ObjectLoader.js
  3. 28 0
      src/math/Box3.js

+ 2 - 8
src/core/Object3D.js

@@ -1296,10 +1296,7 @@ class Object3D extends EventDispatcher {
 
 
 			object.geometryInfo = this._geometryInfo.map( info => ( {
 			object.geometryInfo = this._geometryInfo.map( info => ( {
 				...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 ? {
 				boundingSphere: info.boundingSphere ? {
 					radius: info.boundingSphere.radius,
 					radius: info.boundingSphere.radius,
 					center: info.boundingSphere.center.toArray()
 					center: info.boundingSphere.center.toArray()
@@ -1341,10 +1338,7 @@ class Object3D extends EventDispatcher {
 
 
 			if ( this.boundingBox !== null ) {
 			if ( this.boundingBox !== null ) {
 
 
-				object.boundingBox = {
-					min: this.boundingBox.min.toArray(),
-					max: this.boundingBox.max.toArray()
-				};
+				object.boundingBox = this.boundingBox.toJSON();
 
 
 			}
 			}
 
 

+ 2 - 6
src/loaders/ObjectLoader.js

@@ -979,9 +979,7 @@ class ObjectLoader extends Loader {
 					let sphere = null;
 					let sphere = null;
 					if ( info.boundingBox !== undefined ) {
 					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 );
 
 
 					}
 					}
 
 
@@ -1035,9 +1033,7 @@ class ObjectLoader extends Loader {
 
 
 				if ( data.boundingBox !== undefined ) {
 				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 );
 
 
 				}
 				}
 
 

+ 28 - 0
src/math/Box3.js

@@ -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 = [
 const _points = [

粤ICP备19079148号