Cesium3DTile.js 54 KB

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