Browse Source

RoundedBoxGeometry: add type, parameters and toJSON (#31340)

* Enhance RoundedBoxGeometry: add type, update parameters and override toJSON

* Update RoundedBoxGeometry.js

Clean up.

* Fix create box geometry with 1x1x1 dimensions to avoid normal/shading issues

---------

Co-authored-by: Michael Herzog <michael.herzog@human-interactive.org>
VishaL 10 months ago
parent
commit
6b2f433272
1 changed files with 47 additions and 8 deletions
  1. 47 8
      examples/jsm/geometries/RoundedBoxGeometry.js

+ 47 - 8
examples/jsm/geometries/RoundedBoxGeometry.js

@@ -59,21 +59,40 @@ class RoundedBoxGeometry extends BoxGeometry {
 	 * @param {number} [width=1] - The width. That is, the length of the edges parallel to the X axis.
 	 * @param {number} [height=1] - The height. That is, the length of the edges parallel to the Y axis.
 	 * @param {number} [depth=1] - The depth. That is, the length of the edges parallel to the Z axis.
-	 * @param {number} [segments=2] - Number of segmented that form the rounded corners.
+	 * @param {number} [segments=2] - Number of segments that form the rounded corners.
 	 * @param {number} [radius=0.1] - The radius of the rounded corners.
 	 */
 	constructor( width = 1, height = 1, depth = 1, segments = 2, radius = 0.1 ) {
 
-		// ensure segments is odd so we have a plane connecting the rounded corners
-		segments = segments * 2 + 1;
+		// calculate total segments needed &
+		// ensure it's odd so that we have a plane connecting the rounded corners
+		const totalSegments = segments * 2 + 1;
 
 		// ensure radius isn't bigger than shortest side
 		radius = Math.min( width / 2, height / 2, depth / 2, radius );
 
-		super( width, height, depth, segments, segments, segments );
-
-		// if we just have one segment we're the same as a regular box
-		if ( segments === 1 ) return;
+		// start with a unit box geometry, its vertices will be modified to form the rounded box
+		super( 1, 1, 1, totalSegments, totalSegments, totalSegments );
+
+		this.type = 'RoundedBoxGeometry';
+
+		/**
+		 * Holds the constructor parameters that have been
+		 * used to generate the geometry. Any modification
+		 * after instantiation does not change the geometry.
+		 *
+		 * @type {Object}
+		 */
+		this.parameters = {
+			width: width,
+			height: height,
+			depth: depth,
+			segments: segments,
+			radius: radius,
+		};
+
+		// if totalSegments is 1, no rounding is needed - return regular box
+		if ( totalSegments === 1 ) return;
 
 		const geometry2 = this.toNonIndexed();
 
@@ -95,7 +114,7 @@ class RoundedBoxGeometry extends BoxGeometry {
 
 		const faceTris = positions.length / 6;
 		const faceDirVector = new Vector3();
-		const halfSegmentSize = 0.5 / segments;
+		const halfSegmentSize = 0.5 / totalSegments;
 
 		for ( let i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
 
@@ -172,6 +191,26 @@ class RoundedBoxGeometry extends BoxGeometry {
 
 	}
 
+	/**
+	 * Factory method for creating an instance of this class from the given
+	 * JSON object.
+	 *
+	 * @param {Object} data - A JSON object representing the serialized geometry.
+	 * @returns {RoundedBoxGeometry} A new instance.
+	 */
+	static fromJSON( data ) {
+
+		return new RoundedBoxGeometry(
+			data.width,
+			data.height,
+			data.depth,
+			data.segments,
+			data.radius
+		);
+
+	}
+
+
 }
 
 export { RoundedBoxGeometry };

粤ICP备19079148号