misc_exporter_stl.html 4.3 KB

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