misc_exporter_stl.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. <!-- 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 { STLExporter } from './jsm/exporters/STLExporter.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 STLExporter();
  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( { color: 0x00ff00 } );
  62. mesh = new THREE.Mesh( geometry, material );
  63. mesh.castShadow = true;
  64. mesh.position.y = 25;
  65. scene.add( mesh );
  66. //
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.shadowMap.enabled = true;
  71. document.body.appendChild( renderer.domElement );
  72. //
  73. const controls = new OrbitControls( camera, renderer.domElement );
  74. controls.target.set( 0, 25, 0 );
  75. controls.update();
  76. //
  77. window.addEventListener( 'resize', onWindowResize );
  78. const buttonExportASCII = document.getElementById( 'exportASCII' );
  79. buttonExportASCII.addEventListener( 'click', exportASCII );
  80. const buttonExportBinary = document.getElementById( 'exportBinary' );
  81. buttonExportBinary.addEventListener( 'click', exportBinary );
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. renderer.render( scene, camera );
  91. }
  92. function exportASCII() {
  93. const result = exporter.parse( mesh );
  94. saveString( result, 'box.stl' );
  95. }
  96. function exportBinary() {
  97. const result = exporter.parse( mesh, { binary: true } );
  98. saveArrayBuffer( result, 'box.stl' );
  99. }
  100. const link = document.createElement( 'a' );
  101. link.style.display = 'none';
  102. document.body.appendChild( link );
  103. function save( blob, filename ) {
  104. link.href = URL.createObjectURL( blob );
  105. link.download = filename;
  106. link.click();
  107. }
  108. function saveString( text, filename ) {
  109. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  110. }
  111. function saveArrayBuffer( buffer, filename ) {
  112. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  113. }
  114. </script>
  115. </body>
  116. </html>