misc_exporter_stl.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. display:block;
  22. }
  23. #info a {
  24. color: #046;
  25. font-weight: bold;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="info">
  31. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - stl
  32. </div>
  33. <script src="../build/three.js"></script>
  34. <script src="js/controls/OrbitControls.js"></script>
  35. <script src="js/exporters/STLExporter.js"></script>
  36. <script src="js/Detector.js"></script>
  37. <script src="js/libs/dat.gui.min.js"></script>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var scene, camera, renderer, exporter;
  41. init();
  42. animate();
  43. function init() {
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  45. camera.position.set( 200, 100, 200 );
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0xa0a0a0 );
  48. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  49. exporter = new THREE.STLExporter();
  50. //
  51. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  52. hemiLight.position.set( 0, 200, 0 );
  53. scene.add( hemiLight );
  54. var directionalLight = new THREE.DirectionalLight( 0xffffff );
  55. directionalLight.position.set( 0, 200, 100 );
  56. directionalLight.castShadow = true;
  57. directionalLight.shadow.camera.top = 180;
  58. directionalLight.shadow.camera.bottom = - 100;
  59. directionalLight.shadow.camera.left = - 120;
  60. directionalLight.shadow.camera.right = 120;
  61. scene.add( directionalLight );
  62. // ground
  63. var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  64. ground.rotation.x = - Math.PI / 2;
  65. ground.receiveShadow = true;
  66. scene.add( ground );
  67. var grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  68. grid.material.opacity = 0.2;
  69. grid.material.transparent = true;
  70. scene.add( grid );
  71. // export mesh
  72. var geometry = new THREE.BoxBufferGeometry( 50, 50, 50 );
  73. var material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
  74. mesh = new THREE.Mesh( geometry, material );
  75. mesh.castShadow = true;
  76. mesh.position.y = 25;
  77. scene.add( mesh );
  78. //
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. renderer.shadowMap.enabled = true;
  83. document.body.appendChild( renderer.domElement );
  84. //
  85. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  86. controls.target.set( 0, 25, 0 );
  87. controls.update();
  88. //
  89. var params = {
  90. ASCII: function() {
  91. exportASCII();
  92. },
  93. Binary: function() {
  94. exportBinary();
  95. }
  96. }
  97. var gui = new dat.GUI();
  98. var folder = gui.addFolder( 'Export' );
  99. folder.add( params, 'ASCII' );
  100. folder.add( params, 'Binary' );
  101. folder.open();
  102. //
  103. window.addEventListener( 'resize', onWindowResize, false );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. renderer.render( scene, camera );
  113. }
  114. function exportASCII() {
  115. var result = exporter.parse( mesh );
  116. saveString( result, 'box.stl' );
  117. }
  118. function exportBinary() {
  119. var result = exporter.parse( mesh, { binary: true } );
  120. saveArrayBuffer( result, 'box.stl' );
  121. }
  122. var link = document.createElement( 'a' );
  123. link.style.display = 'none';
  124. document.body.appendChild( link );
  125. function save( blob, filename ) {
  126. link.href = URL.createObjectURL( blob );
  127. link.download = filename;
  128. link.click();
  129. }
  130. function saveString( text, filename ) {
  131. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  132. }
  133. function saveArrayBuffer( buffer, filename ) {
  134. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  135. }
  136. </script>
  137. </body>
  138. </html>