misc_exporter_stl.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - stl</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 - stl<br/><br/>
  12. <button id="exportASCII">export ASCII</button> <button id="exportBinary">export binary</button>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { STLExporter } from './jsm/exporters/STLExporter.js';
  18. var scene, camera, renderer, exporter, mesh;
  19. init();
  20. animate();
  21. function init() {
  22. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  23. camera.position.set( 200, 100, 200 );
  24. scene = new THREE.Scene();
  25. scene.background = new THREE.Color( 0xa0a0a0 );
  26. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  27. exporter = new STLExporter();
  28. //
  29. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  30. hemiLight.position.set( 0, 200, 0 );
  31. scene.add( hemiLight );
  32. var directionalLight = new THREE.DirectionalLight( 0xffffff );
  33. directionalLight.position.set( 0, 200, 100 );
  34. directionalLight.castShadow = true;
  35. directionalLight.shadow.camera.top = 180;
  36. directionalLight.shadow.camera.bottom = - 100;
  37. directionalLight.shadow.camera.left = - 120;
  38. directionalLight.shadow.camera.right = 120;
  39. scene.add( directionalLight );
  40. // ground
  41. var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  42. ground.rotation.x = - Math.PI / 2;
  43. ground.receiveShadow = true;
  44. scene.add( ground );
  45. var grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  46. grid.material.opacity = 0.2;
  47. grid.material.transparent = true;
  48. scene.add( grid );
  49. // export mesh
  50. var geometry = new THREE.BoxBufferGeometry( 50, 50, 50 );
  51. var material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
  52. mesh = new THREE.Mesh( geometry, material );
  53. mesh.castShadow = true;
  54. mesh.position.y = 25;
  55. scene.add( mesh );
  56. //
  57. renderer = new THREE.WebGLRenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.shadowMap.enabled = true;
  61. document.body.appendChild( renderer.domElement );
  62. //
  63. var controls = new OrbitControls( camera, renderer.domElement );
  64. controls.target.set( 0, 25, 0 );
  65. controls.update();
  66. //
  67. window.addEventListener( 'resize', onWindowResize, false );
  68. var buttonExportASCII = document.getElementById( 'exportASCII' );
  69. buttonExportASCII.addEventListener( 'click', exportASCII );
  70. var buttonExportBinary = document.getElementById( 'exportBinary' );
  71. buttonExportBinary.addEventListener( 'click', exportBinary );
  72. }
  73. function onWindowResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. function animate() {
  79. requestAnimationFrame( animate );
  80. renderer.render( scene, camera );
  81. }
  82. function exportASCII() {
  83. var result = exporter.parse( mesh );
  84. saveString( result, 'box.stl' );
  85. }
  86. function exportBinary() {
  87. var result = exporter.parse( mesh, { binary: true } );
  88. saveArrayBuffer( result, 'box.stl' );
  89. }
  90. var link = document.createElement( 'a' );
  91. link.style.display = 'none';
  92. document.body.appendChild( link );
  93. function save( blob, filename ) {
  94. link.href = URL.createObjectURL( blob );
  95. link.download = filename;
  96. link.click();
  97. }
  98. function saveString( text, filename ) {
  99. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  100. }
  101. function saveArrayBuffer( buffer, filename ) {
  102. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  103. }
  104. </script>
  105. </body>
  106. </html>