misc_exporter_stl.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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
  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 { STLExporter } from 'three/addons/exporters/STLExporter.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. exportBinary: exportBinary
  33. };
  34. init();
  35. animate();
  36. function init() {
  37. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  38. camera.position.set( 200, 100, 200 );
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0xa0a0a0 );
  41. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  42. exporter = new STLExporter();
  43. //
  44. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  45. hemiLight.position.set( 0, 200, 0 );
  46. scene.add( hemiLight );
  47. const directionalLight = new THREE.DirectionalLight( 0xffffff );
  48. directionalLight.position.set( 0, 200, 100 );
  49. directionalLight.castShadow = true;
  50. directionalLight.shadow.camera.top = 180;
  51. directionalLight.shadow.camera.bottom = - 100;
  52. directionalLight.shadow.camera.left = - 120;
  53. directionalLight.shadow.camera.right = 120;
  54. scene.add( directionalLight );
  55. // ground
  56. const ground = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  57. ground.rotation.x = - Math.PI / 2;
  58. ground.receiveShadow = true;
  59. scene.add( ground );
  60. const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  61. grid.material.opacity = 0.2;
  62. grid.material.transparent = true;
  63. scene.add( grid );
  64. // export mesh
  65. const geometry = new THREE.BoxGeometry( 50, 50, 50 );
  66. const material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
  67. mesh = new THREE.Mesh( geometry, material );
  68. mesh.castShadow = true;
  69. mesh.position.y = 25;
  70. scene.add( mesh );
  71. //
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.shadowMap.enabled = true;
  76. document.body.appendChild( renderer.domElement );
  77. //
  78. const controls = new OrbitControls( camera, renderer.domElement );
  79. controls.target.set( 0, 25, 0 );
  80. controls.update();
  81. //
  82. window.addEventListener( 'resize', onWindowResize );
  83. const gui = new GUI();
  84. gui.add( params, 'exportASCII' ).name( 'Export STL (ASCII)' );
  85. gui.add( params, 'exportBinary' ).name( 'Export STL (Binary)' );
  86. gui.open();
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. function animate() {
  94. requestAnimationFrame( animate );
  95. renderer.render( scene, camera );
  96. }
  97. function exportASCII() {
  98. const result = exporter.parse( mesh );
  99. saveString( result, 'box.stl' );
  100. }
  101. function exportBinary() {
  102. const result = exporter.parse( mesh, { binary: true } );
  103. saveArrayBuffer( result, 'box.stl' );
  104. }
  105. const link = document.createElement( 'a' );
  106. link.style.display = 'none';
  107. document.body.appendChild( link );
  108. function save( blob, filename ) {
  109. link.href = URL.createObjectURL( blob );
  110. link.download = filename;
  111. link.click();
  112. }
  113. function saveString( text, filename ) {
  114. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  115. }
  116. function saveArrayBuffer( buffer, filename ) {
  117. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  118. }
  119. </script>
  120. </body>
  121. </html>