DebugTilesRenderer.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. import { Box3Helper, Group, MeshStandardMaterial, PointsMaterial } from 'three';
  2. import { getIndexedRandomColor } from './utilities.js';
  3. import { TilesRenderer } from './TilesRenderer.js';
  4. import { SphereHelper } from './SphereHelper.js';
  5. const ORIGINAL_MATERIAL = Symbol( 'ORIGINAL_MATERIAL' );
  6. const HAS_RANDOM_COLOR = Symbol( 'HAS_RANDOM_COLOR' );
  7. function emptyRaycast() {}
  8. export const NONE = 0;
  9. export const SCREEN_ERROR = 1;
  10. export const GEOMETRIC_ERROR = 2;
  11. export const DISTANCE = 3;
  12. export const DEPTH = 4;
  13. export const RELATIVE_DEPTH = 5;
  14. export const IS_LEAF = 6;
  15. export const RANDOM_COLOR = 7;
  16. export const RANDOM_NODE_COLOR = 8;
  17. export class DebugTilesRenderer extends TilesRenderer {
  18. constructor( ...args ) {
  19. super( ...args );
  20. const tilesGroup = this.group;
  21. const boxGroup = new Group();
  22. boxGroup.name = 'DebugTilesRenderer.boxGroup';
  23. tilesGroup.add( boxGroup );
  24. const sphereGroup = new Group();
  25. sphereGroup.name = 'DebugTilesRenderer.sphereGroup';
  26. tilesGroup.add( sphereGroup );
  27. this.displayBoxBounds = false;
  28. this.displaySphereBounds = false;
  29. this.colorMode = NONE;
  30. this.boxGroup = boxGroup;
  31. this.sphereGroup = sphereGroup;
  32. this.maxDebugDepth = - 1;
  33. this.maxDebugDistance = - 1;
  34. this.maxDebugError = - 1;
  35. this.getDebugColor = ( value, target ) => {
  36. target.setRGB( value, value, value );
  37. };
  38. this.extremeDebugDepth = - 1;
  39. this.extremeDebugError = - 1;
  40. }
  41. initExtremes() {
  42. // initialize the extreme values of the hierarchy
  43. let maxDepth = - 1;
  44. this.traverse( tile => {
  45. maxDepth = Math.max( maxDepth, tile.__depth );
  46. } );
  47. let maxError = - 1;
  48. this.traverse( tile => {
  49. maxError = Math.max( maxError, tile.geometricError );
  50. } );
  51. this.extremeDebugDepth = maxDepth;
  52. this.extremeDebugError = maxError;
  53. }
  54. fetchTileSet( ...args ) {
  55. const pr = super.fetchTileSet( ...args );
  56. pr
  57. .then( () => {
  58. // defer to after the loaded tileset has been initialized
  59. Promise.resolve().then( () => {
  60. this.initExtremes();
  61. } );
  62. } )
  63. .catch( () => {
  64. // error is logged internally
  65. } );
  66. return pr;
  67. }
  68. getTileInformationFromActiveObject( object ) {
  69. // Find which tile this scene is associated with. This is slow and
  70. // intended for debug purposes only.
  71. let targetTile = null;
  72. const activeTiles = this.activeTiles;
  73. activeTiles.forEach( tile => {
  74. if ( targetTile ) {
  75. return true;
  76. }
  77. const scene = tile.cached.scene;
  78. if ( scene ) {
  79. scene.traverse( c => {
  80. if ( c === object ) {
  81. targetTile = tile;
  82. }
  83. } );
  84. }
  85. } );
  86. if ( targetTile ) {
  87. return {
  88. distanceToCamera: targetTile.__distanceFromCamera,
  89. geometricError: targetTile.geometricError,
  90. screenSpaceError: targetTile.__error,
  91. depth: targetTile.__depth,
  92. isLeaf: targetTile.__isLeaf
  93. };
  94. } else {
  95. return null;
  96. }
  97. }
  98. update() {
  99. super.update();
  100. if ( ! this.root ) {
  101. return;
  102. }
  103. // set box or sphere visibility
  104. this.boxGroup.visible = this.displayBoxBounds;
  105. this.sphereGroup.visible = this.displaySphereBounds;
  106. // get max values to use for materials
  107. let maxDepth = - 1;
  108. if ( this.maxDebugDepth === - 1 ) {
  109. maxDepth = this.extremeDebugDepth;
  110. } else {
  111. maxDepth = this.maxDebugDepth;
  112. }
  113. let maxError = - 1;
  114. if ( this.maxDebugError === - 1 ) {
  115. maxError = this.extremeDebugError;
  116. } else {
  117. maxError = this.maxDebugError;
  118. }
  119. let maxDistance = - 1;
  120. if ( this.maxDebugDistance === - 1 ) {
  121. maxDistance = this.root.cached.sphere.radius;
  122. } else {
  123. maxDistance = this.maxDebugDistance;
  124. }
  125. const errorTarget = this.errorTarget;
  126. const colorMode = this.colorMode;
  127. const visibleTiles = this.visibleTiles;
  128. visibleTiles.forEach( tile => {
  129. const scene = tile.cached.scene;
  130. // create a random color per-tile
  131. let h, s, l;
  132. if ( colorMode === RANDOM_COLOR ) {
  133. h = Math.random();
  134. s = 0.5 + Math.random() * 0.5;
  135. l = 0.375 + Math.random() * 0.25;
  136. }
  137. scene.traverse( c => {
  138. if ( colorMode === RANDOM_NODE_COLOR ) {
  139. h = Math.random();
  140. s = 0.5 + Math.random() * 0.5;
  141. l = 0.375 + Math.random() * 0.25;
  142. }
  143. const currMaterial = c.material;
  144. if ( currMaterial ) {
  145. // Reset the material if needed
  146. const originalMaterial = c[ ORIGINAL_MATERIAL ];
  147. if ( colorMode === NONE && currMaterial !== originalMaterial ) {
  148. c.material.dispose();
  149. c.material = c[ ORIGINAL_MATERIAL ];
  150. } else if ( colorMode !== NONE && currMaterial === originalMaterial ) {
  151. if ( c.isPoints ) {
  152. const pointsMaterial = new PointsMaterial();
  153. pointsMaterial.size = originalMaterial.size;
  154. pointsMaterial.sizeAttenuation = originalMaterial.sizeAttenuation;
  155. c.material = pointsMaterial;
  156. } else {
  157. c.material = new MeshStandardMaterial();
  158. c.material.flatShading = true;
  159. }
  160. }
  161. if ( colorMode !== RANDOM_COLOR && colorMode !== RANDOM_NODE_COLOR ) {
  162. delete c.material[ HAS_RANDOM_COLOR ];
  163. }
  164. // Set the color on the basic material
  165. switch ( colorMode ) {
  166. case DEPTH: {
  167. const val = tile.__depth / maxDepth;
  168. this.getDebugColor( val, c.material.color );
  169. break;
  170. }
  171. case RELATIVE_DEPTH: {
  172. const val = tile.__depthFromRenderedParent / maxDepth;
  173. this.getDebugColor( val, c.material.color );
  174. break;
  175. }
  176. case SCREEN_ERROR: {
  177. const val = tile.__error / errorTarget;
  178. if ( val > 1.0 ) {
  179. c.material.color.setRGB( 1.0, 0.0, 0.0 );
  180. } else {
  181. this.getDebugColor( val, c.material.color );
  182. }
  183. break;
  184. }
  185. case GEOMETRIC_ERROR: {
  186. const val = Math.min( tile.geometricError / maxError, 1 );
  187. this.getDebugColor( val, c.material.color );
  188. break;
  189. }
  190. case DISTANCE: {
  191. // We don't update the distance if the geometric error is 0.0 so
  192. // it will always be black.
  193. const val = Math.min( tile.__distanceFromCamera / maxDistance, 1 );
  194. this.getDebugColor( val, c.material.color );
  195. break;
  196. }
  197. case IS_LEAF: {
  198. if ( ! tile.children || tile.children.length === 0 ) {
  199. this.getDebugColor( 1.0, c.material.color );
  200. } else {
  201. this.getDebugColor( 0.0, c.material.color );
  202. }
  203. break;
  204. }
  205. case RANDOM_NODE_COLOR: {
  206. if ( ! c.material[ HAS_RANDOM_COLOR ] ) {
  207. c.material.color.setHSL( h, s, l );
  208. c.material[ HAS_RANDOM_COLOR ] = true;
  209. }
  210. break;
  211. }
  212. case RANDOM_COLOR: {
  213. if ( ! c.material[ HAS_RANDOM_COLOR ] ) {
  214. c.material.color.setHSL( h, s, l );
  215. c.material[ HAS_RANDOM_COLOR ] = true;
  216. }
  217. break;
  218. }
  219. }
  220. }
  221. } );
  222. } );
  223. }
  224. setTileVisible( tile, visible ) {
  225. super.setTileVisible( tile, visible );
  226. const cached = tile.cached;
  227. const sphereGroup = this.sphereGroup;
  228. const boxGroup = this.boxGroup;
  229. const boxHelperGroup = cached.boxHelperGroup;
  230. const sphereHelper = cached.sphereHelper;
  231. if ( ! visible ) {
  232. if ( boxHelperGroup ) {
  233. boxGroup.remove( boxHelperGroup );
  234. }
  235. if ( sphereHelper ) {
  236. sphereGroup.remove( sphereHelper );
  237. }
  238. } else {
  239. if ( boxHelperGroup ) {
  240. boxGroup.add( boxHelperGroup );
  241. boxHelperGroup.updateMatrixWorld( true );
  242. }
  243. if ( sphereHelper ) {
  244. sphereGroup.add( sphereHelper );
  245. sphereHelper.updateMatrixWorld( true );
  246. }
  247. }
  248. }
  249. parseTile( buffer, tile, extension ) {
  250. return super
  251. .parseTile( buffer, tile, extension )
  252. .then( () => {
  253. const cached = tile.cached;
  254. const scene = cached.scene;
  255. if ( scene ) {
  256. if ( cached.box && cached.boxTransform ) {
  257. const cachedBox = cached.box;
  258. const cachedBoxMat = cached.boxTransform;
  259. // Create debug bounding box
  260. // In some cases the bounding box may have a scale of 0 in one dimension resulting
  261. // in the NaNs in an extracted rotation so we disable matrix updates instead.
  262. const boxHelperGroup = new Group();
  263. boxHelperGroup.name = 'DebugTilesRenderer.boxHelperGroup';
  264. boxHelperGroup.matrix.copy( cachedBoxMat );
  265. boxHelperGroup.matrixAutoUpdate = false;
  266. const boxHelper = new Box3Helper( cachedBox, getIndexedRandomColor( tile.__depth ) );
  267. boxHelper.raycast = emptyRaycast;
  268. boxHelperGroup.add( boxHelper );
  269. cached.boxHelperGroup = boxHelperGroup;
  270. if ( this.visibleTiles.has( tile ) && this.displayBoxBounds ) {
  271. this.boxGroup.add( boxHelperGroup );
  272. boxHelperGroup.updateMatrixWorld( true );
  273. }
  274. }
  275. if ( cached.sphere ) {
  276. // Create debugbounding sphere
  277. const cachedSphere = cached.sphere;
  278. const sphereHelper = new SphereHelper( cachedSphere );
  279. sphereHelper.raycast = emptyRaycast;
  280. cached.sphereHelper = sphereHelper;
  281. if ( this.visibleTiles.has( tile ) && this.displaySphereBounds ) {
  282. this.sphereGroup.add( sphereHelper );
  283. sphereHelper.updateMatrixWorld( true );
  284. }
  285. }
  286. // Cache the original materials
  287. scene.traverse( c => {
  288. const material = c.material;
  289. if ( material ) {
  290. c[ ORIGINAL_MATERIAL ] = material;
  291. }
  292. } );
  293. }
  294. } );
  295. }
  296. disposeTile( tile ) {
  297. super.disposeTile( tile );
  298. const cached = tile.cached;
  299. if ( cached.boxHelperGroup ) {
  300. cached.boxHelperGroup.children[ 0 ].geometry.dispose();
  301. delete cached.boxHelperGroup;
  302. }
  303. if ( cached.sphereHelper ) {
  304. cached.sphereHelper.geometry.dispose();
  305. delete cached.sphereHelper;
  306. }
  307. }
  308. }