@@ -59,21 +59,40 @@ class RoundedBoxGeometry extends BoxGeometry {
59
59
* @param {number } [width=1] - The width. That is, the length of the edges parallel to the X axis.
60
60
* @param {number } [height=1] - The height. That is, the length of the edges parallel to the Y axis.
61
61
* @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.
63
63
* @param {number } [radius=0.1] - The radius of the rounded corners.
64
64
*/
65
65
constructor ( width = 1 , height = 1 , depth = 1 , segments = 2 , radius = 0.1 ) {
66
66
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 ;
69
70
70
71
// ensure radius isn't bigger than shortest side
71
72
radius = Math . min ( width / 2 , height / 2 , depth / 2 , radius ) ;
72
73
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 ;
77
96
78
97
const geometry2 = this . toNonIndexed ( ) ;
79
98
@@ -95,7 +114,7 @@ class RoundedBoxGeometry extends BoxGeometry {
95
114
96
115
const faceTris = positions . length / 6 ;
97
116
const faceDirVector = new Vector3 ( ) ;
98
- const halfSegmentSize = 0.5 / segments ;
117
+ const halfSegmentSize = 0.5 / totalSegments ;
99
118
100
119
for ( let i = 0 , j = 0 ; i < positions . length ; i += 3 , j += 2 ) {
101
120
@@ -172,6 +191,26 @@ class RoundedBoxGeometry extends BoxGeometry {
172
191
173
192
}
174
193
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
+
175
214
}
176
215
177
216
export { RoundedBoxGeometry } ;
0 commit comments