DebugTilesRenderer.js 8.0 KB

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