misc_exporter_stl.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. animate();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  35. camera.position.set( 4, 2, 4 );
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0xa0a0a0 );
  38. scene.fog = new THREE.Fog( 0xa0a0a0, 4, 20 );
  39. exporter = new STLExporter();
  40. //
  41. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444, 3 );
  42. hemiLight.position.set( 0, 20, 0 );
  43. scene.add( hemiLight );
  44. const directionalLight = new THREE.DirectionalLight( 0xffffff, 3 );
  45. directionalLight.position.set( 0, 20, 10 );
  46. directionalLight.castShadow = true;
  47. directionalLight.shadow.camera.top = 2;
  48. directionalLight.shadow.camera.bottom = - 2;
  49. directionalLight.shadow.camera.left = - 2;
  50. directionalLight.shadow.camera.right = 2;
  51. scene.add( directionalLight );
  52. // ground
  53. const ground = new THREE.Mesh( new THREE.PlaneGeometry( 40, 40 ), new THREE.MeshPhongMaterial( { color: 0xbbbbbb, depthWrite: false } ) );
  54. ground.rotation.x = - Math.PI / 2;
  55. ground.receiveShadow = true;
  56. scene.add( ground );
  57. const grid = new THREE.GridHelper( 40, 20, 0x000000, 0x000000 );
  58. grid.material.opacity = 0.2;
  59. grid.material.transparent = true;
  60. scene.add( grid );
  61. // export mesh
  62. const geometry = new THREE.BoxGeometry();
  63. const material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
  64. mesh = new THREE.Mesh( geometry, material );
  65. mesh.castShadow = true;
  66. mesh.position.y = 0.5;
  67. scene.add( mesh );
  68. //
  69. renderer = new THREE.WebGLRenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  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. requestAnimationFrame( animate );
  92. renderer.render( scene, camera );
  93. }
  94. function exportASCII() {
  95. const result = exporter.parse( mesh );
  96. saveString( result, 'box.stl' );
  97. }
  98. function exportBinary() {
  99. const result = exporter.parse( mesh, { binary: true } );
  100. saveArrayBuffer( result, 'box.stl' );
  101. }
  102. const link = document.createElement( 'a' );
  103. link.style.display = 'none';
  104. document.body.appendChild( link );
  105. function save( blob, filename ) {
  106. link.href = URL.createObjectURL( blob );
  107. link.download = filename;
  108. link.click();
  109. }
  110. function saveString( text, filename ) {
  111. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  112. }
  113. function saveArrayBuffer( buffer, filename ) {
  114. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  115. }
  116. </script>
  117. </body>
  118. </html>