traverseFunctions.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { LOADED } from './constants.js';
  2. function isUsedThisFrame( tile, frameCount ) {
  3. return tile.__lastFrameVisited === frameCount && tile.__used;
  4. }
  5. function resetFrameState( tile, frameCount ) {
  6. if ( tile.__lastFrameVisited !== frameCount ) {
  7. tile.__lastFrameVisited = frameCount;
  8. tile.__used = false;
  9. tile.__inFrustum = false;
  10. tile.__isLeaf = false;
  11. tile.__visible = false;
  12. tile.__active = false;
  13. tile.__error = 0;
  14. tile.__childrenWereVisible = false;
  15. }
  16. }
  17. function recursivelyMarkUsed( tile, frameCount, lruCache ) {
  18. resetFrameState( tile, frameCount );
  19. tile.__used = true;
  20. lruCache.markUsed( tile );
  21. if ( tile.__contentEmpty ) {
  22. const children = tile.children;
  23. for ( let i = 0, l = children.length; i < l; i ++ ) {
  24. recursivelyMarkUsed( children[ i ], frameCount, lruCache );
  25. }
  26. }
  27. }
  28. export function traverseSet( tile, beforeCb = null, afterCb = null, parent = null, depth = 0 ) {
  29. if ( beforeCb && beforeCb( tile, parent, depth ) ) {
  30. if ( afterCb ) {
  31. afterCb( tile, parent, depth );
  32. }
  33. return;
  34. }
  35. const children = tile.children;
  36. for ( let i = 0, l = children.length; i < l; i ++ ) {
  37. traverseSet( children[ i ], beforeCb, afterCb, tile, depth + 1 );
  38. }
  39. if ( afterCb ) {
  40. afterCb( tile, parent, depth );
  41. }
  42. }
  43. // TODO: include frustum mask here?
  44. // TODO: this is marking items as used in the lrucache, which means some data is
  45. // being kept around that isn't being used -- is that okay?
  46. export function determineFrustumSet( tile, renderer ) {
  47. const stats = renderer.stats;
  48. const frameCount = renderer.frameCount;
  49. const errorTarget = renderer.errorTarget;
  50. const maxDepth = renderer.maxDepth;
  51. const loadSiblings = renderer.loadSiblings;
  52. const lruCache = renderer.lruCache;
  53. resetFrameState( tile, frameCount );
  54. const inFrustum = renderer.tileInView( tile );
  55. if ( inFrustum === false ) {
  56. return false;
  57. }
  58. tile.__used = true;
  59. lruCache.markUsed( tile );
  60. tile.__inFrustum = true;
  61. stats.inFrustum ++;
  62. if ( ! tile.__contentEmpty ) {
  63. const error = renderer.calculateError( tile );
  64. tile.__error = error;
  65. if ( error <= errorTarget ) {
  66. return true;
  67. }
  68. }
  69. if ( renderer.maxDepth > 0 && tile.__depth + 1 >= maxDepth ) {
  70. return true;
  71. }
  72. let anyChildrenUsed = false;
  73. const children = tile.children;
  74. for ( let i = 0, l = children.length; i < l; i ++ ) {
  75. const c = children[ i ];
  76. const r = determineFrustumSet( c, renderer );
  77. anyChildrenUsed = anyChildrenUsed || r;
  78. }
  79. if ( anyChildrenUsed && loadSiblings ) {
  80. for ( let i = 0, l = children.length; i < l; i ++ ) {
  81. recursivelyMarkUsed( tile, frameCount, lruCache );
  82. }
  83. }
  84. return true;
  85. }
  86. export function markUsedSetLeaves( tile, renderer ) {
  87. const stats = renderer.stats;
  88. const frameCount = renderer.frameCount;
  89. if ( ! isUsedThisFrame( tile, frameCount ) ) {
  90. return;
  91. }
  92. stats.used ++;
  93. const children = tile.children;
  94. let anyChildrenUsed = false;
  95. for ( let i = 0, l = children.length; i < l; i ++ ) {
  96. const c = children[ i ];
  97. anyChildrenUsed = anyChildrenUsed || isUsedThisFrame( c, frameCount );
  98. }
  99. if ( ! anyChildrenUsed ) {
  100. // TODO: This isn't necessarily right because it's possible that a parent tile is considered in the
  101. // frustum while the child tiles are not, making them unused. If all children have loaded and were properly
  102. // considered to be in the used set then we shouldn't set ourselves to a leaf here.
  103. tile.__isLeaf = true;
  104. // TODO: stats
  105. } else {
  106. let childrenWereVisible = false;
  107. for ( let i = 0, l = children.length; i < l; i ++ ) {
  108. const c = children[ i ];
  109. markUsedSetLeaves( c, renderer );
  110. childrenWereVisible = childrenWereVisible || c.__wasSetVisible || c.__childrenWereVisible;
  111. }
  112. tile.__childrenWereVisible = childrenWereVisible;
  113. }
  114. }
  115. export function skipTraversal( tile, renderer ) {
  116. const stats = renderer.stats;
  117. const frameCount = renderer.frameCount;
  118. if ( ! isUsedThisFrame( tile, frameCount ) ) {
  119. return;
  120. }
  121. const lruCache = renderer.lruCache;
  122. if ( tile.__isLeaf ) {
  123. if ( tile.__loadingState === LOADED ) {
  124. if ( tile.__inFrustum ) {
  125. tile.__visible = true;
  126. stats.visible ++;
  127. }
  128. tile.__active = true;
  129. stats.active ++;
  130. } else if ( ! lruCache.isFull() ) {
  131. renderer.requestTileContents( tile );
  132. }
  133. return;
  134. }
  135. const errorRequirement = renderer.errorTarget * renderer.errorThreshold;
  136. const meetsSSE = tile.__error <= errorRequirement;
  137. const hasContent = ! tile.__contentEmpty;
  138. const loadedContent = tile.__loadingState === LOADED && ! tile.__contentEmpty;
  139. const childrenWereVisible = tile.__childrenWereVisible;
  140. const children = tile.children;
  141. let allChildrenHaveContent = true;
  142. for ( let i = 0, l = children.length; i < l; i ++ ) {
  143. const c = children[ i ];
  144. if ( isUsedThisFrame( c, frameCount ) ) {
  145. // TODO: This doesn't seem right -- we should check down to the next children with content?
  146. const childContent = c.__loadingState === LOADED || tile.__contentEmpty;
  147. allChildrenHaveContent = allChildrenHaveContent && childContent;
  148. }
  149. }
  150. if ( meetsSSE && ! loadedContent && ! lruCache.isFull() && hasContent ) {
  151. renderer.requestTileContents( tile );
  152. }
  153. // Only mark this tile as visible if it meets the screen space error requirements, has loaded content, not
  154. // all children have loaded yet, and if no children were visible last frame. We want to keep children visible
  155. // that _were_ visible to avoid a pop in level of detail as the camera moves around and parent / sibling tiles
  156. // load in.
  157. if ( meetsSSE && ! allChildrenHaveContent && ! childrenWereVisible ) {
  158. if ( loadedContent ) {
  159. if ( tile.__inFrustum ) {
  160. tile.__visible = true;
  161. stats.visible ++;
  162. }
  163. tile.__active = true;
  164. stats.active ++;
  165. for ( let i = 0, l = children.length; i < l; i ++ ) {
  166. const c = children[ i ];
  167. if ( isUsedThisFrame( c, frameCount ) && ! lruCache.isFull() ) {
  168. renderer.requestTileContents( c );
  169. }
  170. }
  171. }
  172. return;
  173. }
  174. for ( let i = 0, l = children.length; i < l; i ++ ) {
  175. const c = children[ i ];
  176. if ( isUsedThisFrame( c, frameCount ) ) {
  177. skipTraversal( c, renderer );
  178. }
  179. }
  180. }
  181. export function toggleTiles( tile, renderer ) {
  182. const frameCount = renderer.frameCount;
  183. const isUsed = isUsedThisFrame( tile, frameCount );
  184. if ( isUsed || tile.__usedLastFrame ) {
  185. let setActive = false;
  186. let setVisible = false;
  187. if ( isUsed ) {
  188. // enable visibility if active due to shadows
  189. setActive = tile.__active;
  190. setVisible = tile.__active || tile.__visible;
  191. }
  192. if ( ! tile.__contentEmpty && tile.__loadingState === LOADED ) {
  193. if ( tile.__wasSetActive !== setActive ) {
  194. renderer.setTileVisible( tile, setActive );
  195. }
  196. if ( tile.__wasSetVisible !== setVisible ) {
  197. renderer.setTileActive( tile, setVisible );
  198. }
  199. }
  200. tile.__wasSetActive = setActive;
  201. tile.__wasSetVisible = setVisible;
  202. tile.__usedLastFrame = isUsed;
  203. const children = tile.children;
  204. for ( let i = 0, l = children.length; i < l; i ++ ) {
  205. const c = children[ i ];
  206. toggleTiles( c, renderer );
  207. }
  208. }
  209. }