浏览代码

GCode loader example

Tentone 7 年之前
父节点
当前提交
50bc249f20
共有 2 个文件被更改,包括 103 次插入7 次删除
  1. 7 7
      examples/js/loaders/GCodeLoader.js
  2. 96 0
      examples/webgl_loader_gcode.html

+ 7 - 7
examples/js/loaders/GCodeLoader.js

@@ -1,20 +1,20 @@
 "use strict";
 "use strict";
 
 
 /**
 /**
- * GCodeLoader is used to load gcode files usually used for 3D printing or CNC applications.
+ * THREE.GCodeLoader is used to load gcode files usually used for 3D printing or CNC applications.
  *
  *
  * Gcode files are composed by commands used by machines to create objects.
  * Gcode files are composed by commands used by machines to create objects.
  *
  *
- * @class GCodeLoader
+ * @class THREE.GCodeLoader
  * @param {Manager} manager Loading manager.
  * @param {Manager} manager Loading manager.
  * @author tentone
  * @author tentone
  */
  */
-function GCodeLoader(manager)
+THREE.GCodeLoader = function(manager)
 {
 {
 	this.manager = (manager !== undefined) ? manager : THREE.DefaultLoadingManager;
 	this.manager = (manager !== undefined) ? manager : THREE.DefaultLoadingManager;
 }
 }
 
 
-GCodeLoader.prototype.load = function(url, onLoad, onProgress, onError)
+THREE.GCodeLoader.prototype.load = function(url, onLoad, onProgress, onError)
 {
 {
 	var self = this;
 	var self = this;
 
 
@@ -28,7 +28,7 @@ GCodeLoader.prototype.load = function(url, onLoad, onProgress, onError)
 	}, onProgress, onError);
 	}, onProgress, onError);
 };
 };
 
 
-GCodeLoader.prototype.parse = function(data)
+THREE.GCodeLoader.prototype.parse = function(data)
 {
 {
 	var currentState = {x:0, y:0, z:0, e:0, f:0, extruding:false};
 	var currentState = {x:0, y:0, z:0, e:0, f:0, extruding:false};
 	var currentLayer = undefined;
 	var currentLayer = undefined;
@@ -149,7 +149,7 @@ GCodeLoader.prototype.parse = function(data)
 		//G2/G3 - Arc Movement (G2 clock wise and G3 counter clock wise)
 		//G2/G3 - Arc Movement (G2 clock wise and G3 counter clock wise)
 		else if(cmd === "G2" || cmd === "G3")
 		else if(cmd === "G2" || cmd === "G3")
 		{
 		{
-			console.warn("GCodeLoader: Arc command not supported");
+			console.warn("THREE.GCodeLoader: Arc command not supported");
 		}
 		}
 		//G90: Set to Absolute Positioning
 		//G90: Set to Absolute Positioning
 		else if(cmd === "G90")
 		else if(cmd === "G90")
@@ -173,7 +173,7 @@ GCodeLoader.prototype.parse = function(data)
 		}
 		}
 		else
 		else
 		{
 		{
-			console.warn("GCodeLoader: Command not supported:" + cmd);
+			console.warn("THREE.GCodeLoader: Command not supported:" + cmd);
 		}
 		}
 	}
 	}
 
 

+ 96 - 0
examples/webgl_loader_gcode.html

@@ -0,0 +1,96 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgl - loaders - GCode loader</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<style>
+			body {
+				font-family: Monospace;
+				background-color: #000;
+				color: #fff;
+				margin: 0px;
+				overflow: hidden;
+			}
+			#info {
+				color: #fff;
+				position: absolute;
+				top: 10px;
+				width: 100%;
+				text-align: center;
+				z-index: 100;
+				display:block;
+			}
+			#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
+		</style>
+	</head>
+
+	<body>
+		<div id="info">
+			<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - GCode loader
+		</div>
+
+		<script src="../build/three.js"></script>
+		<script src="js/controls/OrbitControls.js"></script>
+		<script src="js/loaders/GCodeLoader.js"></script>
+
+		<script>
+
+			var container, controls;
+			var camera, scene, renderer;
+
+			init();
+			animate();
+
+			function init() {
+
+				container = document.createElement( 'div' );
+				document.body.appendChild( container );
+
+				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 10000);
+				camera.position.set( 0, 0, 50 );
+
+				controls = new THREE.OrbitControls( camera );
+
+				scene = new THREE.Scene();
+
+				var loader = new THREE.GCodeLoader( );
+				loader.load( 'models/gcode/benchy.gcode', function ( object ) {
+
+					object.position.set( -100, -20, 100 );
+					object.rotation.set( -Math.PI/2, 0, 0 );
+					scene.add( object );
+
+				});
+
+				renderer = new THREE.WebGLRenderer();
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				container.appendChild( renderer.domElement );
+
+				window.addEventListener( 'resize', resize, false );
+
+			}
+
+			function resize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			function animate() {
+
+				controls.update();
+
+				renderer.render( scene, camera );
+
+				requestAnimationFrame( animate );
+
+			}
+		</script>
+
+	</body>
+</html>