2
0

misc_exporter_ply.html 5.4 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<br/><br/>
  12. <button id="exportASCII">export ASCII</button> <button id="exportBinaryBigEndian">export binary (Big Endian)</button> <button id="exportBinaryLittleEndian">export binary (Little Endian)</button>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  27. import { PLYExporter } from './jsm/exporters/PLYExporter.js';
  28. let scene, camera, renderer, exporter, mesh;
  29. init();
  30. animate();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  33. camera.position.set( 200, 100, 200 );
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0xa0a0a0 );
  36. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  37. exporter = new PLYExporter();
  38. //
  39. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  40. hemiLight.position.set( 0, 200, 0 );
  41. scene.add( hemiLight );
  42. const directionalLight = new THREE.DirectionalLight( 0xffffff );
  43. directionalLight.position.set( 0, 200, 100 );
  44. directionalLight.castShadow = true;
  45. directionalLight.shadow.camera.top = 180;
  46. directionalLight.shadow.camera.bottom = - 100;
  47. directionalLight.shadow.camera.left = - 120;
  48. directionalLight.shadow.camera.right = 120;
  49. scene.add( directionalLight );
  50. // ground
  51. const ground = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  52. ground.rotation.x = - Math.PI / 2;
  53. ground.receiveShadow = true;
  54. scene.add( ground );
  55. const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  56. grid.material.opacity = 0.2;
  57. grid.material.transparent = true;
  58. scene.add( grid );
  59. // export mesh
  60. const geometry = new THREE.BoxGeometry( 50, 50, 50 );
  61. const material = new THREE.MeshPhongMaterial( { vertexColors: true } );
  62. // color vertices based on vertex positions
  63. const colors = geometry.getAttribute( 'position' ).array.slice();
  64. for ( let i = 0, l = colors.length; i < l; i ++ ) {
  65. if ( colors[ i ] > 0 ) colors[ i ] = 0.5;
  66. else colors[ i ] = 0;
  67. }
  68. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3, false ) );
  69. mesh = new THREE.Mesh( geometry, material );
  70. mesh.castShadow = true;
  71. mesh.position.y = 25;
  72. scene.add( mesh );
  73. //
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.outputEncoding = THREE.sRGBEncoding;
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. renderer.shadowMap.enabled = true;
  79. document.body.appendChild( renderer.domElement );
  80. //
  81. const controls = new OrbitControls( camera, renderer.domElement );
  82. controls.target.set( 0, 25, 0 );
  83. controls.update();
  84. //
  85. window.addEventListener( 'resize', onWindowResize );
  86. const buttonExportASCII = document.getElementById( 'exportASCII' );
  87. buttonExportASCII.addEventListener( 'click', exportASCII );
  88. const buttonExportBinaryBE = document.getElementById( 'exportBinaryBigEndian' );
  89. buttonExportBinaryBE.addEventListener( 'click', exportBinaryBigEndian );
  90. const buttonExportBinaryLE = document.getElementById( 'exportBinaryLittleEndian' );
  91. buttonExportBinaryLE.addEventListener( 'click', exportBinaryLittleEndian );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function animate() {
  99. requestAnimationFrame( 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>