Cesium3DTile.js 59 KB

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