b3dmExample.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { B3DMLoader } from '../src/index.js';
  2. import {
  3. Scene,
  4. DirectionalLight,
  5. AmbientLight,
  6. WebGLRenderer,
  7. PerspectiveCamera,
  8. Box3,
  9. OrthographicCamera,
  10. sRGBEncoding,
  11. Group,
  12. PCFSoftShadowMap,
  13. } from 'three';
  14. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  15. import * as dat from 'three/examples/jsm/libs/dat.gui.module.js';
  16. let camera, controls, scene, renderer, tiles, orthoCamera;
  17. let offsetParent, box, dirLight;
  18. let stats;
  19. const params = {
  20. };
  21. init();
  22. animate();
  23. function init() {
  24. scene = new Scene();
  25. // primary camera view
  26. renderer = new WebGLRenderer( { antialias: true } );
  27. renderer.setPixelRatio( window.devicePixelRatio );
  28. renderer.setSize( window.innerWidth, window.innerHeight );
  29. renderer.setClearColor( 0x151c1f );
  30. renderer.shadowMap.enabled = true;
  31. renderer.shadowMap.type = PCFSoftShadowMap;
  32. renderer.outputEncoding = sRGBEncoding;
  33. document.body.appendChild( renderer.domElement );
  34. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  35. camera.position.set( 400, 400, 400 );
  36. orthoCamera = new OrthographicCamera();
  37. // controls
  38. controls = new OrbitControls( camera, renderer.domElement );
  39. controls.screenSpacePanning = false;
  40. controls.minDistance = 1;
  41. controls.maxDistance = 2000;
  42. // lights
  43. dirLight = new DirectionalLight( 0xffffff, 1.25 );
  44. dirLight.position.set( 1, 2, 3 ).multiplyScalar( 40 );
  45. dirLight.castShadow = true;
  46. dirLight.shadow.bias = - 0.01;
  47. dirLight.shadow.mapSize.setScalar( 2048 );
  48. const shadowCam = dirLight.shadow.camera;
  49. shadowCam.left = - 200;
  50. shadowCam.bottom = - 200;
  51. shadowCam.right = 200;
  52. shadowCam.top = 200;
  53. shadowCam.updateProjectionMatrix();
  54. scene.add( dirLight );
  55. const ambLight = new AmbientLight( 0xffffff, 0.05 );
  56. scene.add( ambLight );
  57. box = new Box3();
  58. new B3DMLoader()
  59. .load( 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/tilesets/TilesetWithRequestVolume/city/lr.b3dm' )
  60. .then( res => {
  61. console.log( res );
  62. scene.add( res.scene );
  63. } );
  64. onWindowResize();
  65. window.addEventListener( 'resize', onWindowResize, false );
  66. // GUI
  67. const gui = new dat.GUI();
  68. gui.width = 300;
  69. gui.open();
  70. }
  71. function onWindowResize() {
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. camera.updateProjectionMatrix();
  76. updateOrthoCamera();
  77. }
  78. function updateOrthoCamera() {
  79. orthoCamera.position.copy( camera.position );
  80. orthoCamera.rotation.copy( camera.rotation );
  81. const scale = camera.position.distanceTo( controls.target ) / 2.0;
  82. let aspect = window.innerWidth / window.innerHeight;
  83. orthoCamera.left = - aspect * scale;
  84. orthoCamera.right = aspect * scale;
  85. orthoCamera.bottom = - scale;
  86. orthoCamera.top = scale;
  87. orthoCamera.near = camera.near;
  88. orthoCamera.far = camera.far;
  89. orthoCamera.updateProjectionMatrix();
  90. }
  91. function animate() {
  92. requestAnimationFrame( animate );
  93. render();
  94. }
  95. function render() {
  96. updateOrthoCamera();
  97. renderer.render( scene, params.orthographic ? orthoCamera : camera );
  98. }