Selaa lähdekoodia

integrate VrmlParser with ThreeJs using the VRMLLoader

Bart McLeod 8 vuotta sitten
vanhempi
commit
44b402878e
2 muutettua tiedostoa jossa 50 lisäystä ja 51 poistoa
  1. 36 29
      examples/js/loaders/VRMLLoader.js
  2. 14 22
      examples/webgl_loader_vrml.html

+ 36 - 29
examples/js/loaders/VRMLLoader.js

@@ -10,51 +10,58 @@
  * @param VrmlParser.Renderer.ThreeJs renderer
  * @constructor
  */
-THREE.VRMLLoader = function (parser, manager) {
-
-  this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
-  this.parser = parser;
+THREE.VRMLLoader = function (parser, manager, debug) {
 
+	this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
+	this.parser  = parser;
+	this.debug   = debug;
 };
 
 THREE.VRMLLoader.prototype = {
 
-  constructor: THREE.VRMLLoader,
+	constructor: THREE.VRMLLoader,
+
+	log: function (obj) {
+		if ( this.debug ) {
+			console.log(obj);
+		}
+	},
+
+	parse: function (data) {
+		try {
+			var tree = this.parser.parse(data);
 
-  parse: function (data) {
-    try {
-      var tree = this.parser.parse(data);
-      console.log(tree);
-      return tree;
-    } catch ( e ) {
-      console.log('Exception with message ' + e.message);
+			this.log(tree);
+			return tree;
+		} catch ( e ) {
+			this.log('Exception with message ' + e.message);
 
-      if ( undefined !== e.location ) {
-        console.log('Exception at location start: offset: ' + e.location.start.offset + ' line: ' + e.location.start.line + ' column: ' + e.location.start.column);
-        console.log('Exception at location end: offset: ' + e.location.end.offset + ' line: ' + e.location.end.line + ' column: ' + e.location.end.column);
-      }
+			if ( undefined !== e.location ) {
+				this.log('Exception at location start: offset: ' + e.location.start.offset + ' line: ' + e.location.start.line + ' column: ' + e.location.start.column);
+				this.log('Exception at location end: offset: ' + e.location.end.offset + ' line: ' + e.location.end.line + ' column: ' + e.location.end.column);
+			}
 
-      return;
-    }
-  },
+			return;
+		}
+	},
 
-  load: function (url, onLoad, onProgress, onError) {
+	load: function (url, onLoad, onProgress, onError) {
 
-    var scope = this;
+		var scope = this;
 
-    var loader = new THREE.XHRLoader(this.manager);
-    loader.load(url, function (text) {
+		var loader = new THREE.FileLoader(this.manager);
+		loader.load(url, function (text) {
 
-      onLoad(scope.parse(text));
+			onLoad(scope.parse(text));
 
-    }, onProgress, onError);
+		}, onProgress, onError);
 
-  },
+	},
 
-  setCrossOrigin: function (value) {
+	setCrossOrigin: function (value) {
 
-    this.crossOrigin = value;
+		this.crossOrigin = value;
 
-  },
+	},
 
 };

+ 14 - 22
examples/webgl_loader_vrml.html

@@ -60,6 +60,7 @@
 <script src="../build/three.min.js"></script>
 <script src="js/libs/vrml.min.js"></script>
 
+<script src="js/loaders/VRMLLoader.js"></script>
 <script src="js/renderers/Projector.js"></script>
 <script src="js/controls/OrbitControls.js"></script>
 <script src="js/controls/FlyControls.js"></script>
@@ -146,27 +147,11 @@
 			}
 		}; // key value store of cameras based on VRML viewpoint nodes, stored by their name.
 
-		// VRML parser example:
-		var fileLoader = new THREE.FileLoader();
-		// onLoad, onProgress, onError
-		fileLoader.load('models/vrml/house.wrl', function (data) {
-			try {
-				var tree = vrmlParser.parse(data);
-			} catch ( e ) {
-				console.log('Exception with message ' + e.message);
-
-				if ( undefined !== e.location ) {
-					console.log('Exception at location start: offset: ' + e.location.start.offset + ' line: ' + e.location.start.line + ' column: ' + e.location.start.column);
-					console.log('Exception at location end: offset: ' + e.location.end.offset + ' line: ' + e.location.end.line + ' column: ' + e.location.end.column);
-				}
-
-				return;
-			}
-			console.log(tree);
+		var vrmlLoader = new THREE.VRMLLoader(vrmlParser, new THREE.LoadingManager(), true);
 
-			vrmlConverter.render(tree, scene);
+		var onLoad = function (vrmlTree) {
+            vrmlConverter.render(vrmlTree, scene);
 
-			// @todo: move viewpointSelector to its own class
 			var viewpointSelector = document.getElementById('viewpoints');
 
 			for ( a in vrmlConverter.viewpoints ) {
@@ -181,14 +166,21 @@
 				}
 			}
 
-		}, function () {
-		}, function (error) {
+		};
+
+		var onError = function (error) {
 			var request = error.target;
 			if ( 404 === request.status ) {
 				console.log('VRML Document not found at ' + request.responseURL);
 			}
 			console.log(error);
-		});
+		}
+
+		var onProgress = function () {
+
+		}
+
+		vrmlLoader.load('models/vrml/house.wrl', onLoad, onProgress, onError);
 
 		container = document.createElement('div');
 		document.body.appendChild(container);