@@ -57,9 +57,10 @@ class ExtrudeGeometry extends BufferGeometry {
57
57
58
58
const scope = this ;
59
59
60
- const verticesArray = [ ] ;
60
+ const meshOutVerticesArray = [ ] ;
61
61
const uvArray = [ ] ;
62
62
63
+
63
64
for ( let i = 0 , l = shapes . length ; i < l ; i ++ ) {
64
65
65
66
const shape = shapes [ i ] ;
@@ -69,7 +70,7 @@ class ExtrudeGeometry extends BufferGeometry {
69
70
70
71
// build geometry
71
72
72
- this . setAttribute ( 'position' , new Float32BufferAttribute ( verticesArray , 3 ) ) ;
73
+ this . setAttribute ( 'position' , new Float32BufferAttribute ( meshOutVerticesArray , 3 ) ) ;
73
74
this . setAttribute ( 'uv' , new Float32BufferAttribute ( uvArray , 2 ) ) ;
74
75
75
76
this . computeVertexNormals ( ) ;
@@ -78,7 +79,7 @@ class ExtrudeGeometry extends BufferGeometry {
78
79
79
80
function addShape ( shape ) {
80
81
81
- const placeholder = [ ] ;
82
+ const uniqueShapeVertices = [ ] ;
82
83
83
84
// options
84
85
@@ -162,14 +163,44 @@ class ExtrudeGeometry extends BufferGeometry {
162
163
163
164
}
164
165
166
+ function cleanPoints ( points ) {
167
+
168
+ const THRESHOLD = 0.0002 ;
169
+ const THRESHOLD_SQ = THRESHOLD * THRESHOLD ;
170
+ let prevPos = points [ 0 ] ;
171
+ for ( let i = 1 ; i <= points . length ; i ++ ) {
172
+
173
+ const currentIndex = i % points . length ;
174
+ const currentPos = points [ currentIndex ] ;
175
+ const dx = currentPos . x - prevPos . x ;
176
+ const dy = currentPos . y - prevPos . y ;
177
+ const distSq = dx * dx + dy * dy ;
178
+
179
+ if ( distSq <= THRESHOLD_SQ ) {
180
+
181
+ points . splice ( currentIndex , 1 ) ;
182
+ if ( currentIndex == 0 ) break ;
183
+ i -- ;
184
+ continue ;
185
+
186
+ }
187
+
188
+ prevPos = currentPos ;
189
+
190
+ }
191
+
192
+ }
193
+
194
+ cleanPoints ( vertices ) ;
195
+ holes . forEach ( cleanPoints ) ;
165
196
166
- const faces = ShapeUtils . triangulateShape ( vertices , holes ) ;
197
+ const numHoles = holes . length ;
167
198
168
199
/* Vertices */
169
200
170
201
const contour = vertices ; // vertices has all points but contour has only points of circumference
171
202
172
- for ( let h = 0 , hl = holes . length ; h < hl ; h ++ ) {
203
+ for ( let h = 0 ; h < numHoles ; h ++ ) {
173
204
174
205
const ahole = holes [ h ] ;
175
206
@@ -178,6 +209,11 @@ class ExtrudeGeometry extends BufferGeometry {
178
209
}
179
210
180
211
212
+ /**
213
+ * @param {Vector2 } pt
214
+ * @param {Vector2 } vec
215
+ * @param {number } size
216
+ * @returns {Vector2 } pt + (vec * size) */
181
217
function scalePt2 ( pt , vec , size ) {
182
218
183
219
if ( ! vec ) console . error ( 'THREE.ExtrudeGeometry: vec does not exist' ) ;
@@ -186,7 +222,7 @@ class ExtrudeGeometry extends BufferGeometry {
186
222
187
223
}
188
224
189
- const vlen = vertices . length , flen = faces . length ;
225
+ const vlen = vertices . length ;
190
226
191
227
192
228
// Find directions for point movement
@@ -316,6 +352,7 @@ class ExtrudeGeometry extends BufferGeometry {
316
352
}
317
353
318
354
355
+ /**contourMovements[i] refers to the bevelVec of contour vertices [i-1] -> [i] -> [i+1], wrapping around the array bounds. Which is to say, the normalized vector pointing "outside" of the contour ("left" when the differential is "forward").*/
319
356
const contourMovements = [ ] ;
320
357
321
358
for ( let i = 0 , il = contour . length , j = il - 1 , k = i + 1 ; i < il ; i ++ , j ++ , k ++ ) {
@@ -333,7 +370,7 @@ class ExtrudeGeometry extends BufferGeometry {
333
370
const holesMovements = [ ] ;
334
371
let oneHoleMovements , verticesMovements = contourMovements . concat ( ) ;
335
372
336
- for ( let h = 0 , hl = holes . length ; h < hl ; h ++ ) {
373
+ for ( let h = 0 , hl = numHoles ; h < hl ; h ++ ) {
337
374
338
375
const ahole = holes [ h ] ;
339
376
@@ -354,52 +391,67 @@ class ExtrudeGeometry extends BufferGeometry {
354
391
355
392
}
356
393
394
+ const contractedContourVertices = [ ] ;
395
+ const expandedHoleVertices = [ ] ;
357
396
358
397
// Loop bevelSegments, 1 for the front, 1 for the back
359
398
360
399
for ( let b = 0 ; b < bevelSegments ; b ++ ) {
361
400
362
401
//for ( b = bevelSegments; b > 0; b -- ) {
363
402
403
+ /**Proportion of the way through the bevel. [0,slightly less than 1] */
364
404
const t = b / bevelSegments ;
405
+ /**Decays smoothly from bevelThickness to almost 0.*/
365
406
const z = bevelThickness * Math . cos ( t * Math . PI / 2 ) ;
407
+ /**Grows smoothly from 0 to almost bevelSize, plus bevelOffset.*/
366
408
const bs = bevelSize * Math . sin ( t * Math . PI / 2 ) + bevelOffset ;
367
409
368
410
// contract shape
369
411
370
412
for ( let i = 0 , il = contour . length ; i < il ; i ++ ) {
371
413
414
+ /**Contour point, beveled straight outwards bs units.*/
372
415
const vert = scalePt2 ( contour [ i ] , contourMovements [ i ] , bs ) ;
373
416
374
417
v ( vert . x , vert . y , - z ) ;
418
+ if ( t == 0 ) contractedContourVertices . push ( vert ) ;
375
419
376
420
}
377
421
378
422
// expand holes
379
423
380
- for ( let h = 0 , hl = holes . length ; h < hl ; h ++ ) {
424
+ for ( let h = 0 , hl = numHoles ; h < hl ; h ++ ) {
381
425
382
426
const ahole = holes [ h ] ;
383
427
oneHoleMovements = holesMovements [ h ] ;
384
-
428
+ const oneHoleVertices = [ ] ;
385
429
for ( let i = 0 , il = ahole . length ; i < il ; i ++ ) {
386
430
387
431
const vert = scalePt2 ( ahole [ i ] , oneHoleMovements [ i ] , bs ) ;
388
432
389
433
v ( vert . x , vert . y , - z ) ;
434
+ if ( t == 0 ) oneHoleVertices . push ( vert ) ;
390
435
391
436
}
392
437
438
+ if ( t == 0 ) expandedHoleVertices . push ( oneHoleVertices ) ;
439
+
393
440
}
394
441
395
442
}
396
443
444
+ const faces = ShapeUtils . triangulateShape ( contractedContourVertices , expandedHoleVertices ) ;
445
+
446
+ const flen = faces . length ;
447
+
397
448
const bs = bevelSize + bevelOffset ;
398
449
399
450
// Back facing vertices
400
451
401
452
for ( let i = 0 ; i < vlen ; i ++ ) {
402
453
454
+ //If beveled, move vert "outward" bevelSize + offset units.
403
455
const vert = bevelEnabled ? scalePt2 ( vertices [ i ] , verticesMovements [ i ] , bs ) : vertices [ i ] ;
404
456
405
457
if ( ! extrudeByPath ) {
@@ -512,7 +564,7 @@ class ExtrudeGeometry extends BufferGeometry {
512
564
513
565
function buildLidFaces ( ) {
514
566
515
- const start = verticesArray . length / 3 ;
567
+ const start = meshOutVerticesArray . length / 3 ;
516
568
517
569
if ( bevelEnabled ) {
518
570
@@ -562,15 +614,15 @@ class ExtrudeGeometry extends BufferGeometry {
562
614
563
615
}
564
616
565
- scope . addGroup ( start , verticesArray . length / 3 - start , 0 ) ;
617
+ scope . addGroup ( start , meshOutVerticesArray . length / 3 - start , 0 ) ;
566
618
567
619
}
568
620
569
621
// Create faces for the z-sides of the shape
570
622
571
623
function buildSideFaces ( ) {
572
624
573
- const start = verticesArray . length / 3 ;
625
+ const start = meshOutVerticesArray . length / 3 ;
574
626
let layeroffset = 0 ;
575
627
sidewalls ( contour , layeroffset ) ;
576
628
layeroffset += contour . length ;
@@ -586,7 +638,7 @@ class ExtrudeGeometry extends BufferGeometry {
586
638
}
587
639
588
640
589
- scope . addGroup ( start , verticesArray . length / 3 - start , 1 ) ;
641
+ scope . addGroup ( start , meshOutVerticesArray . length / 3 - start , 1 ) ;
590
642
591
643
592
644
}
@@ -623,28 +675,41 @@ class ExtrudeGeometry extends BufferGeometry {
623
675
624
676
function v ( x , y , z ) {
625
677
626
- placeholder . push ( x ) ;
627
- placeholder . push ( y ) ;
628
- placeholder . push ( z ) ;
678
+ uniqueShapeVertices . push ( x ) ;
679
+ uniqueShapeVertices . push ( y ) ;
680
+ uniqueShapeVertices . push ( z ) ;
629
681
630
682
}
631
683
632
684
685
+ /**Creates vertex and UV definitions in the final mesh buffers for a 3-gon face on the top of the extruded mesh.
686
+ *
687
+ * Uses vertex indices from the uniqueShapeVertices array.
688
+ * @param {number } a
689
+ * @param {number } b
690
+ * @param {number } c*/
633
691
function f3 ( a , b , c ) {
634
692
635
693
addVertex ( a ) ;
636
694
addVertex ( b ) ;
637
695
addVertex ( c ) ;
638
696
639
- const nextIndex = verticesArray . length / 3 ;
640
- const uvs = uvgen . generateTopUV ( scope , verticesArray , nextIndex - 3 , nextIndex - 2 , nextIndex - 1 ) ;
697
+ const nextIndex = meshOutVerticesArray . length / 3 ;
698
+ const uvs = uvgen . generateTopUV ( scope , meshOutVerticesArray , nextIndex - 3 , nextIndex - 2 , nextIndex - 1 ) ;
641
699
642
700
addUV ( uvs [ 0 ] ) ;
643
701
addUV ( uvs [ 1 ] ) ;
644
702
addUV ( uvs [ 2 ] ) ;
645
703
646
704
}
647
705
706
+ /**Creates vertex and UV definitions in the final mesh buffers for a 4-gon face on the side of the extruded mesh.
707
+ *
708
+ * Uses vertex indices from the uniqueShapeVertices array.
709
+ * @param {number } a
710
+ * @param {number } b
711
+ * @param {number } c
712
+ * @param {number } d*/
648
713
function f4 ( a , b , c , d ) {
649
714
650
715
addVertex ( a ) ;
@@ -656,8 +721,8 @@ class ExtrudeGeometry extends BufferGeometry {
656
721
addVertex ( d ) ;
657
722
658
723
659
- const nextIndex = verticesArray . length / 3 ;
660
- const uvs = uvgen . generateSideWallUV ( scope , verticesArray , nextIndex - 6 , nextIndex - 3 , nextIndex - 2 , nextIndex - 1 ) ;
724
+ const nextIndex = meshOutVerticesArray . length / 3 ;
725
+ const uvs = uvgen . generateSideWallUV ( scope , meshOutVerticesArray , nextIndex - 6 , nextIndex - 3 , nextIndex - 2 , nextIndex - 1 ) ;
661
726
662
727
addUV ( uvs [ 0 ] ) ;
663
728
addUV ( uvs [ 1 ] ) ;
@@ -669,11 +734,14 @@ class ExtrudeGeometry extends BufferGeometry {
669
734
670
735
}
671
736
737
+ /**Copies the specified triad of X,Y,Z values from uniqueVertices and appends them to verticesArray.
738
+ * @param {number } index Index of the triad in uniqueVertices.
739
+ */
672
740
function addVertex ( index ) {
673
741
674
- verticesArray . push ( placeholder [ index * 3 + 0 ] ) ;
675
- verticesArray . push ( placeholder [ index * 3 + 1 ] ) ;
676
- verticesArray . push ( placeholder [ index * 3 + 2 ] ) ;
742
+ meshOutVerticesArray . push ( uniqueShapeVertices [ index * 3 + 0 ] ) ;
743
+ meshOutVerticesArray . push ( uniqueShapeVertices [ index * 3 + 1 ] ) ;
744
+ meshOutVerticesArray . push ( uniqueShapeVertices [ index * 3 + 2 ] ) ;
677
745
678
746
}
679
747
0 commit comments