b3dmExample.js 5.4 KB

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