misc_exporter_ply.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - ply</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> webgl - exporter - ply
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  24. import { PLYExporter } from 'three/addons/exporters/PLYExporter.js';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. let scene, camera, renderer, exporter, mesh;
  27. const params = {
  28. exportASCII: exportASCII,
  29. exportBinaryBigEndian: exportBinaryBigEndian,
  30. exportBinaryLittleEndian: exportBinaryLittleEndian
  31. };
  32. init();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  35. camera.position.set( 4, 2, 4 );
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0xa0a0a0 );
  38. scene.fog = new THREE.Fog( 0xa0a0a0, 4, 20 );
  39. exporter = new PLYExporter();
  40. //
  41. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444, 3 );
  42. hemiLight.position.set( 0, 20, 0 );
  43. scene.add( hemiLight );
  44. const directionalLight = new THREE.DirectionalLight( 0xffffff, 3 );
  45. directionalLight.position.set( 0, 20, 10 );
  46. directionalLight.castShadow = true;
  47. directionalLight.shadow.camera.top = 2;
  48. directionalLight.shadow.camera.bottom = - 2;
  49. directionalLight.shadow.camera.left = - 2;
  50. directionalLight.shadow.camera.right = 2;
  51. scene.add( directionalLight );
  52. // ground
  53. const ground = new THREE.Mesh( new THREE.PlaneGeometry( 40, 40 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
  54. ground.rotation.x = - Math.PI / 2;
  55. ground.receiveShadow = true;
  56. scene.add( ground );
  57. const grid = new THREE.GridHelper( 40, 20, 0x000000, 0x000000 );
  58. grid.material.opacity = 0.2;
  59. grid.material.transparent = true;
  60. scene.add( grid );
  61. // export mesh
  62. const geometry = new THREE.BoxGeometry();
  63. const material = new THREE.MeshPhongMaterial( { vertexColors: true } );
  64. // color vertices based on vertex positions
  65. const colors = geometry.getAttribute( 'position' ).array.slice();
  66. for ( let i = 0, l = colors.length; i < l; i ++ ) {
  67. if ( colors[ i ] > 0 ) colors[ i ] = 0.5;
  68. else colors[ i ] = 0;
  69. }
  70. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3, false ) );
  71. mesh = new THREE.Mesh( geometry, material );
  72. mesh.castShadow = true;
  73. mesh.position.y = 0.5;
  74. scene.add( mesh );
  75. //
  76. renderer = new THREE.WebGLRenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. renderer.setAnimationLoop( animate );
  80. renderer.shadowMap.enabled = true;
  81. document.body.appendChild( renderer.domElement );
  82. //
  83. const controls = new OrbitControls( camera, renderer.domElement );
  84. controls.target.set( 0, 0.5, 0 );
  85. controls.update();
  86. //
  87. window.addEventListener( 'resize', onWindowResize );
  88. const gui = new GUI();
  89. gui.add( params, 'exportASCII' ).name( 'Export PLY (ASCII)' );
  90. gui.add( params, 'exportBinaryBigEndian' ).name( 'Export PLY (Binary BE)' );
  91. gui.add( params, 'exportBinaryLittleEndian' ).name( 'Export PLY (Binary LE)' );
  92. gui.open();
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. function animate() {
  100. renderer.render( scene, camera );
  101. }
  102. function exportASCII() {
  103. exporter.parse( mesh, function ( result ) {
  104. saveString( result, 'box.ply' );
  105. } );
  106. }
  107. function exportBinaryBigEndian() {
  108. exporter.parse( mesh, function ( result ) {
  109. saveArrayBuffer( result, 'box.ply' );
  110. }, { binary: true } );
  111. }
  112. function exportBinaryLittleEndian() {
  113. exporter.parse( mesh, function ( result ) {
  114. saveArrayBuffer( result, 'box.ply' );
  115. }, { binary: true, littleEndian: true } );
  116. }
  117. const link = document.createElement( 'a' );
  118. link.style.display = 'none';
  119. document.body.appendChild( link );
  120. function save( blob, filename ) {
  121. link.href = URL.createObjectURL( blob );
  122. link.download = filename;
  123. link.click();
  124. }
  125. function saveString( text, filename ) {
  126. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  127. }
  128. function saveArrayBuffer( buffer, filename ) {
  129. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  130. }
  131. </script>
  132. </body>
  133. </html>