-
Notifications
You must be signed in to change notification settings - Fork 23
/
Ship3D.js
764 lines (543 loc) · 20.4 KB
/
Ship3D.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
//@EliasHasle
/*
Draft for new version. More modularized, and interacts with a ship state.
Uses an additional coordinate system for motions.
The position.xy and rotation.z of the Ship3D object plane the ship in the 3D world.
(Not geographically)
position.z is the (negative) draft.
fluctCont is a "fluctuations container" to be used for dynamically
changing motions like heave, pitch, roll.
cmContainer centers the motion on the center of gravity.
normalizer nulls out the center of gravity height before the draft is applied.
THREE.js Object3D constructed from Vessel.js Ship object.
There are some serious limitations to this:
1. null values encountered are assumed to be either at the top or bottom of the given station.
2. The end caps and bulkheads are sometimes corrected with zeros where they should perhaps have been clipped because of null values.
TODO: Use calculated draft for position.z, and place the ship model in a motion container centered at the calculated metacenter.
*/
//var hMat; //global for debugging
import * as THREE from "../../../examples/3D_engine/three_r126.js";
import { STLLoader } from "../../../examples/3D_engine/STLLoader.js";
import { f as mathFunctions } from "../vessel.js";
const { linearFromArrays, bisectionSearch } = { ...mathFunctions };
class Ship3D extends THREE.Group {
constructor( ship, { shipState, stlPath, deckOpacity = 0.2, objectOpacity = 0.5 } ) {
super();
this.normalizer = new THREE.Group();
this.fluctCont = new THREE.Group();
this.fluctCont.rotation.order = "ZYX"; //right?
this.cmContainer = new THREE.Group();
this.fluctCont.add( this.cmContainer );
this.normalizer.add( this.fluctCont );
this.add( this.normalizer );
this.objectOpacity = objectOpacity;
this.ship = ship;
this.shipState = shipState || ship.designState.clone();
let hull = ship.structure.hull;
let LOA = hull.attributes.LOA;
let BOA = hull.attributes.BOA;
let Depth = hull.attributes.Depth;
//console.log("LOA:%.1f, BOA:%.1f, Depth:%.1f",LOA,BOA,Depth);
let { w: { cg, mass }, T, GMt, GMl } = ship.calculateStability( this.shipState );
this.cmContainer.position.set( - cg.x, - cg.y, - cg.z );
this.normalizer.position.z = cg.z;
this.position.z = - T;
let designDraft = ship.designState.calculationParameters.Draft_design;
this.hull3D = new Hull3D( hull, designDraft );
this.cmContainer.add( this.hull3D );
//DEBUG, to show only hull:
//return;
let stations = hull.halfBreadths.stations;
//Decks:
var decks = new THREE.Group();
let deckMat = new THREE.MeshPhongMaterial( { color: 0xcccccc/*this.randomColor()*/, transparent: true, opacity: deckOpacity, side: THREE.DoubleSide } );
//deckGeom.translate(0,0,-0.5);
let ds = ship.structure.decks;
//let dk = Object.keys(ds);
let stss = stations.map( st => LOA * st ); //use scaled stations for now
//console.log(dk);
//for (let i = 0; i < dk.length; i++) {
for ( let dk in ds ) {
//let d = ds[dk[i]]; //deck in ship structure
let d = ds[ dk ];
//Will eventually use BoxBufferGeometry, but that is harder, because vertices are duplicated in the face planes.
let deckGeom = new THREE.PlaneBufferGeometry( 1, 1, stss.length, 1 );//new THREE.BoxBufferGeometry(1,1,1,sts.length,1,1);
//console.log("d.zFloor=%.1f", d.zFloor); //DEBUG
let zHigh = d.zFloor;
let zLow = d.zFloor - d.thickness;
let wlHigh = hull.getWaterline( zHigh );
let wlLow = hull.getWaterline( zLow );
let pos = deckGeom.getAttribute( "position" );
let pa = pos.array;
for ( let j = 0; j < stss.length + 1; j ++ ) {
//This was totally wrong, and still would benefit from
//not mapping directly to stations, as shorter decks will
//Get zero-width sections
let x = stss[ j ];//d.xAft+(j/stss.length)*(d.xFwd-d.xAft);
if ( isNaN( x ) ) x = stss[ j - 1 ];
x = Math.max( d.xAft, Math.min( d.xFwd, x ) );
let y1 = linearFromArrays( stss, wlHigh, x );
let y2 = linearFromArrays( stss, wlLow, x );
let y = Math.min( 0.5 * d.breadth, y1, y2 );
pa[ 3 * j ] = x;
pa[ 3 * j + 1 ] = y;
pa[ 3 * ( stss.length + 1 ) + 3 * j ] = x;
pa[ 3 * ( stss.length + 1 ) + 3 * j + 1 ] = - y; //test
}
pos.needsUpdate = true;
//DEBUG
//console.log("d.xFwd=%.1f, d.xAft=%.1f, 0.5*d.breadth=%.1f", d.xFwd, d.xAft, 0.5*d.breadth);
//console.log(pa);
let mat = deckMat;
if ( d.style ) {
mat = new THREE.MeshPhongMaterial( { color: typeof d.style.color !== "undefined" ? d.style.color : 0xcccccc, transparent: true, opacity: typeof d.style.opacity !== "undefined" ? d.style.opacity : deckOpacity, side: THREE.DoubleSide } );
}
let deck = new THREE.Mesh( deckGeom, mat );
deck.name = dk;//[i];
// TODO:
// The try verification is used to verify if the group affiliation was inserted in the JSON structure,
// the affiliation must be decided in the future if it will be incorporate into the main structure of the group
// or if there is a better approach to classify it.
// @ferrari212
try {
deck.group = d.affiliations.group;
} catch ( error ) {
console.warn( "Group tag were introduced to deck object" );
console.warn( error );
}
deck.position.z = d.zFloor;
//deck.scale.set(d.xFwd-d.xAft, d.breadth, d.thickness);
//deck.position.set(0.5*(d.xFwd+d.xAft), 0, d.zFloor);
decks.add( deck );
}
this.decks = decks;
this.cmContainer.add( decks );
//Bulkheads:
var bulkheads = new THREE.Group();
// Individually trimmed geometries like the decks @ferrari212
let bhMat = new THREE.MeshPhongMaterial( { color: 0xcccccc/*this.randomColor()*/, transparent: true, opacity: deckOpacity, side: THREE.DoubleSide } );
let bhs = ship.structure.bulkheads;
let maxWl = Math.max( ...hull.halfBreadths.waterlines ) * Depth;
//let bhk = Object.keys(bhs);
//for (let i = 0; i < bhk.length; i++) {
for ( let bhk in bhs ) {
let bh = bhs[ bhk ];//bhs[bhk[i]];
let mat = bhMat;
let station = hull.getStation( bh.xAft );
if ( bh.style ) {
mat = new THREE.MeshPhongMaterial( { color: typeof bh.style.color !== "undefined" ? bh.style.color : 0xcccccc, transparent: true, opacity: typeof bh.style.opacity !== "undefined" ? bh.style.opacity : deckOpacity, side: THREE.DoubleSide } );
}
let bulkheadGeom = new THREE.PlaneBufferGeometry( maxWl, BOA, station.length - 1, 1 );
let pos = bulkheadGeom.getAttribute( "position" );
let pa = pos.array;
for ( let i = 0; i < station.length; i ++ ) {
// Check height in order to trim the bulkhead in the deck
if ( pa[ 3 * i ] < Depth - maxWl / 2 ) {
pa[ 3 * i + 1 ] = station[ i ];
pa[ 3 * station.length + 3 * i + 1 ] = - station[ i ];
} else {
pa[ 3 * i + 1 ] = pa[ 3 * station.length + 3 * i + 1 ] = 0;
}
}
pos.needsUpdate = true;
let bulkhead = new THREE.Mesh( bulkheadGeom, mat );
bulkhead.name = bhk;//[i];
// TODO:
// The try verification is used to verify if the group affiliation was inserted in the JSON structure,
// the affiliation must be decided in the future if it will be incorporate into the main structure of the group
// or if there is a better approach to classify it.
// @ferrari212
try {
bulkhead.group = bh.affiliations.group;
} catch ( error ) {
console.warn( "Group tag were introduced to bulkhead object" );
console.warn( error );
}
bulkhead.rotation.y = - Math.PI / 2;
bulkhead.position.set( bh.xAft, 0, maxWl / 2 );
bulkheads.add( bulkhead );
}
this.bulkheads = bulkheads;
this.cmContainer.add( bulkheads );
//Objects
this.materials = {};
this.stlPath = stlPath;
let stlManager = new THREE.LoadingManager();
this.stlLoader = new STLLoader( stlManager );
/*stlManager.onLoad = function() {
createGUI(materials, deckMat);
}*/
this.blocks = new THREE.Group();
this.cmContainer.add( this.blocks );
//Default placeholder geometry
this.boxGeom = new THREE.BoxBufferGeometry( 1, 1, 1 );
this.boxGeom.translate( 0, 0, 0.5 );
let objects = Object.values( ship.derivedObjects );
for ( let i = 0; i < objects.length; i ++ ) {
this.addObject( objects[ i ] );
}
//console.log("Reached end of Ship3D constructor.");
}
get draft() {
return - this.position.z;
}
set draft( value ) {
this.position.z = - value;
}
get sway() {
return this.fluctCont.position.y;
}
set sway( value ) {
this.fluctCont.position.y = value;
}
get heave() {
return this.fluctCont.position.z;
}
set heave( value ) {
this.fluctCont.position.z = value;
//this.shipState.motion.heave = value;
}
get yaw() {
return this.fluctCont.rotation.z;
}
set yaw( value ) {
this.fluctCont.rotation.z = value;
//this.shipState.motion.yaw = value;
}
get pitch() {
return this.fluctCont.rotation.y;
}
set pitch( value ) {
this.fluctCont.rotation.y = value;
//this.shipState.motion.pitch = value;
}
get roll() {
return this.fluctCont.rotation.x;
}
set roll( value ) {
this.fluctCont.rotation.x = value;
//this.shipState.motion.roll = value;
}
addObject( object ) {
let mat;
if ( typeof object.style.color !== "undefined" || typeof object.style.opacity !== "undefined" ) {
let color = typeof object.style.color !== "undefined" ? object.style.color : this.randomColor();
let opacity = typeof object.style.opacity !== "undefined" ? object.style.opacity : this.objectOpacity;
mat = new THREE.MeshPhongMaterial( { color, transparent: true, opacity } );
} else {
let name = this.stripName( object.id );
if ( this.materials[ name ] !== undefined ) {
mat = this.materials[ name ];
} else {
mat = new THREE.MeshPhongMaterial( { color: this.randomColor(), transparent: true, opacity: this.objectOpacity } );
this.materials[ name ] = mat;
}
}
let bo = object.baseObject;
//Position
let s = this.ship.designState.getObjectState( object );
let x = s.xCentre;
let y = s.yCentre;
let z = s.zBase;
//Small position jitter to avoid z-fighting
let n = 0.01 * ( 2 * Math.random() - 1 );
x += n;
y += n;
z += n;
//Scale
let d = bo.boxDimensions;
if ( bo.file3D ) {
let self = this;
this.stlLoader.load(
this.stlPath + "/" + bo.file3D,
function onLoad( geometry ) {
//Normalize:
geometry.computeBoundingBox();
let b = geometry.boundingBox;
geometry.translate( - b.min.x, - b.min.y, - b.min.z );
geometry.scale( 1 / ( b.max.x - b.min.x ),
1 / ( b.max.y - b.min.y ),
1 / ( b.max.z - b.min.z ) );
//Align with the same coordinate system as placeholder blocks:
geometry.translate( - 0.5, - 0.5, 0 );
let m = new THREE.Mesh( geometry, mat );
m.position.set( x, y, z );
m.scale.set( d.length, d.breadth, d.height );
m.name = object.id;
m.group = bo.affiliations.group != undefined ? bo.affiliations.group : undefined;
self.blocks.add( m );
},
undefined,
function onError() {
console.warn( "Error loading STL file " + bo.file3D + ". Falling back on placeholder." );
let m = new THREE.Mesh( this.boxGeom, mat );
m.position.set( x, y, z );
m.scale.set( d.length, d.breadth, d.height );
m.name = object.id;
m.group = bo.affiliations.group != undefined ? bo.affiliations.group : undefined;
this.blocks.add( m );
}
);
} else {
//Placeholder:
let m = new THREE.Mesh( this.boxGeom, mat );
m.position.set( x, y, z );
m.scale.set( d.length, d.breadth, d.height );
m.name = object.id;
m.group = bo.affiliations.group != undefined ? bo.affiliations.group : undefined;
this.blocks.add( m );
}
}
stripName( s ) {
s = s.replace( /[0-9]/g, "" );
s = s.trim();
return s;
}
randomColor() {
let r = Math.round( Math.random() * 0xff );
let g = Math.round( Math.random() * 0xff );
let b = Math.round( Math.random() * 0xff );
return ( ( r << 16 ) | ( g << 8 ) | b );
}
}
class Hull3D extends THREE.Group {
constructor( hull, design_draft ) {
super();
this.hull = hull;
this.group = "Hull3D";
this.name = "Hull3D";
this.design_draft = design_draft !== undefined ? design_draft : 0.5 * hull.attributes.Depth;
this.upperColor = typeof hull.style.upperColor !== "undefined" ? hull.style.upperColor : 0x33aa33;
this.lowerColor = typeof hull.style.lowerColor !== "undefined" ? hull.style.lowerColor : 0xaa3333;
this.opacity = typeof hull.style.opacity !== "undefined" ? hull.style.opacity : 0.5;
this.update();
}
addStation( p ) {
const hb = this.hull.halfBreadths;
const { index, mu } = bisectionSearch( hb.stations, p );
hb.stations.splice( index, 0, p );
for ( let i = 0; i < hb.waterlines.length; i ++ ) {
hb.table[ i ].splice( index, 0, 0 );
}
this.update();
}
addWaterline( p ) {
const hb = this.hull.halfBreadths;
const { index, mu } = bisectionSearch( hb.waterlines, p );
hb.waterlines.splice( index, 0, p );
hb.table.splice( index, 0, new Array( hb.stations.length ).fill( 0 ) );
this.update();
}
update() {
const hull = this.hull;
const upperColor = this.upperColor;
const lowerColor = this.lowerColor;
const design_draft = this.design_draft;
const opacity = this.opacity;
let LOA = hull.attributes.LOA;
let BOA = hull.attributes.BOA;
let Depth = hull.attributes.Depth;
//None of these are changed during correction of the geometry.
let stations = hull.halfBreadths.stations;
let waterlines = hull.halfBreadths.waterlines;
let table = hull.halfBreadths.table;
if ( this.hGeom ) this.hGeom.dispose();
this.hGeom = new HullSideGeometry( stations, waterlines, table );
let N = stations.length;
let M = waterlines.length;
//Bow cap:
let bowPlaneOffsets = hull.getStation( LOA ).map( str => str / ( 0.5 * BOA ) ); //normalized
if ( this.bowCapG ) this.bowCapG.dispose();
this.bowCapG = new THREE.PlaneBufferGeometry( undefined, undefined, 1, M - 1 );
let pos = this.bowCapG.getAttribute( "position" );
let pa = pos.array;
//constant x-offset yz plane
for ( let j = 0; j < M; j ++ ) {
pa[ 3 * ( 2 * j ) ] = 1;
pa[ 3 * ( 2 * j ) + 1 ] = bowPlaneOffsets[ j ];
pa[ 3 * ( 2 * j ) + 2 ] = waterlines[ j ];
pa[ 3 * ( 2 * j + 1 ) ] = 1;
pa[ 3 * ( 2 * j + 1 ) + 1 ] = - bowPlaneOffsets[ j ];
pa[ 3 * ( 2 * j + 1 ) + 2 ] = waterlines[ j ];
}
pos.needsUpdate = true;
//Aft cap:
let aftPlaneOffsets = hull.getStation( 0 ).map( str => str / ( 0.5 * BOA ) ); //normalized
if ( this.aftCapG ) this.aftCapG.dispose();
this.aftCapG = new THREE.PlaneBufferGeometry( undefined, undefined, 1, M - 1 );
pos = this.aftCapG.getAttribute( "position" );
pa = pos.array;
//constant x-offset yz plane
for ( let j = 0; j < M; j ++ ) {
pa[ 3 * ( 2 * j ) ] = 0;
pa[ 3 * ( 2 * j ) + 1 ] = - aftPlaneOffsets[ j ];
pa[ 3 * ( 2 * j ) + 2 ] = waterlines[ j ];
pa[ 3 * ( 2 * j + 1 ) ] = 0;
pa[ 3 * ( 2 * j + 1 ) + 1 ] = aftPlaneOffsets[ j ];
pa[ 3 * ( 2 * j + 1 ) + 2 ] = waterlines[ j ];
}
pos.needsUpdate = true;
//Bottom cap:
let bottomPlaneOffsets = hull.getWaterline( 0 ).map( hw => hw / ( 0.5 * BOA ) ); //normalized
if ( this.bottomCapG ) this.bottomCapG.dispose();
this.bottomCapG = new THREE.PlaneBufferGeometry( undefined, undefined, N - 1, 1 );
pos = this.bottomCapG.getAttribute( "position" );
pa = pos.array;
//constant z-offset xy plane
for ( let i = 0; i < N; i ++ ) {
pa[ 3 * ( i ) ] = stations[ i ];
pa[ 3 * ( i ) + 1 ] = - bottomPlaneOffsets[ i ];
pa[ 3 * ( i ) + 2 ] = 0;
pa[ 3 * ( N + i ) ] = stations[ i ];
pa[ 3 * ( N + i ) + 1 ] = bottomPlaneOffsets[ i ];
pa[ 3 * ( N + i ) + 2 ] = 0;
}
pos.needsUpdate = true;
//Hull material
if ( ! this.hMat ) {
let phong = THREE.ShaderLib.phong;
let commonDecl = "uniform float wlThreshold;uniform vec3 aboveWL; uniform vec3 belowWL;\nvarying float vZ;";
this.hMat = new THREE.ShaderMaterial( {
uniforms: THREE.UniformsUtils.merge( [ phong.uniforms, {
wlThreshold: new THREE.Uniform( 0.5 ),
aboveWL: new THREE.Uniform( new THREE.Color() ),
belowWL: new THREE.Uniform( new THREE.Color() )
} ] ),
vertexShader: commonDecl + phong.vertexShader.replace( "main() {", "main() {\nvZ = position.z;" ).replace( "#define PHONG", "" ),
fragmentShader: commonDecl + phong.fragmentShader.replace( "vec4 diffuseColor = vec4( diffuse, opacity );",
"vec4 diffuseColor = vec4( (vZ>wlThreshold)? aboveWL.rgb : belowWL.rgb, opacity );" ).replace( "#define PHONG", "" ),
side: THREE.DoubleSide,
lights: true,
transparent: true
} );
}
this.hMat.uniforms.wlThreshold.value = this.design_draft / Depth;
this.hMat.uniforms.aboveWL.value = new THREE.Color( upperColor );
this.hMat.uniforms.belowWL.value = new THREE.Color( lowerColor );
this.hMat.uniforms.opacity.value = opacity;
if ( this.port ) this.remove( this.port );
this.port = new THREE.Mesh( this.hGeom, this.hMat );
if ( this.starboard ) this.remove( this.starboard );
this.starboard = new THREE.Mesh( this.hGeom, this.hMat );
this.starboard.scale.y = - 1;
this.add( this.port, this.starboard );
//Caps:
if ( this.bowCap ) this.remove( this.bowCap );
this.bowCap = new THREE.Mesh( this.bowCapG, this.hMat );
if ( this.aftCap ) this.remove( this.aftCap );
this.aftCap = new THREE.Mesh( this.aftCapG, this.hMat );
if ( this.bottomCap ) this.remove( this.bottomCap );
this.bottomCap = new THREE.Mesh( this.bottomCapG, this.hMat );
this.add( this.bowCap, this.aftCap, this.bottomCap );
this.scale.set( LOA, 0.5 * BOA, Depth );
}
}
class HullSideGeometry extends THREE.PlaneBufferGeometry {
constructor( stations, waterlines, table ) {
const N = stations.length;
const M = waterlines.length;
super( undefined, undefined, N - 1, M - 1 );
this.stations = stations;
this.waterlines = waterlines;
this.table = table;
this.N = N;
this.M = M;
this.update();
}
update() {
let pos = this.getAttribute( "position" );
let pa = pos.array;
const N = this.N;
const M = this.M;
//loop1:
//zs
let c = 0;
//Iterate over waterlines
for ( let j = 0; j < M; j ++ ) {
//loop2:
//xs
//iterate over stations
for ( let i = 0; i < N; i ++ ) {
//if (table[j][i] === null) continue;// loop1;
pa[ c ] = this.stations[ i ]; //x
//DEBUG, OK. No attempts to read outside of table
/*if(typeof table[j] === "undefined") console.error("table[%d] is undefined", j);
else if (typeof table[j][i] === "undefined") console.error("table[%d][%d] is undefined", j, i);*/
//y
pa[ c + 1 ] = this.table[ j ][ i ]; //y
pa[ c + 2 ] = this.waterlines[ j ]; //z
c += 3;
}
}
//console.error("c-pa.length = %d", c-pa.length); //OK, sets all cells
//Get rid of nulls by merging their points with the closest non-null point in the same station:
/*I am joining some uvs too. Then an applied texture will be cropped, not distorted, where the hull is cropped.*/
let uv = this.getAttribute( "uv" );
let uva = uv.array;
//Iterate over stations
for ( let i = 0; i < N; i ++ ) {
let firstNumberJ;
let lastNumberJ;
//Iterate over waterlines
let j;
for ( j = 0; j < M; j ++ ) {
let y = this.table[ j ][ i ];
//If this condition is satisfied (number found),
//the loop will be quitted
//after the extra logic below:
if ( y !== null ) {
firstNumberJ = j;
lastNumberJ = j;
//copy vector for i,j to positions for all null cells below:
let c = firstNumberJ * N + i;
let x = pa[ 3 * c ];
let y = pa[ 3 * c + 1 ];
let z = pa[ 3 * c + 2 ];
let d = c;
while ( firstNumberJ > 0 ) {
firstNumberJ --;
d -= N;
pa[ 3 * d ] = x;
pa[ 3 * d + 1 ] = y;
pa[ 3 * d + 2 ] = z;
uva[ 2 * d ] = uva[ 2 * c ];
uva[ 2 * d + 1 ] = uva[ 2 * c + 1 ];
}
break;
}
//console.log("null encountered.");
}
//Continue up the hull (with same j counter), searching for upper number. This does not account for the existence of numbers above the first null encountered.
for ( ; j < M; j ++ ) {
let y = this.table[ j ][ i ];
if ( y === null ) {
//console.log("null encountered.");
break;
}
//else not null:
lastNumberJ = j;
}
//copy vector for i,j to positions for all null cells above:
let c = lastNumberJ * N + i;
let x = pa[ 3 * c ];
let y = pa[ 3 * c + 1 ];
let z = pa[ 3 * c + 2 ];
let d = c;
while ( lastNumberJ < M - 1 ) {
lastNumberJ ++;
d += N;
pa[ 3 * d ] = x;
pa[ 3 * d + 1 ] = y;
pa[ 3 * d + 2 ] = z;
uva[ 2 * d ] = uva[ 2 * c ];
uva[ 2 * d + 1 ] = uva[ 2 * c + 1 ];
}
//////////
}
//console.log(pa);
pos.needsUpdate = true;
uv.needsUpdate = true;
this.computeVertexNormals();
}
}
export { Ship3D, Hull3D, HullSideGeometry };