b3dmExample.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { B3DMLoader } from '../src/index.js';
  2. import {
  3. Scene,
  4. DirectionalLight,
  5. AmbientLight,
  6. WebGLRenderer,
  7. PerspectiveCamera,
  8. Box3,
  9. sRGBEncoding,
  10. PCFSoftShadowMap,
  11. Vector2,
  12. Raycaster,
  13. } from 'three';
  14. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  15. let camera, controls, scene, renderer;
  16. let box, dirLight;
  17. let raycaster, mouse;
  18. init();
  19. animate();
  20. function init() {
  21. scene = new Scene();
  22. // primary camera view
  23. renderer = new WebGLRenderer( { antialias: true } );
  24. renderer.setPixelRatio( window.devicePixelRatio );
  25. renderer.setSize( window.innerWidth, window.innerHeight );
  26. renderer.setClearColor( 0x151c1f );
  27. renderer.shadowMap.enabled = true;
  28. renderer.shadowMap.type = PCFSoftShadowMap;
  29. renderer.outputEncoding = sRGBEncoding;
  30. document.body.appendChild( renderer.domElement );
  31. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  32. camera.position.set( 400, 400, 400 );
  33. // controls
  34. controls = new OrbitControls( camera, renderer.domElement );
  35. controls.screenSpacePanning = false;
  36. controls.minDistance = 1;
  37. controls.maxDistance = 2000;
  38. // lights
  39. dirLight = new DirectionalLight( 0xffffff, 1.25 );
  40. dirLight.position.set( 1, 2, 3 ).multiplyScalar( 40 );
  41. dirLight.castShadow = true;
  42. dirLight.shadow.bias = - 0.01;
  43. dirLight.shadow.mapSize.setScalar( 2048 );
  44. const shadowCam = dirLight.shadow.camera;
  45. shadowCam.left = - 200;
  46. shadowCam.bottom = - 200;
  47. shadowCam.right = 200;
  48. shadowCam.top = 200;
  49. shadowCam.updateProjectionMatrix();
  50. scene.add( dirLight );
  51. const ambLight = new AmbientLight( 0xffffff, 0.05 );
  52. scene.add( ambLight );
  53. box = new Box3();
  54. new B3DMLoader()
  55. .load( 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/tilesets/TilesetWithRequestVolume/city/lr.b3dm' )
  56. .then( res => {
  57. console.log( res );
  58. scene.add( res.scene );
  59. } );
  60. raycaster = new Raycaster();
  61. mouse = new Vector2();
  62. onWindowResize();
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. renderer.domElement.addEventListener( 'mousemove', onMouseMove, false );
  65. }
  66. function onMouseMove( e ) {
  67. const bounds = this.getBoundingClientRect();
  68. mouse.x = e.clientX - bounds.x;
  69. mouse.y = e.clientY - bounds.y;
  70. mouse.x = ( mouse.x / bounds.width ) * 2 - 1;
  71. mouse.y = - ( mouse.y / bounds.height ) * 2 + 1;
  72. raycaster.setFromCamera( mouse, camera );
  73. const intersects = raycaster.intersectObject( scene, true );
  74. if ( intersects.length ) {
  75. const { face, object } = intersects[ 0 ];
  76. const batchid = object.geometry.getAttribute( '_batchid' );
  77. if ( batchid ) {
  78. console.log( '_batchid', batchid.getX( face.a ), batchid.getX( face.b ), batchid.getX( face.c ) );
  79. }
  80. }
  81. }
  82. function onWindowResize() {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. camera.updateProjectionMatrix();
  87. }
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. render();
  91. }
  92. function render() {
  93. renderer.render( scene, camera );
  94. }