misc_exporter_gcode.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - GCode</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 - <a href="https://github.com/jgphilpott/polyslice" target="_blank" rel="noopener">GCode</a>
  12. <br>Slice 3D models to G-code for 3D printing
  13. <br><a href="https://www.youtube.com/watch?v=V2h3SiafXRc" target="_blank" rel="noopener">Watch the Demo Video</a>
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Polyslice from 'https://unpkg.com/@jgphilpott/polyslice@25.12.8/dist/index.browser.esm.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. // Make THREE available globally for Polyslice
  29. window.THREE = THREE;
  30. let camera, scene, renderer, mesh;
  31. const params = {
  32. addCube: addCube,
  33. addCylinder: addCylinder,
  34. addCone: addCone,
  35. addSphere: addSphere,
  36. addTorus: addTorus,
  37. exportToGCode: exportToGCode,
  38. geometryName: 'cube',
  39. printer: 'Ender3',
  40. filament: 'GenericPLA',
  41. layerHeight: 0.2,
  42. infillDensity: 20,
  43. infillPattern: 'grid'
  44. };
  45. // Dropdown options (extend as supported by Polyslice profiles)
  46. const PRINTER_OPTIONS = [ 'Ender3', 'UltimakerS5', 'PrusaI3MK3S', 'AnycubicI3Mega', 'BambuLabP1P' ];
  47. const FILAMENT_OPTIONS = [ 'GenericPLA', 'GenericPETG', 'GenericABS' ];
  48. init();
  49. function init() {
  50. renderer = new THREE.WebGLRenderer( { antialias: true } );
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. renderer.setAnimationLoop( animate );
  54. document.body.appendChild( renderer.domElement );
  55. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  56. // Use Z-up coordinate system
  57. THREE.Object3D.DEFAULT_UP.set( 0, 0, 1 );
  58. camera.up.set( 0, 0, 1 );
  59. camera.position.set( 42, 42, 42 );
  60. scene = new THREE.Scene();
  61. scene.background = new THREE.Color( 0xa0a0a0 );
  62. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.5 );
  63. scene.add( ambientLight );
  64. const directionalLight = new THREE.DirectionalLight( 0xffffff, 2.5 );
  65. directionalLight.position.set( 0, 200, 100 );
  66. scene.add( directionalLight );
  67. // Add ground plane for reference (XY plane when Z is up)
  68. const gridHelper = new THREE.GridHelper( 220, 10 );
  69. gridHelper.rotation.x = - Math.PI / 2; // rotate from XZ to XY
  70. scene.add( gridHelper );
  71. const gui = new GUI();
  72. let h = gui.addFolder( 'Printer & Filament' );
  73. h.add( params, 'printer', PRINTER_OPTIONS ).name( 'Printer' );
  74. h.add( params, 'filament', FILAMENT_OPTIONS ).name( 'Filament' );
  75. h = gui.addFolder( 'Slicer Settings' );
  76. h.add( params, 'layerHeight', 0.1, 0.4, 0.05 ).name( 'Layer Height (mm)' );
  77. h.add( params, 'infillDensity', 0, 100, 5 ).name( 'Infill Density (%)' );
  78. h.add( params, 'infillPattern', [ 'grid', 'triangles', 'hexagons' ] ).name( 'Infill Pattern' );
  79. h = gui.addFolder( 'Geometry Selection' );
  80. h.add( params, 'addCube' ).name( 'Cube' );
  81. h.add( params, 'addCylinder' ).name( 'Cylinder' );
  82. h.add( params, 'addCone' ).name( 'Cone' );
  83. h.add( params, 'addSphere' ).name( 'Sphere' );
  84. h.add( params, 'addTorus' ).name( 'Torus' );
  85. h = gui.addFolder( 'Export' );
  86. h.add( params, 'exportToGCode' ).name( 'Export G-code' );
  87. gui.open();
  88. addCube();
  89. window.addEventListener( 'resize', onWindowResize );
  90. const controls = new OrbitControls( camera, renderer.domElement );
  91. controls.target.set( 0, 0, 0 );
  92. controls.update();
  93. }
  94. function exportToGCode() {
  95. try {
  96. // Check if Polyslice is loaded
  97. if ( typeof Polyslice === 'undefined' ) {
  98. alert( 'Polyslice library failed to load from CDN.\n\nPossible solutions:\n1. Disable ad blockers or browser extensions\n2. Check your network connection\n3. Ensure unpkg.com is not blocked by your firewall' );
  99. return;
  100. }
  101. // Create printer and filament configurations using Polyslice
  102. const printer = new Polyslice.Printer( params.printer || 'Ender3' );
  103. const filament = new Polyslice.Filament( params.filament || 'GenericPLA' );
  104. // Create the slicer instance with user-defined settings
  105. const slicer = new Polyslice.Polyslice( {
  106. printer: printer,
  107. filament: filament,
  108. layerHeight: params.layerHeight,
  109. infillPattern: params.infillPattern,
  110. infillDensity: params.infillDensity,
  111. verbose: true
  112. } );
  113. // Slice the current mesh directly
  114. const gcode = slicer.slice( mesh );
  115. // Download the G-code file (include geometry name)
  116. const name = params.geometryName || 'model';
  117. saveString( gcode, `${ name }-geometry.gcode` );
  118. } catch ( error ) {
  119. console.error( 'Error exporting to G-code:', error );
  120. alert( 'Error exporting to G-code. Check console for details.' );
  121. alert( 'Note: Polyslice is an external library. Please ensure it is loaded correctly.' );
  122. }
  123. }
  124. function clearScene() {
  125. if ( mesh ) {
  126. mesh.geometry.dispose();
  127. mesh.material.dispose();
  128. scene.remove( mesh );
  129. }
  130. }
  131. // Ensure the mesh sits on the XY plane with min Z = 0
  132. function placeOnXYPlane( object ) {
  133. // Update world matrix so bounding box reflects transforms
  134. object.updateMatrixWorld( true );
  135. const box = new THREE.Box3().setFromObject( object );
  136. const minZ = box.min.z;
  137. if ( isFinite( minZ ) ) {
  138. // Shift object upward by -minZ so it rests on z=0
  139. object.position.z -= minZ;
  140. object.updateMatrixWorld( true );
  141. }
  142. }
  143. function addCube() {
  144. clearScene();
  145. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  146. const geometry = new THREE.BoxGeometry( 10, 10, 10 );
  147. mesh = new THREE.Mesh( geometry, material );
  148. params.geometryName = 'cube';
  149. placeOnXYPlane( mesh );
  150. scene.add( mesh );
  151. }
  152. function addCylinder() {
  153. clearScene();
  154. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  155. const geometry = new THREE.CylinderGeometry( 5, 5, 10, 42 );
  156. mesh = new THREE.Mesh( geometry, material );
  157. mesh.rotation.x = Math.PI / 2;
  158. params.geometryName = 'cylinder';
  159. placeOnXYPlane( mesh );
  160. scene.add( mesh );
  161. }
  162. function addCone() {
  163. clearScene();
  164. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  165. const geometry = new THREE.ConeGeometry( 5, 10, 42 );
  166. mesh = new THREE.Mesh( geometry, material );
  167. mesh.rotation.x = Math.PI / 2;
  168. params.geometryName = 'cone';
  169. placeOnXYPlane( mesh );
  170. scene.add( mesh );
  171. }
  172. function addSphere() {
  173. clearScene();
  174. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  175. const geometry = new THREE.SphereGeometry( 5, 42, 42 );
  176. mesh = new THREE.Mesh( geometry, material );
  177. params.geometryName = 'sphere';
  178. placeOnXYPlane( mesh );
  179. scene.add( mesh );
  180. }
  181. function addTorus() {
  182. clearScene();
  183. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  184. const geometry = new THREE.TorusGeometry( 5, 2, 24, 100 );
  185. mesh = new THREE.Mesh( geometry, material );
  186. params.geometryName = 'torus';
  187. placeOnXYPlane( mesh );
  188. scene.add( mesh );
  189. }
  190. const link = document.createElement( 'a' );
  191. link.style.display = 'none';
  192. document.body.appendChild( link );
  193. function save( blob, filename ) {
  194. link.href = URL.createObjectURL( blob );
  195. link.download = filename;
  196. link.click();
  197. }
  198. function saveString( text, filename ) {
  199. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  200. }
  201. function onWindowResize() {
  202. camera.aspect = window.innerWidth / window.innerHeight;
  203. camera.updateProjectionMatrix();
  204. renderer.setSize( window.innerWidth, window.innerHeight );
  205. }
  206. function animate() {
  207. renderer.render( scene, camera );
  208. }
  209. </script>
  210. </body>
  211. </html>
粤ICP备19079148号