ClassificationPrimitive.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
  2. import combine from "../Core/combine.js";
  3. import defaultValue from "../Core/defaultValue.js";
  4. import defined from "../Core/defined.js";
  5. import deprecationWarning from "../Core/deprecationWarning.js";
  6. import destroyObject from "../Core/destroyObject.js";
  7. import DeveloperError from "../Core/DeveloperError.js";
  8. import GeometryInstance from "../Core/GeometryInstance.js";
  9. import DrawCommand from "../Renderer/DrawCommand.js";
  10. import Pass from "../Renderer/Pass.js";
  11. import RenderState from "../Renderer/RenderState.js";
  12. import ShaderProgram from "../Renderer/ShaderProgram.js";
  13. import ShaderSource from "../Renderer/ShaderSource.js";
  14. import ShadowVolumeAppearanceVS from "../Shaders/ShadowVolumeAppearanceVS.js";
  15. import ShadowVolumeFS from "../Shaders/ShadowVolumeFS.js";
  16. import BlendingState from "./BlendingState.js";
  17. import ClassificationType from "./ClassificationType.js";
  18. import DepthFunction from "./DepthFunction.js";
  19. import PerInstanceColorAppearance from "./PerInstanceColorAppearance.js";
  20. import Primitive from "./Primitive.js";
  21. import SceneMode from "./SceneMode.js";
  22. import ShadowVolumeAppearance from "./ShadowVolumeAppearance.js";
  23. import StencilConstants from "./StencilConstants.js";
  24. import StencilFunction from "./StencilFunction.js";
  25. import StencilOperation from "./StencilOperation.js";
  26. /**
  27. * A classification primitive represents a volume enclosing geometry in the {@link Scene} to be highlighted.
  28. * <p>
  29. * A primitive combines geometry instances with an {@link Appearance} that describes the full shading, including
  30. * {@link Material} and {@link RenderState}. Roughly, the geometry instance defines the structure and placement,
  31. * and the appearance defines the visual characteristics. Decoupling geometry and appearance allows us to mix
  32. * and match most of them and add a new geometry or appearance independently of each other.
  33. * Only {@link PerInstanceColorAppearance} with the same color across all instances is supported at this time when using
  34. * ClassificationPrimitive directly.
  35. * For full {@link Appearance} support when classifying terrain or 3D Tiles use {@link GroundPrimitive} instead.
  36. * </p>
  37. * <p>
  38. * For correct rendering, this feature requires the EXT_frag_depth WebGL extension. For hardware that do not support this extension, there
  39. * will be rendering artifacts for some viewing angles.
  40. * </p>
  41. * <p>
  42. * Valid geometries are {@link BoxGeometry}, {@link CylinderGeometry}, {@link EllipsoidGeometry}, {@link PolylineVolumeGeometry}, and {@link SphereGeometry}.
  43. * </p>
  44. * <p>
  45. * Geometries that follow the surface of the ellipsoid, such as {@link CircleGeometry}, {@link CorridorGeometry}, {@link EllipseGeometry}, {@link PolygonGeometry}, and {@link RectangleGeometry},
  46. * are also valid if they are extruded volumes; otherwise, they will not be rendered.
  47. * </p>
  48. *
  49. * @alias ClassificationPrimitive
  50. * @constructor
  51. *
  52. * @param {object} [options] Object with the following properties:
  53. * @param {Array|GeometryInstance} [options.geometryInstances] The geometry instances to render. This can either be a single instance or an array of length one.
  54. * @param {Appearance} [options.appearance] The appearance used to render the primitive. Defaults to PerInstanceColorAppearance when GeometryInstances have a color attribute.
  55. * @param {boolean} [options.show=true] Determines if this primitive will be shown.
  56. * @param {boolean} [options.vertexCacheOptimize=false] When <code>true</code>, geometry vertices are optimized for the pre and post-vertex-shader caches.
  57. * @param {boolean} [options.interleave=false] When <code>true</code>, geometry vertex attributes are interleaved, which can slightly improve rendering performance but increases load time.
  58. * @param {boolean} [options.compressVertices=true] When <code>true</code>, the geometry vertices are compressed, which will save memory.
  59. * @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.
  60. * @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.
  61. * @param {boolean} [options.asynchronous=true] Determines if the primitive will be created asynchronously or block until ready. If false initializeTerrainHeights() must be called first.
  62. * @param {ClassificationType} [options.classificationType=ClassificationType.BOTH] Determines whether terrain, 3D Tiles or both will be classified.
  63. * @param {boolean} [options.debugShowBoundingVolume=false] For debugging only. Determines if this primitive's commands' bounding spheres are shown.
  64. * @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
  65. * creation for the volumes to be created before the geometry is released or options.releaseGeometryInstance must be <code>false</code>.
  66. *
  67. * @see Primitive
  68. * @see GroundPrimitive
  69. * @see GeometryInstance
  70. * @see Appearance
  71. */
  72. function ClassificationPrimitive(options) {
  73. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  74. const geometryInstances = options.geometryInstances;
  75. /**
  76. * The geometry instance rendered with this primitive. This may
  77. * be <code>undefined</code> if <code>options.releaseGeometryInstances</code>
  78. * is <code>true</code> when the primitive is constructed.
  79. * <p>
  80. * Changing this property after the primitive is rendered has no effect.
  81. * </p>
  82. * <p>
  83. * Because of the rendering technique used, all geometry instances must be the same color.
  84. * If there is an instance with a differing color, a <code>DeveloperError</code> will be thrown
  85. * on the first attempt to render.
  86. * </p>
  87. *
  88. * @readonly
  89. * @type {Array|GeometryInstance}
  90. *
  91. * @default undefined
  92. */
  93. this.geometryInstances = geometryInstances;
  94. /**
  95. * Determines if the primitive will be shown. This affects all geometry
  96. * instances in the primitive.
  97. *
  98. * @type {boolean}
  99. *
  100. * @default true
  101. */
  102. this.show = defaultValue(options.show, true);
  103. /**
  104. * Determines whether terrain, 3D Tiles or both will be classified.
  105. *
  106. * @type {ClassificationType}
  107. *
  108. * @default ClassificationType.BOTH
  109. */
  110. this.classificationType = defaultValue(
  111. options.classificationType,
  112. ClassificationType.BOTH
  113. );
  114. /**
  115. * This property is for debugging only; it is not for production use nor is it optimized.
  116. * <p>
  117. * Draws the bounding sphere for each draw command in the primitive.
  118. * </p>
  119. *
  120. * @type {boolean}
  121. *
  122. * @default false
  123. */
  124. this.debugShowBoundingVolume = defaultValue(
  125. options.debugShowBoundingVolume,
  126. false
  127. );
  128. /**
  129. * This property is for debugging only; it is not for production use nor is it optimized.
  130. * <p>
  131. * Draws the shadow volume for each geometry in the primitive.
  132. * </p>
  133. *
  134. * @type {boolean}
  135. *
  136. * @default false
  137. */
  138. this.debugShowShadowVolume = defaultValue(
  139. options.debugShowShadowVolume,
  140. false
  141. );
  142. this._debugShowShadowVolume = false;
  143. // These are used by GroundPrimitive to augment the shader and uniform map.
  144. this._extruded = defaultValue(options._extruded, false);
  145. this._uniformMap = options._uniformMap;
  146. this._sp = undefined;
  147. this._spStencil = undefined;
  148. this._spPick = undefined;
  149. this._spColor = undefined;
  150. this._spPick2D = undefined; // only derived if necessary
  151. this._spColor2D = undefined; // only derived if necessary
  152. this._rsStencilDepthPass = undefined;
  153. this._rsStencilDepthPass3DTiles = undefined;
  154. this._rsColorPass = undefined;
  155. this._rsPickPass = undefined;
  156. this._commandsIgnoreShow = [];
  157. this._ready = false;
  158. const classificationPrimitive = this;
  159. // This is here for backwards compatibility. This promise wrapper can be removed once readyPromise is removed.
  160. this._readyPromise = new Promise((resolve, reject) => {
  161. classificationPrimitive._completeLoad = () => {
  162. if (this._ready) {
  163. return;
  164. }
  165. this._ready = true;
  166. if (this.releaseGeometryInstances) {
  167. this.geometryInstances = undefined;
  168. }
  169. const error = this._error;
  170. if (!defined(error)) {
  171. resolve(this);
  172. } else {
  173. reject(error);
  174. }
  175. };
  176. });
  177. this._primitive = undefined;
  178. this._pickPrimitive = options._pickPrimitive;
  179. // Set in update
  180. this._hasSphericalExtentsAttribute = false;
  181. this._hasPlanarExtentsAttributes = false;
  182. this._hasPerColorAttribute = false;
  183. this.appearance = options.appearance;
  184. this._createBoundingVolumeFunction = options._createBoundingVolumeFunction;
  185. this._updateAndQueueCommandsFunction =
  186. options._updateAndQueueCommandsFunction;
  187. this._usePickOffsets = false;
  188. this._primitiveOptions = {
  189. geometryInstances: undefined,
  190. appearance: undefined,
  191. vertexCacheOptimize: defaultValue(options.vertexCacheOptimize, false),
  192. interleave: defaultValue(options.interleave, false),
  193. releaseGeometryInstances: defaultValue(
  194. options.releaseGeometryInstances,
  195. true
  196. ),
  197. allowPicking: defaultValue(options.allowPicking, true),
  198. asynchronous: defaultValue(options.asynchronous, true),
  199. compressVertices: defaultValue(options.compressVertices, true),
  200. _createBoundingVolumeFunction: undefined,
  201. _createRenderStatesFunction: undefined,
  202. _createShaderProgramFunction: undefined,
  203. _createCommandsFunction: undefined,
  204. _updateAndQueueCommandsFunction: undefined,
  205. _createPickOffsets: true,
  206. };
  207. }
  208. Object.defineProperties(ClassificationPrimitive.prototype, {
  209. /**
  210. * When <code>true</code>, geometry vertices are optimized for the pre and post-vertex-shader caches.
  211. *
  212. * @memberof ClassificationPrimitive.prototype
  213. *
  214. * @type {boolean}
  215. * @readonly
  216. *
  217. * @default true
  218. */
  219. vertexCacheOptimize: {
  220. get: function () {
  221. return this._primitiveOptions.vertexCacheOptimize;
  222. },
  223. },
  224. /**
  225. * Determines if geometry vertex attributes are interleaved, which can slightly improve rendering performance.
  226. *
  227. * @memberof ClassificationPrimitive.prototype
  228. *
  229. * @type {boolean}
  230. * @readonly
  231. *
  232. * @default false
  233. */
  234. interleave: {
  235. get: function () {
  236. return this._primitiveOptions.interleave;
  237. },
  238. },
  239. /**
  240. * When <code>true</code>, the primitive does not keep a reference to the input <code>geometryInstances</code> to save memory.
  241. *
  242. * @memberof ClassificationPrimitive.prototype
  243. *
  244. * @type {boolean}
  245. * @readonly
  246. *
  247. * @default true
  248. */
  249. releaseGeometryInstances: {
  250. get: function () {
  251. return this._primitiveOptions.releaseGeometryInstances;
  252. },
  253. },
  254. /**
  255. * When <code>true</code>, each geometry instance will only be pickable with {@link Scene#pick}. When <code>false</code>, GPU memory is saved.
  256. *
  257. * @memberof ClassificationPrimitive.prototype
  258. *
  259. * @type {boolean}
  260. * @readonly
  261. *
  262. * @default true
  263. */
  264. allowPicking: {
  265. get: function () {
  266. return this._primitiveOptions.allowPicking;
  267. },
  268. },
  269. /**
  270. * Determines if the geometry instances will be created and batched on a web worker.
  271. *
  272. * @memberof ClassificationPrimitive.prototype
  273. *
  274. * @type {boolean}
  275. * @readonly
  276. *
  277. * @default true
  278. */
  279. asynchronous: {
  280. get: function () {
  281. return this._primitiveOptions.asynchronous;
  282. },
  283. },
  284. /**
  285. * When <code>true</code>, geometry vertices are compressed, which will save memory.
  286. *
  287. * @memberof ClassificationPrimitive.prototype
  288. *
  289. * @type {boolean}
  290. * @readonly
  291. *
  292. * @default true
  293. */
  294. compressVertices: {
  295. get: function () {
  296. return this._primitiveOptions.compressVertices;
  297. },
  298. },
  299. /**
  300. * Determines if the primitive is complete and ready to render. If this property is
  301. * true, the primitive will be rendered the next time that {@link ClassificationPrimitive#update}
  302. * is called.
  303. *
  304. * @memberof ClassificationPrimitive.prototype
  305. *
  306. * @type {boolean}
  307. * @readonly
  308. */
  309. ready: {
  310. get: function () {
  311. return this._ready;
  312. },
  313. },
  314. /**
  315. * Gets a promise that resolves when the primitive is ready to render.
  316. * @memberof ClassificationPrimitive.prototype
  317. * @type {Promise<ClassificationPrimitive>}
  318. * @readonly
  319. * @deprecated
  320. */
  321. readyPromise: {
  322. get: function () {
  323. deprecationWarning(
  324. "ClassificationPrimitive.readyPromise",
  325. "ClassificationPrimitive.readyPromise was deprecated in CesiumJS 1.104. It will be removed in 1.107. Wait for ClassificationPrimitive.ready to return true instead."
  326. );
  327. return this._readyPromise;
  328. },
  329. },
  330. /**
  331. * Returns true if the ClassificationPrimitive needs a separate shader and commands for 2D.
  332. * This is because texture coordinates on ClassificationPrimitives are computed differently,
  333. * and are used for culling when multiple GeometryInstances are batched in one ClassificationPrimitive.
  334. * @memberof ClassificationPrimitive.prototype
  335. * @type {boolean}
  336. * @readonly
  337. * @private
  338. */
  339. _needs2DShader: {
  340. get: function () {
  341. return (
  342. this._hasPlanarExtentsAttributes || this._hasSphericalExtentsAttribute
  343. );
  344. },
  345. },
  346. });
  347. /**
  348. * Determines if ClassificationPrimitive rendering is supported.
  349. *
  350. * @param {Scene} scene The scene.
  351. * @returns {boolean} <code>true</code> if ClassificationPrimitives are supported; otherwise, returns <code>false</code>
  352. */
  353. ClassificationPrimitive.isSupported = function (scene) {
  354. return scene.context.stencilBuffer;
  355. };
  356. function getStencilDepthRenderState(enableStencil, mask3DTiles) {
  357. const stencilFunction = mask3DTiles
  358. ? StencilFunction.EQUAL
  359. : StencilFunction.ALWAYS;
  360. return {
  361. colorMask: {
  362. red: false,
  363. green: false,
  364. blue: false,
  365. alpha: false,
  366. },
  367. stencilTest: {
  368. enabled: enableStencil,
  369. frontFunction: stencilFunction,
  370. frontOperation: {
  371. fail: StencilOperation.KEEP,
  372. zFail: StencilOperation.DECREMENT_WRAP,
  373. zPass: StencilOperation.KEEP,
  374. },
  375. backFunction: stencilFunction,
  376. backOperation: {
  377. fail: StencilOperation.KEEP,
  378. zFail: StencilOperation.INCREMENT_WRAP,
  379. zPass: StencilOperation.KEEP,
  380. },
  381. reference: StencilConstants.CESIUM_3D_TILE_MASK,
  382. mask: StencilConstants.CESIUM_3D_TILE_MASK,
  383. },
  384. stencilMask: StencilConstants.CLASSIFICATION_MASK,
  385. depthTest: {
  386. enabled: true,
  387. func: DepthFunction.LESS_OR_EQUAL,
  388. },
  389. depthMask: false,
  390. };
  391. }
  392. function getColorRenderState(enableStencil) {
  393. return {
  394. stencilTest: {
  395. enabled: enableStencil,
  396. frontFunction: StencilFunction.NOT_EQUAL,
  397. frontOperation: {
  398. fail: StencilOperation.ZERO,
  399. zFail: StencilOperation.ZERO,
  400. zPass: StencilOperation.ZERO,
  401. },
  402. backFunction: StencilFunction.NOT_EQUAL,
  403. backOperation: {
  404. fail: StencilOperation.ZERO,
  405. zFail: StencilOperation.ZERO,
  406. zPass: StencilOperation.ZERO,
  407. },
  408. reference: 0,
  409. mask: StencilConstants.CLASSIFICATION_MASK,
  410. },
  411. stencilMask: StencilConstants.CLASSIFICATION_MASK,
  412. depthTest: {
  413. enabled: false,
  414. },
  415. depthMask: false,
  416. blending: BlendingState.PRE_MULTIPLIED_ALPHA_BLEND,
  417. };
  418. }
  419. const pickRenderState = {
  420. stencilTest: {
  421. enabled: true,
  422. frontFunction: StencilFunction.NOT_EQUAL,
  423. frontOperation: {
  424. fail: StencilOperation.ZERO,
  425. zFail: StencilOperation.ZERO,
  426. zPass: StencilOperation.ZERO,
  427. },
  428. backFunction: StencilFunction.NOT_EQUAL,
  429. backOperation: {
  430. fail: StencilOperation.ZERO,
  431. zFail: StencilOperation.ZERO,
  432. zPass: StencilOperation.ZERO,
  433. },
  434. reference: 0,
  435. mask: StencilConstants.CLASSIFICATION_MASK,
  436. },
  437. stencilMask: StencilConstants.CLASSIFICATION_MASK,
  438. depthTest: {
  439. enabled: false,
  440. },
  441. depthMask: false,
  442. };
  443. function createRenderStates(
  444. classificationPrimitive,
  445. context,
  446. appearance,
  447. twoPasses
  448. ) {
  449. if (defined(classificationPrimitive._rsStencilDepthPass)) {
  450. return;
  451. }
  452. const stencilEnabled = !classificationPrimitive.debugShowShadowVolume;
  453. classificationPrimitive._rsStencilDepthPass = RenderState.fromCache(
  454. getStencilDepthRenderState(stencilEnabled, false)
  455. );
  456. classificationPrimitive._rsStencilDepthPass3DTiles = RenderState.fromCache(
  457. getStencilDepthRenderState(stencilEnabled, true)
  458. );
  459. classificationPrimitive._rsColorPass = RenderState.fromCache(
  460. getColorRenderState(stencilEnabled, false)
  461. );
  462. classificationPrimitive._rsPickPass = RenderState.fromCache(pickRenderState);
  463. }
  464. function modifyForEncodedNormals(primitive, vertexShaderSource) {
  465. if (!primitive.compressVertices) {
  466. return vertexShaderSource;
  467. }
  468. if (vertexShaderSource.search(/in\s+vec3\s+extrudeDirection;/g) !== -1) {
  469. const attributeName = "compressedAttributes";
  470. //only shadow volumes use extrudeDirection, and shadow volumes use vertexFormat: POSITION_ONLY so we don't need to check other attributes
  471. const attributeDecl = `in vec2 ${attributeName};`;
  472. const globalDecl = "vec3 extrudeDirection;\n";
  473. const decode = ` extrudeDirection = czm_octDecode(${attributeName}, 65535.0);\n`;
  474. let modifiedVS = vertexShaderSource;
  475. modifiedVS = modifiedVS.replace(/in\s+vec3\s+extrudeDirection;/g, "");
  476. modifiedVS = ShaderSource.replaceMain(
  477. modifiedVS,
  478. "czm_non_compressed_main"
  479. );
  480. const compressedMain =
  481. `${"void main() \n" + "{ \n"}${decode} czm_non_compressed_main(); \n` +
  482. `}`;
  483. return [attributeDecl, globalDecl, modifiedVS, compressedMain].join("\n");
  484. }
  485. }
  486. function createShaderProgram(classificationPrimitive, frameState) {
  487. const context = frameState.context;
  488. const primitive = classificationPrimitive._primitive;
  489. let vs = ShadowVolumeAppearanceVS;
  490. vs = classificationPrimitive._primitive._batchTable.getVertexShaderCallback()(
  491. vs
  492. );
  493. vs = Primitive._appendDistanceDisplayConditionToShader(primitive, vs);
  494. vs = Primitive._modifyShaderPosition(
  495. classificationPrimitive,
  496. vs,
  497. frameState.scene3DOnly
  498. );
  499. vs = Primitive._updateColorAttribute(primitive, vs);
  500. const planarExtents = classificationPrimitive._hasPlanarExtentsAttributes;
  501. const cullFragmentsUsingExtents =
  502. planarExtents || classificationPrimitive._hasSphericalExtentsAttribute;
  503. if (classificationPrimitive._extruded) {
  504. vs = modifyForEncodedNormals(primitive, vs);
  505. }
  506. const extrudedDefine = classificationPrimitive._extruded
  507. ? "EXTRUDED_GEOMETRY"
  508. : "";
  509. let vsSource = new ShaderSource({
  510. defines: [extrudedDefine],
  511. sources: [vs],
  512. });
  513. const fsSource = new ShaderSource({
  514. sources: [ShadowVolumeFS],
  515. });
  516. const attributeLocations =
  517. classificationPrimitive._primitive._attributeLocations;
  518. const shadowVolumeAppearance = new ShadowVolumeAppearance(
  519. cullFragmentsUsingExtents,
  520. planarExtents,
  521. classificationPrimitive.appearance
  522. );
  523. classificationPrimitive._spStencil = ShaderProgram.replaceCache({
  524. context: context,
  525. shaderProgram: classificationPrimitive._spStencil,
  526. vertexShaderSource: vsSource,
  527. fragmentShaderSource: fsSource,
  528. attributeLocations: attributeLocations,
  529. });
  530. if (classificationPrimitive._primitive.allowPicking) {
  531. let vsPick = ShaderSource.createPickVertexShaderSource(vs);
  532. vsPick = Primitive._appendShowToShader(primitive, vsPick);
  533. vsPick = Primitive._updatePickColorAttribute(vsPick);
  534. const pickFS3D = shadowVolumeAppearance.createPickFragmentShader(false);
  535. const pickVS3D = shadowVolumeAppearance.createPickVertexShader(
  536. [extrudedDefine],
  537. vsPick,
  538. false,
  539. frameState.mapProjection
  540. );
  541. classificationPrimitive._spPick = ShaderProgram.replaceCache({
  542. context: context,
  543. shaderProgram: classificationPrimitive._spPick,
  544. vertexShaderSource: pickVS3D,
  545. fragmentShaderSource: pickFS3D,
  546. attributeLocations: attributeLocations,
  547. });
  548. // Derive a 2D pick shader if the primitive uses texture coordinate-based fragment culling,
  549. // since texture coordinates are computed differently in 2D.
  550. if (cullFragmentsUsingExtents) {
  551. let pickProgram2D = context.shaderCache.getDerivedShaderProgram(
  552. classificationPrimitive._spPick,
  553. "2dPick"
  554. );
  555. if (!defined(pickProgram2D)) {
  556. const pickFS2D = shadowVolumeAppearance.createPickFragmentShader(true);
  557. const pickVS2D = shadowVolumeAppearance.createPickVertexShader(
  558. [extrudedDefine],
  559. vsPick,
  560. true,
  561. frameState.mapProjection
  562. );
  563. pickProgram2D = context.shaderCache.createDerivedShaderProgram(
  564. classificationPrimitive._spPick,
  565. "2dPick",
  566. {
  567. vertexShaderSource: pickVS2D,
  568. fragmentShaderSource: pickFS2D,
  569. attributeLocations: attributeLocations,
  570. }
  571. );
  572. }
  573. classificationPrimitive._spPick2D = pickProgram2D;
  574. }
  575. } else {
  576. classificationPrimitive._spPick = ShaderProgram.fromCache({
  577. context: context,
  578. vertexShaderSource: vsSource,
  579. fragmentShaderSource: fsSource,
  580. attributeLocations: attributeLocations,
  581. });
  582. }
  583. vs = Primitive._appendShowToShader(primitive, vs);
  584. vsSource = new ShaderSource({
  585. defines: [extrudedDefine],
  586. sources: [vs],
  587. });
  588. classificationPrimitive._sp = ShaderProgram.replaceCache({
  589. context: context,
  590. shaderProgram: classificationPrimitive._sp,
  591. vertexShaderSource: vsSource,
  592. fragmentShaderSource: fsSource,
  593. attributeLocations: attributeLocations,
  594. });
  595. // Create a fragment shader that computes only required material hookups using screen space techniques
  596. const fsColorSource = shadowVolumeAppearance.createFragmentShader(false);
  597. const vsColorSource = shadowVolumeAppearance.createVertexShader(
  598. [extrudedDefine],
  599. vs,
  600. false,
  601. frameState.mapProjection
  602. );
  603. classificationPrimitive._spColor = ShaderProgram.replaceCache({
  604. context: context,
  605. shaderProgram: classificationPrimitive._spColor,
  606. vertexShaderSource: vsColorSource,
  607. fragmentShaderSource: fsColorSource,
  608. attributeLocations: attributeLocations,
  609. });
  610. // Derive a 2D shader if the primitive uses texture coordinate-based fragment culling,
  611. // since texture coordinates are computed differently in 2D.
  612. // Any material that uses texture coordinates will also equip texture coordinate-based fragment culling.
  613. if (cullFragmentsUsingExtents) {
  614. let colorProgram2D = context.shaderCache.getDerivedShaderProgram(
  615. classificationPrimitive._spColor,
  616. "2dColor"
  617. );
  618. if (!defined(colorProgram2D)) {
  619. const fsColorSource2D = shadowVolumeAppearance.createFragmentShader(true);
  620. const vsColorSource2D = shadowVolumeAppearance.createVertexShader(
  621. [extrudedDefine],
  622. vs,
  623. true,
  624. frameState.mapProjection
  625. );
  626. colorProgram2D = context.shaderCache.createDerivedShaderProgram(
  627. classificationPrimitive._spColor,
  628. "2dColor",
  629. {
  630. vertexShaderSource: vsColorSource2D,
  631. fragmentShaderSource: fsColorSource2D,
  632. attributeLocations: attributeLocations,
  633. }
  634. );
  635. }
  636. classificationPrimitive._spColor2D = colorProgram2D;
  637. }
  638. }
  639. function createColorCommands(classificationPrimitive, colorCommands) {
  640. const primitive = classificationPrimitive._primitive;
  641. let length = primitive._va.length * 2; // each geometry (pack of vertex attributes) needs 2 commands: front/back stencils and fill
  642. colorCommands.length = length;
  643. let i;
  644. let command;
  645. let derivedCommand;
  646. let vaIndex = 0;
  647. let uniformMap = primitive._batchTable.getUniformMapCallback()(
  648. classificationPrimitive._uniformMap
  649. );
  650. const needs2DShader = classificationPrimitive._needs2DShader;
  651. for (i = 0; i < length; i += 2) {
  652. const vertexArray = primitive._va[vaIndex++];
  653. // Stencil depth command
  654. command = colorCommands[i];
  655. if (!defined(command)) {
  656. command = colorCommands[i] = new DrawCommand({
  657. owner: classificationPrimitive,
  658. primitiveType: primitive._primitiveType,
  659. });
  660. }
  661. command.vertexArray = vertexArray;
  662. command.renderState = classificationPrimitive._rsStencilDepthPass;
  663. command.shaderProgram = classificationPrimitive._sp;
  664. command.uniformMap = uniformMap;
  665. command.pass = Pass.TERRAIN_CLASSIFICATION;
  666. derivedCommand = DrawCommand.shallowClone(
  667. command,
  668. command.derivedCommands.tileset
  669. );
  670. derivedCommand.renderState =
  671. classificationPrimitive._rsStencilDepthPass3DTiles;
  672. derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
  673. command.derivedCommands.tileset = derivedCommand;
  674. // Color command
  675. command = colorCommands[i + 1];
  676. if (!defined(command)) {
  677. command = colorCommands[i + 1] = new DrawCommand({
  678. owner: classificationPrimitive,
  679. primitiveType: primitive._primitiveType,
  680. });
  681. }
  682. command.vertexArray = vertexArray;
  683. command.renderState = classificationPrimitive._rsColorPass;
  684. command.shaderProgram = classificationPrimitive._spColor;
  685. command.pass = Pass.TERRAIN_CLASSIFICATION;
  686. const appearance = classificationPrimitive.appearance;
  687. const material = appearance.material;
  688. if (defined(material)) {
  689. uniformMap = combine(uniformMap, material._uniforms);
  690. }
  691. command.uniformMap = uniformMap;
  692. derivedCommand = DrawCommand.shallowClone(
  693. command,
  694. command.derivedCommands.tileset
  695. );
  696. derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
  697. command.derivedCommands.tileset = derivedCommand;
  698. // Derive for 2D if texture coordinates are ever computed
  699. if (needs2DShader) {
  700. // First derive from the terrain command
  701. let derived2DCommand = DrawCommand.shallowClone(
  702. command,
  703. command.derivedCommands.appearance2D
  704. );
  705. derived2DCommand.shaderProgram = classificationPrimitive._spColor2D;
  706. command.derivedCommands.appearance2D = derived2DCommand;
  707. // Then derive from the 3D Tiles command
  708. derived2DCommand = DrawCommand.shallowClone(
  709. derivedCommand,
  710. derivedCommand.derivedCommands.appearance2D
  711. );
  712. derived2DCommand.shaderProgram = classificationPrimitive._spColor2D;
  713. derivedCommand.derivedCommands.appearance2D = derived2DCommand;
  714. }
  715. }
  716. const commandsIgnoreShow = classificationPrimitive._commandsIgnoreShow;
  717. const spStencil = classificationPrimitive._spStencil;
  718. let commandIndex = 0;
  719. length = commandsIgnoreShow.length = length / 2;
  720. for (let j = 0; j < length; ++j) {
  721. const commandIgnoreShow = (commandsIgnoreShow[j] = DrawCommand.shallowClone(
  722. colorCommands[commandIndex],
  723. commandsIgnoreShow[j]
  724. ));
  725. commandIgnoreShow.shaderProgram = spStencil;
  726. commandIgnoreShow.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION_IGNORE_SHOW;
  727. commandIndex += 2;
  728. }
  729. }
  730. function createPickCommands(classificationPrimitive, pickCommands) {
  731. const usePickOffsets = classificationPrimitive._usePickOffsets;
  732. const primitive = classificationPrimitive._primitive;
  733. let length = primitive._va.length * 2; // each geometry (pack of vertex attributes) needs 2 commands: front/back stencils and fill
  734. // Fallback for batching same-color geometry instances
  735. let pickOffsets;
  736. let pickIndex = 0;
  737. let pickOffset;
  738. if (usePickOffsets) {
  739. pickOffsets = primitive._pickOffsets;
  740. length = pickOffsets.length * 2;
  741. }
  742. pickCommands.length = length;
  743. let j;
  744. let command;
  745. let derivedCommand;
  746. let vaIndex = 0;
  747. const uniformMap = primitive._batchTable.getUniformMapCallback()(
  748. classificationPrimitive._uniformMap
  749. );
  750. const needs2DShader = classificationPrimitive._needs2DShader;
  751. for (j = 0; j < length; j += 2) {
  752. let vertexArray = primitive._va[vaIndex++];
  753. if (usePickOffsets) {
  754. pickOffset = pickOffsets[pickIndex++];
  755. vertexArray = primitive._va[pickOffset.index];
  756. }
  757. // Stencil depth command
  758. command = pickCommands[j];
  759. if (!defined(command)) {
  760. command = pickCommands[j] = new DrawCommand({
  761. owner: classificationPrimitive,
  762. primitiveType: primitive._primitiveType,
  763. pickOnly: true,
  764. });
  765. }
  766. command.vertexArray = vertexArray;
  767. command.renderState = classificationPrimitive._rsStencilDepthPass;
  768. command.shaderProgram = classificationPrimitive._sp;
  769. command.uniformMap = uniformMap;
  770. command.pass = Pass.TERRAIN_CLASSIFICATION;
  771. if (usePickOffsets) {
  772. command.offset = pickOffset.offset;
  773. command.count = pickOffset.count;
  774. }
  775. // Derive for 3D Tiles classification
  776. derivedCommand = DrawCommand.shallowClone(
  777. command,
  778. command.derivedCommands.tileset
  779. );
  780. derivedCommand.renderState =
  781. classificationPrimitive._rsStencilDepthPass3DTiles;
  782. derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
  783. command.derivedCommands.tileset = derivedCommand;
  784. // Pick color command
  785. command = pickCommands[j + 1];
  786. if (!defined(command)) {
  787. command = pickCommands[j + 1] = new DrawCommand({
  788. owner: classificationPrimitive,
  789. primitiveType: primitive._primitiveType,
  790. pickOnly: true,
  791. });
  792. }
  793. command.vertexArray = vertexArray;
  794. command.renderState = classificationPrimitive._rsPickPass;
  795. command.shaderProgram = classificationPrimitive._spPick;
  796. command.uniformMap = uniformMap;
  797. command.pass = Pass.TERRAIN_CLASSIFICATION;
  798. if (usePickOffsets) {
  799. command.offset = pickOffset.offset;
  800. command.count = pickOffset.count;
  801. }
  802. derivedCommand = DrawCommand.shallowClone(
  803. command,
  804. command.derivedCommands.tileset
  805. );
  806. derivedCommand.pass = Pass.CESIUM_3D_TILE_CLASSIFICATION;
  807. command.derivedCommands.tileset = derivedCommand;
  808. // Derive for 2D if texture coordinates are ever computed
  809. if (needs2DShader) {
  810. // First derive from the terrain command
  811. let derived2DCommand = DrawCommand.shallowClone(
  812. command,
  813. command.derivedCommands.pick2D
  814. );
  815. derived2DCommand.shaderProgram = classificationPrimitive._spPick2D;
  816. command.derivedCommands.pick2D = derived2DCommand;
  817. // Then derive from the 3D Tiles command
  818. derived2DCommand = DrawCommand.shallowClone(
  819. derivedCommand,
  820. derivedCommand.derivedCommands.pick2D
  821. );
  822. derived2DCommand.shaderProgram = classificationPrimitive._spPick2D;
  823. derivedCommand.derivedCommands.pick2D = derived2DCommand;
  824. }
  825. }
  826. }
  827. function createCommands(
  828. classificationPrimitive,
  829. appearance,
  830. material,
  831. translucent,
  832. twoPasses,
  833. colorCommands,
  834. pickCommands
  835. ) {
  836. createColorCommands(classificationPrimitive, colorCommands);
  837. createPickCommands(classificationPrimitive, pickCommands);
  838. }
  839. function boundingVolumeIndex(commandIndex, length) {
  840. return Math.floor((commandIndex % length) / 2);
  841. }
  842. function updateAndQueueRenderCommand(
  843. command,
  844. frameState,
  845. modelMatrix,
  846. cull,
  847. boundingVolume,
  848. debugShowBoundingVolume
  849. ) {
  850. command.modelMatrix = modelMatrix;
  851. command.boundingVolume = boundingVolume;
  852. command.cull = cull;
  853. command.debugShowBoundingVolume = debugShowBoundingVolume;
  854. frameState.commandList.push(command);
  855. }
  856. function updateAndQueuePickCommand(
  857. command,
  858. frameState,
  859. modelMatrix,
  860. cull,
  861. boundingVolume
  862. ) {
  863. command.modelMatrix = modelMatrix;
  864. command.boundingVolume = boundingVolume;
  865. command.cull = cull;
  866. frameState.commandList.push(command);
  867. }
  868. function updateAndQueueCommands(
  869. classificationPrimitive,
  870. frameState,
  871. colorCommands,
  872. pickCommands,
  873. modelMatrix,
  874. cull,
  875. debugShowBoundingVolume,
  876. twoPasses
  877. ) {
  878. const primitive = classificationPrimitive._primitive;
  879. Primitive._updateBoundingVolumes(primitive, frameState, modelMatrix);
  880. let boundingVolumes;
  881. if (frameState.mode === SceneMode.SCENE3D) {
  882. boundingVolumes = primitive._boundingSphereWC;
  883. } else if (frameState.mode === SceneMode.COLUMBUS_VIEW) {
  884. boundingVolumes = primitive._boundingSphereCV;
  885. } else if (
  886. frameState.mode === SceneMode.SCENE2D &&
  887. defined(primitive._boundingSphere2D)
  888. ) {
  889. boundingVolumes = primitive._boundingSphere2D;
  890. } else if (defined(primitive._boundingSphereMorph)) {
  891. boundingVolumes = primitive._boundingSphereMorph;
  892. }
  893. const classificationType = classificationPrimitive.classificationType;
  894. const queueTerrainCommands =
  895. classificationType !== ClassificationType.CESIUM_3D_TILE;
  896. const queue3DTilesCommands =
  897. classificationType !== ClassificationType.TERRAIN;
  898. const passes = frameState.passes;
  899. let i;
  900. let boundingVolume;
  901. let command;
  902. if (passes.render) {
  903. const colorLength = colorCommands.length;
  904. for (i = 0; i < colorLength; ++i) {
  905. boundingVolume = boundingVolumes[boundingVolumeIndex(i, colorLength)];
  906. if (queueTerrainCommands) {
  907. command = colorCommands[i];
  908. updateAndQueueRenderCommand(
  909. command,
  910. frameState,
  911. modelMatrix,
  912. cull,
  913. boundingVolume,
  914. debugShowBoundingVolume
  915. );
  916. }
  917. if (queue3DTilesCommands) {
  918. command = colorCommands[i].derivedCommands.tileset;
  919. updateAndQueueRenderCommand(
  920. command,
  921. frameState,
  922. modelMatrix,
  923. cull,
  924. boundingVolume,
  925. debugShowBoundingVolume
  926. );
  927. }
  928. }
  929. if (frameState.invertClassification) {
  930. const ignoreShowCommands = classificationPrimitive._commandsIgnoreShow;
  931. const ignoreShowCommandsLength = ignoreShowCommands.length;
  932. for (i = 0; i < ignoreShowCommandsLength; ++i) {
  933. boundingVolume = boundingVolumes[i];
  934. command = ignoreShowCommands[i];
  935. updateAndQueueRenderCommand(
  936. command,
  937. frameState,
  938. modelMatrix,
  939. cull,
  940. boundingVolume,
  941. debugShowBoundingVolume
  942. );
  943. }
  944. }
  945. }
  946. if (passes.pick) {
  947. const pickLength = pickCommands.length;
  948. const pickOffsets = primitive._pickOffsets;
  949. for (i = 0; i < pickLength; ++i) {
  950. const pickOffset = pickOffsets[boundingVolumeIndex(i, pickLength)];
  951. boundingVolume = boundingVolumes[pickOffset.index];
  952. if (queueTerrainCommands) {
  953. command = pickCommands[i];
  954. updateAndQueuePickCommand(
  955. command,
  956. frameState,
  957. modelMatrix,
  958. cull,
  959. boundingVolume
  960. );
  961. }
  962. if (queue3DTilesCommands) {
  963. command = pickCommands[i].derivedCommands.tileset;
  964. updateAndQueuePickCommand(
  965. command,
  966. frameState,
  967. modelMatrix,
  968. cull,
  969. boundingVolume
  970. );
  971. }
  972. }
  973. }
  974. }
  975. /**
  976. * Called when {@link Viewer} or {@link CesiumWidget} render the scene to
  977. * get the draw commands needed to render this primitive.
  978. * <p>
  979. * Do not call this function directly. This is documented just to
  980. * list the exceptions that may be propagated when the scene is rendered:
  981. * </p>
  982. *
  983. * @exception {DeveloperError} All instance geometries must have the same primitiveType.
  984. * @exception {DeveloperError} Appearance and material have a uniform with the same name.
  985. * @exception {DeveloperError} Not all of the geometry instances have the same color attribute.
  986. */
  987. ClassificationPrimitive.prototype.update = function (frameState) {
  988. if (!defined(this._primitive) && !defined(this.geometryInstances)) {
  989. return;
  990. }
  991. let appearance = this.appearance;
  992. if (defined(appearance) && defined(appearance.material)) {
  993. appearance.material.update(frameState.context);
  994. }
  995. const that = this;
  996. const primitiveOptions = this._primitiveOptions;
  997. if (!defined(this._primitive)) {
  998. const instances = Array.isArray(this.geometryInstances)
  999. ? this.geometryInstances
  1000. : [this.geometryInstances];
  1001. const length = instances.length;
  1002. let i;
  1003. let instance;
  1004. let attributes;
  1005. let hasPerColorAttribute = false;
  1006. let allColorsSame = true;
  1007. let firstColor;
  1008. let hasSphericalExtentsAttribute = false;
  1009. let hasPlanarExtentsAttributes = false;
  1010. if (length > 0) {
  1011. attributes = instances[0].attributes;
  1012. // Not expecting these to be set by users, should only be set via GroundPrimitive.
  1013. // So don't check for mismatch.
  1014. hasSphericalExtentsAttribute = ShadowVolumeAppearance.hasAttributesForSphericalExtents(
  1015. attributes
  1016. );
  1017. hasPlanarExtentsAttributes = ShadowVolumeAppearance.hasAttributesForTextureCoordinatePlanes(
  1018. attributes
  1019. );
  1020. firstColor = attributes.color;
  1021. }
  1022. for (i = 0; i < length; i++) {
  1023. instance = instances[i];
  1024. const color = instance.attributes.color;
  1025. if (defined(color)) {
  1026. hasPerColorAttribute = true;
  1027. }
  1028. //>>includeStart('debug', pragmas.debug);
  1029. else if (hasPerColorAttribute) {
  1030. throw new DeveloperError(
  1031. "All GeometryInstances must have color attributes to use per-instance color."
  1032. );
  1033. }
  1034. //>>includeEnd('debug');
  1035. allColorsSame =
  1036. allColorsSame &&
  1037. defined(color) &&
  1038. ColorGeometryInstanceAttribute.equals(firstColor, color);
  1039. }
  1040. // If no attributes exist for computing spherical extents or fragment culling,
  1041. // throw if the colors aren't all the same.
  1042. if (
  1043. !allColorsSame &&
  1044. !hasSphericalExtentsAttribute &&
  1045. !hasPlanarExtentsAttributes
  1046. ) {
  1047. throw new DeveloperError(
  1048. "All GeometryInstances must have the same color attribute except via GroundPrimitives"
  1049. );
  1050. }
  1051. // default to a color appearance
  1052. if (hasPerColorAttribute && !defined(appearance)) {
  1053. appearance = new PerInstanceColorAppearance({
  1054. flat: true,
  1055. });
  1056. this.appearance = appearance;
  1057. }
  1058. //>>includeStart('debug', pragmas.debug);
  1059. if (
  1060. !hasPerColorAttribute &&
  1061. appearance instanceof PerInstanceColorAppearance
  1062. ) {
  1063. throw new DeveloperError(
  1064. "PerInstanceColorAppearance requires color GeometryInstanceAttributes on all GeometryInstances"
  1065. );
  1066. }
  1067. if (
  1068. defined(appearance.material) &&
  1069. !hasSphericalExtentsAttribute &&
  1070. !hasPlanarExtentsAttributes
  1071. ) {
  1072. throw new DeveloperError(
  1073. "Materials on ClassificationPrimitives are not supported except via GroundPrimitives"
  1074. );
  1075. }
  1076. //>>includeEnd('debug');
  1077. this._usePickOffsets =
  1078. !hasSphericalExtentsAttribute && !hasPlanarExtentsAttributes;
  1079. this._hasSphericalExtentsAttribute = hasSphericalExtentsAttribute;
  1080. this._hasPlanarExtentsAttributes = hasPlanarExtentsAttributes;
  1081. this._hasPerColorAttribute = hasPerColorAttribute;
  1082. const geometryInstances = new Array(length);
  1083. for (i = 0; i < length; ++i) {
  1084. instance = instances[i];
  1085. geometryInstances[i] = new GeometryInstance({
  1086. geometry: instance.geometry,
  1087. attributes: instance.attributes,
  1088. modelMatrix: instance.modelMatrix,
  1089. id: instance.id,
  1090. pickPrimitive: defaultValue(this._pickPrimitive, that),
  1091. });
  1092. }
  1093. primitiveOptions.appearance = appearance;
  1094. primitiveOptions.geometryInstances = geometryInstances;
  1095. if (defined(this._createBoundingVolumeFunction)) {
  1096. primitiveOptions._createBoundingVolumeFunction = function (
  1097. frameState,
  1098. geometry
  1099. ) {
  1100. that._createBoundingVolumeFunction(frameState, geometry);
  1101. };
  1102. }
  1103. primitiveOptions._createRenderStatesFunction = function (
  1104. primitive,
  1105. context,
  1106. appearance,
  1107. twoPasses
  1108. ) {
  1109. createRenderStates(that, context);
  1110. };
  1111. primitiveOptions._createShaderProgramFunction = function (
  1112. primitive,
  1113. frameState,
  1114. appearance
  1115. ) {
  1116. createShaderProgram(that, frameState);
  1117. };
  1118. primitiveOptions._createCommandsFunction = function (
  1119. primitive,
  1120. appearance,
  1121. material,
  1122. translucent,
  1123. twoPasses,
  1124. colorCommands,
  1125. pickCommands
  1126. ) {
  1127. createCommands(
  1128. that,
  1129. undefined,
  1130. undefined,
  1131. true,
  1132. false,
  1133. colorCommands,
  1134. pickCommands
  1135. );
  1136. };
  1137. if (defined(this._updateAndQueueCommandsFunction)) {
  1138. primitiveOptions._updateAndQueueCommandsFunction = function (
  1139. primitive,
  1140. frameState,
  1141. colorCommands,
  1142. pickCommands,
  1143. modelMatrix,
  1144. cull,
  1145. debugShowBoundingVolume,
  1146. twoPasses
  1147. ) {
  1148. that._updateAndQueueCommandsFunction(
  1149. primitive,
  1150. frameState,
  1151. colorCommands,
  1152. pickCommands,
  1153. modelMatrix,
  1154. cull,
  1155. debugShowBoundingVolume,
  1156. twoPasses
  1157. );
  1158. };
  1159. } else {
  1160. primitiveOptions._updateAndQueueCommandsFunction = function (
  1161. primitive,
  1162. frameState,
  1163. colorCommands,
  1164. pickCommands,
  1165. modelMatrix,
  1166. cull,
  1167. debugShowBoundingVolume,
  1168. twoPasses
  1169. ) {
  1170. updateAndQueueCommands(
  1171. that,
  1172. frameState,
  1173. colorCommands,
  1174. pickCommands,
  1175. modelMatrix,
  1176. cull,
  1177. debugShowBoundingVolume,
  1178. twoPasses
  1179. );
  1180. };
  1181. }
  1182. this._primitive = new Primitive(primitiveOptions);
  1183. }
  1184. if (
  1185. this.debugShowShadowVolume &&
  1186. !this._debugShowShadowVolume &&
  1187. this._ready
  1188. ) {
  1189. this._debugShowShadowVolume = true;
  1190. this._rsStencilDepthPass = RenderState.fromCache(
  1191. getStencilDepthRenderState(false, false)
  1192. );
  1193. this._rsStencilDepthPass3DTiles = RenderState.fromCache(
  1194. getStencilDepthRenderState(false, true)
  1195. );
  1196. this._rsColorPass = RenderState.fromCache(getColorRenderState(false));
  1197. } else if (!this.debugShowShadowVolume && this._debugShowShadowVolume) {
  1198. this._debugShowShadowVolume = false;
  1199. this._rsStencilDepthPass = RenderState.fromCache(
  1200. getStencilDepthRenderState(true, false)
  1201. );
  1202. this._rsStencilDepthPass3DTiles = RenderState.fromCache(
  1203. getStencilDepthRenderState(true, true)
  1204. );
  1205. this._rsColorPass = RenderState.fromCache(getColorRenderState(true));
  1206. }
  1207. // Update primitive appearance
  1208. if (this._primitive.appearance !== appearance) {
  1209. //>>includeStart('debug', pragmas.debug);
  1210. // Check if the appearance is supported by the geometry attributes
  1211. if (
  1212. !this._hasSphericalExtentsAttribute &&
  1213. !this._hasPlanarExtentsAttributes &&
  1214. defined(appearance.material)
  1215. ) {
  1216. throw new DeveloperError(
  1217. "Materials on ClassificationPrimitives are not supported except via GroundPrimitive"
  1218. );
  1219. }
  1220. if (
  1221. !this._hasPerColorAttribute &&
  1222. appearance instanceof PerInstanceColorAppearance
  1223. ) {
  1224. throw new DeveloperError(
  1225. "PerInstanceColorAppearance requires color GeometryInstanceAttribute"
  1226. );
  1227. }
  1228. //>>includeEnd('debug');
  1229. this._primitive.appearance = appearance;
  1230. }
  1231. this._primitive.show = this.show;
  1232. this._primitive.debugShowBoundingVolume = this.debugShowBoundingVolume;
  1233. this._primitive.update(frameState);
  1234. frameState.afterRender.push(() => {
  1235. if (defined(this._primitive) && this._primitive.ready) {
  1236. this._completeLoad();
  1237. }
  1238. });
  1239. };
  1240. /**
  1241. * Returns the modifiable per-instance attributes for a {@link GeometryInstance}.
  1242. *
  1243. * @param {*} id The id of the {@link GeometryInstance}.
  1244. * @returns {object} The typed array in the attribute's format or undefined if the is no instance with id.
  1245. *
  1246. * @exception {DeveloperError} must call update before calling getGeometryInstanceAttributes.
  1247. *
  1248. * @example
  1249. * const attributes = primitive.getGeometryInstanceAttributes('an id');
  1250. * attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.AQUA);
  1251. * attributes.show = Cesium.ShowGeometryInstanceAttribute.toValue(true);
  1252. */
  1253. ClassificationPrimitive.prototype.getGeometryInstanceAttributes = function (
  1254. id
  1255. ) {
  1256. //>>includeStart('debug', pragmas.debug);
  1257. if (!defined(this._primitive)) {
  1258. throw new DeveloperError(
  1259. "must call update before calling getGeometryInstanceAttributes"
  1260. );
  1261. }
  1262. //>>includeEnd('debug');
  1263. return this._primitive.getGeometryInstanceAttributes(id);
  1264. };
  1265. /**
  1266. * Returns true if this object was destroyed; otherwise, false.
  1267. * <p>
  1268. * If this object was destroyed, it should not be used; calling any function other than
  1269. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  1270. * </p>
  1271. *
  1272. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  1273. *
  1274. * @see ClassificationPrimitive#destroy
  1275. */
  1276. ClassificationPrimitive.prototype.isDestroyed = function () {
  1277. return false;
  1278. };
  1279. /**
  1280. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  1281. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  1282. * <p>
  1283. * Once an object is destroyed, it should not be used; calling any function other than
  1284. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  1285. * assign the return value (<code>undefined</code>) to the object as done in the example.
  1286. * </p>
  1287. *
  1288. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  1289. *
  1290. * @example
  1291. * e = e && e.destroy();
  1292. *
  1293. * @see ClassificationPrimitive#isDestroyed
  1294. */
  1295. ClassificationPrimitive.prototype.destroy = function () {
  1296. this._primitive = this._primitive && this._primitive.destroy();
  1297. this._sp = this._sp && this._sp.destroy();
  1298. this._spPick = this._spPick && this._spPick.destroy();
  1299. this._spColor = this._spColor && this._spColor.destroy();
  1300. // Derived programs, destroyed above if they existed.
  1301. this._spPick2D = undefined;
  1302. this._spColor2D = undefined;
  1303. return destroyObject(this);
  1304. };
  1305. export default ClassificationPrimitive;