b3dmExample.js 5.3 KB

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