webgl_loader_pcd.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - PCD</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
  12. <a href="http://pointclouds.org/documentation/tutorials/pcd_file_format.php#pcd-file-format" target="_blank" rel="noopener">PCD File format</a>
  13. <div>PCD loader test by <a href="http://filipecaixeta.com.br" target="_blank" rel="noopener">Filipe Caixeta</a></div>
  14. <div>+/-: Increase/Decrease point size</div>
  15. <div>c: Change color</div>
  16. </div>
  17. <script type="module">
  18. import * as THREE from '../build/three.module.js';
  19. import Stats from './jsm/libs/stats.module.js';
  20. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  21. import { PCDLoader } from './jsm/loaders/PCDLoader.js';
  22. var container, stats;
  23. var camera, controls, scene, renderer;
  24. init();
  25. animate();
  26. function init() {
  27. scene = new THREE.Scene();
  28. scene.background = new THREE.Color( 0x000000 );
  29. camera = new THREE.PerspectiveCamera( 15, window.innerWidth / window.innerHeight, 0.01, 40 );
  30. camera.position.x = 0.4;
  31. camera.position.z = - 2;
  32. camera.up.set( 0, 0, 1 );
  33. scene.add( camera );
  34. renderer = new THREE.WebGLRenderer( { antialias: true } );
  35. renderer.setPixelRatio( window.devicePixelRatio );
  36. renderer.setSize( window.innerWidth, window.innerHeight );
  37. document.body.appendChild( renderer.domElement );
  38. var loader = new PCDLoader();
  39. loader.load( './models/pcd/binary/Zaghetto.pcd', function ( points ) {
  40. scene.add( points );
  41. var center = points.geometry.boundingSphere.center;
  42. controls.target.set( center.x, center.y, center.z );
  43. controls.update();
  44. } );
  45. container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. container.appendChild( renderer.domElement );
  48. controls = new TrackballControls( camera, renderer.domElement );
  49. controls.rotateSpeed = 2.0;
  50. controls.zoomSpeed = 0.3;
  51. controls.panSpeed = 0.2;
  52. controls.staticMoving = true;
  53. controls.minDistance = 0.3;
  54. controls.maxDistance = 0.3 * 100;
  55. stats = new Stats();
  56. container.appendChild( stats.dom );
  57. window.addEventListener( 'resize', onWindowResize, false );
  58. window.addEventListener( 'keypress', keyboard );
  59. }
  60. function onWindowResize() {
  61. camera.aspect = window.innerWidth / window.innerHeight;
  62. camera.updateProjectionMatrix();
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. controls.handleResize();
  65. }
  66. function keyboard( ev ) {
  67. var points = scene.getObjectByName( 'Zaghetto.pcd' );
  68. switch ( ev.key || String.fromCharCode( ev.keyCode || ev.charCode ) ) {
  69. case '+':
  70. points.material.size *= 1.2;
  71. points.material.needsUpdate = true;
  72. break;
  73. case '-':
  74. points.material.size /= 1.2;
  75. points.material.needsUpdate = true;
  76. break;
  77. case 'c':
  78. points.material.color.setHex( Math.random() * 0xffffff );
  79. points.material.needsUpdate = true;
  80. break;
  81. }
  82. }
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. controls.update();
  86. renderer.render( scene, camera );
  87. stats.update();
  88. }
  89. </script>
  90. </body>
  91. </html>