b3dmExample.js 5.4 KB

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