traverseFunctions.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import { LOADED } from './constants.js';
  2. // Checks whether this tile was last used on the given frame.
  3. function isUsedThisFrame( tile, frameCount ) {
  4. return tile.__lastFrameVisited === frameCount && tile.__used;
  5. }
  6. // Resets the frame frame information for the given tile
  7. function resetFrameState( tile, frameCount ) {
  8. if ( tile.__lastFrameVisited !== frameCount ) {
  9. tile.__lastFrameVisited = frameCount;
  10. tile.__used = false;
  11. tile.__inFrustum = false;
  12. tile.__isLeaf = false;
  13. tile.__visible = false;
  14. tile.__active = false;
  15. tile.__error = 0;
  16. tile.__childrenWereVisible = false;
  17. tile.__allChildrenLoaded = false;
  18. }
  19. }
  20. // Recursively mark tiles used down to the next tile with content
  21. function recursivelyMarkUsed( tile, frameCount, lruCache ) {
  22. resetFrameState( tile, frameCount );
  23. tile.__used = true;
  24. lruCache.markUsed( tile );
  25. if ( tile.__contentEmpty ) {
  26. const children = tile.children;
  27. for ( let i = 0, l = children.length; i < l; i ++ ) {
  28. recursivelyMarkUsed( children[ i ], frameCount, lruCache );
  29. }
  30. }
  31. }
  32. // Helper function for recursively traversing a tileset. If `beforeCb` returns `true` then the
  33. // traversal will end early.
  34. export function traverseSet( tile, beforeCb = null, afterCb = null, parent = null, depth = 0 ) {
  35. if ( beforeCb && beforeCb( tile, parent, depth ) ) {
  36. if ( afterCb ) {
  37. afterCb( tile, parent, depth );
  38. }
  39. return;
  40. }
  41. const children = tile.children;
  42. for ( let i = 0, l = children.length; i < l; i ++ ) {
  43. traverseSet( children[ i ], beforeCb, afterCb, tile, depth + 1 );
  44. }
  45. if ( afterCb ) {
  46. afterCb( tile, parent, depth );
  47. }
  48. }
  49. // Determine which tiles are within the camera frustum.
  50. // TODO: include frustum mask here?
  51. // TODO: this is marking items as used in the lrucache, which means some data is
  52. // being kept around that isn't being used -- is that okay?
  53. export function determineFrustumSet( tile, renderer ) {
  54. const stats = renderer.stats;
  55. const frameCount = renderer.frameCount;
  56. const errorTarget = renderer.errorTarget;
  57. const maxDepth = renderer.maxDepth;
  58. const loadSiblings = renderer.loadSiblings;
  59. const lruCache = renderer.lruCache;
  60. resetFrameState( tile, frameCount );
  61. // Early out if this tile is not within view.
  62. const inFrustum = renderer.tileInView( tile );
  63. if ( inFrustum === false ) {
  64. return false;
  65. }
  66. tile.__used = true;
  67. lruCache.markUsed( tile );
  68. tile.__inFrustum = true;
  69. stats.inFrustum ++;
  70. // Early out if this tile has less error than we're targeting.
  71. if ( ! tile.__contentEmpty ) {
  72. const error = renderer.calculateError( tile );
  73. tile.__error = error;
  74. if ( error <= errorTarget ) {
  75. return true;
  76. }
  77. }
  78. // Early out if we've reached the maximum allowed depth.
  79. if ( renderer.maxDepth > 0 && tile.__depth + 1 >= maxDepth ) {
  80. return true;
  81. }
  82. // Traverse children and see if any children are in view.
  83. let anyChildrenUsed = false;
  84. const children = tile.children;
  85. for ( let i = 0, l = children.length; i < l; i ++ ) {
  86. const c = children[ i ];
  87. const r = determineFrustumSet( c, renderer );
  88. anyChildrenUsed = anyChildrenUsed || r;
  89. }
  90. // If there are children within view and we are loading siblings then mark
  91. // all sibling tiles as used, as well.
  92. if ( anyChildrenUsed && loadSiblings ) {
  93. for ( let i = 0, l = children.length; i < l; i ++ ) {
  94. const c = children[ i ];
  95. recursivelyMarkUsed( c, frameCount, lruCache );
  96. }
  97. }
  98. return true;
  99. }
  100. // Traverse and mark the tiles that are at the leaf nodes of the "used" tree.
  101. export function markUsedSetLeaves( tile, renderer ) {
  102. const stats = renderer.stats;
  103. const frameCount = renderer.frameCount;
  104. if ( ! isUsedThisFrame( tile, frameCount ) ) {
  105. return;
  106. }
  107. stats.used ++;
  108. // This tile is a leaf if none of the children had been used.
  109. const children = tile.children;
  110. let anyChildrenUsed = false;
  111. for ( let i = 0, l = children.length; i < l; i ++ ) {
  112. const c = children[ i ];
  113. anyChildrenUsed = anyChildrenUsed || isUsedThisFrame( c, frameCount );
  114. }
  115. if ( ! anyChildrenUsed ) {
  116. // TODO: This isn't necessarily right because it's possible that a parent tile is considered in the
  117. // frustum while the child tiles are not, making them unused. If all children have loaded and were properly
  118. // considered to be in the used set then we shouldn't set ourselves to a leaf here.
  119. tile.__isLeaf = true;
  120. // TODO: stats
  121. } else {
  122. let childrenWereVisible = false;
  123. let allChildrenLoaded = true;
  124. for ( let i = 0, l = children.length; i < l; i ++ ) {
  125. const c = children[ i ];
  126. markUsedSetLeaves( c, renderer );
  127. childrenWereVisible = childrenWereVisible || c.__wasSetVisible || c.__childrenWereVisible;
  128. if ( isUsedThisFrame( c, frameCount ) ) {
  129. const childLoaded = ( ! c.__contentEmpty && c.__loadingState === LOADED ) || c.__allChildrenLoaded;
  130. allChildrenLoaded = allChildrenLoaded && childLoaded;
  131. }
  132. }
  133. tile.__childrenWereVisible = childrenWereVisible;
  134. tile.__allChildrenLoaded = allChildrenLoaded;
  135. }
  136. }
  137. // Skip past tiles we consider unrenderable because they are outside the error threshold.
  138. export function skipTraversal( tile, renderer ) {
  139. const stats = renderer.stats;
  140. const frameCount = renderer.frameCount;
  141. if ( ! isUsedThisFrame( tile, frameCount ) ) {
  142. return;
  143. }
  144. const parent = tile.parent;
  145. const parentDepthToParent = parent ? parent.__depthFromRenderedParent : - 1;
  146. tile.__depthFromRenderedParent = parentDepthToParent;
  147. // Request the tile contents or mark it as visible if we've found a leaf.
  148. const lruCache = renderer.lruCache;
  149. if ( tile.__isLeaf ) {
  150. tile.__depthFromRenderedParent = parentDepthToParent + 1;
  151. if ( tile.__loadingState === LOADED ) {
  152. if ( tile.__inFrustum ) {
  153. tile.__visible = true;
  154. stats.visible ++;
  155. }
  156. tile.__active = true;
  157. stats.active ++;
  158. } else if ( ! lruCache.isFull() ) {
  159. renderer.requestTileContents( tile );
  160. }
  161. return;
  162. }
  163. const errorRequirement = ( renderer.errorTarget + 1 ) * renderer.errorThreshold;
  164. const meetsSSE = tile.__error <= errorRequirement;
  165. const hasContent = ! tile.__contentEmpty;
  166. const loadedContent = tile.__loadingState === LOADED && ! tile.__contentEmpty;
  167. const childrenWereVisible = tile.__childrenWereVisible;
  168. const children = tile.children;
  169. let allChildrenHaveContent = tile.__allChildrenLoaded;
  170. // Increment the relative depth of the node to the nearest rendered parent if it has content
  171. // and is being rendered.
  172. if ( meetsSSE && hasContent ) {
  173. tile.__depthFromRenderedParent = parentDepthToParent + 1;
  174. }
  175. // If we've met the SSE requirements and we can load content then fire a fetch.
  176. if ( meetsSSE && ! loadedContent && ! lruCache.isFull() && hasContent ) {
  177. renderer.requestTileContents( tile );
  178. }
  179. // Only mark this tile as visible if it meets the screen space error requirements, has loaded content, not
  180. // all children have loaded yet, and if no children were visible last frame. We want to keep children visible
  181. // that _were_ visible to avoid a pop in level of detail as the camera moves around and parent / sibling tiles
  182. // load in.
  183. if ( meetsSSE && ! allChildrenHaveContent && ! childrenWereVisible ) {
  184. if ( loadedContent ) {
  185. if ( tile.__inFrustum ) {
  186. tile.__visible = true;
  187. stats.visible ++;
  188. }
  189. tile.__active = true;
  190. stats.active ++;
  191. // load the child content if we've found that we've been loaded so we can move down to the next tile
  192. // layer when the data has loaded.
  193. for ( let i = 0, l = children.length; i < l; i ++ ) {
  194. const c = children[ i ];
  195. if ( isUsedThisFrame( c, frameCount ) && ! lruCache.isFull() ) {
  196. renderer.requestTileContents( c );
  197. }
  198. }
  199. }
  200. return;
  201. }
  202. for ( let i = 0, l = children.length; i < l; i ++ ) {
  203. const c = children[ i ];
  204. if ( isUsedThisFrame( c, frameCount ) ) {
  205. skipTraversal( c, renderer );
  206. }
  207. }
  208. }
  209. // Final traverse to toggle tile visibility.
  210. export function toggleTiles( tile, renderer ) {
  211. const frameCount = renderer.frameCount;
  212. const isUsed = isUsedThisFrame( tile, frameCount );
  213. if ( isUsed || tile.__usedLastFrame ) {
  214. let setActive = false;
  215. let setVisible = false;
  216. if ( isUsed ) {
  217. // enable visibility if active due to shadows
  218. setActive = tile.__active;
  219. if ( renderer.displayActiveTiles ) {
  220. setVisible = tile.__active || tile.__visible;
  221. } else {
  222. setVisible = tile.__visible;
  223. }
  224. }
  225. // If the active or visible state changed then call the functions.
  226. if ( ! tile.__contentEmpty && tile.__loadingState === LOADED ) {
  227. if ( tile.__wasSetActive !== setActive ) {
  228. renderer.setTileActive( tile, setActive );
  229. }
  230. if ( tile.__wasSetVisible !== setVisible ) {
  231. renderer.setTileVisible( tile, setVisible );
  232. }
  233. }
  234. tile.__wasSetActive = setActive;
  235. tile.__wasSetVisible = setVisible;
  236. tile.__usedLastFrame = isUsed;
  237. const children = tile.children;
  238. for ( let i = 0, l = children.length; i < l; i ++ ) {
  239. const c = children[ i ];
  240. toggleTiles( c, renderer );
  241. }
  242. }
  243. }