i3dmExample.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { I3DMLoader } from '../src/index.js';
  2. import {
  3. Scene,
  4. DirectionalLight,
  5. AmbientLight,
  6. WebGLRenderer,
  7. PerspectiveCamera,
  8. sRGBEncoding,
  9. PCFSoftShadowMap,
  10. Vector3,
  11. Quaternion,
  12. Matrix4,
  13. } from 'three';
  14. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  15. let camera, controls, scene, renderer;
  16. let 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( 100, 100, 100 );
  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. new I3DMLoader()
  53. .load( 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/main/1.0/TilesetWithTreeBillboards/tree.i3dm' )
  54. .then( res => {
  55. let instance = null;
  56. res.scene.traverse( c => {
  57. if ( ! instance && c.isInstancedMesh ) {
  58. instance = c;
  59. }
  60. } );
  61. if ( instance ) {
  62. res.scene.updateMatrixWorld( true );
  63. const pos = new Vector3();
  64. const quat = new Quaternion();
  65. const sca = new Vector3();
  66. const mat = new Matrix4();
  67. const averagePos = new Vector3();
  68. for ( let i = 0, l = instance.count; i < l; i ++ ) {
  69. instance.getMatrixAt( i, mat );
  70. mat.premultiply( instance.matrixWorld );
  71. mat.decompose( pos, quat, sca );
  72. averagePos.add( pos );
  73. }
  74. averagePos.divideScalar( instance.count );
  75. controls.target.copy( averagePos );
  76. camera.position.add( averagePos );
  77. controls.update();
  78. }
  79. console.log( res );
  80. scene.add( res.scene );
  81. } );
  82. onWindowResize();
  83. window.addEventListener( 'resize', onWindowResize, false );
  84. }
  85. function onWindowResize() {
  86. camera.aspect = window.innerWidth / window.innerHeight;
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. camera.updateProjectionMatrix();
  90. }
  91. function animate() {
  92. requestAnimationFrame( animate );
  93. render();
  94. }
  95. function render() {
  96. renderer.render( scene, camera );
  97. }