b3dmExample.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. ShaderLib,
  14. UniformsUtils,
  15. ShaderMaterial,
  16. Color,
  17. } from 'three';
  18. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  19. let camera, controls, scene, renderer;
  20. let box, dirLight;
  21. let raycaster, mouse;
  22. let model;
  23. init();
  24. animate();
  25. // Adjusts the three.js standard shader to include batchid highlight
  26. function batchIdHighlightShaderMixin( shader ) {
  27. const newShader = { ...shader };
  28. newShader.uniforms = {
  29. highlightedBatchId: { value: - 1 },
  30. highlightColor: { value: new Color( 0xFFC107 ).convertSRGBToLinear() },
  31. ...UniformsUtils.clone( shader.uniforms ),
  32. };
  33. newShader.extensions = {
  34. derivatives: true,
  35. };
  36. newShader.lights = true;
  37. newShader.vertexShader =
  38. `
  39. attribute float _batchid;
  40. varying float batchid;
  41. ` +
  42. newShader.vertexShader.replace(
  43. /#include <uv_vertex>/,
  44. `
  45. #include <uv_vertex>
  46. batchid = _batchid;
  47. `
  48. );
  49. newShader.fragmentShader =
  50. `
  51. varying float batchid;
  52. uniform float highlightedBatchId;
  53. uniform vec3 highlightColor;
  54. ` +
  55. newShader.fragmentShader.replace(
  56. /vec4 diffuseColor = vec4\( diffuse, opacity \);/,
  57. `
  58. vec4 diffuseColor =
  59. abs( batchid - highlightedBatchId ) < 0.5 ?
  60. vec4( highlightColor, opacity ) :
  61. vec4( diffuse, opacity );
  62. `
  63. );
  64. return newShader;
  65. }
  66. function init() {
  67. scene = new Scene();
  68. // primary camera view
  69. renderer = new WebGLRenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setClearColor( 0x151c1f );
  73. renderer.shadowMap.enabled = true;
  74. renderer.shadowMap.type = PCFSoftShadowMap;
  75. renderer.outputEncoding = sRGBEncoding;
  76. document.body.appendChild( renderer.domElement );
  77. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  78. camera.position.set( 400, 400, 400 );
  79. // controls
  80. controls = new OrbitControls( camera, renderer.domElement );
  81. controls.screenSpacePanning = false;
  82. controls.minDistance = 1;
  83. controls.maxDistance = 2000;
  84. // lights
  85. dirLight = new DirectionalLight( 0xffffff, 1.25 );
  86. dirLight.position.set( 1, 2, 3 ).multiplyScalar( 40 );
  87. dirLight.castShadow = true;
  88. dirLight.shadow.bias = - 0.01;
  89. dirLight.shadow.mapSize.setScalar( 2048 );
  90. const shadowCam = dirLight.shadow.camera;
  91. shadowCam.left = - 200;
  92. shadowCam.bottom = - 200;
  93. shadowCam.right = 200;
  94. shadowCam.top = 200;
  95. shadowCam.updateProjectionMatrix();
  96. scene.add( dirLight );
  97. const ambLight = new AmbientLight( 0xffffff, 0.05 );
  98. scene.add( ambLight );
  99. box = new Box3();
  100. new B3DMLoader()
  101. .load( 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/tilesets/TilesetWithRequestVolume/city/lr.b3dm' )
  102. .then( res => {
  103. console.log( res );
  104. model = res.scene;
  105. scene.add( res.scene );
  106. // reassign the material to use the batchid highlight variant.
  107. // in practice this should copy over any needed uniforms from the
  108. // original material.
  109. res.scene.traverse( c => {
  110. if ( c.isMesh ) {
  111. c.material = new ShaderMaterial( batchIdHighlightShaderMixin( ShaderLib.standard ) );
  112. }
  113. } );
  114. } );
  115. raycaster = new Raycaster();
  116. mouse = new Vector2();
  117. onWindowResize();
  118. window.addEventListener( 'resize', onWindowResize, false );
  119. renderer.domElement.addEventListener( 'mousemove', onMouseMove, false );
  120. }
  121. function onMouseMove( e ) {
  122. const bounds = this.getBoundingClientRect();
  123. mouse.x = e.clientX - bounds.x;
  124. mouse.y = e.clientY - bounds.y;
  125. mouse.x = ( mouse.x / bounds.width ) * 2 - 1;
  126. mouse.y = - ( mouse.y / bounds.height ) * 2 + 1;
  127. raycaster.setFromCamera( mouse, camera );
  128. // Get the batch table data
  129. const intersects = raycaster.intersectObject( scene, true );
  130. let hoveredBatchid = - 1;
  131. if ( intersects.length ) {
  132. const { face, object } = intersects[ 0 ];
  133. const batchidAttr = object.geometry.getAttribute( '_batchid' );
  134. if ( batchidAttr ) {
  135. // Traverse the parents to find the batch table.
  136. let batchTableObject = object;
  137. while ( ! batchTableObject.batchTable ) {
  138. batchTableObject = batchTableObject.parent;
  139. }
  140. // Log the batch data
  141. const batchTable = batchTableObject.batchTable;
  142. hoveredBatchid = batchidAttr.getX( face.a );
  143. console.log( '_batchid', hoveredBatchid );
  144. console.log( 'Latitude', batchTable.getData( 'Latitude' )[ hoveredBatchid ] );
  145. console.log( 'Longitude', batchTable.getData( 'Longitude' )[ hoveredBatchid ] );
  146. console.log( 'Height', batchTable.getData( 'Height' )[ hoveredBatchid ] );
  147. }
  148. }
  149. if ( model ) {
  150. model.traverse( c => {
  151. if ( c.isMesh ) {
  152. c.material.uniforms.highlightedBatchId.value = hoveredBatchid;
  153. }
  154. } );
  155. }
  156. }
  157. function onWindowResize() {
  158. camera.aspect = window.innerWidth / window.innerHeight;
  159. renderer.setPixelRatio( window.devicePixelRatio );
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. camera.updateProjectionMatrix();
  162. }
  163. function animate() {
  164. requestAnimationFrame( animate );
  165. render();
  166. }
  167. function render() {
  168. renderer.render( scene, camera );
  169. }