index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. import { DebugTilesRenderer as TilesRenderer } from '../src/index.js';
  2. import {
  3. Scene,
  4. DirectionalLight,
  5. AmbientLight,
  6. WebGLRenderer,
  7. PerspectiveCamera,
  8. CameraHelper,
  9. Box3,
  10. Raycaster,
  11. Vector2,
  12. Mesh,
  13. CylinderBufferGeometry,
  14. MeshBasicMaterial,
  15. Group,
  16. TorusBufferGeometry,
  17. OrthographicCamera
  18. } from 'three';
  19. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  20. import { BufferGeometryUtils } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
  21. import * as dat from 'three/examples/jsm/libs/dat.gui.module.js';
  22. import Stats from 'three/examples/jsm/libs/stats.module.js';
  23. let camera, controls, scene, renderer, tiles, cameraHelper;
  24. let thirdPersonCamera, thirdPersonRenderer, thirdPersonControls;
  25. let orthoCamera, orthoCameraHelper;
  26. let box;
  27. let raycaster, mouse, rayIntersect;
  28. let offsetParent;
  29. let statsContainer, stats;
  30. let params = {
  31. 'enableRaycast': false,
  32. 'enableCacheDisplay': false,
  33. 'orthographic': false,
  34. 'errorTarget': 6,
  35. 'errorThreshold': 60,
  36. 'maxDepth': 15,
  37. 'loadSiblings': true,
  38. 'up': '+Y',
  39. 'displayBoxBounds': false,
  40. 'colorMode': 0,
  41. 'showThirdPerson': true,
  42. 'reload': reinstantiateTiles,
  43. };
  44. init();
  45. animate();
  46. function reinstantiateTiles() {
  47. const url = window.location.hash.replace( /^#/, '' ) || '../data/tileset.json';
  48. if ( tiles ) {
  49. offsetParent.remove( tiles.group );
  50. }
  51. tiles = new TilesRenderer( url );
  52. tiles.camera = camera;
  53. tiles.setResolutionFromRenderer( renderer );
  54. offsetParent.add( tiles.group );
  55. }
  56. function init() {
  57. // Third person camera view
  58. thirdPersonCamera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  59. thirdPersonCamera.position.set( 50, 40, 40 );
  60. thirdPersonCamera.lookAt( 0, 0, 0 );
  61. thirdPersonRenderer = new WebGLRenderer( { antialias: true } );
  62. thirdPersonRenderer.setPixelRatio( window.devicePixelRatio );
  63. thirdPersonRenderer.setSize( window.innerWidth, window.innerHeight );
  64. thirdPersonRenderer.setClearColor( 0x0f1416 );
  65. document.body.appendChild( thirdPersonRenderer.domElement );
  66. thirdPersonRenderer.domElement.style.position = 'fixed';
  67. thirdPersonRenderer.domElement.style.left = '5px';
  68. thirdPersonRenderer.domElement.style.bottom = '5px';
  69. thirdPersonControls = new OrbitControls( thirdPersonCamera, thirdPersonRenderer.domElement );
  70. thirdPersonControls.screenSpacePanning = false;
  71. thirdPersonControls.minDistance = 1;
  72. thirdPersonControls.maxDistance = 2000;
  73. // primary camera view
  74. scene = new Scene();
  75. renderer = new WebGLRenderer( { antialias: true } );
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. renderer.setClearColor( 0x151c1f );
  79. renderer.gammaInput = true;
  80. renderer.gameOutput = true;
  81. document.body.appendChild( renderer.domElement );
  82. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  83. camera.position.set( 400, 400, 400 );
  84. cameraHelper = new CameraHelper( camera );
  85. scene.add( cameraHelper );
  86. orthoCamera = new OrthographicCamera();
  87. orthoCameraHelper = new CameraHelper( orthoCamera );
  88. scene.add( orthoCameraHelper );
  89. // controls
  90. controls = new OrbitControls( camera, renderer.domElement );
  91. controls.screenSpacePanning = false;
  92. controls.minDistance = 1;
  93. controls.maxDistance = 2000;
  94. // lights
  95. var dirLight = new DirectionalLight( 0xffffff );
  96. dirLight.position.set( 1, 2, 3 );
  97. scene.add( dirLight );
  98. var ambLight = new AmbientLight( 0xffffff, 0.2 );
  99. scene.add( ambLight );
  100. box = new Box3();
  101. offsetParent = new Group();
  102. scene.add( offsetParent );
  103. // Raycasting init
  104. raycaster = new Raycaster();
  105. mouse = new Vector2();
  106. rayIntersect = new Group();
  107. const rayIntersectMat = new MeshBasicMaterial( { color: 0xe91e63 } );
  108. const rayMesh = new Mesh( new CylinderBufferGeometry( 0.25, 0.25, 6 ), rayIntersectMat );
  109. rayMesh.rotation.x = Math.PI / 2;
  110. rayMesh.position.z += 3;
  111. rayIntersect.add( rayMesh );
  112. const rayRing = new Mesh( new TorusBufferGeometry( 1.5, 0.2, 16, 100 ), rayIntersectMat );
  113. rayIntersect.add( rayRing );
  114. scene.add( rayIntersect );
  115. rayIntersect.visible = false;
  116. reinstantiateTiles();
  117. onWindowResize();
  118. window.addEventListener( 'resize', onWindowResize, false );
  119. window.addEventListener( 'mousemove', onMouseMove, false );
  120. window.addEventListener( 'mousedown', onMouseDown, false );
  121. window.addEventListener( 'mouseup', onMouseUp, false );
  122. // GUI
  123. const gui = new dat.GUI();
  124. gui.width = 300;
  125. const tileOptions = gui.addFolder( 'Tiles Options' );
  126. tileOptions.add( params, 'orthographic' );
  127. tileOptions.add( params, 'loadSiblings' );
  128. tileOptions.add( params, 'errorTarget' ).min( 0 ).max( 50 );
  129. tileOptions.add( params, 'errorThreshold' ).min( 0 ).max( 1000 );
  130. tileOptions.add( params, 'maxDepth' ).min( 1 ).max( 100 );
  131. tileOptions.add( params, 'up', [ '+Y', '-Z' ] );
  132. tileOptions.open();
  133. const debug = gui.addFolder( 'Debug Options' );
  134. debug.add( params, 'displayBoxBounds' );
  135. debug.add( params, 'colorMode', {
  136. DEFAULT: 0,
  137. SCREEN_ERROR: 1,
  138. GEOMETRIC_ERROR: 2,
  139. DISTANCE: 3,
  140. DEPTH: 4,
  141. IS_LEAF: 5.
  142. } ).onChange( function ( v ) {
  143. tiles.colorMode = parseFloat( v );
  144. } );
  145. debug.open();
  146. gui.add( params, 'showThirdPerson' );
  147. gui.add( params, 'enableRaycast' );
  148. gui.add( params, 'enableCacheDisplay' );
  149. gui.add( params, 'reload' );
  150. gui.open();
  151. statsContainer = document.createElement( 'div' );
  152. statsContainer.style.position = 'absolute';
  153. statsContainer.style.top = 0;
  154. statsContainer.style.left = 0;
  155. statsContainer.style.color = 'white';
  156. statsContainer.style.width = '100%';
  157. statsContainer.style.textAlign = 'center';
  158. statsContainer.style.padding = '5px';
  159. statsContainer.style.pointerEvents = 'none';
  160. statsContainer.style.lineHeight = '1.5em';
  161. document.body.appendChild( statsContainer );
  162. // Stats
  163. stats = new Stats();
  164. stats.showPanel( 0 );
  165. document.body.appendChild( stats.dom );
  166. }
  167. function onWindowResize() {
  168. thirdPersonCamera.aspect = window.innerWidth / window.innerHeight;
  169. thirdPersonCamera.updateProjectionMatrix();
  170. thirdPersonRenderer.setSize( Math.floor( window.innerWidth / 3 ), Math.floor( window.innerHeight / 3 ) );
  171. camera.aspect = window.innerWidth / window.innerHeight;
  172. camera.updateProjectionMatrix();
  173. renderer.setSize( window.innerWidth, window.innerHeight );
  174. updateOrthoCamera();
  175. }
  176. function onMouseMove( e ) {
  177. mouse.x = ( e.clientX / window.innerWidth ) * 2 - 1;
  178. mouse.y = - ( e.clientY / window.innerHeight ) * 2 + 1;
  179. }
  180. const startPos = new Vector2();
  181. const endPos = new Vector2();
  182. function onMouseDown( e ) {
  183. startPos.set( e.clientX, e.clientY );
  184. }
  185. function onMouseUp( e ) {
  186. endPos.set( e.clientX, e.clientY );
  187. if ( startPos.distanceTo( endPos ) > 2 ) {
  188. return;
  189. }
  190. raycaster.setFromCamera( mouse, params.orthographic ? orthoCamera : camera );
  191. raycaster.firstHitOnly = true;
  192. const results = raycaster.intersectObject( tiles.group, true );
  193. if ( results.length ) {
  194. const object = results[ 0 ].object;
  195. const info = tiles.getTileInformationFromActiveObject( object );
  196. let str = '';
  197. for ( const key in info ) {
  198. let val = info[ key ];
  199. if ( typeof val === 'number' ) {
  200. val = Math.floor( val * 1e5 ) / 1e5;
  201. }
  202. let name = key;
  203. while ( name.length < 20 ) {
  204. name += ' ';
  205. }
  206. str += `${ name } : ${ val }\n`;
  207. }
  208. console.log( str );
  209. }
  210. }
  211. function updateOrthoCamera() {
  212. orthoCamera.position.copy( camera.position );
  213. orthoCamera.rotation.copy( camera.rotation );
  214. const scale = camera.position.distanceTo( controls.target ) / 2.0;
  215. const aspect = window.innerWidth / window.innerHeight;
  216. orthoCamera.left = - aspect * scale;
  217. orthoCamera.right = aspect * scale;
  218. orthoCamera.bottom = - scale;
  219. orthoCamera.top = scale;
  220. orthoCamera.near = camera.near;
  221. orthoCamera.far = camera.far;
  222. orthoCamera.updateProjectionMatrix();
  223. }
  224. function animate() {
  225. // update options
  226. tiles.errorTarget = params.errorTarget;
  227. tiles.errorThreshold = params.errorThreshold;
  228. tiles.loadSiblings = params.loadSiblings;
  229. tiles.maxDepth = params.maxDepth;
  230. tiles.camera = params.orthographic ? orthoCamera : camera;
  231. tiles.displayBoxBounds = params.displayBoxBounds;
  232. tiles.setResolutionFromRenderer( renderer );
  233. // update tiles
  234. tiles.update();
  235. window.tiles = tiles;
  236. offsetParent.rotation.set( 0, 0, 0 );
  237. if ( params.up === '-Z' ) {
  238. offsetParent.rotation.x = Math.PI / 2;
  239. }
  240. // update tiles center
  241. if ( tiles.getBounds( box ) ) {
  242. box.getCenter( tiles.group.position );
  243. tiles.group.position.multiplyScalar( - 1 );
  244. }
  245. if ( params.enableRaycast ) {
  246. raycaster.setFromCamera( mouse, params.orthographic ? orthoCamera : camera );
  247. raycaster.firstHitOnly = true;
  248. const results = raycaster.intersectObject( tiles.group, true );
  249. if ( results.length ) {
  250. const closestHit = results[ 0 ];
  251. const point = closestHit.point;
  252. rayIntersect.position.copy( point );
  253. // If the display bounds are visible they get intersected
  254. if ( closestHit.face ) {
  255. const normal = closestHit.face.normal;
  256. normal.transformDirection( closestHit.object.matrixWorld );
  257. rayIntersect.lookAt(
  258. point.x + normal.x,
  259. point.y + normal.y,
  260. point.z + normal.z
  261. );
  262. }
  263. if ( params.orthographic ) {
  264. rayIntersect.scale.setScalar( closestHit.distance / 150 );
  265. } else {
  266. rayIntersect.scale.setScalar( closestHit.distance * camera.fov / 6000 );
  267. }
  268. rayIntersect.visible = true;
  269. } else {
  270. rayIntersect.visible = false;
  271. }
  272. } else {
  273. rayIntersect.visible = false;
  274. }
  275. stats.begin();
  276. render();
  277. stats.end();
  278. requestAnimationFrame( animate );
  279. }
  280. function render() {
  281. updateOrthoCamera();
  282. // render primary view
  283. cameraHelper.visible = false;
  284. orthoCameraHelper.visible = false;
  285. renderer.render( scene, params.orthographic ? orthoCamera : camera );
  286. // render third person view
  287. thirdPersonRenderer.domElement.style.visibility = params.showThirdPerson ? 'visible' : 'hidden';
  288. if ( params.showThirdPerson ) {
  289. cameraHelper.update();
  290. cameraHelper.visible = ! params.orthographic;
  291. orthoCameraHelper.update();
  292. orthoCameraHelper.visible = params.orthographic;
  293. thirdPersonRenderer.render( scene, thirdPersonCamera );
  294. }
  295. const cacheFullness = tiles.lruCache.itemList.length / tiles.lruCache.minSize;
  296. let str = `Downloading: ${ tiles.stats.downloading } Parsing: ${ tiles.stats.parsing } Visible: ${ tiles.group.children.length - 2 }`;
  297. if ( params.enableCacheDisplay ) {
  298. const geomSet = new Set();
  299. tiles.traverse( tile => {
  300. const scene = tile.cached.scene;
  301. if ( scene ) {
  302. scene.traverse( c => {
  303. if ( c.geometry ) {
  304. geomSet.add( c.geometry );
  305. }
  306. } );
  307. }
  308. } );
  309. let count = 0;
  310. geomSet.forEach( g => {
  311. count += BufferGeometryUtils.estimateBytesUsed( g );
  312. } );
  313. str += `<br/>Cache: ${ ( 100 * cacheFullness ).toFixed( 2 ) }% ~${ ( count / 1000 / 1000 ).toFixed( 2 ) }mb`;
  314. }
  315. if ( statsContainer.innerHTML !== str ) {
  316. statsContainer.innerHTML = str
  317. }
  318. }