-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathClassificationPrimitive.js
1260 lines (1122 loc) · 55 KB
/
ClassificationPrimitive.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/ColorGeometryInstanceAttribute',
'../Core/combine',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/GeometryInstance',
'../Core/isArray',
'../Renderer/DrawCommand',
'../Renderer/Pass',
'../Renderer/RenderState',
'../Renderer/ShaderProgram',
'../Renderer/ShaderSource',
'../Shaders/ShadowVolumeFS',
'../Shaders/ShadowVolumeAppearanceVS',
'../ThirdParty/when',
'./BlendingState',
'./ClassificationType',
'./DepthFunction',
'./PerInstanceColorAppearance',
'./Primitive',
'./SceneMode',
'./ShadowVolumeAppearance',
'./StencilConstants',
'./StencilFunction',
'./StencilOperation'
], function(
ColorGeometryInstanceAttribute,
combine,
defaultValue,
defined,
defineProperties,
destroyObject,
DeveloperError,
GeometryInstance,
isArray,
DrawCommand,
Pass,
RenderState,
ShaderProgram,
ShaderSource,
ShadowVolumeFS,
ShadowVolumeAppearanceVS,
when,
BlendingState,
ClassificationType,
DepthFunction,
PerInstanceColorAppearance,
Primitive,
SceneMode,
ShadowVolumeAppearance,
StencilConstants,
StencilFunction,
StencilOperation) {
'use strict';
var ClassificationPrimitiveReadOnlyInstanceAttributes = ['color'];
/**
* A classification primitive represents a volume enclosing geometry in the {@link Scene} to be highlighted.
* <p>
* A primitive combines geometry instances with an {@link Appearance} that describes the full shading, including
* {@link Material} and {@link RenderState}. Roughly, the geometry instance defines the structure and placement,
* and the appearance defines the visual characteristics. Decoupling geometry and appearance allows us to mix
* and match most of them and add a new geometry or appearance independently of each other.
* Only {@link PerInstanceColorAppearance} with the same color across all instances is supported at this time when using
* ClassificationPrimitive directly.
* For full {@link Appearance} support when classifying terrain or 3D Tiles use {@link GroundPrimitive} instead.
* </p>
* <p>
* For correct rendering, this feature requires the EXT_frag_depth WebGL extension. For hardware that do not support this extension, there
* will be rendering artifacts for some viewing angles.
* </p>
* <p>
* Valid geometries are {@link BoxGeometry}, {@link CylinderGeometry}, {@link EllipsoidGeometry}, {@link PolylineVolumeGeometry}, and {@link SphereGeometry}.
* </p>
* <p>
* Geometries that follow the surface of the ellipsoid, such as {@link CircleGeometry}, {@link CorridorGeometry}, {@link EllipseGeometry}, {@link PolygonGeometry}, and {@link RectangleGeometry},
* are also valid if they are extruded volumes; otherwise, they will not be rendered.
* </p>
*
* @alias ClassificationPrimitive
* @constructor
*
* @param {Object} [options] Object with the following properties:
* @param {Array|GeometryInstance} [options.geometryInstances] The geometry instances to render. This can either be a single instance or an array of length one.
* @param {Appearance} [options.appearance] The appearance used to render the primitive. Defaults to PerInstanceColorAppearance when GeometryInstances have a color attribute.
* @param {Boolean} [options.show=true] Determines if this primitive will be shown.
* @param {Boolean} [options.vertexCacheOptimize=false] When <code>true</code>, geometry vertices are optimized for the pre and post-vertex-shader caches.
* @param {Boolean} [options.interleave=false] When <code>true</code>, geometry vertex attributes are interleaved, which can slightly improve rendering performance but increases load time.
* @param {Boolean} [options.compressVertices=true] When <code>true</code>, the geometry vertices are compressed, which will save memory.
* @param {Boolean} [options.releaseGeometryInstances=true] When <code>true</code>, the primitive does not keep a reference to the input <code>geometryInstances</code> to save memory.
* @param {Boolean} [options.allowPicking=true] When <code>true</code>, each geometry instance will only be pickable with {@link Scene#pick}. When <code>false</code>, GPU memory is saved.
* @param {Boolean} [options.asynchronous=true] Determines if the primitive will be created asynchronously or block until ready. If false initializeTerrainHeights() must be called first.
* @param {ClassificationType} [options.classificationType=ClassificationType.BOTH] Determines whether terrain, 3D Tiles or both will be classified.
* @param {Boolean} [options.debugShowBoundingVolume=false] For debugging only. Determines if this primitive's commands' bounding spheres are shown.
* @param {Boolean} [options.debugShowShadowVolume=false] For debugging only. Determines if the shadow volume for each geometry in the primitive is drawn. Must be <code>true</code> on
* creation for the volumes to be created before the geometry is released or options.releaseGeometryInstance must be <code>false</code>.
*
* @see Primitive
* @see GroundPrimitive
* @see GeometryInstance
* @see Appearance
*/
function ClassificationPrimitive(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var geometryInstances = options.geometryInstances;
/**
* The geometry instance rendered with this primitive. This may
* be <code>undefined</code> if <code>options.releaseGeometryInstances</code>
* is <code>true</code> when the primitive is constructed.
* <p>
* Changing this property after the primitive is rendered has no effect.
* </p>
* <p>
* Because of the rendering technique used, all geometry instances must be the same color.
* If there is an instance with a differing color, a <code>DeveloperError</code> will be thrown
* on the first attempt to render.
* </p>
*
* @readonly
* @type {Array|GeometryInstance}
*
* @default undefined
*/
this.geometryInstances = geometryInstances;
/**
* Determines if the primitive will be shown. This affects all geometry
* instances in the primitive.
*
* @type {Boolean}
*
* @default true
*/
this.show = defaultValue(options.show, true);
/**
* Determines whether terrain, 3D Tiles or both will be classified.
*
* @type {ClassificationType}
*
* @default ClassificationType.BOTH
*/
this.classificationType = defaultValue(options.classificationType, ClassificationType.BOTH);
/**
* 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 property is for debugging only; it is not for production use nor is it optimized.
* <p>
* Draws the shadow volume for each geometry in the primitive.
* </p>
*
* @type {Boolean}
*
* @default false
*/
this.debugShowShadowVolume = defaultValue(options.debugShowShadowVolume, false);
this._debugShowShadowVolume = false;
// These are used by GroundPrimitive to augment the shader and uniform map.
this._extruded = defaultValue(options._extruded, false);
this._uniformMap = options._uniformMap;
this._sp = undefined;
this._spStencil = undefined;
this._spPick = undefined;
this._spColor = undefined;
this._spPick2D = undefined; // only derived if necessary
this._spColor2D = undefined; // only derived if necessary
this._rsStencilPreloadPass = undefined;
this._rsStencilPreloadPass3DTiles = undefined;
this._rsStencilDepthPass = undefined;
this._rsStencilDepthPass3DTiles = undefined;
this._rsColorPass = undefined;
this._rsPickPass = undefined;
this._commandsIgnoreShow = [];
this._ready = false;
this._readyPromise = when.defer();
this._primitive = undefined;
this._pickPrimitive = options._pickPrimitive;
// Set in update
this._hasSphericalExtentsAttribute = false;
this._hasPlanarExtentsAttributes = false;
this._hasPerColorAttribute = false;
this.appearance = options.appearance;
var readOnlyAttributes;
if (defined(geometryInstances) && isArray(geometryInstances) && geometryInstances.length > 1) {
readOnlyAttributes = ClassificationPrimitiveReadOnlyInstanceAttributes;
}
this._createBoundingVolumeFunction = options._createBoundingVolumeFunction;
this._updateAndQueueCommandsFunction = options._updateAndQueueCommandsFunction;
this._usePickOffsets = false;
this._primitiveOptions = {
geometryInstances : undefined,
appearance : undefined,
vertexCacheOptimize : defaultValue(options.vertexCacheOptimize, false),
interleave : defaultValue(options.interleave, false),
releaseGeometryInstances : defaultValue(options.releaseGeometryInstances, true),
allowPicking : defaultValue(options.allowPicking, true),
asynchronous : defaultValue(options.asynchronous, true),
compressVertices : defaultValue(options.compressVertices, true),
_readOnlyInstanceAttributes : readOnlyAttributes,
_createBoundingVolumeFunction : undefined,
_createRenderStatesFunction : undefined,
_createShaderProgramFunction : undefined,
_createCommandsFunction : undefined,
_updateAndQueueCommandsFunction : undefined,
_createPickOffsets : true
};
}
defineProperties(ClassificationPrimitive.prototype, {
/**
* When <code>true</code>, geometry vertices are optimized for the pre and post-vertex-shader caches.
*
* @memberof ClassificationPrimitive.prototype
*
* @type {Boolean}
* @readonly
*
* @default true
*/
vertexCacheOptimize : {
get : function() {
return this._primitiveOptions.vertexCacheOptimize;
}
},
/**
* Determines if geometry vertex attributes are interleaved, which can slightly improve rendering performance.
*
* @memberof ClassificationPrimitive.prototype
*
* @type {Boolean}
* @readonly
*
* @default false
*/
interleave : {
get : function() {
return this._primitiveOptions.interleave;
}
},
/**
* When <code>true</code>, the primitive does not keep a reference to the input <code>geometryInstances</code> to save memory.
*
* @memberof ClassificationPrimitive.prototype
*
* @type {Boolean}
* @readonly
*
* @default true
*/
releaseGeometryInstances : {
get : function() {
return this._primitiveOptions.releaseGeometryInstances;
}
},
/**
* When <code>true</code>, each geometry instance will only be pickable with {@link Scene#pick}. When <code>false</code>, GPU memory is saved.
*
* @memberof ClassificationPrimitive.prototype
*
* @type {Boolean}
* @readonly
*
* @default true
*/
allowPicking : {
get : function() {
return this._primitiveOptions.allowPicking;
}
},
/**
* Determines if the geometry instances will be created and batched on a web worker.
*
* @memberof ClassificationPrimitive.prototype
*
* @type {Boolean}
* @readonly
*
* @default true
*/
asynchronous : {
get : function() {
return this._primitiveOptions.asynchronous;
}
},
/**
* When <code>true</code>, geometry vertices are compressed, which will save memory.
*
* @memberof ClassificationPrimitive.prototype
*
* @type {Boolean}
* @readonly
*
* @default true
*/
compressVertices : {
get : function() {
return this._primitiveOptions.compressVertices;
}
},
/**
* Determines if the primitive is complete and ready to render. If this property is
* true, the primitive will be rendered the next time that {@link ClassificationPrimitive#update}
* is called.
*
* @memberof ClassificationPrimitive.prototype
*
* @type {Boolean}
* @readonly
*/
ready : {
get : function() {
return this._ready;
}
},
/**
* Gets a promise that resolves when the primitive is ready to render.
* @memberof ClassificationPrimitive.prototype
* @type {Promise.<ClassificationPrimitive>}
* @readonly
*/
readyPromise : {
get : function() {
return this._readyPromise.promise;
}
},
/**
* Returns true if the ClassificationPrimitive needs a separate shader and commands for 2D.
* This is because texture coordinates on ClassificationPrimitives are computed differently,
* and are used for culling when multiple GeometryInstances are batched in one ClassificationPrimitive.
* @memberof ClassificationPrimitive.prototype
* @type {Boolean}
* @readonly
* @private
*/
_needs2DShader : {
get : function() {
return this._hasPlanarExtentsAttributes || this._hasSphericalExtentsAttribute;
}
}
});
/**
* Determines if ClassificationPrimitive rendering is supported.
*
* @param {Scene} scene The scene.
* @returns {Boolean} <code>true</code> if ClassificationPrimitives are supported; otherwise, returns <code>false</code>
*/
ClassificationPrimitive.isSupported = function(scene) {
return scene.context.stencilBuffer;
};
function getStencilPreloadRenderState(enableStencil, mask3DTiles) {
var stencilFunction = mask3DTiles ? StencilFunction.EQUAL : StencilFunction.ALWAYS;
return {
colorMask : {
red : false,
green : false,
blue : false,
alpha : false
},
stencilTest : {
enabled : enableStencil,
frontFunction : stencilFunction,
frontOperation : {
fail : StencilOperation.KEEP,
zFail : StencilOperation.DECREMENT_WRAP,
zPass : StencilOperation.DECREMENT_WRAP
},
backFunction : stencilFunction,
backOperation : {
fail : StencilOperation.KEEP,
zFail : StencilOperation.INCREMENT_WRAP,
zPass : StencilOperation.INCREMENT_WRAP
},
reference : StencilConstants.CESIUM_3D_TILE_MASK,
mask : StencilConstants.CESIUM_3D_TILE_MASK
},
stencilMask : StencilConstants.CLASSIFICATION_MASK,
depthTest : {
enabled : false
},
depthMask : false
};
}
function getStencilDepthRenderState(enableStencil, mask3DTiles) {
var stencilFunction = mask3DTiles ? StencilFunction.EQUAL : StencilFunction.ALWAYS;
return {
colorMask : {
red : false,
green : false,
blue : false,
alpha : false
},
stencilTest : {
enabled : enableStencil,
frontFunction : stencilFunction,
frontOperation : {
fail : StencilOperation.KEEP,
zFail : StencilOperation.KEEP,
zPass : StencilOperation.INCREMENT_WRAP
},
backFunction : stencilFunction,
backOperation : {
fail : StencilOperation.KEEP,
zFail : StencilOperation.KEEP,
zPass : StencilOperation.DECREMENT_WRAP
},
reference : StencilConstants.CESIUM_3D_TILE_MASK,
mask : StencilConstants.CESIUM_3D_TILE_MASK
},
stencilMask : StencilConstants.CLASSIFICATION_MASK,
depthTest : {
enabled : true,
func : DepthFunction.LESS_OR_EQUAL
},
depthMask : false
};
}
function getColorRenderState(enableStencil) {
return {
stencilTest : {
enabled : enableStencil,
frontFunction : StencilFunction.NOT_EQUAL,
frontOperation : {
fail : StencilOperation.KEEP,
zFail : StencilOperation.KEEP,
zPass : StencilOperation.DECREMENT_WRAP
},
backFunction : StencilFunction.NOT_EQUAL,
backOperation : {
fail : StencilOperation.KEEP,
zFail : StencilOperation.KEEP,
zPass : StencilOperation.DECREMENT_WRAP
},
reference : 0,
mask : StencilConstants.CLASSIFICATION_MASK
},
stencilMask : StencilConstants.CLASSIFICATION_MASK,
depthTest : {
enabled : false
},
depthMask : false,
blending : BlendingState.ALPHA_BLEND
};
}
var pickRenderState = {
stencilTest : {
enabled : true,
frontFunction : StencilFunction.NOT_EQUAL,
frontOperation : {
fail : StencilOperation.KEEP,
zFail : StencilOperation.KEEP,
zPass : StencilOperation.DECREMENT_WRAP
},
backFunction : StencilFunction.NOT_EQUAL,
backOperation : {
fail : StencilOperation.KEEP,
zFail : StencilOperation.KEEP,
zPass : StencilOperation.DECREMENT_WRAP
},
reference : 0,
mask : StencilConstants.CLASSIFICATION_MASK
},
stencilMask : StencilConstants.CLASSIFICATION_MASK,
depthTest : {
enabled : false
},
depthMask : false
};
function createRenderStates(classificationPrimitive, context, appearance, twoPasses) {
if (defined(classificationPrimitive._rsStencilPreloadPass)) {
return;
}
var stencilEnabled = !classificationPrimitive.debugShowShadowVolume;
classificationPrimitive._rsStencilPreloadPass = RenderState.fromCache(getStencilPreloadRenderState(stencilEnabled, false));
classificationPrimitive._rsStencilPreloadPass3DTiles = RenderState.fromCache(getStencilPreloadRenderState(stencilEnabled, true));
classificationPrimitive._rsStencilDepthPass = RenderState.fromCache(getStencilDepthRenderState(stencilEnabled, false));
classificationPrimitive._rsStencilDepthPass3DTiles = RenderState.fromCache(getStencilDepthRenderState(stencilEnabled, true));
classificationPrimitive._rsColorPass = RenderState.fromCache(getColorRenderState(stencilEnabled, false));
classificationPrimitive._rsPickPass = RenderState.fromCache(pickRenderState);
}
function modifyForEncodedNormals(primitive, vertexShaderSource) {
if (!primitive.compressVertices) {
return vertexShaderSource;
}
if (vertexShaderSource.search(/attribute\s+vec3\s+extrudeDirection;/g) !== -1) {
var attributeName = 'compressedAttributes';
//only shadow volumes use extrudeDirection, and shadow volumes use vertexFormat: POSITION_ONLY so we don't need to check other attributes
var attributeDecl = 'attribute vec2 ' + attributeName + ';';
var globalDecl = 'vec3 extrudeDirection;\n';
var decode = ' extrudeDirection = czm_octDecode(' + attributeName + ', 65535.0);\n';
var modifiedVS = vertexShaderSource;
modifiedVS = modifiedVS.replace(/attribute\s+vec3\s+extrudeDirection;/g, '');
modifiedVS = ShaderSource.replaceMain(modifiedVS, 'czm_non_compressed_main');
var compressedMain =
'void main() \n' +
'{ \n' +
decode +
' czm_non_compressed_main(); \n' +
'}';
return [attributeDecl, globalDecl, modifiedVS, compressedMain].join('\n');
}
}
function createShaderProgram(classificationPrimitive, frameState) {
var context = frameState.context;
var primitive = classificationPrimitive._primitive;
var vs = ShadowVolumeAppearanceVS;
vs = classificationPrimitive._primitive._batchTable.getVertexShaderCallback()(vs);
vs = Primitive._appendDistanceDisplayConditionToShader(primitive, vs);
vs = Primitive._modifyShaderPosition(classificationPrimitive, vs, frameState.scene3DOnly);
vs = Primitive._updateColorAttribute(primitive, vs);
var planarExtents = classificationPrimitive._hasPlanarExtentsAttributes;
var cullFragmentsUsingExtents = planarExtents || classificationPrimitive._hasSphericalExtentsAttribute;
if (classificationPrimitive._extruded) {
vs = modifyForEncodedNormals(primitive, vs);
}
var extrudedDefine = classificationPrimitive._extruded ? 'EXTRUDED_GEOMETRY' : '';
// Tesselation on ClassificationPrimitives tends to be low,
// which causes problems when interpolating log depth from vertices.
// So force computing and writing logarithmic depth in the fragment shader.
// Re-enable at far distances to avoid z-fighting.
var disableGlPositionLogDepth = 'ENABLE_GL_POSITION_LOG_DEPTH_AT_HEIGHT';
var vsSource = new ShaderSource({
defines : [extrudedDefine, disableGlPositionLogDepth],
sources : [vs]
});
var fsSource = new ShaderSource({
sources : [ShadowVolumeFS]
});
var attributeLocations = classificationPrimitive._primitive._attributeLocations;
var shadowVolumeAppearance = new ShadowVolumeAppearance(cullFragmentsUsingExtents, planarExtents, classificationPrimitive.appearance);
classificationPrimitive._spStencil = ShaderProgram.replaceCache({
context : context,
shaderProgram : classificationPrimitive._spStencil,
vertexShaderSource : vsSource,
fragmentShaderSource : fsSource,
attributeLocations : attributeLocations
});
if (classificationPrimitive._primitive.allowPicking) {
var vsPick = ShaderSource.createPickVertexShaderSource(vs);
vsPick = Primitive._appendShowToShader(primitive, vsPick);
vsPick = Primitive._updatePickColorAttribute(vsPick);
var pickFS3D = shadowVolumeAppearance.createPickFragmentShader(false);
var pickVS3D = shadowVolumeAppearance.createPickVertexShader([extrudedDefine, disableGlPositionLogDepth], vsPick, false, frameState.mapProjection);
classificationPrimitive._spPick = ShaderProgram.replaceCache({
context : context,
shaderProgram : classificationPrimitive._spPick,
vertexShaderSource : pickVS3D,
fragmentShaderSource : pickFS3D,
attributeLocations : attributeLocations
});
// Derive a 2D pick shader if the primitive uses texture coordinate-based fragment culling,
// since texture coordinates are computed differently in 2D.
if (cullFragmentsUsingExtents) {
var pickProgram2D = context.shaderCache.getDerivedShaderProgram(classificationPrimitive._spPick, '2dPick');
if (!defined(pickProgram2D)) {
var pickFS2D = shadowVolumeAppearance.createPickFragmentShader(true);
var pickVS2D = shadowVolumeAppearance.createPickVertexShader([extrudedDefine, disableGlPositionLogDepth], vsPick, true, frameState.mapProjection);
pickProgram2D = context.shaderCache.createDerivedShaderProgram(classificationPrimitive._spPick, '2dPick', {
vertexShaderSource : pickVS2D,
fragmentShaderSource : pickFS2D,
attributeLocations : attributeLocations
});
}
classificationPrimitive._spPick2D = pickProgram2D;
}
} else {
classificationPrimitive._spPick = ShaderProgram.fromCache({
context : context,
vertexShaderSource : vsSource,
fragmentShaderSource : fsSource,
attributeLocations : attributeLocations
});
}
vs = Primitive._appendShowToShader(primitive, vs);
vsSource = new ShaderSource({
defines : [extrudedDefine, disableGlPositionLogDepth],
sources : [vs]
});
classificationPrimitive._sp = ShaderProgram.replaceCache({
context : context,
shaderProgram : classificationPrimitive._sp,
vertexShaderSource : vsSource,
fragmentShaderSource : fsSource,
attributeLocations : attributeLocations
});
// Create a fragment shader that computes only required material hookups using screen space techniques
var fsColorSource = shadowVolumeAppearance.createFragmentShader(false);
var vsColorSource = shadowVolumeAppearance.createVertexShader([extrudedDefine, disableGlPositionLogDepth], vs, false, frameState.mapProjection);
classificationPrimitive._spColor = ShaderProgram.replaceCache({
context : context,
shaderProgram : classificationPrimitive._spColor,
vertexShaderSource : vsColorSource,
fragmentShaderSource : fsColorSource,
attributeLocations : attributeLocations
});
// Derive a 2D shader if the primitive uses texture coordinate-based fragment culling,
// since texture coordinates are computed differently in 2D.
// Any material that uses texture coordinates will also equip texture coordinate-based fragment culling.
if (cullFragmentsUsingExtents) {
var colorProgram2D = context.shaderCache.getDerivedShaderProgram(classificationPrimitive._spColor, '2dColor');
if (!defined(colorProgram2D)) {
var fsColorSource2D = shadowVolumeAppearance.createFragmentShader(true);
var vsColorSource2D = shadowVolumeAppearance.createVertexShader([extrudedDefine, disableGlPositionLogDepth], vs, true, frameState.mapProjection);
colorProgram2D = context.shaderCache.createDerivedShaderProgram(classificationPrimitive._spColor, '2dColor', {
vertexShaderSource : vsColorSource2D,
fragmentShaderSource : fsColorSource2D,
attributeLocations : attributeLocations
});
}
classificationPrimitive._spColor2D = colorProgram2D;
}
}
function createColorCommands(classificationPrimitive, colorCommands) {
var primitive = classificationPrimitive._primitive;
var length = primitive._va.length * 3; // each geometry (pack of vertex attributes) needs 3 commands: front/back stencils and fill
colorCommands.length = length;
var i;
var command;
var derivedCommand;
var vaIndex = 0;
var uniformMap = primitive._batchTable.getUniformMapCallback()(classificationPrimitive._uniformMap);
var needs2DShader = classificationPrimitive._needs2DShader;
for (i = 0; i < length; i += 3) {
var vertexArray = primitive._va[vaIndex++];
// Stencil preload command
command = colorCommands[i];
if (!defined(command)) {
command = colorCommands[i] = new DrawCommand({
owner : classificationPrimitive,
primitiveType : primitive._primitiveType
});
}
command.vertexArray = vertexArray;
command.renderState = classificationPrimitive._rsStencilPreloadPass;
command.shaderProgram = classificationPrimitive._sp;
command.uniformMap = uniformMap;
command.pass = Pass.TERRAIN_CLASSIFICATION;
derivedCommand = DrawCommand.shallowClone(command, command.derivedCommands.tileset);
derivedCommand.renderState = classificationPrimitive._rsStencilPreloadPass3DTiles;
derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
command.derivedCommands.tileset = derivedCommand;
// Stencil depth command
command = colorCommands[i + 1];
if (!defined(command)) {
command = colorCommands[i + 1] = new DrawCommand({
owner : classificationPrimitive,
primitiveType : primitive._primitiveType
});
}
command.vertexArray = vertexArray;
command.renderState = classificationPrimitive._rsStencilDepthPass;
command.shaderProgram = classificationPrimitive._sp;
command.uniformMap = uniformMap;
command.pass = Pass.TERRAIN_CLASSIFICATION;
derivedCommand = DrawCommand.shallowClone(command, command.derivedCommands.tileset);
derivedCommand.renderState = classificationPrimitive._rsStencilDepthPass3DTiles;
derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
command.derivedCommands.tileset = derivedCommand;
// Color command
command = colorCommands[i + 2];
if (!defined(command)) {
command = colorCommands[i + 2] = new DrawCommand({
owner : classificationPrimitive,
primitiveType : primitive._primitiveType
});
}
command.vertexArray = vertexArray;
command.renderState = classificationPrimitive._rsColorPass;
command.shaderProgram = classificationPrimitive._spColor;
command.pass = Pass.TERRAIN_CLASSIFICATION;
var appearance = classificationPrimitive.appearance;
var material = appearance.material;
if (defined(material)) {
uniformMap = combine(uniformMap, material._uniforms);
}
command.uniformMap = uniformMap;
derivedCommand = DrawCommand.shallowClone(command, command.derivedCommands.tileset);
derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
command.derivedCommands.tileset = derivedCommand;
// Derive for 2D if texture coordinates are ever computed
if (needs2DShader) {
// First derive from the terrain command
var derived2DCommand = DrawCommand.shallowClone(command, command.derivedCommands.appearance2D);
derived2DCommand.shaderProgram = classificationPrimitive._spColor2D;
command.derivedCommands.appearance2D = derived2DCommand;
// Then derive from the 3D Tiles command
derived2DCommand = DrawCommand.shallowClone(derivedCommand, derivedCommand.derivedCommands.appearance2D);
derived2DCommand.shaderProgram = classificationPrimitive._spColor2D;
derived2DCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
derivedCommand.derivedCommands.appearance2D = derived2DCommand;
}
}
var commandsIgnoreShow = classificationPrimitive._commandsIgnoreShow;
var spStencil = classificationPrimitive._spStencil;
var commandIndex = 0;
length = commandsIgnoreShow.length = length / 3 * 2;
for (var j = 0; j < length; j += 2) {
var commandIgnoreShow = commandsIgnoreShow[j] = DrawCommand.shallowClone(colorCommands[commandIndex], commandsIgnoreShow[j]);
commandIgnoreShow.shaderProgram = spStencil;
commandIgnoreShow.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION_IGNORE_SHOW;
commandIgnoreShow = commandsIgnoreShow[j + 1] = DrawCommand.shallowClone(colorCommands[commandIndex + 1], commandsIgnoreShow[j + 1]);
commandIgnoreShow.shaderProgram = spStencil;
commandIgnoreShow.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION_IGNORE_SHOW;
commandIndex += 3;
}
}
function createPickCommands(classificationPrimitive, pickCommands) {
var usePickOffsets = classificationPrimitive._usePickOffsets;
var primitive = classificationPrimitive._primitive;
var length = primitive._va.length * 3; // each geometry (pack of vertex attributes) needs 3 commands: front/back stencils and fill
// Fallback for batching same-color geometry instances
var pickOffsets;
var pickIndex = 0;
var pickOffset;
if (usePickOffsets) {
pickOffsets = primitive._pickOffsets;
length = pickOffsets.length * 3;
}
pickCommands.length = length;
var j;
var command;
var derivedCommand;
var vaIndex = 0;
var uniformMap = primitive._batchTable.getUniformMapCallback()(classificationPrimitive._uniformMap);
var needs2DShader = classificationPrimitive._needs2DShader;
for (j = 0; j < length; j += 3) {
var vertexArray = primitive._va[vaIndex++];
if (usePickOffsets) {
pickOffset = pickOffsets[pickIndex++];
vertexArray = primitive._va[pickOffset.index];
}
// Stencil preload command
command = pickCommands[j];
if (!defined(command)) {
command = pickCommands[j] = new DrawCommand({
owner : classificationPrimitive,
primitiveType : primitive._primitiveType,
pickOnly : true
});
}
command.vertexArray = vertexArray;
command.renderState = classificationPrimitive._rsStencilPreloadPass;
command.shaderProgram = classificationPrimitive._sp;
command.uniformMap = uniformMap;
command.pass = Pass.TERRAIN_CLASSIFICATION;
if (usePickOffsets) {
command.offset = pickOffset.offset;
command.count = pickOffset.count;
}
// Derive for 3D Tiles classification
derivedCommand = DrawCommand.shallowClone(command, command.derivedCommands.tileset);
derivedCommand.renderState = classificationPrimitive._rsStencilPreloadPass3DTiles;
derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
command.derivedCommands.tileset = derivedCommand;
// Stencil depth command
command = pickCommands[j + 1];
if (!defined(command)) {
command = pickCommands[j + 1] = new DrawCommand({
owner : classificationPrimitive,
primitiveType : primitive._primitiveType,
pickOnly : true
});
}
command.vertexArray = vertexArray;
command.renderState = classificationPrimitive._rsStencilDepthPass;
command.shaderProgram = classificationPrimitive._sp;
command.uniformMap = uniformMap;
command.pass = Pass.TERRAIN_CLASSIFICATION;
if (usePickOffsets) {
command.offset = pickOffset.offset;
command.count = pickOffset.count;
}
// Derive for 3D Tiles classification
derivedCommand = DrawCommand.shallowClone(command, command.derivedCommands.tileset);
derivedCommand.renderState = classificationPrimitive._rsStencilDepthPass3DTiles;
derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
command.derivedCommands.tileset = derivedCommand;
// Pick color command
command = pickCommands[j + 2];
if (!defined(command)) {
command = pickCommands[j + 2] = new DrawCommand({
owner : classificationPrimitive,
primitiveType : primitive._primitiveType,
pickOnly : true
});
}
command.vertexArray = vertexArray;
command.renderState = classificationPrimitive._rsPickPass;
command.shaderProgram = classificationPrimitive._spPick;
command.uniformMap = uniformMap;
command.pass = Pass.TERRAIN_CLASSIFICATION;
if (usePickOffsets) {
command.offset = pickOffset.offset;
command.count = pickOffset.count;
}
derivedCommand = DrawCommand.shallowClone(command, command.derivedCommands.tileset);
derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
command.derivedCommands.tileset = derivedCommand;
// Derive for 2D if texture coordinates are ever computed
if (needs2DShader) {
// First derive from the terrain command
var derived2DCommand = DrawCommand.shallowClone(command, command.derivedCommands.pick2D);
derived2DCommand.shaderProgram = classificationPrimitive._spPick2D;
command.derivedCommands.pick2D = derived2DCommand;
// Then derive from the 3D Tiles command
derived2DCommand = DrawCommand.shallowClone(derivedCommand, derivedCommand.derivedCommands.pick2D);
derived2DCommand.shaderProgram = classificationPrimitive._spPick2D;
derived2DCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
derivedCommand.derivedCommands.pick2D = derived2DCommand;
}
}
}
function createCommands(classificationPrimitive, appearance, material, translucent, twoPasses, colorCommands, pickCommands) {
createColorCommands(classificationPrimitive, colorCommands);
createPickCommands(classificationPrimitive, pickCommands);
}
function boundingVolumeIndex(commandIndex, length) {
return Math.floor((commandIndex % length) / 3);
}
function updateAndQueueRenderCommand(command, frameState, modelMatrix, cull, boundingVolume, debugShowBoundingVolume) {
command.modelMatrix = modelMatrix;
command.boundingVolume = boundingVolume;
command.cull = cull;
command.debugShowBoundingVolume = debugShowBoundingVolume;
frameState.commandList.push(command);
}
function updateAndQueuePickCommand(command, frameState, modelMatrix, cull, boundingVolume) {
command.modelMatrix = modelMatrix;
command.boundingVolume = boundingVolume;
command.cull = cull;
frameState.commandList.push(command);
}
function updateAndQueueCommands(classificationPrimitive, frameState, colorCommands, pickCommands, modelMatrix, cull, debugShowBoundingVolume, twoPasses) {
var primitive = classificationPrimitive._primitive;
Primitive._updateBoundingVolumes(primitive, frameState, modelMatrix);
var boundingVolumes;
if (frameState.mode === SceneMode.SCENE3D) {
boundingVolumes = primitive._boundingSphereWC;
} else if (frameState.mode === SceneMode.COLUMBUS_VIEW) {
boundingVolumes = primitive._boundingSphereCV;
} else if (frameState.mode === SceneMode.SCENE2D && defined(primitive._boundingSphere2D)) {
boundingVolumes = primitive._boundingSphere2D;
} else if (defined(primitive._boundingSphereMorph)) {
boundingVolumes = primitive._boundingSphereMorph;
}
var classificationType = classificationPrimitive.classificationType;
var queueTerrainCommands = (classificationType !== ClassificationType.CESIUM_3D_TILE);
var queue3DTilesCommands = (classificationType !== ClassificationType.TERRAIN);
var passes = frameState.passes;
var i;
var boundingVolume;
var command;
if (passes.render) {
var colorLength = colorCommands.length;
for (i = 0; i < colorLength; ++i) {
boundingVolume = boundingVolumes[boundingVolumeIndex(i, colorLength)];
if (queueTerrainCommands) {
command = colorCommands[i];
updateAndQueueRenderCommand(command, frameState, modelMatrix, cull, boundingVolume, debugShowBoundingVolume);
}
if (queue3DTilesCommands) {
command = colorCommands[i].derivedCommands.tileset;
updateAndQueueRenderCommand(command, frameState, modelMatrix, cull, boundingVolume, debugShowBoundingVolume);
}
}
if (frameState.invertClassification) {
var ignoreShowCommands = classificationPrimitive._commandsIgnoreShow;
var ignoreShowCommandsLength = ignoreShowCommands.length;
for (i = 0; i < ignoreShowCommandsLength; ++i) {
boundingVolume = boundingVolumes[Math.floor(i / 2)];
command = ignoreShowCommands[i];
updateAndQueueRenderCommand(command, frameState, modelMatrix, cull, boundingVolume, debugShowBoundingVolume);
}
}
}
if (passes.pick) {
var pickLength = pickCommands.length;
var pickOffsets = primitive._pickOffsets;
for (i = 0; i < pickLength; ++i) {
var pickOffset = pickOffsets[boundingVolumeIndex(i, pickLength)];
boundingVolume = boundingVolumes[pickOffset.index];
if (queueTerrainCommands) {
command = pickCommands[i];