misc_exporter_stl.html 4.4 KB

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