misc_exporter_ply.html 5.5 KB

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