misc_exporter_ply.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. animate();
  34. function init() {
  35. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  36. camera.position.set( 4, 2, 4 );
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0xa0a0a0 );
  39. scene.fog = new THREE.Fog( 0xa0a0a0, 4, 20 );
  40. exporter = new PLYExporter();
  41. //
  42. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444, 3 );
  43. hemiLight.position.set( 0, 20, 0 );
  44. scene.add( hemiLight );
  45. const directionalLight = new THREE.DirectionalLight( 0xffffff, 3 );
  46. directionalLight.position.set( 0, 20, 10 );
  47. directionalLight.castShadow = true;
  48. directionalLight.shadow.camera.top = 2;
  49. directionalLight.shadow.camera.bottom = - 2;
  50. directionalLight.shadow.camera.left = - 2;
  51. directionalLight.shadow.camera.right = 2;
  52. scene.add( directionalLight );
  53. // ground
  54. const ground = new THREE.Mesh( new THREE.PlaneGeometry( 40, 40 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
  55. ground.rotation.x = - Math.PI / 2;
  56. ground.receiveShadow = true;
  57. scene.add( ground );
  58. const grid = new THREE.GridHelper( 40, 20, 0x000000, 0x000000 );
  59. grid.material.opacity = 0.2;
  60. grid.material.transparent = true;
  61. scene.add( grid );
  62. // export mesh
  63. const geometry = new THREE.BoxGeometry();
  64. const material = new THREE.MeshPhongMaterial( { vertexColors: true } );
  65. // color vertices based on vertex positions
  66. const colors = geometry.getAttribute( 'position' ).array.slice();
  67. for ( let i = 0, l = colors.length; i < l; i ++ ) {
  68. if ( colors[ i ] > 0 ) colors[ i ] = 0.5;
  69. else colors[ i ] = 0;
  70. }
  71. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3, false ) );
  72. mesh = new THREE.Mesh( geometry, material );
  73. mesh.castShadow = true;
  74. mesh.position.y = 0.5;
  75. scene.add( mesh );
  76. //
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  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. requestAnimationFrame( animate );
  101. renderer.render( scene, camera );
  102. }
  103. function exportASCII() {
  104. exporter.parse( mesh, function ( result ) {
  105. saveString( result, 'box.ply' );
  106. } );
  107. }
  108. function exportBinaryBigEndian() {
  109. exporter.parse( mesh, function ( result ) {
  110. saveArrayBuffer( result, 'box.ply' );
  111. }, { binary: true } );
  112. }
  113. function exportBinaryLittleEndian() {
  114. exporter.parse( mesh, function ( result ) {
  115. saveArrayBuffer( result, 'box.ply' );
  116. }, { binary: true, littleEndian: true } );
  117. }
  118. const link = document.createElement( 'a' );
  119. link.style.display = 'none';
  120. document.body.appendChild( link );
  121. function save( blob, filename ) {
  122. link.href = URL.createObjectURL( blob );
  123. link.download = filename;
  124. link.click();
  125. }
  126. function saveString( text, filename ) {
  127. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  128. }
  129. function saveArrayBuffer( buffer, filename ) {
  130. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  131. }
  132. </script>
  133. </body>
  134. </html>