-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
PointPrimitiveCollection.js
1045 lines (902 loc) · 41.4 KB
/
PointPrimitiveCollection.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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global define*/
define([
'../Core/BoundingSphere',
'../Core/Color',
'../Core/ComponentDatatype',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/EncodedCartesian3',
'../Core/Math',
'../Core/Matrix4',
'../Core/PrimitiveType',
'../Core/WebGLConstants',
'../Renderer/BufferUsage',
'../Renderer/ContextLimits',
'../Renderer/DrawCommand',
'../Renderer/Pass',
'../Renderer/RenderState',
'../Renderer/ShaderProgram',
'../Renderer/ShaderSource',
'../Renderer/VertexArrayFacade',
'../Shaders/PointPrimitiveCollectionFS',
'../Shaders/PointPrimitiveCollectionVS',
'./BlendingState',
'./PointPrimitive',
'./SceneMode'
], function(
BoundingSphere,
Color,
ComponentDatatype,
defaultValue,
defined,
defineProperties,
destroyObject,
DeveloperError,
EncodedCartesian3,
CesiumMath,
Matrix4,
PrimitiveType,
WebGLConstants,
BufferUsage,
ContextLimits,
DrawCommand,
Pass,
RenderState,
ShaderProgram,
ShaderSource,
VertexArrayFacade,
PointPrimitiveCollectionFS,
PointPrimitiveCollectionVS,
BlendingState,
PointPrimitive,
SceneMode) {
'use strict';
var SHOW_INDEX = PointPrimitive.SHOW_INDEX;
var POSITION_INDEX = PointPrimitive.POSITION_INDEX;
var COLOR_INDEX = PointPrimitive.COLOR_INDEX;
var OUTLINE_COLOR_INDEX = PointPrimitive.OUTLINE_COLOR_INDEX;
var OUTLINE_WIDTH_INDEX = PointPrimitive.OUTLINE_WIDTH_INDEX;
var PIXEL_SIZE_INDEX = PointPrimitive.PIXEL_SIZE_INDEX;
var SCALE_BY_DISTANCE_INDEX = PointPrimitive.SCALE_BY_DISTANCE_INDEX;
var TRANSLUCENCY_BY_DISTANCE_INDEX = PointPrimitive.TRANSLUCENCY_BY_DISTANCE_INDEX;
var DISTANCE_DISPLAY_CONDITION_INDEX = PointPrimitive.DISTANCE_DISPLAY_CONDITION_INDEX;
var NUMBER_OF_PROPERTIES = PointPrimitive.NUMBER_OF_PROPERTIES;
var attributeLocations = {
positionHighAndSize : 0,
positionLowAndOutline : 1,
compressedAttribute0 : 2, // color, outlineColor, pick color
compressedAttribute1 : 3, // show, translucency by distance, some free space
scaleByDistance : 4,
distanceDisplayCondition : 5
};
/**
* A renderable collection of points.
* <br /><br />
* Points are added and removed from the collection using {@link PointPrimitiveCollection#add}
* and {@link PointPrimitiveCollection#remove}.
*
* @alias PointPrimitiveCollection
* @constructor
*
* @param {Object} [options] Object with the following properties:
* @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix that transforms each point from model to world coordinates.
* @param {Boolean} [options.debugShowBoundingVolume=false] For debugging only. Determines if this primitive's commands' bounding spheres are shown.
*
* @performance For best performance, prefer a few collections, each with many points, to
* many collections with only a few points each. Organize collections so that points
* with the same update frequency are in the same collection, i.e., points that do not
* change should be in one collection; points that change every frame should be in another
* collection; and so on.
*
*
* @example
* // Create a pointPrimitive collection with two points
* var points = scene.primitives.add(new Cesium.PointPrimitiveCollection());
* points.add({
* position : new Cesium.Cartesian3(1.0, 2.0, 3.0),
* color : Cesium.Color.YELLOW
* });
* points.add({
* position : new Cesium.Cartesian3(4.0, 5.0, 6.0),
* color : Cesium.Color.CYAN
* });
*
* @see PointPrimitiveCollection#add
* @see PointPrimitiveCollection#remove
* @see PointPrimitive
*/
function PointPrimitiveCollection(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
this._sp = undefined;
this._rs = undefined;
this._vaf = undefined;
this._spTranslucent = undefined;
this._spPick = undefined;
this._pointPrimitives = [];
this._pointPrimitivesToUpdate = [];
this._pointPrimitivesToUpdateIndex = 0;
this._pointPrimitivesRemoved = false;
this._createVertexArray = false;
this._shaderScaleByDistance = false;
this._compiledShaderScaleByDistance = false;
this._compiledShaderScaleByDistancePick = false;
this._shaderTranslucencyByDistance = false;
this._compiledShaderTranslucencyByDistance = false;
this._compiledShaderTranslucencyByDistancePick = false;
this._shaderDistanceDisplayCondition = false;
this._compiledShaderDistanceDisplayCondition = false;
this._compiledShaderDistanceDisplayConditionPick = false;
this._propertiesChanged = new Uint32Array(NUMBER_OF_PROPERTIES);
this._maxPixelSize = 1.0;
this._baseVolume = new BoundingSphere();
this._baseVolumeWC = new BoundingSphere();
this._baseVolume2D = new BoundingSphere();
this._boundingVolume = new BoundingSphere();
this._boundingVolumeDirty = false;
this._colorCommands = [];
this._pickCommands = [];
/**
* The 4x4 transformation matrix that transforms each point in this collection from model to world coordinates.
* When this is the identity matrix, the pointPrimitives are drawn in world coordinates, i.e., Earth's WGS84 coordinates.
* Local reference frames can be used by providing a different transformation matrix, like that returned
* by {@link Transforms.eastNorthUpToFixedFrame}.
*
* @type {Matrix4}
* @default {@link Matrix4.IDENTITY}
*
*
* @example
* var center = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
* pointPrimitives.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center);
* pointPrimitives.add({
* color : Cesium.Color.ORANGE,
* position : new Cesium.Cartesian3(0.0, 0.0, 0.0) // center
* });
* pointPrimitives.add({
* color : Cesium.Color.YELLOW,
* position : new Cesium.Cartesian3(1000000.0, 0.0, 0.0) // east
* });
* pointPrimitives.add({
* color : Cesium.Color.GREEN,
* position : new Cesium.Cartesian3(0.0, 1000000.0, 0.0) // north
* });
* pointPrimitives.add({
* color : Cesium.Color.CYAN,
* position : new Cesium.Cartesian3(0.0, 0.0, 1000000.0) // up
* });
*
* @see Transforms.eastNorthUpToFixedFrame
*/
this.modelMatrix = Matrix4.clone(defaultValue(options.modelMatrix, Matrix4.IDENTITY));
this._modelMatrix = Matrix4.clone(Matrix4.IDENTITY);
/**
* This property is for debugging only; it is not for production use nor is it optimized.
* <p>
* Draws the bounding sphere for each draw command in the primitive.
* </p>
*
* @type {Boolean}
*
* @default false
*/
this.debugShowBoundingVolume = defaultValue(options.debugShowBoundingVolume, false);
this._mode = SceneMode.SCENE3D;
this._maxTotalPointSize = 1;
// The buffer usage for each attribute is determined based on the usage of the attribute over time.
this._buffersUsage = [
BufferUsage.STATIC_DRAW, // SHOW_INDEX
BufferUsage.STATIC_DRAW, // POSITION_INDEX
BufferUsage.STATIC_DRAW, // COLOR_INDEX
BufferUsage.STATIC_DRAW, // OUTLINE_COLOR_INDEX
BufferUsage.STATIC_DRAW, // OUTLINE_WIDTH_INDEX
BufferUsage.STATIC_DRAW, // PIXEL_SIZE_INDEX
BufferUsage.STATIC_DRAW, // SCALE_BY_DISTANCE_INDEX
BufferUsage.STATIC_DRAW, // TRANSLUCENCY_BY_DISTANCE_INDEX
BufferUsage.STATIC_DRAW // DISTANCE_DISPLAY_CONDITION_INDEX
];
var that = this;
this._uniforms = {
u_maxTotalPointSize : function() {
return that._maxTotalPointSize;
}
};
}
defineProperties(PointPrimitiveCollection.prototype, {
/**
* Returns the number of points in this collection. This is commonly used with
* {@link PointPrimitiveCollection#get} to iterate over all the points
* in the collection.
* @memberof PointPrimitiveCollection.prototype
* @type {Number}
*/
length : {
get : function() {
removePointPrimitives(this);
return this._pointPrimitives.length;
}
}
});
function destroyPointPrimitives(pointPrimitives) {
var length = pointPrimitives.length;
for (var i = 0; i < length; ++i) {
if (pointPrimitives[i]) {
pointPrimitives[i]._destroy();
}
}
}
/**
* Creates and adds a point with the specified initial properties to the collection.
* The added point is returned so it can be modified or removed from the collection later.
*
* @param {Object}[pointPrimitive] A template describing the point's properties as shown in Example 1.
* @returns {PointPrimitive} The point that was added to the collection.
*
* @performance Calling <code>add</code> is expected constant time. However, the collection's vertex buffer
* is rewritten - an <code>O(n)</code> operation that also incurs CPU to GPU overhead. For
* best performance, add as many pointPrimitives as possible before calling <code>update</code>.
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
*
* @example
* // Example 1: Add a point, specifying all the default values.
* var p = pointPrimitives.add({
* show : true,
* position : Cesium.Cartesian3.ZERO,
* pixelSize : 10.0,
* color : Cesium.Color.WHITE,
* outlineColor : Cesium.Color.TRANSPARENT,
* outlineWidth : 0.0,
* id : undefined
* });
*
* @example
* // Example 2: Specify only the point's cartographic position.
* var p = pointPrimitives.add({
* position : Cesium.Cartesian3.fromDegrees(longitude, latitude, height)
* });
*
* @see PointPrimitiveCollection#remove
* @see PointPrimitiveCollection#removeAll
*/
PointPrimitiveCollection.prototype.add = function(pointPrimitive) {
var p = new PointPrimitive(pointPrimitive, this);
p._index = this._pointPrimitives.length;
this._pointPrimitives.push(p);
this._createVertexArray = true;
return p;
};
/**
* Removes a point from the collection.
*
* @param {PointPrimitive} pointPrimitive The point to remove.
* @returns {Boolean} <code>true</code> if the point was removed; <code>false</code> if the point was not found in the collection.
*
* @performance Calling <code>remove</code> is expected constant time. However, the collection's vertex buffer
* is rewritten - an <code>O(n)</code> operation that also incurs CPU to GPU overhead. For
* best performance, remove as many points as possible before calling <code>update</code>.
* If you intend to temporarily hide a point, it is usually more efficient to call
* {@link PointPrimitive#show} instead of removing and re-adding the point.
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
*
* @example
* var p = pointPrimitives.add(...);
* pointPrimitives.remove(p); // Returns true
*
* @see PointPrimitiveCollection#add
* @see PointPrimitiveCollection#removeAll
* @see PointPrimitive#show
*/
PointPrimitiveCollection.prototype.remove = function(pointPrimitive) {
if (this.contains(pointPrimitive)) {
this._pointPrimitives[pointPrimitive._index] = null; // Removed later
this._pointPrimitivesRemoved = true;
this._createVertexArray = true;
pointPrimitive._destroy();
return true;
}
return false;
};
/**
* Removes all points from the collection.
*
* @performance <code>O(n)</code>. It is more efficient to remove all the points
* from a collection and then add new ones than to create a new collection entirely.
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
*
* @example
* pointPrimitives.add(...);
* pointPrimitives.add(...);
* pointPrimitives.removeAll();
*
* @see PointPrimitiveCollection#add
* @see PointPrimitiveCollection#remove
*/
PointPrimitiveCollection.prototype.removeAll = function() {
destroyPointPrimitives(this._pointPrimitives);
this._pointPrimitives = [];
this._pointPrimitivesToUpdate = [];
this._pointPrimitivesToUpdateIndex = 0;
this._pointPrimitivesRemoved = false;
this._createVertexArray = true;
};
function removePointPrimitives(pointPrimitiveCollection) {
if (pointPrimitiveCollection._pointPrimitivesRemoved) {
pointPrimitiveCollection._pointPrimitivesRemoved = false;
var newPointPrimitives = [];
var pointPrimitives = pointPrimitiveCollection._pointPrimitives;
var length = pointPrimitives.length;
for (var i = 0, j = 0; i < length; ++i) {
var pointPrimitive = pointPrimitives[i];
if (pointPrimitive) {
pointPrimitive._index = j++;
newPointPrimitives.push(pointPrimitive);
}
}
pointPrimitiveCollection._pointPrimitives = newPointPrimitives;
}
}
PointPrimitiveCollection.prototype._updatePointPrimitive = function(pointPrimitive, propertyChanged) {
if (!pointPrimitive._dirty) {
this._pointPrimitivesToUpdate[this._pointPrimitivesToUpdateIndex++] = pointPrimitive;
}
++this._propertiesChanged[propertyChanged];
};
/**
* Check whether this collection contains a given point.
*
* @param {PointPrimitive} [pointPrimitive] The point to check for.
* @returns {Boolean} true if this collection contains the point, false otherwise.
*
* @see PointPrimitiveCollection#get
*/
PointPrimitiveCollection.prototype.contains = function(pointPrimitive) {
return defined(pointPrimitive) && pointPrimitive._pointPrimitiveCollection === this;
};
/**
* Returns the point in the collection at the specified index. Indices are zero-based
* and increase as points are added. Removing a point shifts all points after
* it to the left, changing their indices. This function is commonly used with
* {@link PointPrimitiveCollection#length} to iterate over all the points
* in the collection.
*
* @param {Number} index The zero-based index of the point.
* @returns {PointPrimitive} The point at the specified index.
*
* @performance Expected constant time. If points were removed from the collection and
* {@link PointPrimitiveCollection#update} was not called, an implicit <code>O(n)</code>
* operation is performed.
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
*
* @example
* // Toggle the show property of every point in the collection
* var len = pointPrimitives.length;
* for (var i = 0; i < len; ++i) {
* var p = pointPrimitives.get(i);
* p.show = !p.show;
* }
*
* @see PointPrimitiveCollection#length
*/
PointPrimitiveCollection.prototype.get = function(index) {
//>>includeStart('debug', pragmas.debug);
if (!defined(index)) {
throw new DeveloperError('index is required.');
}
//>>includeEnd('debug');
removePointPrimitives(this);
return this._pointPrimitives[index];
};
PointPrimitiveCollection.prototype.computeNewBuffersUsage = function() {
var buffersUsage = this._buffersUsage;
var usageChanged = false;
var properties = this._propertiesChanged;
for ( var k = 0; k < NUMBER_OF_PROPERTIES; ++k) {
var newUsage = (properties[k] === 0) ? BufferUsage.STATIC_DRAW : BufferUsage.STREAM_DRAW;
usageChanged = usageChanged || (buffersUsage[k] !== newUsage);
buffersUsage[k] = newUsage;
}
return usageChanged;
};
function createVAF(context, numberOfPointPrimitives, buffersUsage) {
return new VertexArrayFacade(context, [{
index : attributeLocations.positionHighAndSize,
componentsPerAttribute : 4,
componentDatatype : ComponentDatatype.FLOAT,
usage : buffersUsage[POSITION_INDEX]
}, {
index : attributeLocations.positionLowAndShow,
componentsPerAttribute : 4,
componentDatatype : ComponentDatatype.FLOAT,
usage : buffersUsage[POSITION_INDEX]
}, {
index : attributeLocations.compressedAttribute0,
componentsPerAttribute : 4,
componentDatatype : ComponentDatatype.FLOAT,
usage : buffersUsage[COLOR_INDEX]
}, {
index : attributeLocations.compressedAttribute1,
componentsPerAttribute : 4,
componentDatatype : ComponentDatatype.FLOAT,
usage : buffersUsage[TRANSLUCENCY_BY_DISTANCE_INDEX]
}, {
index : attributeLocations.scaleByDistance,
componentsPerAttribute : 4,
componentDatatype : ComponentDatatype.FLOAT,
usage : buffersUsage[SCALE_BY_DISTANCE_INDEX]
}, {
index : attributeLocations.distanceDisplayCondition,
componentsPerAttribute : 2,
componentDatatype : ComponentDatatype.FLOAT,
usage : buffersUsage[DISTANCE_DISPLAY_CONDITION_INDEX]
}], numberOfPointPrimitives); // 1 vertex per pointPrimitive
}
///////////////////////////////////////////////////////////////////////////
// PERFORMANCE_IDEA: Save memory if a property is the same for all pointPrimitives, use a latched attribute state,
// instead of storing it in a vertex buffer.
var writePositionScratch = new EncodedCartesian3();
function writePositionSizeAndOutline(pointPrimitiveCollection, context, vafWriters, pointPrimitive) {
var i = pointPrimitive._index;
var position = pointPrimitive._getActualPosition();
if (pointPrimitiveCollection._mode === SceneMode.SCENE3D) {
BoundingSphere.expand(pointPrimitiveCollection._baseVolume, position, pointPrimitiveCollection._baseVolume);
pointPrimitiveCollection._boundingVolumeDirty = true;
}
EncodedCartesian3.fromCartesian(position, writePositionScratch);
var pixelSize = pointPrimitive.pixelSize;
var outlineWidth = pointPrimitive.outlineWidth;
pointPrimitiveCollection._maxPixelSize = Math.max(pointPrimitiveCollection._maxPixelSize, pixelSize + outlineWidth);
var positionHighWriter = vafWriters[attributeLocations.positionHighAndSize];
var high = writePositionScratch.high;
positionHighWriter(i, high.x, high.y, high.z, pixelSize);
var positionLowWriter = vafWriters[attributeLocations.positionLowAndOutline];
var low = writePositionScratch.low;
positionLowWriter(i, low.x, low.y, low.z, outlineWidth);
}
var LEFT_SHIFT16 = 65536.0; // 2^16
var LEFT_SHIFT8 = 256.0; // 2^8
function writeCompressedAttrib0(pointPrimitiveCollection, context, vafWriters, pointPrimitive) {
var i = pointPrimitive._index;
var color = pointPrimitive.color;
var pickColor = pointPrimitive.getPickId(context).color;
var outlineColor = pointPrimitive.outlineColor;
var red = Color.floatToByte(color.red);
var green = Color.floatToByte(color.green);
var blue = Color.floatToByte(color.blue);
var compressed0 = red * LEFT_SHIFT16 + green * LEFT_SHIFT8 + blue;
red = Color.floatToByte(outlineColor.red);
green = Color.floatToByte(outlineColor.green);
blue = Color.floatToByte(outlineColor.blue);
var compressed1 = red * LEFT_SHIFT16 + green * LEFT_SHIFT8 + blue;
red = Color.floatToByte(pickColor.red);
green = Color.floatToByte(pickColor.green);
blue = Color.floatToByte(pickColor.blue);
var compressed2 = red * LEFT_SHIFT16 + green * LEFT_SHIFT8 + blue;
var compressed3 =
Color.floatToByte(color.alpha) * LEFT_SHIFT16 +
Color.floatToByte(outlineColor.alpha) * LEFT_SHIFT8 +
Color.floatToByte(pickColor.alpha);
var writer = vafWriters[attributeLocations.compressedAttribute0];
writer(i, compressed0, compressed1, compressed2, compressed3);
}
function writeCompressedAttrib1(pointPrimitiveCollection, context, vafWriters, pointPrimitive) {
var i = pointPrimitive._index;
var near = 0.0;
var nearValue = 1.0;
var far = 1.0;
var farValue = 1.0;
var translucency = pointPrimitive.translucencyByDistance;
if (defined(translucency)) {
near = translucency.near;
nearValue = translucency.nearValue;
far = translucency.far;
farValue = translucency.farValue;
if (nearValue !== 1.0 || farValue !== 1.0) {
// translucency by distance calculation in shader need not be enabled
// until a pointPrimitive with near and far !== 1.0 is found
pointPrimitiveCollection._shaderTranslucencyByDistance = true;
}
}
var show = pointPrimitive.show && pointPrimitive.clusterShow;
// If the color alphas are zero, do not show this pointPrimitive. This lets us avoid providing
// color during the pick pass and also eliminates a discard in the fragment shader.
if (pointPrimitive.color.alpha === 0.0 && pointPrimitive.outlineColor.alpha === 0.0) {
show = false;
}
nearValue = CesiumMath.clamp(nearValue, 0.0, 1.0);
nearValue = nearValue === 1.0 ? 255.0 : (nearValue * 255.0) | 0;
var compressed0 = (show ? 1.0 : 0.0) * LEFT_SHIFT8 + nearValue;
farValue = CesiumMath.clamp(farValue, 0.0, 1.0);
farValue = farValue === 1.0 ? 255.0 : (farValue * 255.0) | 0;
var compressed1 = farValue;
var writer = vafWriters[attributeLocations.compressedAttribute1];
writer(i, compressed0, compressed1, near, far);
}
function writeScaleByDistance(pointPrimitiveCollection, context, vafWriters, pointPrimitive) {
var i = pointPrimitive._index;
var writer = vafWriters[attributeLocations.scaleByDistance];
var near = 0.0;
var nearValue = 1.0;
var far = 1.0;
var farValue = 1.0;
var scale = pointPrimitive.scaleByDistance;
if (defined(scale)) {
near = scale.near;
nearValue = scale.nearValue;
far = scale.far;
farValue = scale.farValue;
if (nearValue !== 1.0 || farValue !== 1.0) {
// scale by distance calculation in shader need not be enabled
// until a pointPrimitive with near and far !== 1.0 is found
pointPrimitiveCollection._shaderScaleByDistance = true;
}
}
writer(i, near, nearValue, far, farValue);
}
function writeDistanceDisplayCondition(pointPrimitiveCollection, context, vafWriters, pointPrimitive) {
var i = pointPrimitive._index;
var writer = vafWriters[attributeLocations.distanceDisplayCondition];
var near = 0.0;
var far = Number.MAX_VALUE;
var distanceDisplayCondition = pointPrimitive.distanceDisplayCondition;
if (defined(distanceDisplayCondition)) {
near = distanceDisplayCondition.near;
far = distanceDisplayCondition.far;
pointPrimitiveCollection._shaderDistanceDisplayCondition = true;
}
writer(i, near, far);
}
function writePointPrimitive(pointPrimitiveCollection, context, vafWriters, pointPrimitive) {
writePositionSizeAndOutline(pointPrimitiveCollection, context, vafWriters, pointPrimitive);
writeCompressedAttrib0(pointPrimitiveCollection, context, vafWriters, pointPrimitive);
writeCompressedAttrib1(pointPrimitiveCollection, context, vafWriters, pointPrimitive);
writeScaleByDistance(pointPrimitiveCollection, context, vafWriters, pointPrimitive);
writeDistanceDisplayCondition(pointPrimitiveCollection, context, vafWriters, pointPrimitive);
}
function recomputeActualPositions(pointPrimitiveCollection, pointPrimitives, length, frameState, modelMatrix, recomputeBoundingVolume) {
var boundingVolume;
if (frameState.mode === SceneMode.SCENE3D) {
boundingVolume = pointPrimitiveCollection._baseVolume;
pointPrimitiveCollection._boundingVolumeDirty = true;
} else {
boundingVolume = pointPrimitiveCollection._baseVolume2D;
}
var positions = [];
for ( var i = 0; i < length; ++i) {
var pointPrimitive = pointPrimitives[i];
var position = pointPrimitive.position;
var actualPosition = PointPrimitive._computeActualPosition(position, frameState, modelMatrix);
if (defined(actualPosition)) {
pointPrimitive._setActualPosition(actualPosition);
if (recomputeBoundingVolume) {
positions.push(actualPosition);
} else {
BoundingSphere.expand(boundingVolume, actualPosition, boundingVolume);
}
}
}
if (recomputeBoundingVolume) {
BoundingSphere.fromPoints(positions, boundingVolume);
}
}
function updateMode(pointPrimitiveCollection, frameState) {
var mode = frameState.mode;
var pointPrimitives = pointPrimitiveCollection._pointPrimitives;
var pointPrimitivesToUpdate = pointPrimitiveCollection._pointPrimitivesToUpdate;
var modelMatrix = pointPrimitiveCollection._modelMatrix;
if (pointPrimitiveCollection._createVertexArray ||
pointPrimitiveCollection._mode !== mode ||
mode !== SceneMode.SCENE3D &&
!Matrix4.equals(modelMatrix, pointPrimitiveCollection.modelMatrix)) {
pointPrimitiveCollection._mode = mode;
Matrix4.clone(pointPrimitiveCollection.modelMatrix, modelMatrix);
pointPrimitiveCollection._createVertexArray = true;
if (mode === SceneMode.SCENE3D || mode === SceneMode.SCENE2D || mode === SceneMode.COLUMBUS_VIEW) {
recomputeActualPositions(pointPrimitiveCollection, pointPrimitives, pointPrimitives.length, frameState, modelMatrix, true);
}
} else if (mode === SceneMode.MORPHING) {
recomputeActualPositions(pointPrimitiveCollection, pointPrimitives, pointPrimitives.length, frameState, modelMatrix, true);
} else if (mode === SceneMode.SCENE2D || mode === SceneMode.COLUMBUS_VIEW) {
recomputeActualPositions(pointPrimitiveCollection, pointPrimitivesToUpdate, pointPrimitiveCollection._pointPrimitivesToUpdateIndex, frameState, modelMatrix, false);
}
}
function updateBoundingVolume(collection, frameState, boundingVolume) {
var pixelSize = frameState.camera.getPixelSize(boundingVolume, frameState.context.drawingBufferWidth, frameState.context.drawingBufferHeight);
var size = pixelSize * collection._maxPixelSize;
boundingVolume.radius += size;
}
var scratchWriterArray = [];
/**
* @private
*/
PointPrimitiveCollection.prototype.update = function(frameState) {
removePointPrimitives(this);
this._maxTotalPointSize = ContextLimits.maximumAliasedPointSize;
updateMode(this, frameState);
var pointPrimitives = this._pointPrimitives;
var pointPrimitivesLength = pointPrimitives.length;
var pointPrimitivesToUpdate = this._pointPrimitivesToUpdate;
var pointPrimitivesToUpdateLength = this._pointPrimitivesToUpdateIndex;
var properties = this._propertiesChanged;
var createVertexArray = this._createVertexArray;
var vafWriters;
var context = frameState.context;
var pass = frameState.passes;
var picking = pass.pick;
// PERFORMANCE_IDEA: Round robin multiple buffers.
if (createVertexArray || (!picking && this.computeNewBuffersUsage())) {
this._createVertexArray = false;
for (var k = 0; k < NUMBER_OF_PROPERTIES; ++k) {
properties[k] = 0;
}
this._vaf = this._vaf && this._vaf.destroy();
if (pointPrimitivesLength > 0) {
// PERFORMANCE_IDEA: Instead of creating a new one, resize like std::vector.
this._vaf = createVAF(context, pointPrimitivesLength, this._buffersUsage);
vafWriters = this._vaf.writers;
// Rewrite entire buffer if pointPrimitives were added or removed.
for (var i = 0; i < pointPrimitivesLength; ++i) {
var pointPrimitive = this._pointPrimitives[i];
pointPrimitive._dirty = false; // In case it needed an update.
writePointPrimitive(this, context, vafWriters, pointPrimitive);
}
this._vaf.commit();
}
this._pointPrimitivesToUpdateIndex = 0;
} else {
// PointPrimitives were modified, but none were added or removed.
if (pointPrimitivesToUpdateLength > 0) {
var writers = scratchWriterArray;
writers.length = 0;
if (properties[POSITION_INDEX] || properties[OUTLINE_WIDTH_INDEX] || properties[PIXEL_SIZE_INDEX]) {
writers.push(writePositionSizeAndOutline);
}
if (properties[COLOR_INDEX] || properties[OUTLINE_COLOR_INDEX]) {
writers.push(writeCompressedAttrib0);
}
if (properties[SHOW_INDEX] || properties[TRANSLUCENCY_BY_DISTANCE_INDEX]) {
writers.push(writeCompressedAttrib1);
}
if (properties[SCALE_BY_DISTANCE_INDEX]) {
writers.push(writeScaleByDistance);
}
if (properties[DISTANCE_DISPLAY_CONDITION_INDEX]) {
writers.push(writeDistanceDisplayCondition);
}
var numWriters = writers.length;
vafWriters = this._vaf.writers;
if ((pointPrimitivesToUpdateLength / pointPrimitivesLength) > 0.1) {
// If more than 10% of pointPrimitive change, rewrite the entire buffer.
// PERFORMANCE_IDEA: I totally made up 10% :).
for (var m = 0; m < pointPrimitivesToUpdateLength; ++m) {
var b = pointPrimitivesToUpdate[m];
b._dirty = false;
for ( var n = 0; n < numWriters; ++n) {
writers[n](this, context, vafWriters, b);
}
}
this._vaf.commit();
} else {
for (var h = 0; h < pointPrimitivesToUpdateLength; ++h) {
var bb = pointPrimitivesToUpdate[h];
bb._dirty = false;
for ( var o = 0; o < numWriters; ++o) {
writers[o](this, context, vafWriters, bb);
}
this._vaf.subCommit(bb._index, 1);
}
this._vaf.endSubCommits();
}
this._pointPrimitivesToUpdateIndex = 0;
}
}
// If the number of total pointPrimitives ever shrinks considerably
// Truncate pointPrimitivesToUpdate so that we free memory that we're
// not going to be using.
if (pointPrimitivesToUpdateLength > pointPrimitivesLength * 1.5) {
pointPrimitivesToUpdate.length = pointPrimitivesLength;
}
if (!defined(this._vaf) || !defined(this._vaf.va)) {
return;
}
if (this._boundingVolumeDirty) {
this._boundingVolumeDirty = false;
BoundingSphere.transform(this._baseVolume, this.modelMatrix, this._baseVolumeWC);
}
var boundingVolume;
var modelMatrix = Matrix4.IDENTITY;
if (frameState.mode === SceneMode.SCENE3D) {
modelMatrix = this.modelMatrix;
boundingVolume = BoundingSphere.clone(this._baseVolumeWC, this._boundingVolume);
} else {
boundingVolume = BoundingSphere.clone(this._baseVolume2D, this._boundingVolume);
}
updateBoundingVolume(this, frameState, boundingVolume);
var va;
var vaLength;
var command;
var j;
var vs;
var fs;
var commandList = frameState.commandList;
if (pass.render) {
var colorList = this._colorCommands;
if (!defined(this._rs)) {
this._rs = RenderState.fromCache({
depthTest : {
enabled : true,
func : WebGLConstants.LEQUAL
},
blending : BlendingState.ALPHA_BLEND
});
}
if (!defined(this._sp) ||
(this._shaderScaleByDistance && !this._compiledShaderScaleByDistance) ||
(this._shaderTranslucencyByDistance && !this._compiledShaderTranslucencyByDistance) ||
(this._shaderDistanceDisplayCondition && !this._compiledShaderDistanceDisplayCondition)) {
vs = new ShaderSource({
sources : [PointPrimitiveCollectionVS]
});
if (this._shaderScaleByDistance) {
vs.defines.push('EYE_DISTANCE_SCALING');
}
if (this._shaderTranslucencyByDistance) {
vs.defines.push('EYE_DISTANCE_TRANSLUCENCY');
}
if (this._shaderDistanceDisplayCondition) {
vs.defines.push('DISTANCE_DISPLAY_CONDITION');
}
fs = new ShaderSource({
defines : ['OPAQUE'],
sources : [PointPrimitiveCollectionFS]
});
this._sp = ShaderProgram.replaceCache({
context : context,
shaderProgram : this._sp,
vertexShaderSource : vs,
fragmentShaderSource : fs,
attributeLocations : attributeLocations
});
fs = new ShaderSource({
defines : ['TRANSLUCENT'],
sources : [PointPrimitiveCollectionFS]
});
this._spTranslucent = ShaderProgram.replaceCache({
context : context,
shaderProgram : this._spTranslucent,
vertexShaderSource : vs,
fragmentShaderSource : fs,
attributeLocations : attributeLocations
});
this._compiledShaderScaleByDistance = this._shaderScaleByDistance;
this._compiledShaderTranslucencyByDistance = this._shaderTranslucencyByDistance;
this._compiledShaderDistanceDisplayCondition = this._shaderDistanceDisplayCondition;
}
va = this._vaf.va;
vaLength = va.length;
colorList.length = vaLength;
for (j = 0; j < vaLength * 2; ++j) {
command = colorList[j];
if (!defined(command)) {
command = colorList[j] = new DrawCommand();
}
command.primitiveType = PrimitiveType.POINTS;
command.pass = (j % 2 === 0) ? Pass.OPAQUE : Pass.TRANSLUCENT;
command.owner = this;
var index = Math.floor(j / 2.0);
command.boundingVolume = boundingVolume;
command.modelMatrix = modelMatrix;
command.shaderProgram = (j % 2 === 0) ? this._sp : this._spTranslucent;
command.uniformMap = this._uniforms;
command.vertexArray = va[index].va;
command.renderState = this._rs;
command.debugShowBoundingVolume = this.debugShowBoundingVolume;
commandList.push(command);
}
}
if (picking) {
var pickList = this._pickCommands;
if (!defined(this._spPick) ||
(this._shaderScaleByDistance && !this._compiledShaderScaleByDistancePick) ||
(this._shaderTranslucencyByDistance && !this._compiledShaderTranslucencyByDistancePick) ||
(this._shaderDistanceDisplayCondition && !this._compiledShaderDistanceDisplayConditionPick)) {
vs = new ShaderSource({
defines : ['RENDER_FOR_PICK'],
sources : [PointPrimitiveCollectionVS]
});
if (this._shaderScaleByDistance) {
vs.defines.push('EYE_DISTANCE_SCALING');
}
if (this._shaderTranslucencyByDistance) {
vs.defines.push('EYE_DISTANCE_TRANSLUCENCY');
}
if (this._shaderDistanceDisplayCondition) {
vs.defines.push('DISTANCE_DISPLAY_CONDITION');
}
fs = new ShaderSource({
defines : ['RENDER_FOR_PICK'],
sources : [PointPrimitiveCollectionFS]
});
this._spPick = ShaderProgram.replaceCache({
context : context,
shaderProgram : this._spPick,
vertexShaderSource : vs,
fragmentShaderSource : fs,
attributeLocations : attributeLocations
});
this._compiledShaderScaleByDistancePick = this._shaderScaleByDistance;
this._compiledShaderTranslucencyByDistancePick = this._shaderTranslucencyByDistance;
this._compiledShaderDistanceDisplayConditionPick = this._shaderDistanceDisplayCondition;
}
va = this._vaf.va;
vaLength = va.length;
pickList.length = vaLength;
for (j = 0; j < vaLength; ++j) {
command = pickList[j];
if (!defined(command)) {
command = pickList[j] = new DrawCommand({
primitiveType : PrimitiveType.POINTS,
pass : Pass.OPAQUE,
owner : this
});
}
command.boundingVolume = boundingVolume;
command.modelMatrix = modelMatrix;
command.shaderProgram = this._spPick;
command.uniformMap = this._uniforms;
command.vertexArray = va[j].va;
command.renderState = this._rs;
commandList.push(command);
}
}
};