/
RectangleGeometry.js
920 lines (819 loc) · 38.1 KB
/
RectangleGeometry.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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/*global define*/
define([
'./BoundingSphere',
'./Cartesian2',
'./Cartesian3',
'./Cartographic',
'./Check',
'./ComponentDatatype',
'./defaultValue',
'./defined',
'./defineProperties',
'./DeveloperError',
'./Ellipsoid',
'./Geometry',
'./GeometryAttribute',
'./GeometryAttributes',
'./GeometryInstance',
'./GeometryPipeline',
'./IndexDatatype',
'./Math',
'./Matrix2',
'./Matrix3',
'./PolygonPipeline',
'./PrimitiveType',
'./Quaternion',
'./Rectangle',
'./RectangleGeometryLibrary',
'./VertexFormat'
], function(
BoundingSphere,
Cartesian2,
Cartesian3,
Cartographic,
Check,
ComponentDatatype,
defaultValue,
defined,
defineProperties,
DeveloperError,
Ellipsoid,
Geometry,
GeometryAttribute,
GeometryAttributes,
GeometryInstance,
GeometryPipeline,
IndexDatatype,
CesiumMath,
Matrix2,
Matrix3,
PolygonPipeline,
PrimitiveType,
Quaternion,
Rectangle,
RectangleGeometryLibrary,
VertexFormat) {
'use strict';
var positionScratch = new Cartesian3();
var normalScratch = new Cartesian3();
var tangentScratch = new Cartesian3();
var bitangentScratch = new Cartesian3();
var rectangleScratch = new Rectangle();
var stScratch = new Cartesian2();
var bottomBoundingSphere = new BoundingSphere();
var topBoundingSphere = new BoundingSphere();
function createAttributes(vertexFormat, attributes) {
var geo = new Geometry({
attributes : new GeometryAttributes(),
primitiveType : PrimitiveType.TRIANGLES
});
geo.attributes.position = new GeometryAttribute({
componentDatatype : ComponentDatatype.DOUBLE,
componentsPerAttribute : 3,
values : attributes.positions
});
if (vertexFormat.normal) {
geo.attributes.normal = new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 3,
values : attributes.normals
});
}
if (vertexFormat.tangent) {
geo.attributes.tangent = new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 3,
values : attributes.tangents
});
}
if (vertexFormat.bitangent) {
geo.attributes.bitangent = new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 3,
values : attributes.bitangents
});
}
return geo;
}
function calculateAttributes(positions, vertexFormat, ellipsoid, tangentRotationMatrix) {
var length = positions.length;
var normals = (vertexFormat.normal) ? new Float32Array(length) : undefined;
var tangents = (vertexFormat.tangent) ? new Float32Array(length) : undefined;
var bitangents = (vertexFormat.bitangent) ? new Float32Array(length) : undefined;
var attrIndex = 0;
var bitangent = bitangentScratch;
var tangent = tangentScratch;
var normal = normalScratch;
for (var i = 0; i < length; i += 3) {
var p = Cartesian3.fromArray(positions, i, positionScratch);
var attrIndex1 = attrIndex + 1;
var attrIndex2 = attrIndex + 2;
if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
normal = ellipsoid.geodeticSurfaceNormal(p, normal);
if (vertexFormat.tangent || vertexFormat.bitangent) {
Cartesian3.cross(Cartesian3.UNIT_Z, normal, tangent);
Matrix3.multiplyByVector(tangentRotationMatrix, tangent, tangent);
Cartesian3.normalize(tangent, tangent);
if (vertexFormat.bitangent) {
Cartesian3.normalize(Cartesian3.cross(normal, tangent, bitangent), bitangent);
}
}
if (vertexFormat.normal) {
normals[attrIndex] = normal.x;
normals[attrIndex1] = normal.y;
normals[attrIndex2] = normal.z;
}
if (vertexFormat.tangent) {
tangents[attrIndex] = tangent.x;
tangents[attrIndex1] = tangent.y;
tangents[attrIndex2] = tangent.z;
}
if (vertexFormat.bitangent) {
bitangents[attrIndex] = bitangent.x;
bitangents[attrIndex1] = bitangent.y;
bitangents[attrIndex2] = bitangent.z;
}
}
attrIndex += 3;
}
return createAttributes(vertexFormat, {
positions : positions,
normals : normals,
tangents : tangents,
bitangents : bitangents
});
}
var v1Scratch = new Cartesian3();
var v2Scratch = new Cartesian3();
function calculateAttributesWall(positions, vertexFormat, ellipsoid) {
var length = positions.length;
var normals = (vertexFormat.normal) ? new Float32Array(length) : undefined;
var tangents = (vertexFormat.tangent) ? new Float32Array(length) : undefined;
var bitangents = (vertexFormat.bitangent) ? new Float32Array(length) : undefined;
var normalIndex = 0;
var tangentIndex = 0;
var bitangentIndex = 0;
var recomputeNormal = true;
var bitangent = bitangentScratch;
var tangent = tangentScratch;
var normal = normalScratch;
for (var i = 0; i < length; i += 6) {
var p = Cartesian3.fromArray(positions, i, positionScratch);
if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
var p1 = Cartesian3.fromArray(positions, (i + 6) % length, v1Scratch);
if (recomputeNormal) {
var p2 = Cartesian3.fromArray(positions, (i + 3) % length, v2Scratch);
Cartesian3.subtract(p1, p, p1);
Cartesian3.subtract(p2, p, p2);
normal = Cartesian3.normalize(Cartesian3.cross(p2, p1, normal), normal);
recomputeNormal = false;
}
if (Cartesian3.equalsEpsilon(p1, p, CesiumMath.EPSILON10)) { // if we've reached a corner
recomputeNormal = true;
}
if (vertexFormat.tangent || vertexFormat.bitangent) {
bitangent = ellipsoid.geodeticSurfaceNormal(p, bitangent);
if (vertexFormat.tangent) {
tangent = Cartesian3.normalize(Cartesian3.cross(bitangent, normal, tangent), tangent);
}
}
if (vertexFormat.normal) {
normals[normalIndex++] = normal.x;
normals[normalIndex++] = normal.y;
normals[normalIndex++] = normal.z;
normals[normalIndex++] = normal.x;
normals[normalIndex++] = normal.y;
normals[normalIndex++] = normal.z;
}
if (vertexFormat.tangent) {
tangents[tangentIndex++] = tangent.x;
tangents[tangentIndex++] = tangent.y;
tangents[tangentIndex++] = tangent.z;
tangents[tangentIndex++] = tangent.x;
tangents[tangentIndex++] = tangent.y;
tangents[tangentIndex++] = tangent.z;
}
if (vertexFormat.bitangent) {
bitangents[bitangentIndex++] = bitangent.x;
bitangents[bitangentIndex++] = bitangent.y;
bitangents[bitangentIndex++] = bitangent.z;
bitangents[bitangentIndex++] = bitangent.x;
bitangents[bitangentIndex++] = bitangent.y;
bitangents[bitangentIndex++] = bitangent.z;
}
}
}
return createAttributes(vertexFormat, {
positions : positions,
normals : normals,
tangents : tangents,
bitangents : bitangents
});
}
function constructRectangle(options) {
var vertexFormat = options.vertexFormat;
var ellipsoid = options.ellipsoid;
var size = options.size;
var height = options.height;
var width = options.width;
var positions = (vertexFormat.position) ? new Float64Array(size * 3) : undefined;
var textureCoordinates = (vertexFormat.st) ? new Float32Array(size * 2) : undefined;
var posIndex = 0;
var stIndex = 0;
var position = positionScratch;
var st = stScratch;
var minX = Number.MAX_VALUE;
var minY = Number.MAX_VALUE;
var maxX = -Number.MAX_VALUE;
var maxY = -Number.MAX_VALUE;
for (var row = 0; row < height; ++row) {
for (var col = 0; col < width; ++col) {
RectangleGeometryLibrary.computePosition(options, row, col, position, st);
positions[posIndex++] = position.x;
positions[posIndex++] = position.y;
positions[posIndex++] = position.z;
if (vertexFormat.st) {
textureCoordinates[stIndex++] = st.x;
textureCoordinates[stIndex++] = st.y;
minX = Math.min(minX, st.x);
minY = Math.min(minY, st.y);
maxX = Math.max(maxX, st.x);
maxY = Math.max(maxY, st.y);
}
}
}
if (vertexFormat.st && (minX < 0.0 || minY < 0.0 || maxX > 1.0 || maxY > 1.0)) {
for (var k = 0; k < textureCoordinates.length; k += 2) {
textureCoordinates[k] = (textureCoordinates[k] - minX) / (maxX - minX);
textureCoordinates[k + 1] = (textureCoordinates[k + 1] - minY) / (maxY - minY);
}
}
var geo = calculateAttributes(positions, vertexFormat, ellipsoid, options.tangentRotationMatrix);
var indicesSize = 6 * (width - 1) * (height - 1);
var indices = IndexDatatype.createTypedArray(size, indicesSize);
var index = 0;
var indicesIndex = 0;
for (var i = 0; i < height - 1; ++i) {
for (var j = 0; j < width - 1; ++j) {
var upperLeft = index;
var lowerLeft = upperLeft + width;
var lowerRight = lowerLeft + 1;
var upperRight = upperLeft + 1;
indices[indicesIndex++] = upperLeft;
indices[indicesIndex++] = lowerLeft;
indices[indicesIndex++] = upperRight;
indices[indicesIndex++] = upperRight;
indices[indicesIndex++] = lowerLeft;
indices[indicesIndex++] = lowerRight;
++index;
}
++index;
}
geo.indices = indices;
if (vertexFormat.st) {
geo.attributes.st = new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 2,
values : textureCoordinates
});
}
return geo;
}
function addWallPositions(wallPositions, posIndex, i, topPositions, bottomPositions) {
wallPositions[posIndex++] = topPositions[i];
wallPositions[posIndex++] = topPositions[i + 1];
wallPositions[posIndex++] = topPositions[i + 2];
wallPositions[posIndex++] = bottomPositions[i];
wallPositions[posIndex++] = bottomPositions[i + 1];
wallPositions[posIndex++] = bottomPositions[i + 2];
return wallPositions;
}
function addWallTextureCoordinates(wallTextures, stIndex, i, st) {
wallTextures[stIndex++] = st[i];
wallTextures[stIndex++] = st[i + 1];
wallTextures[stIndex++] = st[i];
wallTextures[stIndex++] = st[i + 1];
return wallTextures;
}
var scratchVertexFormat = new VertexFormat();
function constructExtrudedRectangle(options) {
var shadowVolume = options.shadowVolume;
var vertexFormat = options.vertexFormat;
var surfaceHeight = options.surfaceHeight;
var extrudedHeight = options.extrudedHeight;
var minHeight = Math.min(extrudedHeight, surfaceHeight);
var maxHeight = Math.max(extrudedHeight, surfaceHeight);
var height = options.height;
var width = options.width;
var ellipsoid = options.ellipsoid;
var i;
if (shadowVolume) {
options.vertexFormat = VertexFormat.clone(vertexFormat, scratchVertexFormat);
options.vertexFormat.normal = true;
}
var topBottomGeo = constructRectangle(options);
if (CesiumMath.equalsEpsilon(minHeight, maxHeight, CesiumMath.EPSILON10)) {
return topBottomGeo;
}
var topPositions = PolygonPipeline.scaleToGeodeticHeight(topBottomGeo.attributes.position.values, maxHeight, ellipsoid, false);
topPositions = new Float64Array(topPositions);
var length = topPositions.length;
var newLength = length*2;
var positions = new Float64Array(newLength);
positions.set(topPositions);
var bottomPositions = PolygonPipeline.scaleToGeodeticHeight(topBottomGeo.attributes.position.values, minHeight, ellipsoid);
positions.set(bottomPositions, length);
topBottomGeo.attributes.position.values = positions;
var normals = (vertexFormat.normal) ? new Float32Array(newLength) : undefined;
var tangents = (vertexFormat.tangent) ? new Float32Array(newLength) : undefined;
var bitangents = (vertexFormat.bitangent) ? new Float32Array(newLength) : undefined;
var textures = (vertexFormat.st) ? new Float32Array(newLength/3*2) : undefined;
var topSt;
var topNormals;
if (vertexFormat.normal) {
topNormals = topBottomGeo.attributes.normal.values;
normals.set(topNormals);
for (i = 0; i < length; i ++) {
topNormals[i] = -topNormals[i];
}
normals.set(topNormals, length);
topBottomGeo.attributes.normal.values = normals;
}
if (shadowVolume) {
topNormals = topBottomGeo.attributes.normal.values;
if (!vertexFormat.normal) {
topBottomGeo.attributes.normal = undefined;
}
var extrudeNormals = new Float32Array(newLength);
for (i = 0; i < length; i ++) {
topNormals[i] = -topNormals[i];
}
extrudeNormals.set(topNormals, length); //only get normals for bottom layer that's going to be pushed down
topBottomGeo.attributes.extrudeDirection = new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 3,
values : extrudeNormals
});
}
if (vertexFormat.tangent) {
var topTangents = topBottomGeo.attributes.tangent.values;
tangents.set(topTangents);
for (i = 0; i < length; i ++) {
topTangents[i] = -topTangents[i];
}
tangents.set(topTangents, length);
topBottomGeo.attributes.tangent.values = tangents;
}
if (vertexFormat.bitangent) {
var topBitangents = topBottomGeo.attributes.bitangent.values;
bitangents.set(topBitangents);
bitangents.set(topBitangents, length);
topBottomGeo.attributes.bitangent.values = bitangents;
}
if (vertexFormat.st) {
topSt = topBottomGeo.attributes.st.values;
textures.set(topSt);
textures.set(topSt, length/3*2);
topBottomGeo.attributes.st.values = textures;
}
var indices = topBottomGeo.indices;
var indicesLength = indices.length;
var posLength = length / 3;
var newIndices = IndexDatatype.createTypedArray(newLength/3, indicesLength*2);
newIndices.set(indices);
for (i = 0; i < indicesLength; i += 3) {
newIndices[i + indicesLength] = indices[i + 2] + posLength;
newIndices[i + 1 + indicesLength] = indices[i + 1] + posLength;
newIndices[i + 2 + indicesLength] = indices[i] + posLength;
}
topBottomGeo.indices = newIndices;
var perimeterPositions = 2 * width + 2 * height - 4;
var wallCount = (perimeterPositions + 4) * 2;
var wallPositions = new Float64Array(wallCount * 3);
var wallExtrudeNormals = shadowVolume ? new Float32Array(wallCount * 3) : undefined;
var wallTextures = (vertexFormat.st) ? new Float32Array(wallCount * 2) : undefined;
var posIndex = 0;
var stIndex = 0;
var extrudeNormalIndex = 0;
var area = width * height;
var threeI;
for (i = 0; i < area; i+=width) {
threeI = i * 3;
wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
posIndex += 6;
if (vertexFormat.st) {
wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i*2, topSt);
stIndex += 4;
}
if (shadowVolume) {
extrudeNormalIndex += 3;
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
}
}
for (i = area-width; i < area; i++) {
threeI = i * 3;
wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
posIndex += 6;
if (vertexFormat.st) {
wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i*2, topSt);
stIndex += 4;
}
if (shadowVolume) {
extrudeNormalIndex += 3;
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
}
}
for (i = area-1; i > 0; i-=width) {
threeI = i * 3;
wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
posIndex += 6;
if (vertexFormat.st) {
wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i*2, topSt);
stIndex += 4;
}
if (shadowVolume) {
extrudeNormalIndex += 3;
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
}
}
for (i = width-1; i >= 0; i--) {
threeI = i * 3;
wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
posIndex += 6;
if (vertexFormat.st) {
wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i*2, topSt);
stIndex += 4;
}
if (shadowVolume) {
extrudeNormalIndex += 3;
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
}
}
var geo = calculateAttributesWall(wallPositions, vertexFormat, ellipsoid);
if (vertexFormat.st) {
geo.attributes.st = new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 2,
values : wallTextures
});
}
if (shadowVolume) {
geo.attributes.extrudeDirection = new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 3,
values : wallExtrudeNormals
});
}
var wallIndices = IndexDatatype.createTypedArray(wallCount, perimeterPositions * 6);
var upperLeft;
var lowerLeft;
var lowerRight;
var upperRight;
length = wallPositions.length / 3;
var index = 0;
for (i = 0; i < length - 1; i+=2) {
upperLeft = i;
upperRight = (upperLeft + 2) % length;
var p1 = Cartesian3.fromArray(wallPositions, upperLeft * 3, v1Scratch);
var p2 = Cartesian3.fromArray(wallPositions, upperRight * 3, v2Scratch);
if (Cartesian3.equalsEpsilon(p1, p2, CesiumMath.EPSILON10)) {
continue;
}
lowerLeft = (upperLeft + 1) % length;
lowerRight = (lowerLeft + 2) % length;
wallIndices[index++] = upperLeft;
wallIndices[index++] = lowerLeft;
wallIndices[index++] = upperRight;
wallIndices[index++] = upperRight;
wallIndices[index++] = lowerLeft;
wallIndices[index++] = lowerRight;
}
geo.indices = wallIndices;
geo = GeometryPipeline.combineInstances([
new GeometryInstance({
geometry : topBottomGeo
}),
new GeometryInstance({
geometry : geo
})
]);
return geo[0];
}
var scratchRotationMatrix = new Matrix3();
var scratchCartesian3 = new Cartesian3();
var scratchQuaternion = new Quaternion();
var scratchRectanglePoints = [new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3()];
var scratchCartographicPoints = [new Cartographic(), new Cartographic(), new Cartographic(), new Cartographic()];
function computeRectangle(rectangle, ellipsoid, rotation) {
if (rotation === 0.0) {
return Rectangle.clone(rectangle);
}
Rectangle.northeast(rectangle, scratchCartographicPoints[0]);
Rectangle.northwest(rectangle, scratchCartographicPoints[1]);
Rectangle.southeast(rectangle, scratchCartographicPoints[2]);
Rectangle.southwest(rectangle, scratchCartographicPoints[3]);
ellipsoid.cartographicArrayToCartesianArray(scratchCartographicPoints, scratchRectanglePoints);
var surfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(Rectangle.center(rectangle, scratchCartesian3));
Quaternion.fromAxisAngle(surfaceNormal, rotation, scratchQuaternion);
Matrix3.fromQuaternion(scratchQuaternion, scratchRotationMatrix);
for (var i = 0; i < 4; ++i) {
// Apply the rotation
Matrix3.multiplyByVector(scratchRotationMatrix, scratchRectanglePoints[i], scratchRectanglePoints[i]);
}
ellipsoid.cartesianArrayToCartographicArray(scratchRectanglePoints, scratchCartographicPoints);
return Rectangle.fromCartographicArray(scratchCartographicPoints);
}
/**
* A description of a cartographic rectangle on an ellipsoid centered at the origin. Rectangle geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
*
* @alias RectangleGeometry
* @constructor
*
* @param {Object} options Object with the following properties:
* @param {Rectangle} options.rectangle A cartographic rectangle with north, south, east and west properties in radians.
* @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
* @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle lies.
* @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
* @param {Number} [options.height=0.0] The distance in meters between the rectangle and the ellipsoid surface.
* @param {Number} [options.rotation=0.0] The rotation of the rectangle, in radians. A positive rotation is counter-clockwise.
* @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
* @param {Number} [options.extrudedHeight] The distance in meters between the rectangle's extruded face and the ellipsoid surface.
* @param {Boolean} [options.closeTop=true] Specifies whether the rectangle has a top cover when extruded.
* @param {Boolean} [options.closeBottom=true] Specifies whether the rectangle has a bottom cover when extruded.
*
* @exception {DeveloperError} <code>options.rectangle.north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
* @exception {DeveloperError} <code>options.rectangle.south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
* @exception {DeveloperError} <code>options.rectangle.east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
* @exception {DeveloperError} <code>options.rectangle.west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
* @exception {DeveloperError} <code>options.rectangle.north</code> must be greater than <code>options.rectangle.south</code>.
*
* @see RectangleGeometry#createGeometry
*
* @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Rectangle.html|Cesium Sandcastle Rectangle Demo}
*
* @example
* // 1. create an rectangle
* var rectangle = new Cesium.RectangleGeometry({
* ellipsoid : Cesium.Ellipsoid.WGS84,
* rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
* height : 10000.0
* });
* var geometry = Cesium.RectangleGeometry.createGeometry(rectangle);
*
* // 2. create an extruded rectangle without a top
* var rectangle = new Cesium.RectangleGeometry({
* ellipsoid : Cesium.Ellipsoid.WGS84,
* rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
* height : 10000.0,
* extrudedHeight: 300000,
* closeTop: false
* });
* var geometry = Cesium.RectangleGeometry.createGeometry(rectangle);
*/
function RectangleGeometry(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var rectangle = options.rectangle;
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object('rectangle', rectangle);
Rectangle.validate(rectangle);
if (rectangle.north < rectangle.south) {
throw new DeveloperError('options.rectangle.north must be greater than or equal to options.rectangle.south');
}
//>>includeEnd('debug');
var rotation = defaultValue(options.rotation, 0.0);
this._rectangle = rectangle;
this._granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
this._ellipsoid = Ellipsoid.clone(defaultValue(options.ellipsoid, Ellipsoid.WGS84));
this._surfaceHeight = defaultValue(options.height, 0.0);
this._rotation = rotation;
this._stRotation = defaultValue(options.stRotation, 0.0);
this._vertexFormat = VertexFormat.clone(defaultValue(options.vertexFormat, VertexFormat.DEFAULT));
this._extrudedHeight = defaultValue(options.extrudedHeight, 0.0);
this._extrude = defined(options.extrudedHeight);
this._closeTop = defaultValue(options.closeTop, true);
this._closeBottom = defaultValue(options.closeBottom, true);
this._shadowVolume = defaultValue(options.shadowVolume, false);
this._workerName = 'createRectangleGeometry';
this._rotatedRectangle = computeRectangle(this._rectangle, this._ellipsoid, rotation);
}
/**
* The number of elements used to pack the object into an array.
* @type {Number}
*/
RectangleGeometry.packedLength = Rectangle.packedLength + Ellipsoid.packedLength + VertexFormat.packedLength + Rectangle.packedLength + 9;
/**
* Stores the provided instance into the provided array.
*
* @param {RectangleGeometry} value The value to pack.
* @param {Number[]} array The array to pack into.
* @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
*
* @returns {Number[]} The array that was packed into
*/
RectangleGeometry.pack = function(value, array, startingIndex) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object('value', value);
Check.defined('array', array);
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
Rectangle.pack(value._rectangle, array, startingIndex);
startingIndex += Rectangle.packedLength;
Ellipsoid.pack(value._ellipsoid, array, startingIndex);
startingIndex += Ellipsoid.packedLength;
VertexFormat.pack(value._vertexFormat, array, startingIndex);
startingIndex += VertexFormat.packedLength;
Rectangle.pack(value._rotatedRectangle, array, startingIndex);
startingIndex += Rectangle.packedLength;
array[startingIndex++] = value._granularity;
array[startingIndex++] = value._surfaceHeight;
array[startingIndex++] = value._rotation;
array[startingIndex++] = value._stRotation;
array[startingIndex++] = value._extrudedHeight;
array[startingIndex++] = value._extrude ? 1.0 : 0.0;
array[startingIndex++] = value._closeTop ? 1.0 : 0.0;
array[startingIndex++] = value._closeBottom ? 1.0 : 0.0;
array[startingIndex] = value._shadowVolume ? 1.0 : 0.0;
return array;
};
var scratchRectangle = new Rectangle();
var scratchRotatedRectangle = new Rectangle();
var scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
var scratchOptions = {
rectangle : scratchRectangle,
ellipsoid : scratchEllipsoid,
vertexFormat : scratchVertexFormat,
granularity : undefined,
height : undefined,
rotation : undefined,
stRotation : undefined,
extrudedHeight : undefined,
closeTop : undefined,
closeBottom : undefined,
shadowVolume: undefined
};
/**
* Retrieves an instance from a packed array.
*
* @param {Number[]} array The packed array.
* @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
* @param {RectangleGeometry} [result] The object into which to store the result.
* @returns {RectangleGeometry} The modified result parameter or a new RectangleGeometry instance if one was not provided.
*/
RectangleGeometry.unpack = function(array, startingIndex, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined('array', array);
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
var rectangle = Rectangle.unpack(array, startingIndex, scratchRectangle);
startingIndex += Rectangle.packedLength;
var ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
startingIndex += Ellipsoid.packedLength;
var vertexFormat = VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
startingIndex += VertexFormat.packedLength;
var rotatedRectangle = Rectangle.unpack(array, startingIndex, scratchRotatedRectangle);
startingIndex += Rectangle.packedLength;
var granularity = array[startingIndex++];
var surfaceHeight = array[startingIndex++];
var rotation = array[startingIndex++];
var stRotation = array[startingIndex++];
var extrudedHeight = array[startingIndex++];
var extrude = array[startingIndex++] === 1.0;
var closeTop = array[startingIndex++] === 1.0;
var closeBottom = array[startingIndex++] === 1.0;
var shadowVolume = array[startingIndex] === 1.0;
if (!defined(result)) {
scratchOptions.granularity = granularity;
scratchOptions.height = surfaceHeight;
scratchOptions.rotation = rotation;
scratchOptions.stRotation = stRotation;
scratchOptions.extrudedHeight = extrude ? extrudedHeight : undefined;
scratchOptions.closeTop = closeTop;
scratchOptions.closeBottom = closeBottom;
scratchOptions.shadowVolume = shadowVolume;
return new RectangleGeometry(scratchOptions);
}
result._rectangle = Rectangle.clone(rectangle, result._rectangle);
result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
result._granularity = granularity;
result._surfaceHeight = surfaceHeight;
result._rotation = rotation;
result._stRotation = stRotation;
result._extrudedHeight = extrude ? extrudedHeight : undefined;
result._extrude = extrude;
result._closeTop = closeTop;
result._closeBottom = closeBottom;
result._rotatedRectangle = rotatedRectangle;
result._shadowVolume = shadowVolume;
return result;
};
var tangentRotationMatrixScratch = new Matrix3();
var nwScratch = new Cartographic();
var stNwScratch = new Cartographic();
var quaternionScratch = new Quaternion();
var centerScratch = new Cartographic();
/**
* Computes the geometric representation of an rectangle, including its vertices, indices, and a bounding sphere.
*
* @param {RectangleGeometry} rectangleGeometry A description of the rectangle.
* @returns {Geometry|undefined} The computed vertices and indices.
*
* @exception {DeveloperError} Rotated rectangle is invalid.
*/
RectangleGeometry.createGeometry = function(rectangleGeometry) {
if ((CesiumMath.equalsEpsilon(rectangleGeometry._rectangle.north, rectangleGeometry._rectangle.south, CesiumMath.EPSILON10) ||
(CesiumMath.equalsEpsilon(rectangleGeometry._rectangle.east, rectangleGeometry._rectangle.west, CesiumMath.EPSILON10)))) {
return undefined;
}
var rectangle = Rectangle.clone(rectangleGeometry._rectangle, rectangleScratch);
var ellipsoid = rectangleGeometry._ellipsoid;
var surfaceHeight = rectangleGeometry._surfaceHeight;
var extrude = rectangleGeometry._extrude;
var extrudedHeight = rectangleGeometry._extrudedHeight;
var rotation = rectangleGeometry._rotation;
var stRotation = rectangleGeometry._stRotation;
var vertexFormat = rectangleGeometry._vertexFormat;
var options = RectangleGeometryLibrary.computeOptions(rectangleGeometry, rectangle, nwScratch, stNwScratch);
var tangentRotationMatrix = tangentRotationMatrixScratch;
if (stRotation !== 0 || rotation !== 0) {
var center = Rectangle.center(rectangle, centerScratch);
var axis = ellipsoid.geodeticSurfaceNormalCartographic(center, v1Scratch);
Quaternion.fromAxisAngle(axis, -stRotation, quaternionScratch);
Matrix3.fromQuaternion(quaternionScratch, tangentRotationMatrix);
} else {
Matrix3.clone(Matrix3.IDENTITY, tangentRotationMatrix);
}
options.lonScalar = 1.0 / rectangleGeometry._rectangle.width;
options.latScalar = 1.0 / rectangleGeometry._rectangle.height;
options.vertexFormat = vertexFormat;
options.rotation = rotation;
options.stRotation = stRotation;
options.tangentRotationMatrix = tangentRotationMatrix;
options.size = options.width * options.height;
var geometry;
var boundingSphere;
rectangle = rectangleGeometry._rectangle;
if (extrude) {
options.shadowVolume = rectangleGeometry._shadowVolume;
geometry = constructExtrudedRectangle(options);
var topBS = BoundingSphere.fromRectangle3D(rectangle, ellipsoid, surfaceHeight, topBoundingSphere);
var bottomBS = BoundingSphere.fromRectangle3D(rectangle, ellipsoid, extrudedHeight, bottomBoundingSphere);
boundingSphere = BoundingSphere.union(topBS, bottomBS);
} else {
geometry = constructRectangle(options);
geometry.attributes.position.values = PolygonPipeline.scaleToGeodeticHeight(geometry.attributes.position.values, surfaceHeight, ellipsoid, false);
boundingSphere = BoundingSphere.fromRectangle3D(rectangle, ellipsoid, surfaceHeight);
}
if (!vertexFormat.position) {
delete geometry.attributes.position;
}
return new Geometry({
attributes : geometry.attributes,
indices : geometry.indices,
primitiveType : geometry.primitiveType,
boundingSphere : boundingSphere
});
};
/**
* @private
*/
RectangleGeometry.createShadowVolume = function(rectangleGeometry, minHeightFunc, maxHeightFunc) {
var granularity = rectangleGeometry._granularity;
var ellipsoid = rectangleGeometry._ellipsoid;
var minHeight = minHeightFunc(granularity, ellipsoid);
var maxHeight = maxHeightFunc(granularity, ellipsoid);
// TODO: stRotation
return new RectangleGeometry({
rectangle : rectangleGeometry._rectangle,
rotation : rectangleGeometry._rotation,
ellipsoid : ellipsoid,
stRotation : rectangleGeometry._stRotation,
granularity : granularity,
extrudedHeight : maxHeight,
height : minHeight,
closeTop : true,
closeBottom : true,
vertexFormat : VertexFormat.POSITION_ONLY,
shadowVolume: true
});
};
defineProperties(RectangleGeometry.prototype, {
/**
* @private
*/
rectangle : {
get : function() {
return this._rotatedRectangle;
}
}
});
return RectangleGeometry;
});