DebugTilesRenderer.js 9.9 KB

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