b3dmExample.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. PCFSoftShadowMap,
  12. } from 'three';
  13. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  14. import * as dat from 'three/examples/jsm/libs/dat.gui.module.js';
  15. let camera, controls, scene, renderer;
  16. let box, dirLight;
  17. init();
  18. animate();
  19. function init() {
  20. scene = new Scene();
  21. // primary camera view
  22. renderer = new WebGLRenderer( { antialias: true } );
  23. renderer.setPixelRatio( window.devicePixelRatio );
  24. renderer.setSize( window.innerWidth, window.innerHeight );
  25. renderer.setClearColor( 0x151c1f );
  26. renderer.shadowMap.enabled = true;
  27. renderer.shadowMap.type = PCFSoftShadowMap;
  28. renderer.outputEncoding = sRGBEncoding;
  29. document.body.appendChild( renderer.domElement );
  30. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  31. camera.position.set( 400, 400, 400 );
  32. // controls
  33. controls = new OrbitControls( camera, renderer.domElement );
  34. controls.screenSpacePanning = false;
  35. controls.minDistance = 1;
  36. controls.maxDistance = 2000;
  37. // lights
  38. dirLight = new DirectionalLight( 0xffffff, 1.25 );
  39. dirLight.position.set( 1, 2, 3 ).multiplyScalar( 40 );
  40. dirLight.castShadow = true;
  41. dirLight.shadow.bias = - 0.01;
  42. dirLight.shadow.mapSize.setScalar( 2048 );
  43. const shadowCam = dirLight.shadow.camera;
  44. shadowCam.left = - 200;
  45. shadowCam.bottom = - 200;
  46. shadowCam.right = 200;
  47. shadowCam.top = 200;
  48. shadowCam.updateProjectionMatrix();
  49. scene.add( dirLight );
  50. const ambLight = new AmbientLight( 0xffffff, 0.05 );
  51. scene.add( ambLight );
  52. box = new Box3();
  53. new B3DMLoader()
  54. .load( 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/tilesets/TilesetWithRequestVolume/city/lr.b3dm' )
  55. .then( res => {
  56. console.log( res );
  57. scene.add( res.scene );
  58. } );
  59. onWindowResize();
  60. window.addEventListener( 'resize', onWindowResize, false );
  61. // GUI
  62. const gui = new dat.GUI();
  63. gui.width = 300;
  64. gui.open();
  65. }
  66. function onWindowResize() {
  67. camera.aspect = window.innerWidth / window.innerHeight;
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. camera.updateProjectionMatrix();
  71. }
  72. function animate() {
  73. requestAnimationFrame( animate );
  74. render();
  75. }
  76. function render() {
  77. updateOrthoCamera();
  78. renderer.render( scene, camera );
  79. }