index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { ThreeTilesRenderer } from '../src/ThreeTilesRenderer.js';
  2. import { Scene, DirectionalLight, AmbientLight, WebGLRenderer, PerspectiveCamera, CameraHelper, Box3, Raycaster, Vector2, Ray, Mesh, CylinderBufferGeometry, MeshBasicMaterial, Group } from 'three';
  3. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  4. import * as dat from 'three/examples/jsm/libs/dat.gui.module.js';
  5. import Stats from 'three/examples/jsm/libs/stats.module.js';
  6. let camera, controls, scene, renderer, tiles, cameraHelper;
  7. let thirdPersonCamera, thirdPersonRenderer, thirdPersonControls;
  8. let box;
  9. let raycaster, mouse, rayIntersect;
  10. let offsetParent;
  11. let statsContainer, stats;
  12. let params = {
  13. 'errorTarget': 6,
  14. 'errorThreshold': 60,
  15. 'maxDepth': 15,
  16. 'loadSiblings': true,
  17. 'up': '+Y',
  18. 'displayBounds': false,
  19. 'showThirdPerson': true,
  20. 'reload': reinstantiateTiles,
  21. };
  22. init();
  23. animate();
  24. function reinstantiateTiles() {
  25. const url = window.location.hash.replace( /^#/, '' ) || './SampleTileset/tileset.json';
  26. if ( tiles ) {
  27. offsetParent.remove( tiles.group );
  28. }
  29. tiles = new ThreeTilesRenderer( url, camera, renderer );
  30. offsetParent.add( tiles.group );
  31. }
  32. function init() {
  33. // Third person camera view
  34. thirdPersonCamera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 3000 );
  35. thirdPersonCamera.position.set( 50, 40, 40 );
  36. thirdPersonCamera.lookAt( 0, 0, 0 );
  37. thirdPersonRenderer = new WebGLRenderer( { antialias: true } );
  38. thirdPersonRenderer.setPixelRatio( window.devicePixelRatio );
  39. thirdPersonRenderer.setSize( window.innerWidth, window.innerHeight );
  40. thirdPersonRenderer.setClearColor( 0xdddddd );
  41. document.body.appendChild( thirdPersonRenderer.domElement );
  42. thirdPersonRenderer.domElement.style.position = 'fixed';
  43. thirdPersonRenderer.domElement.style.left = '5px';
  44. thirdPersonRenderer.domElement.style.bottom = '5px';
  45. thirdPersonControls = new OrbitControls( thirdPersonCamera, thirdPersonRenderer.domElement );
  46. thirdPersonControls.screenSpacePanning = false;
  47. thirdPersonControls.minDistance = 1;
  48. thirdPersonControls.maxDistance = 2000;
  49. // primary camera view
  50. scene = new Scene();
  51. renderer = new WebGLRenderer( { antialias: true } );
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. renderer.setClearColor( 0xcccccc );
  55. renderer.gammaInput = true;
  56. renderer.gameOutput = true;
  57. document.body.appendChild( renderer.domElement );
  58. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
  59. camera.position.set( 100, 100, 100 );
  60. cameraHelper = new CameraHelper( camera );
  61. scene.add( cameraHelper );
  62. // controls
  63. controls = new OrbitControls( camera, renderer.domElement );
  64. controls.screenSpacePanning = false;
  65. controls.minDistance = 1;
  66. controls.maxDistance = 2000;
  67. // lights
  68. var dirLight = new DirectionalLight( 0xffffff );
  69. dirLight.position.set( 1, 1, 1 );
  70. scene.add( dirLight );
  71. var ambLight = new AmbientLight( 0x222222 );
  72. scene.add( ambLight );
  73. box = new Box3();
  74. raycaster = new Raycaster();
  75. mouse = new Vector2();
  76. rayIntersect = new Mesh( new CylinderBufferGeometry( 0.25, 0.25, 5 ), new MeshBasicMaterial( { color: 0xff0000 } ) );
  77. scene.add( rayIntersect );
  78. offsetParent = new Group();
  79. scene.add( offsetParent );
  80. reinstantiateTiles();
  81. onWindowResize();
  82. window.addEventListener( 'resize', onWindowResize, false );
  83. window.addEventListener( 'mousemove', onMouseMove, false );
  84. // GUI
  85. const gui = new dat.GUI();
  86. const tiles = gui.addFolder( 'Tiles Options' );
  87. tiles.add( params, 'loadSiblings' );
  88. tiles.add( params, 'errorTarget' ).min( 0 ).max( 50 );
  89. tiles.add( params, 'errorThreshold' ).min( 0 ).max( 1000 );
  90. tiles.add( params, 'maxDepth' ).min( 1 ).max( 100 );
  91. tiles.add( params, 'up', [ '+Y', '-Z' ] );
  92. tiles.open();
  93. gui.add( params, 'displayBounds' );
  94. gui.add( params, 'showThirdPerson' );
  95. gui.add( params, 'reload' );
  96. gui.open();
  97. // TilesRenderer stats display
  98. let textShadow = [];
  99. for ( let x = - 1; x <= 1; x ++ ) {
  100. for ( let y = - 1; y <= 1; y ++ ) {
  101. let valX = x;
  102. let valY = y;
  103. if ( valX !== 0 && valY !== 0 ) {
  104. valX *= Math.cos( Math.PI / 4 );
  105. valY *= Math.sin( Math.PI / 4 );
  106. }
  107. valX *= 1.5;
  108. valY *= 1.5;
  109. textShadow.push( `white ${ valX }px ${ valY }px 0` );
  110. }
  111. }
  112. statsContainer = document.createElement( 'div' );
  113. statsContainer.style.position = 'absolute';
  114. statsContainer.style.top = 0;
  115. statsContainer.style.left = 0;
  116. statsContainer.style.width = '100%';
  117. statsContainer.style.textAlign = 'center';
  118. statsContainer.style.textShadow = textShadow.join( ',' );
  119. statsContainer.style.padding = '10px';
  120. document.body.appendChild( statsContainer );
  121. // Stats
  122. stats = new Stats();
  123. stats.showPanel( 0 );
  124. document.body.appendChild( stats.dom );
  125. }
  126. function onWindowResize() {
  127. thirdPersonCamera.aspect = window.innerWidth / window.innerHeight;
  128. thirdPersonCamera.updateProjectionMatrix();
  129. thirdPersonRenderer.setSize( Math.floor( window.innerWidth / 3 ), Math.floor( window.innerHeight / 3 ) );
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. function onMouseMove( e ) {
  135. mouse.x = ( e.clientX / window.innerWidth ) * 2 - 1;
  136. mouse.y = - ( e.clientY / window.innerHeight ) * 2 + 1;
  137. raycaster.setFromCamera( mouse, camera );
  138. const results = raycaster.intersectObject( tiles.group, true );
  139. if ( results.length ) {
  140. const closestHit = results[ 0 ];
  141. rayIntersect.position.copy( closestHit.point );
  142. closestHit.face.normal.add( closestHit.point );
  143. rayIntersect.lookAt( closestHit.face.normal );
  144. }
  145. }
  146. function animate() {
  147. // update options
  148. tiles.errorTarget = params.errorTarget;
  149. tiles.errorThreshold = params.errorThreshold;
  150. tiles.loadSiblings = params.loadSiblings;
  151. tiles.maxDepth = params.maxDepth;
  152. tiles.displayBounds = params.displayBounds;
  153. // update tiles
  154. tiles.update();
  155. window.tiles = tiles;
  156. offsetParent.rotation.set( 0, 0, 0 );
  157. if ( params.up === '-Z' ) {
  158. offsetParent.rotation.x = Math.PI / 2;
  159. }
  160. // update tiles center
  161. if ( tiles.getBounds( box ) ) {
  162. box.getCenter( tiles.group.position );
  163. tiles.group.position.multiplyScalar( - 1 );
  164. }
  165. stats.begin();
  166. render();
  167. stats.end();
  168. requestAnimationFrame( animate );
  169. }
  170. function render() {
  171. // render primary view
  172. cameraHelper.visible = false;
  173. renderer.render( scene, camera );
  174. // render third person view
  175. thirdPersonRenderer.domElement.style.visibility = params.showThirdPerson ? 'visible' : 'hidden';
  176. if ( params.showThirdPerson ) {
  177. cameraHelper.visible = true;
  178. thirdPersonRenderer.render( scene, thirdPersonCamera );
  179. }
  180. statsContainer.innerText =
  181. `Downloading: ${tiles.stats.downloading} Parsing: ${tiles.stats.parsing} Visible: ${tiles.group.children.length}`;
  182. }