DebugTilesRenderer.js 7.1 KB

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