/
PointCloud3DTileContent.js
1263 lines (1098 loc) · 46.4 KB
/
PointCloud3DTileContent.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
define([
'../Core/Cartesian2',
'../Core/Cartesian3',
'../Core/Cartesian4',
'../Core/Color',
'../Core/combine',
'../Core/ComponentDatatype',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/FeatureDetection',
'../Core/getStringFromTypedArray',
'../Core/Matrix4',
'../Core/oneTimeWarning',
'../Core/PrimitiveType',
'../Core/RuntimeError',
'../Core/Transforms',
'../Renderer/Buffer',
'../Renderer/BufferUsage',
'../Renderer/DrawCommand',
'../Renderer/Pass',
'../Renderer/RenderState',
'../Renderer/ShaderProgram',
'../Renderer/ShaderSource',
'../Renderer/VertexArray',
'../ThirdParty/when',
'./BlendingState',
'./Cesium3DTileBatchTable',
'./Cesium3DTileFeature',
'./Cesium3DTileFeatureTable',
'./SceneMode',
'./ShadowMode'
], function(
Cartesian2,
Cartesian3,
Cartesian4,
Color,
combine,
ComponentDatatype,
defaultValue,
defined,
defineProperties,
destroyObject,
DeveloperError,
FeatureDetection,
getStringFromTypedArray,
Matrix4,
oneTimeWarning,
PrimitiveType,
RuntimeError,
Transforms,
Buffer,
BufferUsage,
DrawCommand,
Pass,
RenderState,
ShaderProgram,
ShaderSource,
VertexArray,
when,
BlendingState,
Cesium3DTileBatchTable,
Cesium3DTileFeature,
Cesium3DTileFeatureTable,
SceneMode,
ShadowMode) {
'use strict';
// Bail out if the browser doesn't support typed arrays, to prevent the setup function
// from failing, since we won't be able to create a WebGL context anyway.
if (!FeatureDetection.supportsTypedArrays()) {
return {};
}
/**
* Represents the contents of a
* {@link https://github.com/AnalyticalGraphicsInc/3d-tiles/blob/master/TileFormats/PointCloud/README.md|Points}
* tile in a {@link https://github.com/AnalyticalGraphicsInc/3d-tiles/blob/master/README.md|3D Tiles} tileset.
* <p>
* Implements the {@link Cesium3DTileContent} interface.
* </p>
*
* @alias PointCloud3DTileContent
* @constructor
*
* @private
*/
function PointCloud3DTileContent(tileset, tile, url, arrayBuffer, byteOffset) {
this._tileset = tileset;
this._tile = tile;
this._url = url;
// Hold onto the payload until the render resources are created
this._parsedContent = undefined;
this._drawCommand = undefined;
this._pickCommand = undefined;
this._pickId = undefined; // Only defined when batchTable is undefined
this._isTranslucent = false;
this._styleTranslucent = false;
this._constantColor = Color.clone(Color.WHITE);
this._rtcCenter = undefined;
this._batchTable = undefined; // Used when feature table contains BATCH_ID semantic
// These values are used to regenerate the shader when the style changes
this._styleableShaderAttributes = undefined;
this._isQuantized = false;
this._isOctEncoded16P = false;
this._isRGB565 = false;
this._hasColors = false;
this._hasNormals = false;
this._hasBatchIds = false;
// Use per-point normals to hide back-facing points.
this.backFaceCulling = false;
this._backFaceCulling = false;
this._opaqueRenderState = undefined;
this._translucentRenderState = undefined;
this._highlightColor = Color.clone(Color.WHITE);
this._pointSize = 1.0;
this._quantizedVolumeScale = undefined;
this._quantizedVolumeOffset = undefined;
this._modelMatrix = Matrix4.clone(Matrix4.IDENTITY);
this._mode = undefined;
this._readyPromise = when.defer();
this._pointsLength = 0;
this._geometryByteLength = 0;
this._features = undefined;
/**
* @inheritdoc Cesium3DTileContent#featurePropertiesDirty
*/
this.featurePropertiesDirty = false;
initialize(this, arrayBuffer, byteOffset);
}
defineProperties(PointCloud3DTileContent.prototype, {
/**
* @inheritdoc Cesium3DTileContent#featuresLength
*/
featuresLength : {
get : function() {
if (defined(this._batchTable)) {
return this._batchTable.featuresLength;
}
return 0;
}
},
/**
* @inheritdoc Cesium3DTileContent#pointsLength
*/
pointsLength : {
get : function() {
return this._pointsLength;
}
},
/**
* @inheritdoc Cesium3DTileContent#trianglesLength
*/
trianglesLength : {
get : function() {
return 0;
}
},
/**
* @inheritdoc Cesium3DTileContent#geometryByteLength
*/
geometryByteLength : {
get : function() {
return this._geometryByteLength;
}
},
/**
* @inheritdoc Cesium3DTileContent#texturesByteLength
*/
texturesByteLength : {
get : function() {
return 0;
}
},
/**
* @inheritdoc Cesium3DTileContent#batchTableByteLength
*/
batchTableByteLength : {
get : function() {
if (defined(this._batchTable)) {
return this._batchTable.memorySizeInBytes;
}
return 0;
}
},
/**
* @inheritdoc Cesium3DTileContent#innerContents
*/
innerContents : {
get : function() {
return undefined;
}
},
/**
* @inheritdoc Cesium3DTileContent#readyPromise
*/
readyPromise : {
get : function() {
return this._readyPromise.promise;
}
},
/**
* @inheritdoc Cesium3DTileContent#tileset
*/
tileset : {
get : function() {
return this._tileset;
}
},
/**
* @inheritdoc Cesium3DTileContent#tile
*/
tile : {
get : function() {
return this._tile;
}
},
/**
* @inheritdoc Cesium3DTileContent#url
*/
url : {
get : function() {
return this._url;
}
},
/**
* @inheritdoc Cesium3DTileContent#batchTable
*/
batchTable : {
get : function() {
return this._batchTable;
}
}
});
var sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
function initialize(content, arrayBuffer, byteOffset) {
byteOffset = defaultValue(byteOffset, 0);
var uint8Array = new Uint8Array(arrayBuffer);
var view = new DataView(arrayBuffer);
byteOffset += sizeOfUint32; // Skip magic
var version = view.getUint32(byteOffset, true);
if (version !== 1) {
throw new RuntimeError('Only Point Cloud tile version 1 is supported. Version ' + version + ' is not.');
}
byteOffset += sizeOfUint32;
// Skip byteLength
byteOffset += sizeOfUint32;
var featureTableJsonByteLength = view.getUint32(byteOffset, true);
if (featureTableJsonByteLength === 0) {
throw new RuntimeError('Feature table must have a byte length greater than zero');
}
byteOffset += sizeOfUint32;
var featureTableBinaryByteLength = view.getUint32(byteOffset, true);
byteOffset += sizeOfUint32;
var batchTableJsonByteLength = view.getUint32(byteOffset, true);
byteOffset += sizeOfUint32;
var batchTableBinaryByteLength = view.getUint32(byteOffset, true);
byteOffset += sizeOfUint32;
var featureTableString = getStringFromTypedArray(uint8Array, byteOffset, featureTableJsonByteLength);
var featureTableJson = JSON.parse(featureTableString);
byteOffset += featureTableJsonByteLength;
var featureTableBinary = new Uint8Array(arrayBuffer, byteOffset, featureTableBinaryByteLength);
byteOffset += featureTableBinaryByteLength;
// Get the batch table JSON and binary
var batchTableJson;
var batchTableBinary;
if (batchTableJsonByteLength > 0) {
// Has a batch table JSON
var batchTableString = getStringFromTypedArray(uint8Array, byteOffset, batchTableJsonByteLength);
batchTableJson = JSON.parse(batchTableString);
byteOffset += batchTableJsonByteLength;
if (batchTableBinaryByteLength > 0) {
// Has a batch table binary
batchTableBinary = new Uint8Array(arrayBuffer, byteOffset, batchTableBinaryByteLength);
byteOffset += batchTableBinaryByteLength;
}
}
var featureTable = new Cesium3DTileFeatureTable(featureTableJson, featureTableBinary);
var pointsLength = featureTable.getGlobalProperty('POINTS_LENGTH');
featureTable.featuresLength = pointsLength;
if (!defined(pointsLength)) {
throw new RuntimeError('Feature table global property: POINTS_LENGTH must be defined');
}
// Get the positions
var positions;
var isQuantized = false;
if (defined(featureTableJson.POSITION)) {
positions = featureTable.getPropertyArray('POSITION', ComponentDatatype.FLOAT, 3);
var rtcCenter = featureTable.getGlobalProperty('RTC_CENTER', ComponentDatatype.FLOAT, 3);
if (defined(rtcCenter)) {
content._rtcCenter = Cartesian3.unpack(rtcCenter);
}
} else if (defined(featureTableJson.POSITION_QUANTIZED)) {
positions = featureTable.getPropertyArray('POSITION_QUANTIZED', ComponentDatatype.UNSIGNED_SHORT, 3);
isQuantized = true;
var quantizedVolumeScale = featureTable.getGlobalProperty('QUANTIZED_VOLUME_SCALE', ComponentDatatype.FLOAT, 3);
if (!defined(quantizedVolumeScale)) {
throw new RuntimeError('Global property: QUANTIZED_VOLUME_SCALE must be defined for quantized positions.');
}
content._quantizedVolumeScale = Cartesian3.unpack(quantizedVolumeScale);
var quantizedVolumeOffset = featureTable.getGlobalProperty('QUANTIZED_VOLUME_OFFSET', ComponentDatatype.FLOAT, 3);
if (!defined(quantizedVolumeOffset)) {
throw new RuntimeError('Global property: QUANTIZED_VOLUME_OFFSET must be defined for quantized positions.');
}
content._quantizedVolumeOffset = Cartesian3.unpack(quantizedVolumeOffset);
}
if (!defined(positions)) {
throw new RuntimeError('Either POSITION or POSITION_QUANTIZED must be defined.');
}
// Get the colors
var colors;
var isTranslucent = false;
var isRGB565 = false;
if (defined(featureTableJson.RGBA)) {
colors = featureTable.getPropertyArray('RGBA', ComponentDatatype.UNSIGNED_BYTE, 4);
isTranslucent = true;
} else if (defined(featureTableJson.RGB)) {
colors = featureTable.getPropertyArray('RGB', ComponentDatatype.UNSIGNED_BYTE, 3);
} else if (defined(featureTableJson.RGB565)) {
colors = featureTable.getPropertyArray('RGB565', ComponentDatatype.UNSIGNED_SHORT, 1);
isRGB565 = true;
} else if (defined(featureTableJson.CONSTANT_RGBA)) {
var constantRGBA = featureTable.getGlobalProperty('CONSTANT_RGBA', ComponentDatatype.UNSIGNED_BYTE, 4);
content._constantColor = Color.fromBytes(constantRGBA[0], constantRGBA[1], constantRGBA[2], constantRGBA[3], content._constantColor);
} else {
// Use a default constant color
content._constantColor = Color.clone(Color.DARKGRAY, content._constantColor);
}
content._isTranslucent = isTranslucent;
// Get the normals
var normals;
var isOctEncoded16P = false;
if (defined(featureTableJson.NORMAL)) {
normals = featureTable.getPropertyArray('NORMAL', ComponentDatatype.FLOAT, 3);
} else if (defined(featureTableJson.NORMAL_OCT16P)) {
normals = featureTable.getPropertyArray('NORMAL_OCT16P', ComponentDatatype.UNSIGNED_BYTE, 2);
isOctEncoded16P = true;
}
// Get the batchIds and batch table. BATCH_ID does not need to be defined when the point cloud has per-point properties.
var batchIds;
if (defined(featureTableJson.BATCH_ID)) {
batchIds = featureTable.getPropertyArray('BATCH_ID', ComponentDatatype.UNSIGNED_SHORT, 1);
var batchLength = featureTable.getGlobalProperty('BATCH_LENGTH');
if (!defined(batchLength)) {
throw new RuntimeError('Global property: BATCH_LENGTH must be defined when BATCH_ID is defined.');
}
if (defined(batchTableBinary)) {
// Copy the batchTableBinary section and let the underlying ArrayBuffer be freed
batchTableBinary = new Uint8Array(batchTableBinary);
}
content._batchTable = new Cesium3DTileBatchTable(content, batchLength, batchTableJson, batchTableBinary);
}
// If points are not batched and there are per-point properties, use these properties for styling purposes
var styleableProperties;
if (!defined(batchIds) && defined(batchTableBinary)) {
styleableProperties = Cesium3DTileBatchTable.getBinaryProperties(pointsLength, batchTableJson, batchTableBinary);
// WebGL does not support UNSIGNED_INT, INT, or DOUBLE vertex attributes. Convert these to FLOAT.
for (var name in styleableProperties) {
if (styleableProperties.hasOwnProperty(name)) {
var property = styleableProperties[name];
var typedArray = property.typedArray;
var componentDatatype = ComponentDatatype.fromTypedArray(typedArray);
if (componentDatatype === ComponentDatatype.INT || componentDatatype === ComponentDatatype.UNSIGNED_INT || componentDatatype === ComponentDatatype.DOUBLE) {
oneTimeWarning('Cast pnts property to floats', 'Point cloud property "' + name + '" will be casted to a float array because INT, UNSIGNED_INT, and DOUBLE are not valid WebGL vertex attribute types. Some precision may be lost.');
property.typedArray = new Float32Array(typedArray);
}
}
}
}
content._parsedContent = {
positions : positions,
colors : colors,
normals : normals,
batchIds : batchIds,
styleableProperties : styleableProperties
};
content._pointsLength = pointsLength;
content._isQuantized = isQuantized;
content._isOctEncoded16P = isOctEncoded16P;
content._isRGB565 = isRGB565;
content._hasColors = defined(colors);
content._hasNormals = defined(normals);
content._hasBatchIds = defined(batchIds);
}
var scratchPointSizeAndTilesetTime = new Cartesian2();
var positionLocation = 0;
var colorLocation = 1;
var normalLocation = 2;
var batchIdLocation = 3;
var numberOfAttributes = 4;
function createResources(content, frameState) {
var context = frameState.context;
var parsedContent = content._parsedContent;
var pointsLength = content._pointsLength;
var positions = parsedContent.positions;
var colors = parsedContent.colors;
var normals = parsedContent.normals;
var batchIds = parsedContent.batchIds;
var styleableProperties = parsedContent.styleableProperties;
var hasStyleableProperties = defined(styleableProperties);
var isQuantized = content._isQuantized;
var isOctEncoded16P = content._isOctEncoded16P;
var isRGB565 = content._isRGB565;
var isTranslucent = content._isTranslucent;
var hasColors = content._hasColors;
var hasNormals = content._hasNormals;
var hasBatchIds = content._hasBatchIds;
var batchTable = content._batchTable;
var hasBatchTable = defined(batchTable);
var styleableVertexAttributes = [];
var styleableShaderAttributes = {};
content._styleableShaderAttributes = styleableShaderAttributes;
if (hasStyleableProperties) {
var attributeLocation = numberOfAttributes;
for (var name in styleableProperties) {
if (styleableProperties.hasOwnProperty(name)) {
var property = styleableProperties[name];
var typedArray = property.typedArray;
var componentCount = property.componentCount;
var componentDatatype = ComponentDatatype.fromTypedArray(typedArray);
var vertexBuffer = Buffer.createVertexBuffer({
context : context,
typedArray : property.typedArray,
usage : BufferUsage.STATIC_DRAW
});
content._geometryByteLength += vertexBuffer.sizeInBytes;
var vertexAttribute = {
index : attributeLocation,
vertexBuffer : vertexBuffer,
componentsPerAttribute : componentCount,
componentDatatype : componentDatatype,
normalize : false,
offsetInBytes : 0,
strideInBytes : 0
};
styleableVertexAttributes.push(vertexAttribute);
styleableShaderAttributes[name] = {
location : attributeLocation,
componentCount : componentCount
};
++attributeLocation;
}
}
}
var uniformMap = {
u_pointSizeAndTilesetTime : function() {
scratchPointSizeAndTilesetTime.x = content._pointSize;
scratchPointSizeAndTilesetTime.y = content._tileset.timeSinceLoad;
return scratchPointSizeAndTilesetTime;
},
u_highlightColor : function() {
return content._highlightColor;
},
u_constantColor : function() {
return content._constantColor;
}
};
if (isQuantized) {
uniformMap = combine(uniformMap, {
u_quantizedVolumeScale : function() {
return content._quantizedVolumeScale;
}
});
}
var positionsVertexBuffer = Buffer.createVertexBuffer({
context : context,
typedArray : positions,
usage : BufferUsage.STATIC_DRAW
});
content._geometryByteLength += positionsVertexBuffer.sizeInBytes;
var colorsVertexBuffer;
if (hasColors) {
colorsVertexBuffer = Buffer.createVertexBuffer({
context : context,
typedArray : colors,
usage : BufferUsage.STATIC_DRAW
});
content._geometryByteLength += colorsVertexBuffer.sizeInBytes;
}
var normalsVertexBuffer;
if (hasNormals) {
normalsVertexBuffer = Buffer.createVertexBuffer({
context : context,
typedArray : normals,
usage : BufferUsage.STATIC_DRAW
});
content._geometryByteLength += normalsVertexBuffer.sizeInBytes;
}
var batchIdsVertexBuffer;
if (hasBatchIds) {
batchIdsVertexBuffer = Buffer.createVertexBuffer({
context : context,
typedArray : batchIds,
usage : BufferUsage.STATIC_DRAW
});
content._geometryByteLength += batchIdsVertexBuffer.sizeInBytes;
}
var attributes = [];
if (isQuantized) {
attributes.push({
index : positionLocation,
vertexBuffer : positionsVertexBuffer,
componentsPerAttribute : 3,
componentDatatype : ComponentDatatype.UNSIGNED_SHORT,
normalize : true, // Convert position to 0 to 1 before entering the shader
offsetInBytes : 0,
strideInBytes : 0
});
} else {
attributes.push({
index : positionLocation,
vertexBuffer : positionsVertexBuffer,
componentsPerAttribute : 3,
componentDatatype : ComponentDatatype.FLOAT,
normalize : false,
offsetInBytes : 0,
strideInBytes : 0
});
}
if (hasColors) {
if (isRGB565) {
attributes.push({
index : colorLocation,
vertexBuffer : colorsVertexBuffer,
componentsPerAttribute : 1,
componentDatatype : ComponentDatatype.UNSIGNED_SHORT,
normalize : false,
offsetInBytes : 0,
strideInBytes : 0
});
} else {
var colorComponentsPerAttribute = isTranslucent ? 4 : 3;
attributes.push({
index : colorLocation,
vertexBuffer : colorsVertexBuffer,
componentsPerAttribute : colorComponentsPerAttribute,
componentDatatype : ComponentDatatype.UNSIGNED_BYTE,
normalize : true,
offsetInBytes : 0,
strideInBytes : 0
});
}
}
if (hasNormals) {
if (isOctEncoded16P) {
attributes.push({
index : normalLocation,
vertexBuffer : normalsVertexBuffer,
componentsPerAttribute : 2,
componentDatatype : ComponentDatatype.UNSIGNED_BYTE,
normalize : false,
offsetInBytes : 0,
strideInBytes : 0
});
} else {
attributes.push({
index : normalLocation,
vertexBuffer : normalsVertexBuffer,
componentsPerAttribute : 3,
componentDatatype : ComponentDatatype.FLOAT,
normalize : false,
offsetInBytes : 0,
strideInBytes : 0
});
}
}
if (hasBatchIds) {
attributes.push({
index : batchIdLocation,
vertexBuffer : batchIdsVertexBuffer,
componentsPerAttribute : 1,
componentDatatype : ComponentDatatype.fromTypedArray(batchIds),
normalize : false,
offsetInBytes : 0,
strideInBytes : 0
});
}
if (hasStyleableProperties) {
attributes = attributes.concat(styleableVertexAttributes);
}
var vertexArray = new VertexArray({
context : context,
attributes : attributes
});
var drawUniformMap = uniformMap;
if (hasBatchTable) {
drawUniformMap = batchTable.getUniformMapCallback()(uniformMap);
}
var pickUniformMap;
if (hasBatchTable) {
pickUniformMap = batchTable.getPickUniformMapCallback()(uniformMap);
} else {
content._pickId = context.createPickId({
primitive : content._tileset,
content : content
});
pickUniformMap = combine(uniformMap, {
czm_pickColor : function() {
return content._pickId.color;
}
});
}
content._opaqueRenderState = RenderState.fromCache({
depthTest : {
enabled : true
}
});
content._translucentRenderState = RenderState.fromCache({
depthTest : {
enabled : true
},
depthMask : false,
blending : BlendingState.ALPHA_BLEND
});
content._drawCommand = new DrawCommand({
boundingVolume : undefined, // Updated in update
cull : false, // Already culled by 3D Tiles
modelMatrix : new Matrix4(),
primitiveType : PrimitiveType.POINTS,
vertexArray : vertexArray,
count : pointsLength,
shaderProgram : undefined, // Updated in createShaders
uniformMap : drawUniformMap,
renderState : isTranslucent ? content._translucentRenderState : content._opaqueRenderState,
pass : isTranslucent ? Pass.TRANSLUCENT : Pass.CESIUM_3D_TILE,
owner : content,
castShadows : false,
receiveShadows : false
});
content._pickCommand = new DrawCommand({
boundingVolume : undefined, // Updated in update
cull : false, // Already culled by 3D Tiles
modelMatrix : new Matrix4(),
primitiveType : PrimitiveType.POINTS,
vertexArray : vertexArray,
count : pointsLength,
shaderProgram : undefined, // Updated in createShaders
uniformMap : pickUniformMap,
renderState : isTranslucent ? content._translucentRenderState : content._opaqueRenderState,
pass : isTranslucent ? Pass.TRANSLUCENT : Pass.CESIUM_3D_TILE,
owner : content
});
}
var defaultProperties = ['POSITION', 'COLOR', 'NORMAL', 'POSITION_ABSOLUTE'];
function getStyleableProperties(source, properties) {
// Get all the properties used by this style
var regex = /czm_tiles3d_style_(\w+)/g;
var matches = regex.exec(source);
while (matches !== null) {
var name = matches[1];
if (properties.indexOf(name) === -1) {
properties.push(name);
}
matches = regex.exec(source);
}
}
function getVertexAttribute(vertexArray, index) {
var numberOfAttributes = vertexArray.numberOfAttributes;
for (var i = 0; i < numberOfAttributes; ++i) {
var attribute = vertexArray.getAttribute(i);
if (attribute.index === index) {
return attribute;
}
}
}
function modifyStyleFunction(source) {
// Replace occurrences of czm_tiles3d_style_DEFAULTPROPERTY
var length = defaultProperties.length;
for (var i = 0; i < length; ++i) {
var property = defaultProperties[i];
var styleName = 'czm_tiles3d_style_' + property;
var replaceName = property.toLowerCase();
source = source.replace(new RegExp(styleName + '(\\W)', 'g'), replaceName + '$1');
}
// Edit the function header to accept the point position, color, and normal
return source.replace('()', '(vec3 position, vec3 position_absolute, vec4 color, vec3 normal)');
}
function createShaders(content, frameState, style) {
var i;
var name;
var attribute;
var context = frameState.context;
var batchTable = content._batchTable;
var hasBatchTable = defined(batchTable);
var hasStyle = defined(style);
var isQuantized = content._isQuantized;
var isOctEncoded16P = content._isOctEncoded16P;
var isRGB565 = content._isRGB565;
var isTranslucent = content._isTranslucent;
var hasColors = content._hasColors;
var hasNormals = content._hasNormals;
var hasBatchIds = content._hasBatchIds;
var backFaceCulling = content._backFaceCulling;
var vertexArray = content._drawCommand.vertexArray;
var colorStyleFunction;
var showStyleFunction;
var pointSizeStyleFunction;
var styleTranslucent = isTranslucent;
if (hasBatchTable) {
// Styling is handled in the batch table
hasStyle = false;
}
if (hasStyle) {
var shaderState = {
translucent : false
};
colorStyleFunction = style.getColorShaderFunction('getColorFromStyle', 'czm_tiles3d_style_', shaderState);
showStyleFunction = style.getShowShaderFunction('getShowFromStyle', 'czm_tiles3d_style_', shaderState);
pointSizeStyleFunction = style.getPointSizeShaderFunction('getPointSizeFromStyle', 'czm_tiles3d_style_', shaderState);
if (defined(colorStyleFunction) && shaderState.translucent) {
styleTranslucent = true;
}
}
content._styleTranslucent = styleTranslucent;
var hasColorStyle = defined(colorStyleFunction);
var hasShowStyle = defined(showStyleFunction);
var hasPointSizeStyle = defined(pointSizeStyleFunction);
// Get the properties in use by the style
var styleableProperties = [];
if (hasColorStyle) {
getStyleableProperties(colorStyleFunction, styleableProperties);
colorStyleFunction = modifyStyleFunction(colorStyleFunction);
}
if (hasShowStyle) {
getStyleableProperties(showStyleFunction, styleableProperties);
showStyleFunction = modifyStyleFunction(showStyleFunction);
}
if (hasPointSizeStyle) {
getStyleableProperties(pointSizeStyleFunction, styleableProperties);
pointSizeStyleFunction = modifyStyleFunction(pointSizeStyleFunction);
}
var usesColorSemantic = styleableProperties.indexOf('COLOR') >= 0;
var usesNormalSemantic = styleableProperties.indexOf('NORMAL') >= 0;
// Split default properties from user properties
var userProperties = styleableProperties.filter(function(property) { return defaultProperties.indexOf(property) === -1; });
if (usesNormalSemantic && !hasNormals) {
throw new RuntimeError('Style references the NORMAL semantic but the point cloud does not have normals');
}
// Disable vertex attributes that aren't used in the style, enable attributes that are
var styleableShaderAttributes = content._styleableShaderAttributes;
for (name in styleableShaderAttributes) {
if (styleableShaderAttributes.hasOwnProperty(name)) {
attribute = styleableShaderAttributes[name];
var enabled = (userProperties.indexOf(name) >= 0);
var vertexAttribute = getVertexAttribute(vertexArray, attribute.location);
vertexAttribute.enabled = enabled;
}
}
var usesColors = hasColors && (!hasColorStyle || usesColorSemantic);
if (hasColors) {
// Disable the color vertex attribute if the color style does not reference the color semantic
var colorVertexAttribute = getVertexAttribute(vertexArray, colorLocation);
colorVertexAttribute.enabled = usesColors;
}
var attributeLocations = {
a_position : positionLocation
};
if (usesColors) {
attributeLocations.a_color = colorLocation;
}
if (hasNormals) {
attributeLocations.a_normal = normalLocation;
}
if (hasBatchIds) {
attributeLocations.a_batchId = batchIdLocation;
}
var attributeDeclarations = '';
var length = userProperties.length;
for (i = 0; i < length; ++i) {
name = userProperties[i];
attribute = styleableShaderAttributes[name];
if (!defined(attribute)) {
throw new RuntimeError('Style references a property "' + name + '" that does not exist or is not styleable.');
}
var componentCount = attribute.componentCount;
var attributeName = 'czm_tiles3d_style_' + name;
var attributeType;
if (componentCount === 1) {
attributeType = 'float';
} else {
attributeType = 'vec' + componentCount;
}
attributeDeclarations += 'attribute ' + attributeType + ' ' + attributeName + '; \n';
attributeLocations[attributeName] = attribute.location;
}
var vs = 'attribute vec3 a_position; \n' +
'varying vec4 v_color; \n' +
'uniform vec2 u_pointSizeAndTilesetTime; \n' +
'uniform vec4 u_constantColor; \n' +
'uniform vec4 u_highlightColor; \n' +
'float u_pointSize; \n' +
'float u_tilesetTime; \n';
vs += attributeDeclarations;
if (usesColors) {
if (isTranslucent) {
vs += 'attribute vec4 a_color; \n';
} else if (isRGB565) {
vs += 'attribute float a_color; \n' +
'const float SHIFT_RIGHT_11 = 1.0 / 2048.0; \n' +
'const float SHIFT_RIGHT_5 = 1.0 / 32.0; \n' +
'const float SHIFT_LEFT_11 = 2048.0; \n' +
'const float SHIFT_LEFT_5 = 32.0; \n' +
'const float NORMALIZE_6 = 1.0 / 64.0; \n' +
'const float NORMALIZE_5 = 1.0 / 32.0; \n';
} else {
vs += 'attribute vec3 a_color; \n';
}
}
if (hasNormals) {
if (isOctEncoded16P) {
vs += 'attribute vec2 a_normal; \n';
} else {
vs += 'attribute vec3 a_normal; \n';
}
}
if (hasBatchIds) {
vs += 'attribute float a_batchId; \n';
}
if (isQuantized) {
vs += 'uniform vec3 u_quantizedVolumeScale; \n';
}
if (hasColorStyle) {
vs += colorStyleFunction;
}
if (hasShowStyle) {
vs += showStyleFunction;
}
if (hasPointSizeStyle) {
vs += pointSizeStyleFunction;
}
vs += 'void main() \n' +
'{ \n' +
' u_pointSize = u_pointSizeAndTilesetTime.x; \n' +
' u_tilesetTime = u_pointSizeAndTilesetTime.y; \n';
if (usesColors) {
if (isTranslucent) {
vs += ' vec4 color = a_color; \n';
} else if (isRGB565) {
vs += ' float compressed = a_color; \n' +
' float r = floor(compressed * SHIFT_RIGHT_11); \n' +
' compressed -= r * SHIFT_LEFT_11; \n' +
' float g = floor(compressed * SHIFT_RIGHT_5); \n' +
' compressed -= g * SHIFT_LEFT_5; \n' +
' float b = compressed; \n' +
' vec3 rgb = vec3(r * NORMALIZE_5, g * NORMALIZE_6, b * NORMALIZE_5); \n' +
' vec4 color = vec4(rgb, 1.0); \n';
} else {
vs += ' vec4 color = vec4(a_color, 1.0); \n';
}
} else {
vs += ' vec4 color = u_constantColor; \n';
}
if (isQuantized) {
vs += ' vec3 position = a_position * u_quantizedVolumeScale; \n';
} else {
vs += ' vec3 position = a_position; \n';
}
vs += ' vec3 position_absolute = vec3(czm_model * vec4(position, 1.0)); \n';
if (hasNormals) {
if (isOctEncoded16P) {
vs += ' vec3 normal = czm_octDecode(a_normal); \n';
} else {
vs += ' vec3 normal = a_normal; \n';
}
} else {
vs += ' vec3 normal = vec3(1.0); \n';
}
if (hasColorStyle) {
vs += ' color = getColorFromStyle(position, position_absolute, color, normal); \n';
}
if (hasShowStyle) {
vs += ' float show = float(getShowFromStyle(position, position_absolute, color, normal)); \n';
}