Cesium3DTile.js 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Color from "../Core/Color.js";
  4. import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
  5. import CullingVolume from "../Core/CullingVolume.js";
  6. import defaultValue from "../Core/defaultValue.js";
  7. import defer from "../Core/defer.js";
  8. import defined from "../Core/defined.js";
  9. import deprecationWarning from "../Core/deprecationWarning.js";
  10. import destroyObject from "../Core/destroyObject.js";
  11. import Ellipsoid from "../Core/Ellipsoid.js";
  12. import Intersect from "../Core/Intersect.js";
  13. import JulianDate from "../Core/JulianDate.js";
  14. import CesiumMath from "../Core/Math.js";
  15. import Matrix3 from "../Core/Matrix3.js";
  16. import Matrix4 from "../Core/Matrix4.js";
  17. import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
  18. import OrthographicFrustum from "../Core/OrthographicFrustum.js";
  19. import Rectangle from "../Core/Rectangle.js";
  20. import Request from "../Core/Request.js";
  21. import RequestScheduler from "../Core/RequestScheduler.js";
  22. import RequestState from "../Core/RequestState.js";
  23. import RequestType from "../Core/RequestType.js";
  24. import Resource from "../Core/Resource.js";
  25. import RuntimeError from "../Core/RuntimeError.js";
  26. import Cesium3DContentGroup from "./Cesium3DContentGroup.js";
  27. import Cesium3DTileContentFactory from "./Cesium3DTileContentFactory.js";
  28. import Cesium3DTileContentState from "./Cesium3DTileContentState.js";
  29. import Cesium3DTileContentType from "./Cesium3DTileContentType.js";
  30. import Cesium3DTileOptimizationHint from "./Cesium3DTileOptimizationHint.js";
  31. import Cesium3DTilePass from "./Cesium3DTilePass.js";
  32. import Cesium3DTileRefine from "./Cesium3DTileRefine.js";
  33. import Empty3DTileContent from "./Empty3DTileContent.js";
  34. import findContentMetadata from "./findContentMetadata.js";
  35. import findGroupMetadata from "./findGroupMetadata.js";
  36. import findTileMetadata from "./findTileMetadata.js";
  37. import hasExtension from "./hasExtension.js";
  38. import Multiple3DTileContent from "./Multiple3DTileContent.js";
  39. import preprocess3DTileContent from "./preprocess3DTileContent.js";
  40. import SceneMode from "./SceneMode.js";
  41. import TileBoundingRegion from "./TileBoundingRegion.js";
  42. import TileBoundingS2Cell from "./TileBoundingS2Cell.js";
  43. import TileBoundingSphere from "./TileBoundingSphere.js";
  44. import TileOrientedBoundingBox from "./TileOrientedBoundingBox.js";
  45. import Pass from "../Renderer/Pass.js";
  46. /**
  47. * A tile in a {@link Cesium3DTileset}. When a tile is first created, its content is not loaded;
  48. * the content is loaded on-demand when needed based on the view.
  49. * <p>
  50. * Do not construct this directly, instead access tiles through {@link Cesium3DTileset#tileVisible}.
  51. * </p>
  52. *
  53. * @alias Cesium3DTile
  54. * @constructor
  55. */
  56. function Cesium3DTile(tileset, baseResource, header, parent) {
  57. this._tileset = tileset;
  58. this._header = header;
  59. const hasContentsArray = defined(header.contents);
  60. const hasMultipleContents =
  61. (hasContentsArray && header.contents.length > 1) ||
  62. hasExtension(header, "3DTILES_multiple_contents");
  63. // In the 1.0 schema, content is stored in tile.content instead of tile.contents
  64. const contentHeader =
  65. hasContentsArray && !hasMultipleContents
  66. ? header.contents[0]
  67. : header.content;
  68. /**
  69. * The local transform of this tile.
  70. * @type {Matrix4}
  71. */
  72. this.transform = defined(header.transform)
  73. ? Matrix4.unpack(header.transform)
  74. : Matrix4.clone(Matrix4.IDENTITY);
  75. const parentTransform = defined(parent)
  76. ? parent.computedTransform
  77. : tileset.modelMatrix;
  78. const computedTransform = Matrix4.multiply(
  79. parentTransform,
  80. this.transform,
  81. new Matrix4()
  82. );
  83. const parentInitialTransform = defined(parent)
  84. ? parent._initialTransform
  85. : Matrix4.IDENTITY;
  86. this._initialTransform = Matrix4.multiply(
  87. parentInitialTransform,
  88. this.transform,
  89. new Matrix4()
  90. );
  91. /**
  92. * The final computed transform of this tile.
  93. * @type {Matrix4}
  94. * @readonly
  95. */
  96. this.computedTransform = computedTransform;
  97. this._boundingVolume = this.createBoundingVolume(
  98. header.boundingVolume,
  99. computedTransform
  100. );
  101. this._boundingVolume2D = undefined;
  102. let contentBoundingVolume;
  103. if (defined(contentHeader) && defined(contentHeader.boundingVolume)) {
  104. // Non-leaf tiles may have a content bounding-volume, which is a tight-fit bounding volume
  105. // around only the features in the tile. This box is useful for culling for rendering,
  106. // but not for culling for traversing the tree since it does not guarantee spatial coherence, i.e.,
  107. // since it only bounds features in the tile, not the entire tile, children may be
  108. // outside of this box.
  109. contentBoundingVolume = this.createBoundingVolume(
  110. contentHeader.boundingVolume,
  111. computedTransform
  112. );
  113. }
  114. this._contentBoundingVolume = contentBoundingVolume;
  115. this._contentBoundingVolume2D = undefined;
  116. let viewerRequestVolume;
  117. if (defined(header.viewerRequestVolume)) {
  118. viewerRequestVolume = this.createBoundingVolume(
  119. header.viewerRequestVolume,
  120. computedTransform
  121. );
  122. }
  123. this._viewerRequestVolume = viewerRequestVolume;
  124. /**
  125. * The error, in meters, introduced if this tile is rendered and its children are not.
  126. * This is used to compute screen space error, i.e., the error measured in pixels.
  127. *
  128. * @type {Number}
  129. * @readonly
  130. */
  131. this.geometricError = header.geometricError;
  132. this._geometricError = header.geometricError;
  133. if (!defined(this._geometricError)) {
  134. this._geometricError = defined(parent)
  135. ? parent.geometricError
  136. : tileset._geometricError;
  137. Cesium3DTile._deprecationWarning(
  138. "geometricErrorUndefined",
  139. "Required property geometricError is undefined for this tile. Using parent's geometric error instead."
  140. );
  141. }
  142. this.updateGeometricErrorScale();
  143. let refine;
  144. if (defined(header.refine)) {
  145. if (header.refine === "replace" || header.refine === "add") {
  146. Cesium3DTile._deprecationWarning(
  147. "lowercase-refine",
  148. `This tile uses a lowercase refine "${
  149. header.refine
  150. }". Instead use "${header.refine.toUpperCase()}".`
  151. );
  152. }
  153. refine =
  154. header.refine.toUpperCase() === "REPLACE"
  155. ? Cesium3DTileRefine.REPLACE
  156. : Cesium3DTileRefine.ADD;
  157. } else if (defined(parent)) {
  158. // Inherit from parent tile if omitted.
  159. refine = parent.refine;
  160. } else {
  161. refine = Cesium3DTileRefine.REPLACE;
  162. }
  163. /**
  164. * Specifies the type of refinement that is used when traversing this tile for rendering.
  165. *
  166. * @type {Cesium3DTileRefine}
  167. * @readonly
  168. * @private
  169. */
  170. this.refine = refine;
  171. /**
  172. * Gets the tile's children.
  173. *
  174. * @type {Cesium3DTile[]}
  175. * @readonly
  176. */
  177. this.children = [];
  178. /**
  179. * This tile's parent or <code>undefined</code> if this tile is the root.
  180. * <p>
  181. * When a tile's content points to an external tileset JSON file, the external tileset's
  182. * root tile's parent is not <code>undefined</code>; instead, the parent references
  183. * the tile (with its content pointing to an external tileset JSON file) as if the two tilesets were merged.
  184. * </p>
  185. *
  186. * @type {Cesium3DTile}
  187. * @readonly
  188. */
  189. this.parent = parent;
  190. let content;
  191. let hasEmptyContent = false;
  192. let contentState;
  193. let contentResource;
  194. let serverKey;
  195. baseResource = Resource.createIfNeeded(baseResource);
  196. if (hasMultipleContents) {
  197. contentState = Cesium3DTileContentState.UNLOADED;
  198. // Each content may have its own URI, but they all need to be resolved
  199. // relative to the tileset, so the base resource is used.
  200. contentResource = baseResource.clone();
  201. } else if (defined(contentHeader)) {
  202. let contentHeaderUri = contentHeader.uri;
  203. if (defined(contentHeader.url)) {
  204. Cesium3DTile._deprecationWarning(
  205. "contentUrl",
  206. 'This tileset JSON uses the "content.url" property which has been deprecated. Use "content.uri" instead.'
  207. );
  208. contentHeaderUri = contentHeader.url;
  209. }
  210. contentState = Cesium3DTileContentState.UNLOADED;
  211. contentResource = baseResource.getDerivedResource({
  212. url: contentHeaderUri,
  213. });
  214. serverKey = RequestScheduler.getServerKey(
  215. contentResource.getUrlComponent()
  216. );
  217. } else {
  218. content = new Empty3DTileContent(tileset, this);
  219. hasEmptyContent = true;
  220. contentState = Cesium3DTileContentState.READY;
  221. }
  222. this._content = content;
  223. this._contentResource = contentResource;
  224. this._contentState = contentState;
  225. this._contentReadyToProcessPromise = undefined;
  226. this._contentReadyPromise = undefined;
  227. this._expiredContent = undefined;
  228. this._serverKey = serverKey;
  229. /**
  230. * When <code>true</code>, the tile has no content.
  231. *
  232. * @type {Boolean}
  233. * @readonly
  234. *
  235. * @private
  236. */
  237. this.hasEmptyContent = hasEmptyContent;
  238. /**
  239. * When <code>true</code>, the tile's content points to an external tileset.
  240. * <p>
  241. * This is <code>false</code> until the tile's content is loaded.
  242. * </p>
  243. *
  244. * @type {Boolean}
  245. * @readonly
  246. *
  247. * @private
  248. */
  249. this.hasTilesetContent = false;
  250. /**
  251. * When <code>true</code>, the tile's content is an implicit tileset.
  252. * <p>
  253. * This is <code>false</code> until the tile's implicit content is loaded.
  254. * </p>
  255. *
  256. * @type {Boolean}
  257. * @readonly
  258. *
  259. * @private
  260. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  261. */
  262. this.hasImplicitContent = false;
  263. /**
  264. * When <code>true</code>, the tile contains content metadata from implicit tiling. This flag is set
  265. * for tiles transcoded by <code>Implicit3DTileContent</code>.
  266. * <p>
  267. * This is <code>false</code> until the tile's content is loaded.
  268. * </p>
  269. *
  270. * @type {Boolean}
  271. * @readonly
  272. *
  273. * @private
  274. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  275. */
  276. this.hasImplicitContentMetadata = false;
  277. /**
  278. * When <code>true</code>, the tile has multiple contents, either in the tile JSON (3D Tiles 1.1)
  279. * or via the <code>3DTILES_multiple_contents</code> extension.
  280. *
  281. * @see {@link https://github.com/CesiumGS/3d-tiles/tree/main/extensions/3DTILES_multiple_contents|3DTILES_multiple_contents extension}
  282. *
  283. * @type {Boolean}
  284. * @readonly
  285. *
  286. * @private
  287. */
  288. this.hasMultipleContents = hasMultipleContents;
  289. /**
  290. * When tile metadata is present (3D Tiles 1.1) or the <code>3DTILES_metadata</code> extension is used,
  291. * this stores a {@link TileMetadata} object for accessing tile metadata.
  292. *
  293. * @type {TileMetadata}
  294. * @readonly
  295. * @private
  296. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  297. */
  298. this.metadata = findTileMetadata(tileset, header);
  299. /**
  300. * The node in the tileset's LRU cache, used to determine when to unload a tile's content.
  301. *
  302. * See {@link Cesium3DTilesetCache}
  303. *
  304. * @type {DoublyLinkedListNode}
  305. * @readonly
  306. *
  307. * @private
  308. */
  309. this.cacheNode = undefined;
  310. const expire = header.expire;
  311. let expireDuration;
  312. let expireDate;
  313. if (defined(expire)) {
  314. expireDuration = expire.duration;
  315. if (defined(expire.date)) {
  316. expireDate = JulianDate.fromIso8601(expire.date);
  317. }
  318. }
  319. /**
  320. * The time in seconds after the tile's content is ready when the content expires and new content is requested.
  321. *
  322. * @type {Number}
  323. */
  324. this.expireDuration = expireDuration;
  325. /**
  326. * The date when the content expires and new content is requested.
  327. *
  328. * @type {JulianDate}
  329. */
  330. this.expireDate = expireDate;
  331. /**
  332. * The time when a style was last applied to this tile.
  333. *
  334. * @type {Number}
  335. *
  336. * @private
  337. */
  338. this.lastStyleTime = 0.0;
  339. /**
  340. * Marks whether the tile's children bounds are fully contained within the tile's bounds
  341. *
  342. * @type {Cesium3DTileOptimizationHint}
  343. *
  344. * @private
  345. */
  346. this._optimChildrenWithinParent = Cesium3DTileOptimizationHint.NOT_COMPUTED;
  347. /**
  348. * Tracks if the tile's relationship with a ClippingPlaneCollection has changed with regards
  349. * to the ClippingPlaneCollection's state.
  350. *
  351. * @type {Boolean}
  352. *
  353. * @private
  354. */
  355. this.clippingPlanesDirty = false;
  356. /**
  357. * Tracks if the tile's request should be deferred until all non-deferred
  358. * tiles load.
  359. *
  360. * @type {Boolean}
  361. *
  362. * @private
  363. */
  364. this.priorityDeferred = false;
  365. /**
  366. * For implicit tiling, an ImplicitTileset object will be attached to a
  367. * placeholder tile with either implicit tiling in the JSON (3D Tiles 1.1)
  368. * or the <code>3DTILES_implicit_tiling</code> extension.
  369. * This way the {@link Implicit3DTileContent} can access the tile later once the content is fetched.
  370. *
  371. * @type {ImplicitTileset|undefined}
  372. *
  373. * @private
  374. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  375. */
  376. this.implicitTileset = undefined;
  377. /**
  378. * For implicit tiling, the (level, x, y, [z]) coordinates within the
  379. * implicit tileset are stored in the tile.
  380. *
  381. * @type {ImplicitTileCoordinates|undefined}
  382. *
  383. * @private
  384. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  385. */
  386. this.implicitCoordinates = undefined;
  387. /**
  388. * For implicit tiling, each transcoded tile will hold a weak reference to
  389. * the {@link ImplicitSubtree}.
  390. *
  391. * @type {ImplicitSubtree|undefined}
  392. *
  393. * @private
  394. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  395. */
  396. this.implicitSubtree = undefined;
  397. // Members that are updated every frame for tree traversal and rendering optimizations:
  398. this._distanceToCamera = 0.0;
  399. this._centerZDepth = 0.0;
  400. this._screenSpaceError = 0.0;
  401. this._screenSpaceErrorProgressiveResolution = 0.0; // The screen space error at a given screen height of tileset.progressiveResolutionHeightFraction * screenHeight
  402. this._visibilityPlaneMask = 0;
  403. this._visible = false;
  404. this._inRequestVolume = false;
  405. this._finalResolution = true;
  406. this._depth = 0;
  407. this._stackLength = 0;
  408. this._selectionDepth = 0;
  409. this._updatedVisibilityFrame = 0;
  410. this._touchedFrame = 0;
  411. this._visitedFrame = 0;
  412. this._selectedFrame = 0;
  413. this._requestedFrame = 0;
  414. this._ancestorWithContent = undefined;
  415. this._ancestorWithContentAvailable = undefined;
  416. this._refines = false;
  417. this._shouldSelect = false;
  418. this._isClipped = true;
  419. this._clippingPlanesState = 0; // encapsulates (_isClipped, clippingPlanes.enabled) and number/function
  420. this._debugBoundingVolume = undefined;
  421. this._debugContentBoundingVolume = undefined;
  422. this._debugViewerRequestVolume = undefined;
  423. this._debugColor = Color.fromRandom({ alpha: 1.0 });
  424. this._debugColorizeTiles = false;
  425. this._priority = 0.0; // The priority used for request sorting
  426. this._priorityHolder = this; // Reference to the ancestor up the tree that holds the _foveatedFactor and _distanceToCamera for all tiles in the refinement chain.
  427. this._priorityProgressiveResolution = false;
  428. this._priorityProgressiveResolutionScreenSpaceErrorLeaf = false;
  429. this._priorityReverseScreenSpaceError = 0.0;
  430. this._foveatedFactor = 0.0;
  431. this._wasMinPriorityChild = false; // Needed for knowing when to continue a refinement chain. Gets reset in updateTile in traversal and gets set in updateAndPushChildren in traversal.
  432. this._loadTimestamp = new JulianDate();
  433. this._commandsLength = 0;
  434. this._color = undefined;
  435. this._colorDirty = false;
  436. this._request = undefined;
  437. }
  438. // This can be overridden for testing purposes
  439. Cesium3DTile._deprecationWarning = deprecationWarning;
  440. Object.defineProperties(Cesium3DTile.prototype, {
  441. /**
  442. * The tileset containing this tile.
  443. *
  444. * @memberof Cesium3DTile.prototype
  445. *
  446. * @type {Cesium3DTileset}
  447. * @readonly
  448. */
  449. tileset: {
  450. get: function () {
  451. return this._tileset;
  452. },
  453. },
  454. /**
  455. * The tile's content. This represents the actual tile's payload,
  456. * not the content's metadata in the tileset JSON file.
  457. *
  458. * @memberof Cesium3DTile.prototype
  459. *
  460. * @type {Cesium3DTileContent}
  461. * @readonly
  462. */
  463. content: {
  464. get: function () {
  465. return this._content;
  466. },
  467. },
  468. /**
  469. * Get the tile's bounding volume.
  470. *
  471. * @memberof Cesium3DTile.prototype
  472. *
  473. * @type {TileBoundingVolume}
  474. * @readonly
  475. * @private
  476. */
  477. boundingVolume: {
  478. get: function () {
  479. return this._boundingVolume;
  480. },
  481. },
  482. /**
  483. * Get the bounding volume of the tile's contents. This defaults to the
  484. * tile's bounding volume when the content's bounding volume is
  485. * <code>undefined</code>.
  486. *
  487. * @memberof Cesium3DTile.prototype
  488. *
  489. * @type {TileBoundingVolume}
  490. * @readonly
  491. * @private
  492. */
  493. contentBoundingVolume: {
  494. get: function () {
  495. return defaultValue(this._contentBoundingVolume, this._boundingVolume);
  496. },
  497. },
  498. /**
  499. * Get the bounding sphere derived from the tile's bounding volume.
  500. *
  501. * @memberof Cesium3DTile.prototype
  502. *
  503. * @type {BoundingSphere}
  504. * @readonly
  505. */
  506. boundingSphere: {
  507. get: function () {
  508. return this._boundingVolume.boundingSphere;
  509. },
  510. },
  511. /**
  512. * Returns the <code>extras</code> property in the tileset JSON for this tile, which contains application specific metadata.
  513. * Returns <code>undefined</code> if <code>extras</code> does not exist.
  514. *
  515. * @memberof Cesium3DTile.prototype
  516. *
  517. * @type {*}
  518. * @readonly
  519. * @see {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification#specifying-extensions-and-application-specific-extras|Extras in the 3D Tiles specification.}
  520. */
  521. extras: {
  522. get: function () {
  523. return this._header.extras;
  524. },
  525. },
  526. /**
  527. * Gets or sets the tile's highlight color.
  528. *
  529. * @memberof Cesium3DTile.prototype
  530. *
  531. * @type {Color}
  532. *
  533. * @default {@link Color.WHITE}
  534. *
  535. * @private
  536. */
  537. color: {
  538. get: function () {
  539. if (!defined(this._color)) {
  540. this._color = new Color();
  541. }
  542. return Color.clone(this._color);
  543. },
  544. set: function (value) {
  545. this._color = Color.clone(value, this._color);
  546. this._colorDirty = true;
  547. },
  548. },
  549. /**
  550. * Determines if the tile has available content to render. <code>true</code> if the tile's
  551. * content is ready or if it has expired content that renders while new content loads; otherwise,
  552. * <code>false</code>.
  553. *
  554. * @memberof Cesium3DTile.prototype
  555. *
  556. * @type {Boolean}
  557. * @readonly
  558. *
  559. * @private
  560. */
  561. contentAvailable: {
  562. get: function () {
  563. return (
  564. (this.contentReady &&
  565. !this.hasEmptyContent &&
  566. !this.hasTilesetContent &&
  567. !this.hasImplicitContent) ||
  568. (defined(this._expiredContent) && !this.contentFailed)
  569. );
  570. },
  571. },
  572. /**
  573. * Determines if the tile's content is ready. This is automatically <code>true</code> for
  574. * tile's with empty content.
  575. *
  576. * @memberof Cesium3DTile.prototype
  577. *
  578. * @type {Boolean}
  579. * @readonly
  580. *
  581. * @private
  582. */
  583. contentReady: {
  584. get: function () {
  585. return this._contentState === Cesium3DTileContentState.READY;
  586. },
  587. },
  588. /**
  589. * Determines if the tile's content has not be requested. <code>true</code> if tile's
  590. * content has not be requested; otherwise, <code>false</code>.
  591. *
  592. * @memberof Cesium3DTile.prototype
  593. *
  594. * @type {Boolean}
  595. * @readonly
  596. *
  597. * @private
  598. */
  599. contentUnloaded: {
  600. get: function () {
  601. return this._contentState === Cesium3DTileContentState.UNLOADED;
  602. },
  603. },
  604. /**
  605. * Determines if the tile's content is expired. <code>true</code> if tile's
  606. * content is expired; otherwise, <code>false</code>.
  607. *
  608. * @memberof Cesium3DTile.prototype
  609. *
  610. * @type {Boolean}
  611. * @readonly
  612. *
  613. * @private
  614. */
  615. contentExpired: {
  616. get: function () {
  617. return this._contentState === Cesium3DTileContentState.EXPIRED;
  618. },
  619. },
  620. /**
  621. * Determines if the tile's content failed to load. <code>true</code> if the tile's
  622. * content failed to load; otherwise, <code>false</code>.
  623. *
  624. * @memberof Cesium3DTile.prototype
  625. *
  626. * @type {Boolean}
  627. * @readonly
  628. *
  629. * @private
  630. */
  631. contentFailed: {
  632. get: function () {
  633. return this._contentState === Cesium3DTileContentState.FAILED;
  634. },
  635. },
  636. /**
  637. * Gets the promise that will be resolved when the tile's content is ready to process.
  638. * This happens after the content is downloaded but before the content is ready
  639. * to render.
  640. * <p>
  641. * The promise remains <code>undefined</code> until the tile's content is requested.
  642. * </p>
  643. *
  644. * @type {Promise.<Cesium3DTileContent>}
  645. * @readonly
  646. *
  647. * @private
  648. */
  649. contentReadyToProcessPromise: {
  650. get: function () {
  651. if (defined(this._contentReadyToProcessPromise)) {
  652. return this._contentReadyToProcessPromise.promise;
  653. }
  654. return undefined;
  655. },
  656. },
  657. /**
  658. * Gets the promise that will be resolved when the tile's content is ready to render.
  659. * <p>
  660. * The promise remains <code>undefined</code> until the tile's content is requested.
  661. * </p>
  662. *
  663. * @type {Promise.<Cesium3DTileContent>}
  664. * @readonly
  665. *
  666. * @private
  667. */
  668. contentReadyPromise: {
  669. get: function () {
  670. if (defined(this._contentReadyPromise)) {
  671. return this._contentReadyPromise.promise;
  672. }
  673. return undefined;
  674. },
  675. },
  676. /**
  677. * Returns the number of draw commands used by this tile.
  678. *
  679. * @readonly
  680. *
  681. * @private
  682. */
  683. commandsLength: {
  684. get: function () {
  685. return this._commandsLength;
  686. },
  687. },
  688. });
  689. const scratchCartesian = new Cartesian3();
  690. function isPriorityDeferred(tile, frameState) {
  691. const tileset = tile._tileset;
  692. // If closest point on line is inside the sphere then set foveatedFactor to 0. Otherwise, the dot product is with the line from camera to the point on the sphere that is closest to the line.
  693. const camera = frameState.camera;
  694. const boundingSphere = tile.boundingSphere;
  695. const radius = boundingSphere.radius;
  696. const scaledCameraDirection = Cartesian3.multiplyByScalar(
  697. camera.directionWC,
  698. tile._centerZDepth,
  699. scratchCartesian
  700. );
  701. const closestPointOnLine = Cartesian3.add(
  702. camera.positionWC,
  703. scaledCameraDirection,
  704. scratchCartesian
  705. );
  706. // The distance from the camera's view direction to the tile.
  707. const toLine = Cartesian3.subtract(
  708. closestPointOnLine,
  709. boundingSphere.center,
  710. scratchCartesian
  711. );
  712. const distanceToCenterLine = Cartesian3.magnitude(toLine);
  713. const notTouchingSphere = distanceToCenterLine > radius;
  714. // If camera's direction vector is inside the bounding sphere then consider
  715. // this tile right along the line of sight and set _foveatedFactor to 0.
  716. // Otherwise,_foveatedFactor is one minus the dot product of the camera's direction
  717. // and the vector between the camera and the point on the bounding sphere closest to the view line.
  718. if (notTouchingSphere) {
  719. const toLineNormalized = Cartesian3.normalize(toLine, scratchCartesian);
  720. const scaledToLine = Cartesian3.multiplyByScalar(
  721. toLineNormalized,
  722. radius,
  723. scratchCartesian
  724. );
  725. const closestOnSphere = Cartesian3.add(
  726. boundingSphere.center,
  727. scaledToLine,
  728. scratchCartesian
  729. );
  730. const toClosestOnSphere = Cartesian3.subtract(
  731. closestOnSphere,
  732. camera.positionWC,
  733. scratchCartesian
  734. );
  735. const toClosestOnSphereNormalize = Cartesian3.normalize(
  736. toClosestOnSphere,
  737. scratchCartesian
  738. );
  739. tile._foveatedFactor =
  740. 1.0 -
  741. Math.abs(Cartesian3.dot(camera.directionWC, toClosestOnSphereNormalize));
  742. } else {
  743. tile._foveatedFactor = 0.0;
  744. }
  745. // Skip this feature if: non-skipLevelOfDetail and replace refine, if the foveated settings are turned off, if tile is progressive resolution and replace refine and skipLevelOfDetail (will help get rid of ancestor artifacts faster)
  746. // Or if the tile is a preload of any kind
  747. const replace = tile.refine === Cesium3DTileRefine.REPLACE;
  748. const skipLevelOfDetail = tileset._skipLevelOfDetail;
  749. if (
  750. (replace && !skipLevelOfDetail) ||
  751. !tileset.foveatedScreenSpaceError ||
  752. tileset.foveatedConeSize === 1.0 ||
  753. (tile._priorityProgressiveResolution && replace && skipLevelOfDetail) ||
  754. tileset._pass === Cesium3DTilePass.PRELOAD_FLIGHT ||
  755. tileset._pass === Cesium3DTilePass.PRELOAD
  756. ) {
  757. return false;
  758. }
  759. const maximumFovatedFactor = 1.0 - Math.cos(camera.frustum.fov * 0.5); // 0.14 for fov = 60. NOTE very hard to defer vertically foveated tiles since max is based on fovy (which is fov). Lowering the 0.5 to a smaller fraction of the screen height will start to defer vertically foveated tiles.
  760. const foveatedConeFactor = tileset.foveatedConeSize * maximumFovatedFactor;
  761. // If it's inside the user-defined view cone, then it should not be deferred.
  762. if (tile._foveatedFactor <= foveatedConeFactor) {
  763. return false;
  764. }
  765. // Relax SSE based on how big the angle is between the tile and the edge of the foveated cone.
  766. const range = maximumFovatedFactor - foveatedConeFactor;
  767. const normalizedFoveatedFactor = CesiumMath.clamp(
  768. (tile._foveatedFactor - foveatedConeFactor) / range,
  769. 0.0,
  770. 1.0
  771. );
  772. const sseRelaxation = tileset.foveatedInterpolationCallback(
  773. tileset.foveatedMinimumScreenSpaceErrorRelaxation,
  774. tileset.maximumScreenSpaceError,
  775. normalizedFoveatedFactor
  776. );
  777. const sse =
  778. tile._screenSpaceError === 0.0 && defined(tile.parent)
  779. ? tile.parent._screenSpaceError * 0.5
  780. : tile._screenSpaceError;
  781. return tileset.maximumScreenSpaceError - sseRelaxation <= sse;
  782. }
  783. const scratchJulianDate = new JulianDate();
  784. /**
  785. * Get the tile's screen space error.
  786. *
  787. * @private
  788. */
  789. Cesium3DTile.prototype.getScreenSpaceError = function (
  790. frameState,
  791. useParentGeometricError,
  792. progressiveResolutionHeightFraction
  793. ) {
  794. const tileset = this._tileset;
  795. const heightFraction = defaultValue(progressiveResolutionHeightFraction, 1.0);
  796. const parentGeometricError = defined(this.parent)
  797. ? this.parent.geometricError
  798. : tileset._geometricError;
  799. const geometricError = useParentGeometricError
  800. ? parentGeometricError
  801. : this.geometricError;
  802. if (geometricError === 0.0) {
  803. // Leaf tiles do not have any error so save the computation
  804. return 0.0;
  805. }
  806. const camera = frameState.camera;
  807. let frustum = camera.frustum;
  808. const context = frameState.context;
  809. const width = context.drawingBufferWidth;
  810. const height = context.drawingBufferHeight * heightFraction;
  811. let error;
  812. if (
  813. frameState.mode === SceneMode.SCENE2D ||
  814. frustum instanceof OrthographicFrustum
  815. ) {
  816. if (defined(frustum._offCenterFrustum)) {
  817. frustum = frustum._offCenterFrustum;
  818. }
  819. const pixelSize =
  820. Math.max(frustum.top - frustum.bottom, frustum.right - frustum.left) /
  821. Math.max(width, height);
  822. error = geometricError / pixelSize;
  823. } else {
  824. // Avoid divide by zero when viewer is inside the tile
  825. const distance = Math.max(this._distanceToCamera, CesiumMath.EPSILON7);
  826. const sseDenominator = camera.frustum.sseDenominator;
  827. error = (geometricError * height) / (distance * sseDenominator);
  828. if (tileset.dynamicScreenSpaceError) {
  829. const density = tileset._dynamicScreenSpaceErrorComputedDensity;
  830. const factor = tileset.dynamicScreenSpaceErrorFactor;
  831. const dynamicError = CesiumMath.fog(distance, density) * factor;
  832. error -= dynamicError;
  833. }
  834. }
  835. error /= frameState.pixelRatio;
  836. return error;
  837. };
  838. function isPriorityProgressiveResolution(tileset, tile) {
  839. if (
  840. tileset.progressiveResolutionHeightFraction <= 0.0 ||
  841. tileset.progressiveResolutionHeightFraction > 0.5
  842. ) {
  843. return false;
  844. }
  845. let isProgressiveResolutionTile =
  846. tile._screenSpaceErrorProgressiveResolution >
  847. tileset._maximumScreenSpaceError; // Mark non-SSE leaves
  848. tile._priorityProgressiveResolutionScreenSpaceErrorLeaf = false; // Needed for skipLOD
  849. const parent = tile.parent;
  850. const maximumScreenSpaceError = tileset._maximumScreenSpaceError;
  851. const tilePasses =
  852. tile._screenSpaceErrorProgressiveResolution <= maximumScreenSpaceError;
  853. const parentFails =
  854. defined(parent) &&
  855. parent._screenSpaceErrorProgressiveResolution > maximumScreenSpaceError;
  856. if (tilePasses && parentFails) {
  857. // A progressive resolution SSE leaf, promote its priority as well
  858. tile._priorityProgressiveResolutionScreenSpaceErrorLeaf = true;
  859. isProgressiveResolutionTile = true;
  860. }
  861. return isProgressiveResolutionTile;
  862. }
  863. function getPriorityReverseScreenSpaceError(tileset, tile) {
  864. const parent = tile.parent;
  865. const useParentScreenSpaceError =
  866. defined(parent) &&
  867. (!tileset._skipLevelOfDetail ||
  868. tile._screenSpaceError === 0.0 ||
  869. parent.hasTilesetContent ||
  870. parent.hasImplicitContent);
  871. const screenSpaceError = useParentScreenSpaceError
  872. ? parent._screenSpaceError
  873. : tile._screenSpaceError;
  874. return tileset.root._screenSpaceError - screenSpaceError;
  875. }
  876. /**
  877. * Update the tile's visibility.
  878. *
  879. * @private
  880. */
  881. Cesium3DTile.prototype.updateVisibility = function (frameState) {
  882. const parent = this.parent;
  883. const tileset = this._tileset;
  884. const parentTransform = defined(parent)
  885. ? parent.computedTransform
  886. : tileset.modelMatrix;
  887. const parentVisibilityPlaneMask = defined(parent)
  888. ? parent._visibilityPlaneMask
  889. : CullingVolume.MASK_INDETERMINATE;
  890. this.updateTransform(parentTransform);
  891. this._distanceToCamera = this.distanceToTile(frameState);
  892. this._centerZDepth = this.distanceToTileCenter(frameState);
  893. this._screenSpaceError = this.getScreenSpaceError(frameState, false);
  894. this._screenSpaceErrorProgressiveResolution = this.getScreenSpaceError(
  895. frameState,
  896. false,
  897. tileset.progressiveResolutionHeightFraction
  898. );
  899. this._visibilityPlaneMask = this.visibility(
  900. frameState,
  901. parentVisibilityPlaneMask
  902. ); // Use parent's plane mask to speed up visibility test
  903. this._visible = this._visibilityPlaneMask !== CullingVolume.MASK_OUTSIDE;
  904. this._inRequestVolume = this.insideViewerRequestVolume(frameState);
  905. this._priorityReverseScreenSpaceError = getPriorityReverseScreenSpaceError(
  906. tileset,
  907. this
  908. );
  909. this._priorityProgressiveResolution = isPriorityProgressiveResolution(
  910. tileset,
  911. this
  912. );
  913. this.priorityDeferred = isPriorityDeferred(this, frameState);
  914. };
  915. /**
  916. * Update whether the tile has expired.
  917. *
  918. * @private
  919. */
  920. Cesium3DTile.prototype.updateExpiration = function () {
  921. if (
  922. defined(this.expireDate) &&
  923. this.contentReady &&
  924. !this.hasEmptyContent &&
  925. !this.hasMultipleContents
  926. ) {
  927. const now = JulianDate.now(scratchJulianDate);
  928. if (JulianDate.lessThan(this.expireDate, now)) {
  929. this._contentState = Cesium3DTileContentState.EXPIRED;
  930. this._expiredContent = this._content;
  931. }
  932. }
  933. };
  934. function updateExpireDate(tile) {
  935. if (defined(tile.expireDuration)) {
  936. const expireDurationDate = JulianDate.now(scratchJulianDate);
  937. JulianDate.addSeconds(
  938. expireDurationDate,
  939. tile.expireDuration,
  940. expireDurationDate
  941. );
  942. if (defined(tile.expireDate)) {
  943. if (JulianDate.lessThan(tile.expireDate, expireDurationDate)) {
  944. JulianDate.clone(expireDurationDate, tile.expireDate);
  945. }
  946. } else {
  947. tile.expireDate = JulianDate.clone(expireDurationDate);
  948. }
  949. }
  950. }
  951. function createPriorityFunction(tile) {
  952. return function () {
  953. return tile._priority;
  954. };
  955. }
  956. /**
  957. * Requests the tile's content.
  958. * <p>
  959. * The request may not be made if the Cesium Request Scheduler can't prioritize it.
  960. * </p>
  961. *
  962. * @return {Number} The number of requests that were attempted but not scheduled.
  963. * @private
  964. */
  965. Cesium3DTile.prototype.requestContent = function () {
  966. // empty contents don't require any HTTP requests
  967. if (this.hasEmptyContent) {
  968. return 0;
  969. }
  970. if (this.hasMultipleContents) {
  971. return requestMultipleContents(this);
  972. }
  973. return requestSingleContent(this);
  974. };
  975. /**
  976. * Multiple {@link Cesium3DTileContent}s are allowed within a single tile either through
  977. * the tile JSON (3D Tiles 1.1) or the <code>3DTILES_multiple_contents</code> extension.
  978. * Due to differences in request scheduling, this is handled separately.
  979. * <p>
  980. * This implementation of multiple contents does not
  981. * support tile expiry like requestSingleContent does. If this changes,
  982. * note that the resource.setQueryParameters() details must go inside {@link Multiple3DTileContent} since that is per-request.
  983. * </p>
  984. *
  985. * @private
  986. */
  987. function requestMultipleContents(tile) {
  988. let multipleContents = tile._content;
  989. const tileset = tile._tileset;
  990. if (!defined(multipleContents)) {
  991. // Create the content object immediately, it will handle scheduling
  992. // requests for inner contents.
  993. const contentsJson = hasExtension(tile._header, "3DTILES_multiple_contents")
  994. ? tile._header.extensions["3DTILES_multiple_contents"]
  995. : tile._header;
  996. multipleContents = new Multiple3DTileContent(
  997. tileset,
  998. tile,
  999. tile._contentResource.clone(),
  1000. contentsJson
  1001. );
  1002. tile._content = multipleContents;
  1003. }
  1004. const backloggedRequestCount = multipleContents.requestInnerContents();
  1005. if (backloggedRequestCount > 0) {
  1006. return backloggedRequestCount;
  1007. }
  1008. tile._contentState = Cesium3DTileContentState.LOADING;
  1009. tile._contentReadyToProcessPromise = defer();
  1010. tile._contentReadyPromise = defer();
  1011. multipleContents.contentsFetchedPromise
  1012. .then(function () {
  1013. if (tile._contentState !== Cesium3DTileContentState.LOADING) {
  1014. // tile was canceled, short circuit.
  1015. return;
  1016. }
  1017. if (tile.isDestroyed()) {
  1018. multipleContentFailed(
  1019. tile,
  1020. tileset,
  1021. "Tile was unloaded while content was loading"
  1022. );
  1023. return;
  1024. }
  1025. tile._contentState = Cesium3DTileContentState.PROCESSING;
  1026. tile._contentReadyToProcessPromise.resolve(multipleContents);
  1027. return multipleContents.readyPromise.then(function (content) {
  1028. if (tile.isDestroyed()) {
  1029. multipleContentFailed(
  1030. tile,
  1031. tileset,
  1032. "Tile was unloaded while content was processing"
  1033. );
  1034. return;
  1035. }
  1036. // Refresh style for expired content
  1037. tile._selectedFrame = 0;
  1038. tile.lastStyleTime = 0.0;
  1039. JulianDate.now(tile._loadTimestamp);
  1040. tile._contentState = Cesium3DTileContentState.READY;
  1041. tile._contentReadyPromise.resolve(content);
  1042. });
  1043. })
  1044. .catch(function (error) {
  1045. multipleContentFailed(tile, tileset, error);
  1046. });
  1047. return 0;
  1048. }
  1049. function multipleContentFailed(tile, tileset, error) {
  1050. // note: The Multiple3DTileContent handles decrementing the number of pending
  1051. // requests if the state is LOADING.
  1052. if (tile._contentState === Cesium3DTileContentState.PROCESSING) {
  1053. --tileset.statistics.numberOfTilesProcessing;
  1054. }
  1055. tile._contentState = Cesium3DTileContentState.FAILED;
  1056. tile._contentReadyPromise.reject(error);
  1057. tile._contentReadyToProcessPromise.reject(error);
  1058. }
  1059. function requestSingleContent(tile) {
  1060. // it is important to clone here. The fetchArrayBuffer() below uses
  1061. // throttling, but other uses of the resources do not.
  1062. const resource = tile._contentResource.clone();
  1063. const expired = tile.contentExpired;
  1064. if (expired) {
  1065. // Append a query parameter of the tile expiration date to prevent caching
  1066. resource.setQueryParameters({
  1067. expired: tile.expireDate.toString(),
  1068. });
  1069. }
  1070. const request = new Request({
  1071. throttle: true,
  1072. throttleByServer: true,
  1073. type: RequestType.TILES3D,
  1074. priorityFunction: createPriorityFunction(tile),
  1075. serverKey: tile._serverKey,
  1076. });
  1077. tile._request = request;
  1078. resource.request = request;
  1079. const promise = resource.fetchArrayBuffer();
  1080. if (!defined(promise)) {
  1081. return 1;
  1082. }
  1083. const previousState = tile._contentState;
  1084. const tileset = tile._tileset;
  1085. tile._contentState = Cesium3DTileContentState.LOADING;
  1086. tile._contentReadyToProcessPromise = defer();
  1087. tile._contentReadyPromise = defer();
  1088. ++tileset.statistics.numberOfPendingRequests;
  1089. promise
  1090. .then(function (arrayBuffer) {
  1091. if (tile.isDestroyed()) {
  1092. // Tile is unloaded before the content finishes loading
  1093. singleContentFailed(tile, tileset);
  1094. return;
  1095. }
  1096. const content = makeContent(tile, arrayBuffer);
  1097. if (expired) {
  1098. tile.expireDate = undefined;
  1099. }
  1100. tile._content = content;
  1101. tile._contentState = Cesium3DTileContentState.PROCESSING;
  1102. tile._contentReadyToProcessPromise.resolve(content);
  1103. --tileset.statistics.numberOfPendingRequests;
  1104. return content.readyPromise.then(function (content) {
  1105. if (tile.isDestroyed()) {
  1106. // Tile is unloaded before the content finishes processing
  1107. singleContentFailed(tile, tileset);
  1108. return;
  1109. }
  1110. updateExpireDate(tile);
  1111. // Refresh style for expired content
  1112. tile._selectedFrame = 0;
  1113. tile.lastStyleTime = 0.0;
  1114. JulianDate.now(tile._loadTimestamp);
  1115. tile._contentState = Cesium3DTileContentState.READY;
  1116. tile._contentReadyPromise.resolve(content);
  1117. });
  1118. })
  1119. .catch(function (error) {
  1120. if (request.state === RequestState.CANCELLED) {
  1121. // Cancelled due to low priority - try again later.
  1122. tile._contentState = previousState;
  1123. --tileset.statistics.numberOfPendingRequests;
  1124. ++tileset.statistics.numberOfAttemptedRequests;
  1125. return;
  1126. }
  1127. singleContentFailed(tile, tileset, error);
  1128. });
  1129. return 0;
  1130. }
  1131. function singleContentFailed(tile, tileset, error) {
  1132. if (tile._contentState === Cesium3DTileContentState.PROCESSING) {
  1133. --tileset.statistics.numberOfTilesProcessing;
  1134. } else {
  1135. --tileset.statistics.numberOfPendingRequests;
  1136. }
  1137. tile._contentState = Cesium3DTileContentState.FAILED;
  1138. tile._contentReadyPromise.reject(error);
  1139. tile._contentReadyToProcessPromise.reject(error);
  1140. }
  1141. /**
  1142. * Given a downloaded content payload, construct a {@link Cesium3DTileContent}.
  1143. * <p>
  1144. * This is only used for single contents.
  1145. * </p>
  1146. *
  1147. * @param {Cesium3DTile} tile The tile
  1148. * @param {ArrayBuffer} arrayBuffer The downloaded payload containing data for the content
  1149. * @return {Cesium3DTileContent} A content object
  1150. * @private
  1151. */
  1152. function makeContent(tile, arrayBuffer) {
  1153. const preprocessed = preprocess3DTileContent(arrayBuffer);
  1154. // Vector and Geometry tile rendering do not support the skip LOD optimization.
  1155. const tileset = tile._tileset;
  1156. tileset._disableSkipLevelOfDetail =
  1157. tileset._disableSkipLevelOfDetail ||
  1158. preprocessed.contentType === Cesium3DTileContentType.GEOMETRY ||
  1159. preprocessed.contentType === Cesium3DTileContentType.VECTOR;
  1160. if (
  1161. preprocessed.contentType === Cesium3DTileContentType.IMPLICIT_SUBTREE ||
  1162. preprocessed.contentType === Cesium3DTileContentType.IMPLICIT_SUBTREE_JSON
  1163. ) {
  1164. tile.hasImplicitContent = true;
  1165. }
  1166. if (preprocessed.contentType === Cesium3DTileContentType.EXTERNAL_TILESET) {
  1167. tile.hasTilesetContent = true;
  1168. }
  1169. let content;
  1170. const contentFactory = Cesium3DTileContentFactory[preprocessed.contentType];
  1171. if (defined(preprocessed.binaryPayload)) {
  1172. content = contentFactory(
  1173. tileset,
  1174. tile,
  1175. tile._contentResource,
  1176. preprocessed.binaryPayload.buffer,
  1177. 0
  1178. );
  1179. } else {
  1180. // JSON formats
  1181. content = contentFactory(
  1182. tileset,
  1183. tile,
  1184. tile._contentResource,
  1185. preprocessed.jsonPayload
  1186. );
  1187. }
  1188. const contentHeader = defined(tile._header.contents)
  1189. ? tile._header.contents[0]
  1190. : tile._header.content;
  1191. if (tile.hasImplicitContentMetadata) {
  1192. const subtree = tile.implicitSubtree;
  1193. const coordinates = tile.implicitCoordinates;
  1194. content.metadata = subtree.getContentMetadataView(coordinates, 0);
  1195. } else if (!tile.hasImplicitContent) {
  1196. content.metadata = findContentMetadata(tileset, contentHeader);
  1197. }
  1198. const groupMetadata = findGroupMetadata(tileset, contentHeader);
  1199. if (defined(groupMetadata)) {
  1200. content.group = new Cesium3DContentGroup({
  1201. metadata: groupMetadata,
  1202. });
  1203. }
  1204. return content;
  1205. }
  1206. /**
  1207. * Cancel requests for the tile's contents. This is called when the tile
  1208. * goes out of view.
  1209. *
  1210. * @private
  1211. */
  1212. Cesium3DTile.prototype.cancelRequests = function () {
  1213. if (this.hasMultipleContents) {
  1214. this._content.cancelRequests();
  1215. } else {
  1216. this._request.cancel();
  1217. }
  1218. };
  1219. /**
  1220. * Unloads the tile's content.
  1221. *
  1222. * @private
  1223. */
  1224. Cesium3DTile.prototype.unloadContent = function () {
  1225. if (
  1226. this.hasEmptyContent ||
  1227. this.hasTilesetContent ||
  1228. this.hasImplicitContent
  1229. ) {
  1230. return;
  1231. }
  1232. this._content = this._content && this._content.destroy();
  1233. this._contentState = Cesium3DTileContentState.UNLOADED;
  1234. this._contentReadyToProcessPromise = undefined;
  1235. this._contentReadyPromise = undefined;
  1236. this.lastStyleTime = 0.0;
  1237. this.clippingPlanesDirty = this._clippingPlanesState === 0;
  1238. this._clippingPlanesState = 0;
  1239. this._debugColorizeTiles = false;
  1240. this._debugBoundingVolume =
  1241. this._debugBoundingVolume && this._debugBoundingVolume.destroy();
  1242. this._debugContentBoundingVolume =
  1243. this._debugContentBoundingVolume &&
  1244. this._debugContentBoundingVolume.destroy();
  1245. this._debugViewerRequestVolume =
  1246. this._debugViewerRequestVolume && this._debugViewerRequestVolume.destroy();
  1247. };
  1248. const scratchProjectedBoundingSphere = new BoundingSphere();
  1249. function getBoundingVolume(tile, frameState) {
  1250. if (
  1251. frameState.mode !== SceneMode.SCENE3D &&
  1252. !defined(tile._boundingVolume2D)
  1253. ) {
  1254. const boundingSphere = tile._boundingVolume.boundingSphere;
  1255. const sphere = BoundingSphere.projectTo2D(
  1256. boundingSphere,
  1257. frameState.mapProjection,
  1258. scratchProjectedBoundingSphere
  1259. );
  1260. tile._boundingVolume2D = new TileBoundingSphere(
  1261. sphere.center,
  1262. sphere.radius
  1263. );
  1264. }
  1265. return frameState.mode !== SceneMode.SCENE3D
  1266. ? tile._boundingVolume2D
  1267. : tile._boundingVolume;
  1268. }
  1269. function getContentBoundingVolume(tile, frameState) {
  1270. if (
  1271. frameState.mode !== SceneMode.SCENE3D &&
  1272. !defined(tile._contentBoundingVolume2D)
  1273. ) {
  1274. const boundingSphere = tile._contentBoundingVolume.boundingSphere;
  1275. const sphere = BoundingSphere.projectTo2D(
  1276. boundingSphere,
  1277. frameState.mapProjection,
  1278. scratchProjectedBoundingSphere
  1279. );
  1280. tile._contentBoundingVolume2D = new TileBoundingSphere(
  1281. sphere.center,
  1282. sphere.radius
  1283. );
  1284. }
  1285. return frameState.mode !== SceneMode.SCENE3D
  1286. ? tile._contentBoundingVolume2D
  1287. : tile._contentBoundingVolume;
  1288. }
  1289. /**
  1290. * Determines whether the tile's bounding volume intersects the culling volume.
  1291. *
  1292. * @param {FrameState} frameState The frame state.
  1293. * @param {Number} parentVisibilityPlaneMask The parent's plane mask to speed up the visibility check.
  1294. * @returns {Number} A plane mask as described above in {@link CullingVolume#computeVisibilityWithPlaneMask}.
  1295. *
  1296. * @private
  1297. */
  1298. Cesium3DTile.prototype.visibility = function (
  1299. frameState,
  1300. parentVisibilityPlaneMask
  1301. ) {
  1302. const cullingVolume = frameState.cullingVolume;
  1303. const boundingVolume = getBoundingVolume(this, frameState);
  1304. const tileset = this._tileset;
  1305. const clippingPlanes = tileset.clippingPlanes;
  1306. if (defined(clippingPlanes) && clippingPlanes.enabled) {
  1307. const intersection = clippingPlanes.computeIntersectionWithBoundingVolume(
  1308. boundingVolume,
  1309. tileset.clippingPlanesOriginMatrix
  1310. );
  1311. this._isClipped = intersection !== Intersect.INSIDE;
  1312. if (intersection === Intersect.OUTSIDE) {
  1313. return CullingVolume.MASK_OUTSIDE;
  1314. }
  1315. }
  1316. return cullingVolume.computeVisibilityWithPlaneMask(
  1317. boundingVolume,
  1318. parentVisibilityPlaneMask
  1319. );
  1320. };
  1321. /**
  1322. * Assuming the tile's bounding volume intersects the culling volume, determines
  1323. * whether the tile's content's bounding volume intersects the culling volume.
  1324. *
  1325. * @param {FrameState} frameState The frame state.
  1326. * @returns {Intersect} The result of the intersection: the tile's content is completely outside, completely inside, or intersecting the culling volume.
  1327. *
  1328. * @private
  1329. */
  1330. Cesium3DTile.prototype.contentVisibility = function (frameState) {
  1331. // Assumes the tile's bounding volume intersects the culling volume already, so
  1332. // just return Intersect.INSIDE if there is no content bounding volume.
  1333. if (!defined(this._contentBoundingVolume)) {
  1334. return Intersect.INSIDE;
  1335. }
  1336. if (this._visibilityPlaneMask === CullingVolume.MASK_INSIDE) {
  1337. // The tile's bounding volume is completely inside the culling volume so
  1338. // the content bounding volume must also be inside.
  1339. return Intersect.INSIDE;
  1340. }
  1341. // PERFORMANCE_IDEA: is it possible to burn less CPU on this test since we know the
  1342. // tile's (not the content's) bounding volume intersects the culling volume?
  1343. const cullingVolume = frameState.cullingVolume;
  1344. const boundingVolume = getContentBoundingVolume(this, frameState);
  1345. const tileset = this._tileset;
  1346. const clippingPlanes = tileset.clippingPlanes;
  1347. if (defined(clippingPlanes) && clippingPlanes.enabled) {
  1348. const intersection = clippingPlanes.computeIntersectionWithBoundingVolume(
  1349. boundingVolume,
  1350. tileset.clippingPlanesOriginMatrix
  1351. );
  1352. this._isClipped = intersection !== Intersect.INSIDE;
  1353. if (intersection === Intersect.OUTSIDE) {
  1354. return Intersect.OUTSIDE;
  1355. }
  1356. }
  1357. return cullingVolume.computeVisibility(boundingVolume);
  1358. };
  1359. /**
  1360. * Computes the (potentially approximate) distance from the closest point of the tile's bounding volume to the camera.
  1361. *
  1362. * @param {FrameState} frameState The frame state.
  1363. * @returns {Number} The distance, in meters, or zero if the camera is inside the bounding volume.
  1364. *
  1365. * @private
  1366. */
  1367. Cesium3DTile.prototype.distanceToTile = function (frameState) {
  1368. const boundingVolume = getBoundingVolume(this, frameState);
  1369. return boundingVolume.distanceToCamera(frameState);
  1370. };
  1371. const scratchToTileCenter = new Cartesian3();
  1372. /**
  1373. * Computes the distance from the center of the tile's bounding volume to the camera's plane defined by its position and view direction.
  1374. *
  1375. * @param {FrameState} frameState The frame state.
  1376. * @returns {Number} The distance, in meters.
  1377. *
  1378. * @private
  1379. */
  1380. Cesium3DTile.prototype.distanceToTileCenter = function (frameState) {
  1381. const tileBoundingVolume = getBoundingVolume(this, frameState);
  1382. const boundingVolume = tileBoundingVolume.boundingVolume; // Gets the underlying OrientedBoundingBox or BoundingSphere
  1383. const toCenter = Cartesian3.subtract(
  1384. boundingVolume.center,
  1385. frameState.camera.positionWC,
  1386. scratchToTileCenter
  1387. );
  1388. return Cartesian3.dot(frameState.camera.directionWC, toCenter);
  1389. };
  1390. /**
  1391. * Checks if the camera is inside the viewer request volume.
  1392. *
  1393. * @param {FrameState} frameState The frame state.
  1394. * @returns {Boolean} Whether the camera is inside the volume.
  1395. *
  1396. * @private
  1397. */
  1398. Cesium3DTile.prototype.insideViewerRequestVolume = function (frameState) {
  1399. const viewerRequestVolume = this._viewerRequestVolume;
  1400. return (
  1401. !defined(viewerRequestVolume) ||
  1402. viewerRequestVolume.distanceToCamera(frameState) === 0.0
  1403. );
  1404. };
  1405. const scratchMatrix = new Matrix3();
  1406. const scratchScale = new Cartesian3();
  1407. const scratchHalfAxes = new Matrix3();
  1408. const scratchCenter = new Cartesian3();
  1409. const scratchRectangle = new Rectangle();
  1410. const scratchOrientedBoundingBox = new OrientedBoundingBox();
  1411. const scratchTransform = new Matrix4();
  1412. function createBox(box, transform, result) {
  1413. let center = Cartesian3.fromElements(box[0], box[1], box[2], scratchCenter);
  1414. let halfAxes = Matrix3.fromArray(box, 3, scratchHalfAxes);
  1415. // Find the transformed center and halfAxes
  1416. center = Matrix4.multiplyByPoint(transform, center, center);
  1417. const rotationScale = Matrix4.getMatrix3(transform, scratchMatrix);
  1418. halfAxes = Matrix3.multiply(rotationScale, halfAxes, halfAxes);
  1419. if (defined(result)) {
  1420. result.update(center, halfAxes);
  1421. return result;
  1422. }
  1423. return new TileOrientedBoundingBox(center, halfAxes);
  1424. }
  1425. function createBoxFromTransformedRegion(
  1426. region,
  1427. transform,
  1428. initialTransform,
  1429. result
  1430. ) {
  1431. const rectangle = Rectangle.unpack(region, 0, scratchRectangle);
  1432. const minimumHeight = region[4];
  1433. const maximumHeight = region[5];
  1434. const orientedBoundingBox = OrientedBoundingBox.fromRectangle(
  1435. rectangle,
  1436. minimumHeight,
  1437. maximumHeight,
  1438. Ellipsoid.WGS84,
  1439. scratchOrientedBoundingBox
  1440. );
  1441. let center = orientedBoundingBox.center;
  1442. let halfAxes = orientedBoundingBox.halfAxes;
  1443. // A region bounding volume is not transformed by the transform in the tileset JSON,
  1444. // but may be transformed by additional transforms applied in Cesium.
  1445. // This is why the transform is calculated as the difference between the initial transform and the current transform.
  1446. transform = Matrix4.multiplyTransformation(
  1447. transform,
  1448. Matrix4.inverseTransformation(initialTransform, scratchTransform),
  1449. scratchTransform
  1450. );
  1451. center = Matrix4.multiplyByPoint(transform, center, center);
  1452. const rotationScale = Matrix4.getMatrix3(transform, scratchMatrix);
  1453. halfAxes = Matrix3.multiply(rotationScale, halfAxes, halfAxes);
  1454. if (defined(result) && result instanceof TileOrientedBoundingBox) {
  1455. result.update(center, halfAxes);
  1456. return result;
  1457. }
  1458. return new TileOrientedBoundingBox(center, halfAxes);
  1459. }
  1460. function createRegion(region, transform, initialTransform, result) {
  1461. if (
  1462. !Matrix4.equalsEpsilon(transform, initialTransform, CesiumMath.EPSILON8)
  1463. ) {
  1464. return createBoxFromTransformedRegion(
  1465. region,
  1466. transform,
  1467. initialTransform,
  1468. result
  1469. );
  1470. }
  1471. if (defined(result)) {
  1472. return result;
  1473. }
  1474. const rectangleRegion = Rectangle.unpack(region, 0, scratchRectangle);
  1475. return new TileBoundingRegion({
  1476. rectangle: rectangleRegion,
  1477. minimumHeight: region[4],
  1478. maximumHeight: region[5],
  1479. });
  1480. }
  1481. function createSphere(sphere, transform, result) {
  1482. let center = Cartesian3.fromElements(
  1483. sphere[0],
  1484. sphere[1],
  1485. sphere[2],
  1486. scratchCenter
  1487. );
  1488. let radius = sphere[3];
  1489. // Find the transformed center and radius
  1490. center = Matrix4.multiplyByPoint(transform, center, center);
  1491. const scale = Matrix4.getScale(transform, scratchScale);
  1492. const uniformScale = Cartesian3.maximumComponent(scale);
  1493. radius *= uniformScale;
  1494. if (defined(result)) {
  1495. result.update(center, radius);
  1496. return result;
  1497. }
  1498. return new TileBoundingSphere(center, radius);
  1499. }
  1500. /**
  1501. * Create a bounding volume from the tile's bounding volume header.
  1502. *
  1503. * @param {Object} boundingVolumeHeader The tile's bounding volume header.
  1504. * @param {Matrix4} transform The transform to apply to the bounding volume.
  1505. * @param {TileBoundingVolume} [result] The object onto which to store the result.
  1506. *
  1507. * @returns {TileBoundingVolume} The modified result parameter or a new TileBoundingVolume instance if none was provided.
  1508. *
  1509. * @private
  1510. */
  1511. Cesium3DTile.prototype.createBoundingVolume = function (
  1512. boundingVolumeHeader,
  1513. transform,
  1514. result
  1515. ) {
  1516. if (!defined(boundingVolumeHeader)) {
  1517. throw new RuntimeError("boundingVolume must be defined");
  1518. }
  1519. if (hasExtension(boundingVolumeHeader, "3DTILES_bounding_volume_S2")) {
  1520. return new TileBoundingS2Cell(
  1521. boundingVolumeHeader.extensions["3DTILES_bounding_volume_S2"]
  1522. );
  1523. }
  1524. if (defined(boundingVolumeHeader.box)) {
  1525. return createBox(boundingVolumeHeader.box, transform, result);
  1526. }
  1527. if (defined(boundingVolumeHeader.region)) {
  1528. return createRegion(
  1529. boundingVolumeHeader.region,
  1530. transform,
  1531. this._initialTransform,
  1532. result
  1533. );
  1534. }
  1535. if (defined(boundingVolumeHeader.sphere)) {
  1536. return createSphere(boundingVolumeHeader.sphere, transform, result);
  1537. }
  1538. throw new RuntimeError(
  1539. "boundingVolume must contain a sphere, region, or box"
  1540. );
  1541. };
  1542. /**
  1543. * Update the tile's transform. The transform is applied to the tile's bounding volumes.
  1544. *
  1545. * @private
  1546. */
  1547. Cesium3DTile.prototype.updateTransform = function (parentTransform) {
  1548. parentTransform = defaultValue(parentTransform, Matrix4.IDENTITY);
  1549. const computedTransform = Matrix4.multiply(
  1550. parentTransform,
  1551. this.transform,
  1552. scratchTransform
  1553. );
  1554. const transformChanged = !Matrix4.equals(
  1555. computedTransform,
  1556. this.computedTransform
  1557. );
  1558. if (!transformChanged) {
  1559. return;
  1560. }
  1561. Matrix4.clone(computedTransform, this.computedTransform);
  1562. // Update the bounding volumes
  1563. const header = this._header;
  1564. const content = this._header.content;
  1565. this._boundingVolume = this.createBoundingVolume(
  1566. header.boundingVolume,
  1567. this.computedTransform,
  1568. this._boundingVolume
  1569. );
  1570. if (defined(this._contentBoundingVolume)) {
  1571. this._contentBoundingVolume = this.createBoundingVolume(
  1572. content.boundingVolume,
  1573. this.computedTransform,
  1574. this._contentBoundingVolume
  1575. );
  1576. }
  1577. if (defined(this._viewerRequestVolume)) {
  1578. this._viewerRequestVolume = this.createBoundingVolume(
  1579. header.viewerRequestVolume,
  1580. this.computedTransform,
  1581. this._viewerRequestVolume
  1582. );
  1583. }
  1584. this.updateGeometricErrorScale();
  1585. // Destroy the debug bounding volumes. They will be generated fresh.
  1586. this._debugBoundingVolume =
  1587. this._debugBoundingVolume && this._debugBoundingVolume.destroy();
  1588. this._debugContentBoundingVolume =
  1589. this._debugContentBoundingVolume &&
  1590. this._debugContentBoundingVolume.destroy();
  1591. this._debugViewerRequestVolume =
  1592. this._debugViewerRequestVolume && this._debugViewerRequestVolume.destroy();
  1593. };
  1594. Cesium3DTile.prototype.updateGeometricErrorScale = function () {
  1595. const scale = Matrix4.getScale(this.computedTransform, scratchScale);
  1596. const uniformScale = Cartesian3.maximumComponent(scale);
  1597. this.geometricError = this._geometricError * uniformScale;
  1598. };
  1599. function applyDebugSettings(tile, tileset, frameState, passOptions) {
  1600. if (!passOptions.isRender) {
  1601. return;
  1602. }
  1603. const hasContentBoundingVolume =
  1604. defined(tile._header.content) &&
  1605. defined(tile._header.content.boundingVolume);
  1606. const empty =
  1607. tile.hasEmptyContent || tile.hasTilesetContent || tile.hasImplicitContent;
  1608. const showVolume =
  1609. tileset.debugShowBoundingVolume ||
  1610. (tileset.debugShowContentBoundingVolume && !hasContentBoundingVolume);
  1611. if (showVolume) {
  1612. let color;
  1613. if (!tile._finalResolution) {
  1614. color = Color.YELLOW;
  1615. } else if (empty) {
  1616. color = Color.DARKGRAY;
  1617. } else {
  1618. color = Color.WHITE;
  1619. }
  1620. if (!defined(tile._debugBoundingVolume)) {
  1621. tile._debugBoundingVolume = tile._boundingVolume.createDebugVolume(color);
  1622. }
  1623. tile._debugBoundingVolume.update(frameState);
  1624. const attributes = tile._debugBoundingVolume.getGeometryInstanceAttributes(
  1625. "outline"
  1626. );
  1627. attributes.color = ColorGeometryInstanceAttribute.toValue(
  1628. color,
  1629. attributes.color
  1630. );
  1631. } else if (!showVolume && defined(tile._debugBoundingVolume)) {
  1632. tile._debugBoundingVolume = tile._debugBoundingVolume.destroy();
  1633. }
  1634. if (tileset.debugShowContentBoundingVolume && hasContentBoundingVolume) {
  1635. if (!defined(tile._debugContentBoundingVolume)) {
  1636. tile._debugContentBoundingVolume = tile._contentBoundingVolume.createDebugVolume(
  1637. Color.BLUE
  1638. );
  1639. }
  1640. tile._debugContentBoundingVolume.update(frameState);
  1641. } else if (
  1642. !tileset.debugShowContentBoundingVolume &&
  1643. defined(tile._debugContentBoundingVolume)
  1644. ) {
  1645. tile._debugContentBoundingVolume = tile._debugContentBoundingVolume.destroy();
  1646. }
  1647. if (
  1648. tileset.debugShowViewerRequestVolume &&
  1649. defined(tile._viewerRequestVolume)
  1650. ) {
  1651. if (!defined(tile._debugViewerRequestVolume)) {
  1652. tile._debugViewerRequestVolume = tile._viewerRequestVolume.createDebugVolume(
  1653. Color.YELLOW
  1654. );
  1655. }
  1656. tile._debugViewerRequestVolume.update(frameState);
  1657. } else if (
  1658. !tileset.debugShowViewerRequestVolume &&
  1659. defined(tile._debugViewerRequestVolume)
  1660. ) {
  1661. tile._debugViewerRequestVolume = tile._debugViewerRequestVolume.destroy();
  1662. }
  1663. const debugColorizeTilesOn =
  1664. (tileset.debugColorizeTiles && !tile._debugColorizeTiles) ||
  1665. defined(tileset._heatmap.tilePropertyName);
  1666. const debugColorizeTilesOff =
  1667. !tileset.debugColorizeTiles && tile._debugColorizeTiles;
  1668. if (debugColorizeTilesOn) {
  1669. tileset._heatmap.colorize(tile, frameState); // Skipped if tileset._heatmap.tilePropertyName is undefined
  1670. tile._debugColorizeTiles = true;
  1671. tile.color = tile._debugColor;
  1672. } else if (debugColorizeTilesOff) {
  1673. tile._debugColorizeTiles = false;
  1674. tile.color = Color.WHITE;
  1675. }
  1676. if (tile._colorDirty) {
  1677. tile._colorDirty = false;
  1678. tile._content.applyDebugSettings(true, tile._color);
  1679. }
  1680. if (debugColorizeTilesOff) {
  1681. tileset.makeStyleDirty(); // Re-apply style now that colorize is switched off
  1682. }
  1683. }
  1684. function updateContent(tile, tileset, frameState) {
  1685. const content = tile._content;
  1686. const expiredContent = tile._expiredContent;
  1687. // expired content is not supported for multiple contents
  1688. if (!tile.hasMultipleContents && defined(expiredContent)) {
  1689. if (!tile.contentReady) {
  1690. // Render the expired content while the content loads
  1691. expiredContent.update(tileset, frameState);
  1692. return;
  1693. }
  1694. // New content is ready, destroy expired content
  1695. tile._expiredContent.destroy();
  1696. tile._expiredContent = undefined;
  1697. }
  1698. content.update(tileset, frameState);
  1699. }
  1700. function updateClippingPlanes(tile, tileset) {
  1701. // Compute and compare ClippingPlanes state:
  1702. // - enabled-ness - are clipping planes enabled? is this tile clipped?
  1703. // - clipping plane count
  1704. // - clipping function (union v. intersection)
  1705. const clippingPlanes = tileset.clippingPlanes;
  1706. let currentClippingPlanesState = 0;
  1707. if (defined(clippingPlanes) && tile._isClipped && clippingPlanes.enabled) {
  1708. currentClippingPlanesState = clippingPlanes.clippingPlanesState;
  1709. }
  1710. // If clippingPlaneState for tile changed, mark clippingPlanesDirty so content can update
  1711. if (currentClippingPlanesState !== tile._clippingPlanesState) {
  1712. tile._clippingPlanesState = currentClippingPlanesState;
  1713. tile.clippingPlanesDirty = true;
  1714. }
  1715. }
  1716. /**
  1717. * Get the draw commands needed to render this tile.
  1718. *
  1719. * @private
  1720. */
  1721. Cesium3DTile.prototype.update = function (tileset, frameState, passOptions) {
  1722. const commandStart = frameState.commandList.length;
  1723. updateClippingPlanes(this, tileset);
  1724. applyDebugSettings(this, tileset, frameState, passOptions);
  1725. updateContent(this, tileset, frameState);
  1726. const commandEnd = frameState.commandList.length;
  1727. const commandsLength = commandEnd - commandStart;
  1728. this._commandsLength = commandsLength;
  1729. for (let i = 0; i < commandsLength; ++i) {
  1730. const command = frameState.commandList[commandStart + i];
  1731. const translucent = command.pass === Pass.TRANSLUCENT;
  1732. command.depthForTranslucentClassification = translucent;
  1733. }
  1734. this.clippingPlanesDirty = false; // reset after content update
  1735. };
  1736. const scratchCommandList = [];
  1737. /**
  1738. * Processes the tile's content, e.g., create WebGL resources, to move from the PROCESSING to READY state.
  1739. *
  1740. * @param {Cesium3DTileset} tileset The tileset containing this tile.
  1741. * @param {FrameState} frameState The frame state.
  1742. *
  1743. * @private
  1744. */
  1745. Cesium3DTile.prototype.process = function (tileset, frameState) {
  1746. const savedCommandList = frameState.commandList;
  1747. frameState.commandList = scratchCommandList;
  1748. this._content.update(tileset, frameState);
  1749. scratchCommandList.length = 0;
  1750. frameState.commandList = savedCommandList;
  1751. };
  1752. function isolateDigits(normalizedValue, numberOfDigits, leftShift) {
  1753. const scaled = normalizedValue * Math.pow(10, numberOfDigits);
  1754. const integer = parseInt(scaled);
  1755. return integer * Math.pow(10, leftShift);
  1756. }
  1757. function priorityNormalizeAndClamp(value, minimum, maximum) {
  1758. return Math.max(
  1759. CesiumMath.normalize(value, minimum, maximum) - CesiumMath.EPSILON7,
  1760. 0.0
  1761. ); // Subtract epsilon since we only want decimal digits present in the output.
  1762. }
  1763. /**
  1764. * Sets the priority of the tile based on distance and depth
  1765. * @private
  1766. */
  1767. Cesium3DTile.prototype.updatePriority = function () {
  1768. const tileset = this.tileset;
  1769. const preferLeaves = tileset.preferLeaves;
  1770. const minimumPriority = tileset._minimumPriority;
  1771. const maximumPriority = tileset._maximumPriority;
  1772. // Combine priority systems together by mapping them into a base 10 number where each priority controls a specific set of digits in the number.
  1773. // For number priorities, map them to a 0.xxxxx number then left shift it up into a set number of digits before the decimal point. Chop of the fractional part then left shift again into the position it needs to go.
  1774. // For blending number priorities, normalize them to 0-1 and interpolate to get a combined 0-1 number, then proceed as normal.
  1775. // Booleans can just be 0 or 10^leftshift.
  1776. // Think of digits as penalties since smaller numbers are higher priority. If a tile has some large quantity or has a flag raised it's (usually) penalized for it, expressed as a higher number for the digit.
  1777. // Priority number format: preloadFlightDigits(1) | foveatedDeferDigits(1) | foveatedDigits(4) | preloadProgressiveResolutionDigits(1) | preferredSortingDigits(4) . depthDigits(the decimal digits)
  1778. // Certain flags like preferLeaves will flip / turn off certain digits to get desired load order.
  1779. // Setup leftShifts, digit counts, and scales (for booleans)
  1780. const digitsForANumber = 4;
  1781. const digitsForABoolean = 1;
  1782. const preferredSortingLeftShift = 0;
  1783. const preferredSortingDigitsCount = digitsForANumber;
  1784. const foveatedLeftShift =
  1785. preferredSortingLeftShift + preferredSortingDigitsCount;
  1786. const foveatedDigitsCount = digitsForANumber;
  1787. const preloadProgressiveResolutionLeftShift =
  1788. foveatedLeftShift + foveatedDigitsCount;
  1789. const preloadProgressiveResolutionDigitsCount = digitsForABoolean;
  1790. const preloadProgressiveResolutionScale = Math.pow(
  1791. 10,
  1792. preloadProgressiveResolutionLeftShift
  1793. );
  1794. const foveatedDeferLeftShift =
  1795. preloadProgressiveResolutionLeftShift +
  1796. preloadProgressiveResolutionDigitsCount;
  1797. const foveatedDeferDigitsCount = digitsForABoolean;
  1798. const foveatedDeferScale = Math.pow(10, foveatedDeferLeftShift);
  1799. const preloadFlightLeftShift =
  1800. foveatedDeferLeftShift + foveatedDeferDigitsCount;
  1801. const preloadFlightScale = Math.pow(10, preloadFlightLeftShift);
  1802. // Compute the digits for each priority
  1803. let depthDigits = priorityNormalizeAndClamp(
  1804. this._depth,
  1805. minimumPriority.depth,
  1806. maximumPriority.depth
  1807. );
  1808. depthDigits = preferLeaves ? 1.0 - depthDigits : depthDigits;
  1809. // Map 0-1 then convert to digit. Include a distance sort when doing non-skipLOD and replacement refinement, helps things like non-skipLOD photogrammetry
  1810. const useDistance =
  1811. !tileset._skipLevelOfDetail && this.refine === Cesium3DTileRefine.REPLACE;
  1812. const normalizedPreferredSorting = useDistance
  1813. ? priorityNormalizeAndClamp(
  1814. this._priorityHolder._distanceToCamera,
  1815. minimumPriority.distance,
  1816. maximumPriority.distance
  1817. )
  1818. : priorityNormalizeAndClamp(
  1819. this._priorityReverseScreenSpaceError,
  1820. minimumPriority.reverseScreenSpaceError,
  1821. maximumPriority.reverseScreenSpaceError
  1822. );
  1823. const preferredSortingDigits = isolateDigits(
  1824. normalizedPreferredSorting,
  1825. preferredSortingDigitsCount,
  1826. preferredSortingLeftShift
  1827. );
  1828. const preloadProgressiveResolutionDigits = this._priorityProgressiveResolution
  1829. ? 0
  1830. : preloadProgressiveResolutionScale;
  1831. const normalizedFoveatedFactor = priorityNormalizeAndClamp(
  1832. this._priorityHolder._foveatedFactor,
  1833. minimumPriority.foveatedFactor,
  1834. maximumPriority.foveatedFactor
  1835. );
  1836. const foveatedDigits = isolateDigits(
  1837. normalizedFoveatedFactor,
  1838. foveatedDigitsCount,
  1839. foveatedLeftShift
  1840. );
  1841. const foveatedDeferDigits = this.priorityDeferred ? foveatedDeferScale : 0;
  1842. const preloadFlightDigits =
  1843. tileset._pass === Cesium3DTilePass.PRELOAD_FLIGHT ? 0 : preloadFlightScale;
  1844. // Get the final base 10 number
  1845. this._priority =
  1846. depthDigits +
  1847. preferredSortingDigits +
  1848. preloadProgressiveResolutionDigits +
  1849. foveatedDigits +
  1850. foveatedDeferDigits +
  1851. preloadFlightDigits;
  1852. };
  1853. /**
  1854. * @private
  1855. */
  1856. Cesium3DTile.prototype.isDestroyed = function () {
  1857. return false;
  1858. };
  1859. /**
  1860. * @private
  1861. */
  1862. Cesium3DTile.prototype.destroy = function () {
  1863. // For the interval between new content being requested and downloaded, expiredContent === content, so don't destroy twice
  1864. this._content = this._content && this._content.destroy();
  1865. this._expiredContent =
  1866. this._expiredContent &&
  1867. !this._expiredContent.isDestroyed() &&
  1868. this._expiredContent.destroy();
  1869. this._debugBoundingVolume =
  1870. this._debugBoundingVolume && this._debugBoundingVolume.destroy();
  1871. this._debugContentBoundingVolume =
  1872. this._debugContentBoundingVolume &&
  1873. this._debugContentBoundingVolume.destroy();
  1874. this._debugViewerRequestVolume =
  1875. this._debugViewerRequestVolume && this._debugViewerRequestVolume.destroy();
  1876. return destroyObject(this);
  1877. };
  1878. export default Cesium3DTile;