misc_exporter_ply.html 5.2 KB

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