-
Notifications
You must be signed in to change notification settings - Fork 3
/
ScrewGeometry.js
797 lines (686 loc) · 33.8 KB
/
ScrewGeometry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
import {
Float32BufferAttribute,
BufferGeometry,
Vector2,
Vector3
} from 'three'
import * as Curves from 'three/src/extras/curves/Curves.js';
class ScrewGeometry extends BufferGeometry {
constructor(
screwLength = 1,
shaftOuterRadius = 1,
shaftInnerRadius = 0.8,
threadRadius = 2,
threadThickness = .2,
threadStarts = 2,
baseDistanceAlongScrew = 0,
initialVelocity = 1,
initialDistance = 5,
revolutionsPerSecond = 1,
acceleration = 0,
radialSegments = 8,
minLengthSegmentsPerMeter = 4) {
super();
this.type = 'ScrewGeometry';
this.parameters = {
screwLength: screwLength,
shaftOuterRadius: shaftOuterRadius,
shaftInnerRadius: shaftInnerRadius,
threadRadius: threadRadius,
threadThickness: threadThickness,
threadStarts: threadStarts, // Indicates how many flights the thread has. 1 means that the thread is a single helix. 2 means that the thread is a double helix. 3 means that the thread is a triple helix. etc.
baseDistanceAlongScrew: baseDistanceAlongScrew, // This the distance from the start of the mass driver to the start of this segment of the screw.
initialVelocity: initialVelocity,
initialDistance: initialDistance,
revolutionsPerSecond: revolutionsPerSecond,
acceleration: acceleration,
radialSegments: radialSegments,
minLengthSegmentsPerMeter: minLengthSegmentsPerMeter
};
// helper variables
const vertex = new Vector3()
const normal = new Vector3()
const uv = new Vector2()
const vertexArray = []
const uvArray = []
let P = new Vector3()
// buffer
const vertices = []
const normals = []
const uvs = []
const indices = []
const renderInnerSurface = shaftInnerRadius > 0
// const verticesTag = []
// const normalsTag = []
// const uvsTag = []
// const indicesTag = []
// Estimate how many length segments we will need to properly represent the screw
const distanceAlongScrew = baseDistanceAlongScrew + 0.5 * screwLength
//0 = 0.5 * a * t**2 + v0 * t - d
const cA = 0.5 * acceleration
const cB = initialVelocity
const cC = initialDistance - distanceAlongScrew
let time
if (cB**2 - 4*cA*cC < 0) {
time = 0
}
else {
const time0 = (-cB + Math.sqrt(cB**2 - 4*cA*cC)) / (2*cA)
const time1 = (-cB - Math.sqrt(cB**2 - 4*cA*cC)) / (2*cA)
time = Math.max(time0, time1)
}
const rotations = revolutionsPerSecond * time
const rateOfChangeInRotationalDistance = 2 * Math.PI * threadRadius * Math.abs(revolutionsPerSecond)
const rateOfChangeInForwardDisplacement = initialVelocity + acceleration * time // We're going to assume that the launch sled does not start from zero velocity because this would require an thread pitch of zero, which is not manufacturable.
const minimumTubularSegments = Math.floor(minLengthSegmentsPerMeter * screwLength)
const tubularSegments = Math.ceil(Math.sqrt(rateOfChangeInRotationalDistance**2 + rateOfChangeInForwardDisplacement**2) / rateOfChangeInForwardDisplacement * minimumTubularSegments)
// create buffer data
generateBufferData();
// console.log(vertices.length, normals.length, uvs.length, verticesTag.length, normalsTag.length, uvsTag.length)
// let c = 0
// vertices.forEach((v, i) => {
// //for (let i = 0; i<1000; i++) {
// if ((c<1000) && ((verticesTag[i]!==normalsTag[i]) || (verticesTag[i]!==uvsTag[i]))) {
// console.log(i, verticesTag[i], normalsTag[i], uvsTag[i])
// c++
// }
// })
// build geometry
this.setIndex( indices );
this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
// functions
function generateBufferData() {
const twoPi = 2 * Math.PI
for ( let i = 0; i <= tubularSegments; i++) {
generateSegmentsAndUVs( i, screwLength, 0);
}
// Need an extra vertices at each end of the screw to construct the flat surfaces that cap the ends
const vertexAnglesBreachEnd = []
const vertexAnglesMuzzleEnd = []
generateSegmentsAndUVs(0, screwLength, 1, vertexAnglesBreachEnd);
generateSegmentsAndUVs(tubularSegments, screwLength, 2, vertexAnglesMuzzleEnd);
if (renderInnerSurface) {
const interiorPlateMuzzleEndInset = 0.003
const nubOuterRadius = 0.008
const nubInnerRadius = 0.005
const bracketThickness = 0.002
const motorMountingDiskThickness = 0.01
const interiorPlateBreachEndInset = 0.052
const motorShaftRadius = 0.0025
const profilePoints = [
// Start at the muzzle-facing end of the screw and work back towards the breach-facing end
// Shaft end face
{r: shaftInnerRadius, y: 0.5 * screwLength, normalAngleOffset: Math.PI/2},
// Interior Shaft Wall
{r: shaftInnerRadius, y: 0.5 * screwLength, normalAngleOffset: -Math.PI},
{r: shaftInnerRadius, y: 0.5 * screwLength - interiorPlateMuzzleEndInset, normalAngleOffset: -Math.PI},
// Motor mounting disk muzzle facing face
{r: shaftInnerRadius, y: 0.5 * screwLength - interiorPlateMuzzleEndInset, normalAngleOffset: Math.PI/2},
{r: nubOuterRadius, y: 0.5 * screwLength - interiorPlateMuzzleEndInset, normalAngleOffset: Math.PI/2},
// Nub outer wall
{r: nubOuterRadius, y: 0.5 * screwLength - interiorPlateMuzzleEndInset, normalAngleOffset: 0},
{r: nubOuterRadius, y: 0.5 * screwLength + bracketThickness, normalAngleOffset: 0},
// Nub muzzle facing face
{r: nubOuterRadius, y: 0.5 * screwLength + bracketThickness, normalAngleOffset: Math.PI/2},
{r: nubInnerRadius, y: 0.5 * screwLength + bracketThickness, normalAngleOffset: Math.PI/2},
// Nub inner wall
{r: nubInnerRadius, y: 0.5 * screwLength + bracketThickness, normalAngleOffset: -Math.PI},
{r: nubInnerRadius, y: 0.5 * screwLength - interiorPlateMuzzleEndInset - motorMountingDiskThickness, normalAngleOffset: -Math.PI},
// Motor mounting disk breach facing face
{r: nubInnerRadius, y: 0.5 * screwLength - interiorPlateMuzzleEndInset - motorMountingDiskThickness, normalAngleOffset: -Math.PI/2},
{r: shaftInnerRadius, y: 0.5 * screwLength - interiorPlateMuzzleEndInset - motorMountingDiskThickness - shaftInnerRadius, normalAngleOffset: -Math.PI/2},
// Interior shaft wall
{r: shaftInnerRadius, y: 0.5 * screwLength - interiorPlateMuzzleEndInset - motorMountingDiskThickness - shaftInnerRadius, normalAngleOffset: -Math.PI},
{r: shaftInnerRadius, y: -0.5 * screwLength + interiorPlateBreachEndInset + motorMountingDiskThickness, normalAngleOffset: -Math.PI},
// Motor mounting disk muzzle facing face
{r: shaftInnerRadius, y: -0.5 * screwLength + interiorPlateBreachEndInset + motorMountingDiskThickness, normalAngleOffset: Math.PI/2},
{r: motorShaftRadius, y: -0.5 * screwLength + interiorPlateBreachEndInset + motorMountingDiskThickness, normalAngleOffset: Math.PI/2},
// Motor shaft inner wall
{r: motorShaftRadius, y: -0.5 * screwLength + interiorPlateBreachEndInset + motorMountingDiskThickness, normalAngleOffset: -Math.PI},
{r: motorShaftRadius, y: -0.5 * screwLength + interiorPlateBreachEndInset, normalAngleOffset: -Math.PI},
// Motor mounting disk breach facing face
{r: motorShaftRadius, y: -0.5 * screwLength + interiorPlateBreachEndInset, normalAngleOffset: -Math.PI/2},
{r: shaftInnerRadius, y: -0.5 * screwLength + interiorPlateBreachEndInset, normalAngleOffset: -Math.PI/2},
// Interior shaft wall
{r: shaftInnerRadius, y: -0.5 * screwLength + interiorPlateBreachEndInset, normalAngleOffset: -Math.PI},
{r: shaftInnerRadius, y: -0.5 * screwLength, normalAngleOffset: -Math.PI},
// Shaft end face
{r: shaftInnerRadius, y: -0.5 * screwLength, normalAngleOffset: -Math.PI/2},
]
const T = new Vector3(0, 1, 0)
const N = new Vector3(-1, 0, 0) // z-axis is down
const B = new Vector3(0, 0, 1) // x-axis is to the right when looking at the back of the launcher
const numPointsOuter = (radialSegments + 1) * threadStarts
const numPointsInner = (radialSegments + 1) * threadStarts * 2
const extraOffsetToOuterShaftFirstVertex = 6
const verticesPerFlightOnShaft = (radialSegments + 1)
const verticesPerFlight = extraOffsetToOuterShaftFirstVertex + verticesPerFlightOnShaft // Each flight comprises 3 faces with 2 verticies per face plus the vertices on the shaft between two adjacent flights
const lastProfileEntry = profilePoints.length-1
const ringOfVerticesOffset = []
ringOfVerticesOffset[0] = verticesPerFlight * threadStarts * (tubularSegments+2) // Offset to the start of the vertices for the tubular segment for the muzzle-facing face
profilePoints.forEach((profilePoint, k) => {
P = new Vector3(0, profilePoint.y, 0);
ringOfVerticesOffset[k+1] = addRingOfVertices(P, T, N, B, numPointsInner, profilePoint.r, profilePoint.normalAngleOffset, 0.5+profilePoint.y, threadRadius, vertices, normals, uvs)
})
ringOfVerticesOffset[lastProfileEntry+2] = verticesPerFlight * threadStarts * (tubularSegments+1) // Offset to the start of the vertices for the tubular segment for the breach-facing face
let indexGenerator1, indexGenerator2, angleGenerator1, angleGenerator2
// Muzzle end faces
indexGenerator1 = (i) => {return ringOfVerticesOffset[0] + (Math.floor(i / verticesPerFlightOnShaft) * verticesPerFlight) + extraOffsetToOuterShaftFirstVertex + (i%verticesPerFlightOnShaft)}
indexGenerator2 = (i) => {return ringOfVerticesOffset[1]+i}
angleGenerator1 = (i) => {return vertexAnglesMuzzleEnd[i]}
angleGenerator2 = (i) => {return i * twoPi / numPointsInner}
smartStitch(indexGenerator1, indexGenerator2, angleGenerator1, angleGenerator2, numPointsOuter, numPointsInner, indices, true)
// Inner profile of shaft
for (let k = 2; k < profilePoints.length-1; k+=2) {
indexGenerator1 = (i) => {return ringOfVerticesOffset[k]+i}
indexGenerator2 = (i) => {return ringOfVerticesOffset[k+1]+i}
dumbStitch(indexGenerator1, indexGenerator2, numPointsInner, indices, true)
}
// Breach end faces
indexGenerator1 = (i) => {return ringOfVerticesOffset[lastProfileEntry+1]+i}
indexGenerator2 = (i) => { return ringOfVerticesOffset[lastProfileEntry+2] + (Math.floor(i / verticesPerFlightOnShaft) * verticesPerFlight) + extraOffsetToOuterShaftFirstVertex + (i%verticesPerFlightOnShaft)}
angleGenerator1 = (i) => {return i * twoPi / numPointsInner}
angleGenerator2 = (i) => {return vertexAnglesBreachEnd[i]}
smartStitch(indexGenerator1, indexGenerator2, angleGenerator1, angleGenerator2, numPointsInner, numPointsOuter, indices, true)
}
else {
// Need an extra vertex at the center of each end of the screw. These will be used to construct the flat surfaces that cap the ends
for (let i = 0; i <= tubularSegments; i+=tubularSegments) {
P = new Vector3(0, (i / tubularSegments - 0.5) * screwLength, 0);
vertices.push( P.x, P.y, P.z );
//verticesTag.push( 'end' + i, 'end' + i, 'end' + i);
const T = new Vector3(0, 1, 0);
if (i==0) {
normals.push( -T.x, -T.y, -T.z );
//normalsTag.push( 'end' + i, 'end' + i, 'end' + i);
}
else {
normals.push( T.x, T.y, T.z );
//normalsTag.push( 'end' + i, 'end' + i, 'end' + i);
}
uvs.push( 0.5, 0.5 );
//uvsTag.push( 'end' + i, 'end' + i, 'end' + i);
}
}
generateIndices()
}
function generateSegmentsAndUVs( i, screwLength, sideOrEndsSelector, vertexAngles = null) {
// we use getPointAt to sample evenly distributed points from the given path
P = new Vector3(0, (i / tubularSegments - 0.5) * screwLength, 0 );
// Note: y-axis is in the direction the rocket is pointing, z-axis is up when the rocket is lying on it's side)
const T = new Vector3(0, 1, 0)
const N = new Vector3(-1, 0, 0) // z-axis is down
const B = new Vector3(0, 0, 1) // x-axis is to the right when looking at the back of the launcher
// Figure out the start angle and end angle given the thickness, pitch, and the number of starts of the thread.
const distanceAlongScrew = baseDistanceAlongScrew + i / tubularSegments * screwLength
//0 = 0.5 * a * t**2 + v0 * t - d
const cA = 0.5 * acceleration
const cB = initialVelocity
const cC = initialDistance - distanceAlongScrew
let time
if (cB**2 - 4*cA*cC < 0) {
time = 0
}
else {
time = (-cB - Math.sqrt(cB**2 - 4*cA*cC)) / (2*cA)
}
// const time0 = (-cB + Math.sqrt(cB**2 - 4*cA*cC)) / (2*cA)
// const time1 = (-cB - Math.sqrt(cB**2 - 4*cA*cC)) / (2*cA)
// const time = Math.max(time0, time1)
const rotations = revolutionsPerSecond * time
const rateOfChangeInRotationalDistance = 2 * Math.PI * threadRadius * Math.abs(revolutionsPerSecond)
const rateOfChangeInForwardDisplacement = initialVelocity + acceleration * time // We're going to assume that the launch sled does not start from zero velocity because this would require an thread pitch of zero, which is not manufacturable.
const threadPitch = rateOfChangeInForwardDisplacement / rateOfChangeInRotationalDistance
const threadHalfOfCrossWidth = Math.min(threadThickness * Math.sqrt(threadPitch**2+1) / Math.abs(threadPitch), shaftOuterRadius/2);
const threadBaseHalfAngle = Math.asin(threadHalfOfCrossWidth/shaftOuterRadius)
const threadBaseEndAngle = 2 * Math.PI / threadStarts
const threadTopHalfAngle = Math.asin(threadHalfOfCrossWidth/threadRadius)
// generate normals and vertices for the current segment
const angularStep1 = (threadBaseEndAngle - threadBaseHalfAngle) / radialSegments
const x0 = Math.cos(threadBaseHalfAngle) * shaftOuterRadius
const y0 = Math.sin(threadBaseHalfAngle) * shaftOuterRadius
const x1 = Math.cos(threadTopHalfAngle) * threadRadius
const y1 = Math.sin(threadTopHalfAngle) * threadRadius
const threadSideWallHeight = Math.sqrt((x1-x0)**2 + (y1-y0)**2)
const perimeterLength = (2 * Math.PI - threadStarts * 2 * threadBaseHalfAngle) * shaftOuterRadius + threadStarts * (2 * threadSideWallHeight + threadThickness)
for (let k = 0; k < threadStarts; k++) {
const precomputedPartOfAngle1 = 2 * Math.PI * ((rotations + k / threadStarts) )
const precomputedPartOfAngle2 = precomputedPartOfAngle1 + threadBaseHalfAngle
// Thread Faces
// Generate the normals and vertices for the three faces of each flight of the screw thread
const v = []
v.push(precomputedPartOfAngle1)
v.push(precomputedPartOfAngle1)
v.push(precomputedPartOfAngle1 + threadTopHalfAngle)
v.push(precomputedPartOfAngle1 + threadBaseHalfAngle)
for (let j = 0; j<3; j++) {
const nv = precomputedPartOfAngle1 + ((j-1) * Math.PI/2)
const sin = Math.sin(nv)
const cos = Math.cos(nv)
normal[j] = new Vector3()
normal[j].x = cos * B.x + sin * N.x
normal[j].y = cos * B.y + sin * N.y
normal[j].z = cos * B.z + sin * N.z
normal[j].normalize()
}
for (let j = 0; j<4; j++) {
const vv = v[j]
const sin = Math.sin(vv)
const cos = Math.cos(vv)
vertexArray[j] = new Vector3()
const r = (j==0 || j==3) ? shaftOuterRadius : threadRadius
vertexArray[j].x = P.x + r * (cos * B.x + sin * N.x)
vertexArray[j].y = P.y + r * (cos * B.y + sin * N.y)
vertexArray[j].z = P.z + r * (cos * B.z + sin * N.z)
}
// Thread back face
vertices.push( vertexArray[0].x, vertexArray[0].y, vertexArray[0].z )
//verticesTag.push( 'tbf0', 'tbf0', 'tbf0')
vertices.push( vertexArray[1].x, vertexArray[1].y, vertexArray[1].z )
//verticesTag.push( 'tbf1', 'tbf1', 'tbf1')
// Thread top face
vertices.push( vertexArray[1].x, vertexArray[1].y, vertexArray[1].z )
//verticesTag.push( 'ttf1', 'ttf1', 'ttf1')
vertices.push( vertexArray[2].x, vertexArray[2].y, vertexArray[2].z )
//verticesTag.push( 'ttf2', 'ttf2', 'ttf2')
// Thread front face
vertices.push( vertexArray[2].x, vertexArray[2].y, vertexArray[2].z )
//verticesTag.push( 'tff2', 'tff2', 'tff2')
vertices.push( vertexArray[3].x, vertexArray[3].y, vertexArray[3].z )
//verticesTag.push( 'tff3', 'tff3', 'tff3')
switch (sideOrEndsSelector) {
case 0:
// Flight side faces
// Thread back face
normals.push( normal[0].x, normal[0].y, normal[0].z )
//normalsTag.push( 'tbf0', 'tbf0', 'tbf0')
normals.push( normal[0].x, normal[0].y, normal[0].z )
//normalsTag.push( 'tbf1', 'tbf1', 'tbf1')
// Thread top face
normals.push( normal[1].x, normal[1].y, normal[1].z )
//normalsTag.push( 'ttf1', 'ttf1', 'ttf1')
normals.push( normal[1].x, normal[1].y, normal[1].z )
//normalsTag.push( 'ttf2', 'ttf2', 'ttf2')
// Thread front face
normals.push( normal[2].x, normal[2].y, normal[2].z )
//normalsTag.push( 'tff2', 'tff2', 'tff2')
normals.push( normal[2].x, normal[2].y, normal[2].z )
//normalsTag.push( 'tff3', 'tff3', 'tff3')
break;
case 1:
// Flight backward end faces
for (let j = 0; j<6; j++) {
normals.push( -T.x, -T.y, -T.z )
//const label = ((j<2) ? 'tbf' : (j<4) ? 'ttf' : 'tff' ) + [0, 1, 1, 2, 2, 3][j]
//normalsTag.push( label, label, label)
}
break;
case 2:
// Flight forward end faces
for (let j = 0; j<6; j++) {
normals.push( T.x, T.y, T.z );
//const label = ((j<2) ? 'tbf' : (j<4) ? 'ttf' : 'tff' ) + [0, 1, 1, 2, 2, 3][j]
//normalsTag.push( label, label, label)
}
break;
}
// Generate the normals and vertices that define the curves for the outer surface of the shaft
for (let j = 0; j <= radialSegments; j++) {
const v = precomputedPartOfAngle2 + j * angularStep1;
if (vertexAngles!==null) {
vertexAngles.push(v)
}
const sin = Math.sin( v );
const cos = Math.cos( v );
// normal
normal.x = ( cos * B.x + sin * N.x );
normal.y = ( cos * B.y + sin * N.y );
normal.z = ( cos * B.z + sin * N.z );
normal.normalize(); // Really needed?
switch (sideOrEndsSelector) {
case 0:
normals.push( normal.x, normal.y, normal.z );
break;
case 1:
normals.push( -T.x, -T.y, -T.z );
break;
case 2:
normals.push( T.x, T.y, T.z );
break;
}
//normalsTag.push( 'out' + j, 'out' + j, 'out' + j);
// vertex
vertex.x = P.x + shaftOuterRadius * normal.x;
vertex.y = P.y + shaftOuterRadius * normal.y;
vertex.z = P.z + shaftOuterRadius * normal.z;
vertices.push( vertex.x, vertex.y, vertex.z );
//verticesTag.push( 'out' + j, 'out' + j, 'out' + j);
}
// Now compute the UVs
switch (sideOrEndsSelector) {
// Side faces
case 0:
// Screw flight back face
uv.x = i / tubularSegments;
uv.y = Math.abs(rotations + ( k * perimeterLength / threadStarts + (threadBaseEndAngle - threadBaseHalfAngle) * shaftOuterRadius) / perimeterLength) % 1;
uvs.push( uv.x, uv.y );
//uvsTag.push( 'tbf0', 'tbf0', 'tbf0');
uv.x = i / tubularSegments;
uv.y = Math.abs(rotations + (k * perimeterLength / threadStarts + (threadBaseEndAngle - threadBaseHalfAngle) * shaftOuterRadius + threadSideWallHeight) / perimeterLength) % 1;
uvs.push( uv.x, uv.y );
//uvsTag.push( 'tbf1', 'tbf1', 'tbf1');
// Screw flight top face
uvs.push( uv.x, uv.y );
//uvsTag.push( 'ttf1', 'ttf1', 'ttf1');
uv.x = i / tubularSegments;
uv.y = Math.abs(rotations + (k * perimeterLength / threadStarts + (threadBaseEndAngle - threadBaseHalfAngle) * shaftOuterRadius + threadSideWallHeight + threadThickness) / perimeterLength) % 1;
uvs.push( uv.x, uv.y );
//uvsTag.push( 'ttf2', 'ttf2', 'ttf2');
// Screw flight front face
uvs.push( uv.x, uv.y );
//uvsTag.push( 'tff2', 'tff2', 'tff2');
uv.x = i / tubularSegments;
uv.y = Math.abs(rotations + (k * perimeterLength / threadStarts + (threadBaseEndAngle - threadBaseHalfAngle) * shaftOuterRadius + threadSideWallHeight + threadThickness + threadSideWallHeight) / perimeterLength) % 1;
uvs.push( uv.x, uv.y );
//uvsTag.push( 'tff3', 'tff3', 'tff3');
// Outer surface of shaft
for ( let j = 0; j <= radialSegments; j++ ) {
uv.x = i / tubularSegments;
uv.y = Math.abs(rotations + (k * perimeterLength / threadStarts + (threadBaseEndAngle - threadBaseHalfAngle) * j / radialSegments * shaftOuterRadius) / perimeterLength) % 1;
uvs.push( uv.x, uv.y );
//uvsTag.push( 'out' + j, 'out' + j, 'out' + j);
}
break;
case 1: case 2:
// End faces
for (let j = 0; j<4; j++) {
const vv = v[j]
const sin = Math.sin(vv);
const cos = Math.cos(vv);
uvArray[j] = new Vector2()
const r = (j==0 || j==3) ? shaftOuterRadius : threadRadius;
uvArray[j].x = r * cos;
uvArray[j].y = r * sin;
}
// Screw flight back edge
uvs.push( uvArray[0].x, uvArray[0].y );
//uvsTag.push( 'tbf0', 'tbf0', 'tbf0');
uvs.push( uvArray[1].x, uvArray[1].y );
//uvsTag.push( 'tbf1', 'tbf1', 'tbf1');
// Screw flight top edge
uvs.push( uvArray[1].x, uvArray[1].y );
//uvsTag.push( 'ttf1', 'ttf1', 'ttf1');
uvs.push( uvArray[2].x, uvArray[2].y );
//uvsTag.push( 'ttf2', 'ttf2', 'ttf2');
// Screw flight front edge
uvs.push( uvArray[2].x, uvArray[2].y );
//uvsTag.push( 'tff2', 'tff2', 'tff2');
uvs.push( uvArray[3].x, uvArray[3].y );
//uvsTag.push( 'tff3', 'tff3', 'tff3');
for ( let j = 0; j <= radialSegments; j++ ) {
const v = precomputedPartOfAngle2 + j * angularStep1;
const sin = Math.sin( v );
const cos = Math.cos( v );
uv.x = shaftOuterRadius * cos
uv.y = shaftOuterRadius * sin
uvs.push( uv.x, uv.y );
//uvsTag.push( 'out' + j, 'out' + j, 'out' + j);
}
break;
}
}
}
function addRingOfVertices(P, T, N, B, numPoints, radius, normalAngleOffset, axialPosition, maxRadius, vertices, normals, uvs) {
// normalAngleOffset indicates how much to tilt the normals away from being perpendicular to the curve and towards being more tangential to the curve.
const normalCos = Math.cos(normalAngleOffset)
const normalSin = Math.sin(normalAngleOffset)
const pathNormal = new Vector3()
const lightingNormal = new Vector3()
const vertex = new Vector3()
const isSide = Math.abs(normalAngleOffset) < Math.PI/4
const pointerToVertices = vertices.length / 3
const angularStep = 2 * Math.PI / numPoints
for (let j = 0; j < numPoints; j++) {
const v = j * angularStep
const sin = Math.sin( v )
const cos = Math.cos( v )
// A direction perpendicular to the path
pathNormal.x = cos * B.x + sin * N.x
pathNormal.y = cos * B.y + sin * N.y
pathNormal.z = cos * B.z + sin * N.z
// vertex
vertex.x = P.x + radius * pathNormal.x
vertex.y = P.y + radius * pathNormal.y
vertex.z = P.z + radius * pathNormal.z
vertices.push( vertex.x, vertex.y, vertex.z )
//verticesTag.push( 'out' + j, 'out' + j, 'out' + j)
// vertex normal (for lighting calculations)
lightingNormal.x = normalCos * pathNormal.x + normalSin * T.x
lightingNormal.y = normalCos * pathNormal.y + normalSin * T.y
lightingNormal.z = normalCos * pathNormal.z + normalSin * T.z
normals.push( lightingNormal.x, lightingNormal.y, lightingNormal.z )
//normalsTag.push( 'out' + j, 'out' + j, 'out' + j)
// Now compute the UVs
if (isSide) {
uv.x = axialPosition
uv.y = j / (numPoints-1)
}
else {
// ... is end
uv.x = cos * radius / maxRadius * 0.5 + 0.5
uv.y = sin * radius / maxRadius * 0.5 + 0.5
}
uvs.push( uv.x, uv.y )
//uvsTag.push( 'out' + j, 'out' + j, 'out' + j)
}
return pointerToVertices
}
function angleDiff(a, b) {
const twoPi = 2 * Math.PI
const diff = a - b
const diff_ab = (twoPi + diff) % twoPi
const diff_ba = (twoPi - diff) % twoPi
return Math.min(diff_ab, diff_ba)
}
function smartStitch(indexGenerator1, indexGenerator2, angleGenerator1, angleGenerator2, numPoints1, numPoints2, indices, wrapAround = false) {
// This function creates a set of triangles between two rings of verticies. It analyzes the angles associated
// with the verticies to determine how to construct the triangles optimally.
const twoPi = 2 * Math.PI
let a, b, c, d // These are values in the range of 0 to numPoints-1
let angleA, angleB, angleC, angleD // These are values in the range of 0 to 2*PI
let indexA, indexB, indexC, indexD // These are pointers into the array of vertices.
a = 0
angleA = angleGenerator1(a)
angleA = angleA - Math.floor(angleA / twoPi) * twoPi
indexA = indexGenerator1(a)
let bestFirstB = 0
let bestAngleB = 0
let smallestDifference
for (let i = 0; i<numPoints2; i++) {
angleB = angleGenerator2(i)
angleB = angleB - Math.floor(angleB / twoPi) * twoPi
// Determine the absolute value of the difference between the two angles in a manner that handles the wrap arond case
const angleDifference = angleDiff(angleA, angleB)
if ((bestFirstB==0) || (angleDifference < smallestDifference)) {
bestFirstB = i
bestAngleB = angleB
smallestDifference = angleDifference
}
}
b = bestFirstB
angleB = bestAngleB
indexB = indexGenerator2(b)
c = (b+1) % numPoints1
d = (a+1) % numPoints2
angleC = angleGenerator2(c)
angleC = angleC - Math.floor(angleC / twoPi) * twoPi
angleD = angleGenerator1(d)
angleD = angleD - Math.floor(angleD / twoPi) * twoPi
indexC = indexGenerator2(c)
indexD = indexGenerator1(d)
let moreCs = true
let moreDs = true
let k = 0
do {
const angleDiffAC = angleDiff(angleA, angleC)
const angleDiffBD = angleDiff(angleB, angleD)
const useC = !moreCs ? false : !moreDs ? true : (angleDiffAC < angleDiffBD)
if (useC) {
// Use point C to complete the triangle
indices.push( indexA, indexB, indexC );
//console.log(a, b, c, angleA, angleB, angleC)
k++
b = c
angleB = angleC
indexB = indexC
moreCs = wrapAround ? (c != bestFirstB) : ((c+1) % numPoints2 != bestFirstB)
if (moreCs) {
c = (b+1) % numPoints2
angleC = angleGenerator2(c)
angleC = angleC - Math.floor(angleC / twoPi) * twoPi
indexC = indexGenerator2(c)
}
}
else {
// Use point D to complete the triangle
indices.push( indexA, indexB, indexD );
//console.log(a, b, d, angleA, angleB, angleD)
k++
a = d
angleA = angleD
indexA = indexD
moreDs = wrapAround ? (d != 0) : ((d+1) % numPoints1 != 0)
if (moreDs) {
d = (a+1) % numPoints1
angleD = angleGenerator1(d)
angleD = angleD - Math.floor(angleD / twoPi) * twoPi
indexD = indexGenerator1(d)
}
}
} while (moreCs || moreDs)
//console.log('k = ' + k, numPoints1+numpoints2)
}
function dumbStitch(indexGenerator1, indexGenerator2, numPoints, indices, wrapAround = false) {
for (let i=0; i<numPoints + (wrapAround?1:0); i++) {
const indexA = indexGenerator1(i)
const indexB = indexGenerator2(i)
const indexC = indexGenerator2((i+1)%numPoints)
const indexD = indexGenerator1((i+1)%numPoints)
indices.push( indexA, indexB, indexC );
indices.push( indexC, indexD, indexA );
}
}
function generateIndices() {
let verticesPerFlight
if (false && renderInnerSurface) {
verticesPerFlight = 2*(radialSegments + 1) + 6 // Part of shaft (outer and inner) plus each thread comprises 3 faces with 2 verticies per face
}
else {
verticesPerFlight = (radialSegments + 1) + 6 // Part of shaft (outer) plus each thread comprises 3 faces with 2 verticies per face
}
const extraOffsetToOuterShaftFirstVertex = 6
const extraOffsetToInnerShaftFirstVertex = (radialSegments + 1) + 6
for (let i = 1; i <= tubularSegments; i++) {
for (let k = 0; k < threadStarts; k++) {
// Generate the indices for the thread
for (let j = 0; j < 3; j++) {
let l = k * verticesPerFlight + j * 2 + 1
const a = verticesPerFlight * threadStarts * ( i - 1 ) + ( l - 1 );
const b = verticesPerFlight * threadStarts * i + ( l - 1 );
const c = verticesPerFlight * threadStarts * i + l;
const d = verticesPerFlight * threadStarts * ( i - 1 ) + l;
indices.push( a, b, c );
indices.push( c, d, a );
}
// Generate the indices for the outer shaft
for ( let j = 1; j <= radialSegments; j++) {
let l = extraOffsetToOuterShaftFirstVertex + k * verticesPerFlight + j
const a = verticesPerFlight * threadStarts * ( i - 1 ) + ( l - 1 );
const b = verticesPerFlight * threadStarts * i + ( l - 1 );
const c = verticesPerFlight * threadStarts * i + l;
const d = verticesPerFlight * threadStarts * ( i - 1 ) + l;
indices.push( a, b, c );
indices.push( c, d, a );
}
}
}
// Next create the triangles needed to cover the ends of the flights
if (renderInnerSurface) {
let l
for (let ii = 0; ii < 2; ii++) {
// Iterating through the two ends of the screw...
const i = tubularSegments + 1 + ii
// Select one of the two extra vertices located at the center of each end of the screw
const offsetToTubularSegment = verticesPerFlight * threadStarts * i
// Cap the ends of the flights
for (let k = 0; k < threadStarts; k++) {
const ss = offsetToTubularSegment + k * verticesPerFlight
for (let j = 0; j < 2; j++) {
const a = ss + 0
const b = ss + 1
const c = ss + 4
const d = ss + 5
if (ii==0) {
//indices.push( a, b, c );
indices.push( a, b, c );
indices.push( c, d, a );
}
else {
//indices.push( c, b, a );
indices.push( c, b, a );
indices.push( a, d, c );
}
}
}
}
}
else {
// Next create the triangles needed to cover the ends of the screws
// For texture mapping, it might look better if we created an additional set of vericies so that we could then assign unique UV values to them
let b
let l
for (let ii = 0; ii < 2; ii++) {
const i = tubularSegments + 1 + ii
// Select one of the two extra vertices located at the center of each end of the screw
const a = (tubularSegments+3) * threadStarts * verticesPerFlight + ii
b = verticesPerFlight * threadStarts * i
for (let k = 0; k < threadStarts; k++) {
for (let j = 0; j < 2; j ++ ) {
// Just need to hit the verticies at (radialSegments+1)+1 and (radialSegments+1)+3 to reach the top of the thread
l = k * verticesPerFlight + j * 2 + 1
const c = verticesPerFlight * threadStarts * i + l;
if (ii==0) {
indices.push( a, b, c );
}
else {
indices.push( c, b, a );
}
b = c
}
for ( let j = 0; j <= radialSegments; j++) {
l = 6 + k * verticesPerFlight + j
const c = verticesPerFlight * threadStarts * i + l;
if (ii==0) {
indices.push( a, b, c );
}
else {
indices.push( c, b, a );
}
b = c
}
}
const c = verticesPerFlight * threadStarts * i
if (ii==0) {
indices.push( a, b, c );
}
else {
indices.push( c, b, a );
}
}
}
}
}
}
export { ScrewGeometry };