misc_exporter_stl.html 4.2 KB

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