Skip to content

Commit 6b2f433

Browse files
vis-primeMugen87
andauthored
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>
1 parent 5737a9b commit 6b2f433

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

examples/jsm/geometries/RoundedBoxGeometry.js

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,40 @@ class RoundedBoxGeometry extends BoxGeometry {
5959
* @param {number} [width=1] - The width. That is, the length of the edges parallel to the X axis.
6060
* @param {number} [height=1] - The height. That is, the length of the edges parallel to the Y axis.
6161
* @param {number} [depth=1] - The depth. That is, the length of the edges parallel to the Z axis.
62-
* @param {number} [segments=2] - Number of segmented that form the rounded corners.
62+
* @param {number} [segments=2] - Number of segments that form the rounded corners.
6363
* @param {number} [radius=0.1] - The radius of the rounded corners.
6464
*/
6565
constructor( width = 1, height = 1, depth = 1, segments = 2, radius = 0.1 ) {
6666

67-
// ensure segments is odd so we have a plane connecting the rounded corners
68-
segments = segments * 2 + 1;
67+
// calculate total segments needed &
68+
// ensure it's odd so that we have a plane connecting the rounded corners
69+
const totalSegments = segments * 2 + 1;
6970

7071
// ensure radius isn't bigger than shortest side
7172
radius = Math.min( width / 2, height / 2, depth / 2, radius );
7273

73-
super( width, height, depth, segments, segments, segments );
74-
75-
// if we just have one segment we're the same as a regular box
76-
if ( segments === 1 ) return;
74+
// start with a unit box geometry, its vertices will be modified to form the rounded box
75+
super( 1, 1, 1, totalSegments, totalSegments, totalSegments );
76+
77+
this.type = 'RoundedBoxGeometry';
78+
79+
/**
80+
* Holds the constructor parameters that have been
81+
* used to generate the geometry. Any modification
82+
* after instantiation does not change the geometry.
83+
*
84+
* @type {Object}
85+
*/
86+
this.parameters = {
87+
width: width,
88+
height: height,
89+
depth: depth,
90+
segments: segments,
91+
radius: radius,
92+
};
93+
94+
// if totalSegments is 1, no rounding is needed - return regular box
95+
if ( totalSegments === 1 ) return;
7796

7897
const geometry2 = this.toNonIndexed();
7998

@@ -95,7 +114,7 @@ class RoundedBoxGeometry extends BoxGeometry {
95114

96115
const faceTris = positions.length / 6;
97116
const faceDirVector = new Vector3();
98-
const halfSegmentSize = 0.5 / segments;
117+
const halfSegmentSize = 0.5 / totalSegments;
99118

100119
for ( let i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
101120

@@ -172,6 +191,26 @@ class RoundedBoxGeometry extends BoxGeometry {
172191

173192
}
174193

194+
/**
195+
* Factory method for creating an instance of this class from the given
196+
* JSON object.
197+
*
198+
* @param {Object} data - A JSON object representing the serialized geometry.
199+
* @returns {RoundedBoxGeometry} A new instance.
200+
*/
201+
static fromJSON( data ) {
202+
203+
return new RoundedBoxGeometry(
204+
data.width,
205+
data.height,
206+
data.depth,
207+
data.segments,
208+
data.radius
209+
);
210+
211+
}
212+
213+
175214
}
176215

177216
export { RoundedBoxGeometry };

0 commit comments

Comments
 (0)