DebugTilesRenderer.js 9.2 KB

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