VRMLLoader.js 19.4 KB
Newer Older
M
r59  
Mr.doob 已提交
1 2 3 4 5 6 7 8
/**
 * @author mrdoob / http://mrdoob.com/
 */

THREE.VRMLLoader = function () {};

THREE.VRMLLoader.prototype = {

M
r62  
Mr.doob 已提交
9
	constructor: THREE.VRMLLoader,
M
r59  
Mr.doob 已提交
10

M
r66  
Mr.doob 已提交
11 12 13 14 15
	// for IndexedFaceSet support
	isRecordingPoints: false,
	isRecordingFaces: false,
	points: [],
	indexes : [],
M
r63  
Mr.doob 已提交
16

M
r66  
Mr.doob 已提交
17 18 19 20 21
	// for Background support
	isRecordingAngles: false,
	isRecordingColors: false,
	angles: [],
	colors: [],
M
r63  
Mr.doob 已提交
22

M
r66  
Mr.doob 已提交
23
	recordingFieldname: null,
M
r63  
Mr.doob 已提交
24

M
r59  
Mr.doob 已提交
25 26 27 28 29 30 31 32 33 34 35
	load: function ( url, callback ) {

		var scope = this;
		var request = new XMLHttpRequest();

		request.addEventListener( 'load', function ( event ) {

			var object = scope.parse( event.target.responseText );

			scope.dispatchEvent( { type: 'load', content: object } );

M
r66  
Mr.doob 已提交
36 37
			if ( callback ) callback( object );

M
r59  
Mr.doob 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		}, false );

		request.addEventListener( 'progress', function ( event ) {

			scope.dispatchEvent( { type: 'progress', loaded: event.loaded, total: event.total } );

		}, false );

		request.addEventListener( 'error', function () {

			scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );

		}, false );

		request.open( 'GET', url, true );
		request.send( null );

	},

	parse: function ( data ) {

		var parseV1 = function ( lines, scene ) {

			console.warn( 'VRML V1.0 not supported yet' );

		};

		var parseV2 = function ( lines, scene ) {
M
r66  
Mr.doob 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
			var defines = {};
			var float_pattern = /(\b|\-|\+)([\d\.e]+)/;
			var float3_pattern = /([\d\.\+\-e]+),?\s+([\d\.\+\-e]+),?\s+([\d\.\+\-e]+)/;

			/**
			* Interpolates colors a and b following their relative distance
			* expressed by t.
			*
			* @param float a
			* @param float b
			* @param float t
			* @returns {Color}
			*/
			var interpolateColors = function(a, b, t) {
			   var deltaR = a.r - b.r;
			   var deltaG = a.g - b.g;
			   var deltaB = a.b - b.b;

			   var c = new THREE.Color();

			   c.r = a.r - t * deltaR;
			   c.g = a.g - t * deltaG;
			   c.b = a.b - t * deltaB;

			   return c;
			};

			/**
			 * Vertically paints the faces interpolating between the
			 * specified colors at the specified angels. This is used for the Background
			 * node, but could be applied to other nodes with multiple faces as well.
			 *
			 * When used with the Background node, default is directionIsDown is true if
			 * interpolating the skyColor down from the Zenith. When interpolationg up from
			 * the Nadir i.e. interpolating the groundColor, the directionIsDown is false.
			 *
			 * The first angle is never specified, it is the Zenith (0 rad). Angles are specified
			 * in radians. The geometry is thought a sphere, but could be anything. The color interpolation
			 * is linear along the Y axis in any case.
			 *
			 * You must specify one more color than you have angles at the beginning of the colors array.
			 * This is the color of the Zenith (the top of the shape).
			 *
			 * @param geometry
			 * @param radius
			 * @param angles
			 * @param colors
			 * @param boolean directionIsDown Whether to work bottom up or top down.
			 */
			var paintFaces = function (geometry, radius, angles, colors, directionIsDown) {

				var f, n, p, vertexIndex, color;

				var direction = directionIsDown ? 1 : -1;

				var faceIndices = [ 'a', 'b', 'c', 'd' ];

				var coord = [ ], aColor, bColor, t = 1, A = {}, B = {}, applyColor = false, colorIndex;

				for ( var k = 0; k < angles.length; k++ ) {

					var vec = { };

					// push the vector at which the color changes
					vec.y = direction * ( Math.cos( angles[k] ) * radius);

					vec.x = direction * ( Math.sin( angles[k] ) * radius);

					coord.push( vec );

				}

				// painting the colors on the faces
				for ( var i = 0; i < geometry.faces.length ; i++ ) {

					f  = geometry.faces[ i ];

					n = ( f instanceof THREE.Face3 ) ? 3 : 4;

					for ( var j = 0; j < n; j++ ) {

						vertexIndex = f[ faceIndices[ j ] ];

						p = geometry.vertices[ vertexIndex ];

						for ( var index = 0; index < colors.length; index++ ) {

							// linear interpolation between aColor and bColor, calculate proportion
							// A is previous point (angle)
							if ( index === 0 ) {

								A.x = 0;
								A.y = directionIsDown ? radius : -1 * radius;

							} else {

								A.x = coord[ index-1 ].x;
								A.y = coord[ index-1 ].y;

							}
M
r63  
Mr.doob 已提交
166

M
r66  
Mr.doob 已提交
167 168
							// B is current point (angle)
							B = coord[index];
M
r63  
Mr.doob 已提交
169

M
r66  
Mr.doob 已提交
170 171 172
							if ( undefined !== B ) {
								// p has to be between the points A and B which we interpolate
								applyColor = directionIsDown ? p.y <= A.y && p.y > B.y : p.y >= A.y && p.y < B.y;
M
r63  
Mr.doob 已提交
173

M
r66  
Mr.doob 已提交
174
								if (applyColor) {
M
r63  
Mr.doob 已提交
175

M
r66  
Mr.doob 已提交
176
									bColor = colors[ index + 1 ];
M
r63  
Mr.doob 已提交
177

M
r66  
Mr.doob 已提交
178
									aColor = colors[ index ];
M
r63  
Mr.doob 已提交
179

M
r66  
Mr.doob 已提交
180 181
									// below is simple linear interpolation
									t = Math.abs( p.y - A.y ) / ( A.y - B.y );
M
r63  
Mr.doob 已提交
182

M
r66  
Mr.doob 已提交
183 184
									// to make it faster, you can only calculate this if the y coord changes, the color is the same for points with the same y
									color = interpolateColors( aColor, bColor, t );
M
r63  
Mr.doob 已提交
185

M
r66  
Mr.doob 已提交
186 187
									f.vertexColors[ j ] = color;
								}
M
r63  
Mr.doob 已提交
188

M
r66  
Mr.doob 已提交
189 190 191
							} else if ( undefined === f.vertexColors[ j ] ) {
								colorIndex = directionIsDown ? colors.length -1 : 0;
								f.vertexColors[ j ] = colors[ colorIndex ];
M
r63  
Mr.doob 已提交
192

M
r66  
Mr.doob 已提交
193 194 195 196
							}
						}

					}
M
r63  
Mr.doob 已提交
197

M
r66  
Mr.doob 已提交
198 199 200 201
				}
			};

			var parseProperty = function (node, line) {
M
r63  
Mr.doob 已提交
202

M
r66  
Mr.doob 已提交
203
				var parts = [], part, property = {}, fieldName;
M
r63  
Mr.doob 已提交
204

M
r66  
Mr.doob 已提交
205 206 207 208 209
				/**
				 * Expression for matching relevant information, such as a name or value, but not the separators
				 * @type {RegExp}
				 */
				var regex = /[^\s,\[\]]+/g;
M
r63  
Mr.doob 已提交
210

M
r66  
Mr.doob 已提交
211
				var point, index, angles, colors;
M
r63  
Mr.doob 已提交
212

M
r66  
Mr.doob 已提交
213 214 215
				while (null != ( part = regex.exec(line) ) ) {
					parts.push(part[0]);
				}
M
r63  
Mr.doob 已提交
216

M
r66  
Mr.doob 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
				fieldName = parts[0];


				// trigger several recorders
				switch (fieldName) {
					case 'skyAngle':
					case 'groundAngle':
						this.recordingFieldname = fieldName;
						this.isRecordingAngles = true;
						this.angles = [];
						break;
					case 'skyColor':
					case 'groundColor':
						this.recordingFieldname = fieldName;
						this.isRecordingColors = true;
						this.colors = [];
						break;
					case 'point':
						this.recordingFieldname = fieldName;
						this.isRecordingPoints = true;
						this.points = [];
						break;
					case 'coordIndex':
						this.recordingFieldname = fieldName;
						this.isRecordingFaces = true;
						this.indexes = [];
						break;
				}
M
r63  
Mr.doob 已提交
245

M
r66  
Mr.doob 已提交
246
				if (this.isRecordingFaces) {
M
r63  
Mr.doob 已提交
247

M
r66  
Mr.doob 已提交
248 249 250
					// the parts hold the indexes as strings
					if (parts.length > 0) {
						index = [];
M
r63  
Mr.doob 已提交
251

M
r66  
Mr.doob 已提交
252
						for (var ind = 0;ind < parts.length; ind++) {
M
r63  
Mr.doob 已提交
253

M
r66  
Mr.doob 已提交
254 255 256 257
							// the part should either be positive integer or -1
							if (!/(-?\d+)/.test( parts[ind]) ) {
								continue;
							}
M
r63  
Mr.doob 已提交
258

M
r66  
Mr.doob 已提交
259 260 261 262 263
							// end of current face
							if (parts[ind] === "-1") {
								if (index.length > 0) {
								   this.indexes.push(index);
								}
M
r63  
Mr.doob 已提交
264

M
r66  
Mr.doob 已提交
265 266 267 268 269 270
								// start new one
								index = [];
							} else {
								index.push(parseInt( parts[ind]) );
							}
						}
M
r63  
Mr.doob 已提交
271

M
r66  
Mr.doob 已提交
272
					}
M
r63  
Mr.doob 已提交
273

M
r66  
Mr.doob 已提交
274 275 276 277 278
					// end
					if (/]/.exec(line)) {
						this.isRecordingFaces = false;
						node.coordIndex = this.indexes;
					}
M
r63  
Mr.doob 已提交
279

M
r66  
Mr.doob 已提交
280
				} else if (this.isRecordingPoints) {
M
r63  
Mr.doob 已提交
281

M
r66  
Mr.doob 已提交
282
					parts = float3_pattern.exec(line);
M
r63  
Mr.doob 已提交
283

M
r66  
Mr.doob 已提交
284 285 286 287 288 289 290
					// parts may be empty on first and last line
					if (null != parts) {
						point = {
							x: parseFloat(parts[1]),
							y: parseFloat(parts[2]),
							z: parseFloat(parts[3])
						};
M
r63  
Mr.doob 已提交
291

M
r66  
Mr.doob 已提交
292 293
						this.points.push(point);
					}
M
r63  
Mr.doob 已提交
294

M
r66  
Mr.doob 已提交
295 296 297 298 299
					// end
					if ( /]/.exec(line) ) {
						this.isRecordingPoints = false;
						node.points = this.points;
					}
M
r63  
Mr.doob 已提交
300

M
r66  
Mr.doob 已提交
301
				} else if ( this.isRecordingAngles ) {
M
r63  
Mr.doob 已提交
302

M
r66  
Mr.doob 已提交
303 304
					// the parts hold the angles as strings
					if ( parts.length > 0 ) {
M
r63  
Mr.doob 已提交
305

M
r66  
Mr.doob 已提交
306
						for ( var ind = 0;ind < parts.length; ind++ ) {
M
r63  
Mr.doob 已提交
307

M
r66  
Mr.doob 已提交
308 309 310 311
							// the part should be a float
							if ( ! float_pattern.test( parts[ind] ) ) {
								continue;
							}
M
r63  
Mr.doob 已提交
312

M
r66  
Mr.doob 已提交
313 314
							this.angles.push( parseFloat( parts[ind] ) );
						}
M
r63  
Mr.doob 已提交
315

M
r66  
Mr.doob 已提交
316
					}
M
r63  
Mr.doob 已提交
317

M
r66  
Mr.doob 已提交
318 319 320 321 322
					// end
					if ( /]/.exec(line) ) {
						this.isRecordingAngles = false;
						node[this.recordingFieldname] = this.angles;
					}
M
r63  
Mr.doob 已提交
323

M
r66  
Mr.doob 已提交
324 325 326
				} else if (this.isRecordingColors) {
					// this is the float3 regex with the g modifier added, you could also explode the line by comma first (faster probably)
					var float3_repeatable = /([\d\.\+\-e]+),?\s+([\d\.\+\-e]+),?\s+([\d\.\+\-e]+)/g;
M
r63  
Mr.doob 已提交
327

M
r66  
Mr.doob 已提交
328
					while( null !== (parts = float3_repeatable.exec(line) ) ) {
M
r63  
Mr.doob 已提交
329

M
r66  
Mr.doob 已提交
330 331 332 333 334
						color = {
							r: parseFloat(parts[1]),
							g: parseFloat(parts[2]),
							b: parseFloat(parts[3])
						};
M
r63  
Mr.doob 已提交
335

M
r66  
Mr.doob 已提交
336
						this.colors.push(color);
M
r63  
Mr.doob 已提交
337

M
r66  
Mr.doob 已提交
338
					}
M
r63  
Mr.doob 已提交
339

M
r66  
Mr.doob 已提交
340 341 342 343 344
					// end
					if (/]/.exec(line)) {
						this.isRecordingColors = false;
						node[this.recordingFieldname] = this.colors;
					}
M
r63  
Mr.doob 已提交
345

M
r66  
Mr.doob 已提交
346
				} else if ( parts[parts.length -1] !== 'NULL' && fieldName !== 'children') {
M
r63  
Mr.doob 已提交
347

M
r66  
Mr.doob 已提交
348
					switch (fieldName) {
M
r63  
Mr.doob 已提交
349

M
r66  
Mr.doob 已提交
350 351 352 353
						case 'diffuseColor':
						case 'emissiveColor':
						case 'specularColor':
						case 'color':
M
r63  
Mr.doob 已提交
354

M
r66  
Mr.doob 已提交
355 356 357 358
							if (parts.length != 4) {
								console.warn('Invalid color format detected for ' + fieldName );
								break;
							}
M
r63  
Mr.doob 已提交
359

M
r66  
Mr.doob 已提交
360 361 362 363 364
							property = {
								'r'         : parseFloat(parts[1]),
								'g'         : parseFloat(parts[2]),
								'b'         : parseFloat(parts[3])
							}
M
r63  
Mr.doob 已提交
365

M
r66  
Mr.doob 已提交
366
							break;
M
r63  
Mr.doob 已提交
367

M
r66  
Mr.doob 已提交
368 369 370 371 372 373 374
						case 'translation':
						case 'scale':
						case 'size':
							if (parts.length != 4) {
								console.warn('Invalid vector format detected for ' + fieldName);
								break;
							}
M
r63  
Mr.doob 已提交
375

M
r66  
Mr.doob 已提交
376 377 378 379 380
							property = {
								'x'         : parseFloat(parts[1]),
								'y'         : parseFloat(parts[2]),
								'z'         : parseFloat(parts[3])
							}
M
r63  
Mr.doob 已提交
381

M
r66  
Mr.doob 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394
							break;

						case 'radius':
						case 'topRadius':
						case 'bottomRadius':
						case 'height':
						case 'transparency':
						case 'shininess':
						case 'ambientIntensity':
							if (parts.length != 2) {
								console.warn('Invalid single float value specification detected for ' + fieldName);
								break;
							}
M
r63  
Mr.doob 已提交
395

M
r66  
Mr.doob 已提交
396
							property = parseFloat(parts[1]);
M
r63  
Mr.doob 已提交
397

M
r66  
Mr.doob 已提交
398
							break;
M
r63  
Mr.doob 已提交
399

M
r66  
Mr.doob 已提交
400 401 402 403 404
						case 'rotation':
							if (parts.length != 5) {
								console.warn('Invalid quaternion format detected for ' + fieldName);
								break;
							}
M
r63  
Mr.doob 已提交
405

M
r66  
Mr.doob 已提交
406 407 408 409 410 411
							property = {
								'x'         : parseFloat(parts[1]),
								'y'         : parseFloat(parts[2]),
								'z'         : parseFloat(parts[3]),
								'w'         : parseFloat(parts[4])
							}
M
r63  
Mr.doob 已提交
412

M
r66  
Mr.doob 已提交
413
							break;
M
r63  
Mr.doob 已提交
414

M
r66  
Mr.doob 已提交
415 416 417 418 419 420 421 422
						case 'ccw':
						case 'solid':
						case 'colorPerVertex':
						case 'convex':
							if (parts.length != 2) {
								console.warn('Invalid format detected for ' + fieldName);
								break;
							}
M
r63  
Mr.doob 已提交
423

M
r66  
Mr.doob 已提交
424
							property = parts[1] === 'TRUE' ? true : false;
M
r63  
Mr.doob 已提交
425

M
r66  
Mr.doob 已提交
426 427
							break;
					}
M
r63  
Mr.doob 已提交
428

M
r66  
Mr.doob 已提交
429 430
					node[fieldName] = property;
				}
M
r63  
Mr.doob 已提交
431

M
r66  
Mr.doob 已提交
432 433
				return property;
			};
M
r59  
Mr.doob 已提交
434 435 436 437 438

			var getTree = function ( lines ) {

				var tree = { 'string': 'Scene', children: [] };
				var current = tree;
M
r66  
Mr.doob 已提交
439 440
				var matches;
				var specification;
M
r59  
Mr.doob 已提交
441 442 443

				for ( var i = 0; i < lines.length; i ++ ) {

M
r66  
Mr.doob 已提交
444
					var comment = '';
M
r62  
Mr.doob 已提交
445

M
r59  
Mr.doob 已提交
446 447
					var line = lines[ i ];

M
r66  
Mr.doob 已提交
448 449 450 451
					// omit whitespace only lines
					if ( null !== ( result = /^\s+?$/g.exec( line ) ) ) {
						continue;
					}
M
r62  
Mr.doob 已提交
452

M
r66  
Mr.doob 已提交
453
					line = line.trim();
M
r63  
Mr.doob 已提交
454

M
r66  
Mr.doob 已提交
455 456 457 458
					// skip empty lines
					if (line === '') {
						continue;
					}
M
r63  
Mr.doob 已提交
459

M
r62  
Mr.doob 已提交
460 461
					if ( /#/.exec( line ) ) {

M
r66  
Mr.doob 已提交
462
						var parts = line.split('#');
M
r59  
Mr.doob 已提交
463

M
r66  
Mr.doob 已提交
464 465
						// discard everything after the #, it is a comment
						line = parts[0];
M
r59  
Mr.doob 已提交
466

M
r66  
Mr.doob 已提交
467 468
						// well, let's also keep the comment
						comment = parts[1];
M
r62  
Mr.doob 已提交
469
					}
M
r63  
Mr.doob 已提交
470

M
r66  
Mr.doob 已提交
471
					if ( matches = /([^\s]*){1}\s?{/.exec( line ) ) { // first subpattern should match the Node name
M
r59  
Mr.doob 已提交
472

M
r63  
Mr.doob 已提交
473
						var block = { 'nodeType' : matches[1], 'string': line, 'parent': current, 'children': [],'comment' : comment};
M
r59  
Mr.doob 已提交
474 475 476 477
						current.children.push( block );
						current = block;

						if ( /}/.exec( line ) ) {
M
r66  
Mr.doob 已提交
478 479
							// example: geometry Box { size 1 1 1 } # all on the same line
							specification = /{(.*)}/.exec( line )[ 1 ];
M
r63  
Mr.doob 已提交
480

M
r66  
Mr.doob 已提交
481
							// todo: remove once new parsing is complete?
M
r63  
Mr.doob 已提交
482 483
							block.children.push( specification );

M
r66  
Mr.doob 已提交
484
							parseProperty(current, specification);
M
r59  
Mr.doob 已提交
485 486 487 488 489 490 491 492 493 494 495

							current = current.parent;

						}

					} else if ( /}/.exec( line ) ) {

						current = current.parent;

					} else if ( line !== '' ) {

M
r66  
Mr.doob 已提交
496 497 498
						parseProperty(current, line);
						// todo: remove once new parsing is complete? we still do not parse geometry and appearance the new way
						current.children.push( line );
M
r59  
Mr.doob 已提交
499 500 501 502 503

					}

				}

M
r66  
Mr.doob 已提交
504
				return tree;
M
r59  
Mr.doob 已提交
505 506 507 508 509 510 511 512 513 514
			}

			var parseNode = function ( data, parent ) {

				// console.log( data );

				if ( typeof data === 'string' ) {

					if ( /USE/.exec( data ) ) {

M
r66  
Mr.doob 已提交
515
						var defineKey = /USE\s+?(\w+)/.exec( data )[ 1 ];
M
r59  
Mr.doob 已提交
516

M
r66  
Mr.doob 已提交
517 518 519
						if (undefined == defines[defineKey]) {
							console.warn(defineKey + ' is not defined.');
						} else {
M
r59  
Mr.doob 已提交
520

M
r66  
Mr.doob 已提交
521
							if ( /appearance/.exec( data ) && defineKey ) {
M
r59  
Mr.doob 已提交
522

M
r66  
Mr.doob 已提交
523
								parent.material = defines[ defineKey ].clone();
M
r62  
Mr.doob 已提交
524

M
r66  
Mr.doob 已提交
525
							} else if ( /geometry/.exec( data ) && defineKey ) {
M
r62  
Mr.doob 已提交
526

M
r66  
Mr.doob 已提交
527
								parent.geometry = defines[ defineKey ].clone();
M
r63  
Mr.doob 已提交
528

M
r66  
Mr.doob 已提交
529 530 531 532 533
								// the solid property is not cloned with clone(), is only needed for VRML loading, so we need to transfer it
								if (undefined !== defines[ defineKey ].solid && defines[ defineKey ].solid === false) {
									parent.geometry.solid = false;
									parent.material.side = THREE.DoubleSide;
								}
M
r62  
Mr.doob 已提交
534

M
r66  
Mr.doob 已提交
535
							} else if (defineKey){
M
r63  
Mr.doob 已提交
536

M
r66  
Mr.doob 已提交
537 538
								var object = defines[ defineKey ].clone();
								parent.add( object );
M
r62  
Mr.doob 已提交
539

M
r66  
Mr.doob 已提交
540
							}
M
r62  
Mr.doob 已提交
541

M
r66  
Mr.doob 已提交
542
						}
M
r59  
Mr.doob 已提交
543 544 545 546 547 548 549 550 551

					}

					return;

				}

				var object = parent;

M
r63  
Mr.doob 已提交
552 553
				if ( 'Transform' === data.nodeType || 'Group' === data.nodeType ) {

M
r59  
Mr.doob 已提交
554 555 556
					object = new THREE.Object3D();

					if ( /DEF/.exec( data.string ) ) {
M
r62  
Mr.doob 已提交
557
						object.name = /DEF\s+(\w+)/.exec( data.string )[ 1 ];
M
r59  
Mr.doob 已提交
558 559 560
						defines[ object.name ] = object;
					}

M
r66  
Mr.doob 已提交
561
					if ( undefined !== data['translation'] ) {
M
r59  
Mr.doob 已提交
562

M
r66  
Mr.doob 已提交
563
						var t = data.translation;
M
r59  
Mr.doob 已提交
564

M
r66  
Mr.doob 已提交
565
						object.position.set(t.x, t.y, t.z);
M
r59  
Mr.doob 已提交
566

M
r66  
Mr.doob 已提交
567
					}
M
r59  
Mr.doob 已提交
568

M
r66  
Mr.doob 已提交
569
					if ( undefined !== data.rotation ) {
M
r59  
Mr.doob 已提交
570

M
r66  
Mr.doob 已提交
571
						var r = data.rotation;
M
r59  
Mr.doob 已提交
572

M
r66  
Mr.doob 已提交
573
						object.quaternion.setFromAxisAngle( new THREE.Vector3( r.x, r.y, r.z ), r.w );
M
r62  
Mr.doob 已提交
574

M
r66  
Mr.doob 已提交
575
					}
M
r59  
Mr.doob 已提交
576

M
r66  
Mr.doob 已提交
577
					if ( undefined !== data.scale ) {
M
r59  
Mr.doob 已提交
578

M
r66  
Mr.doob 已提交
579
						var s = data.scale;
M
r59  
Mr.doob 已提交
580

M
r66  
Mr.doob 已提交
581
						object.scale.set( s.x, s.y, s.z );
M
r59  
Mr.doob 已提交
582

M
r66  
Mr.doob 已提交
583
					}
M
r59  
Mr.doob 已提交
584 585 586

					parent.add( object );

M
r63  
Mr.doob 已提交
587
				} else if ( 'Shape' === data.nodeType ) {
M
r59  
Mr.doob 已提交
588 589 590 591

					object = new THREE.Mesh();

					if ( /DEF/.exec( data.string ) ) {
M
r63  
Mr.doob 已提交
592

M
r59  
Mr.doob 已提交
593
						object.name = /DEF (\w+)/.exec( data.string )[ 1 ];
M
r63  
Mr.doob 已提交
594

M
r59  
Mr.doob 已提交
595 596 597 598 599
						defines[ object.name ] = object;
					}

					parent.add( object );

M
r63  
Mr.doob 已提交
600
				} else if ( 'Background' === data.nodeType ) {
M
r59  
Mr.doob 已提交
601

M
r66  
Mr.doob 已提交
602
					var segments = 20;
M
r59  
Mr.doob 已提交
603

M
r66  
Mr.doob 已提交
604 605
					// sky (full sphere):
					var radius = 2e4;
M
r59  
Mr.doob 已提交
606

M
r66  
Mr.doob 已提交
607
					var skyGeometry = new THREE.SphereGeometry( radius, segments, segments );
M
r59  
Mr.doob 已提交
608

M
r66  
Mr.doob 已提交
609
					var skyMaterial = new THREE.MeshBasicMaterial( { color: 'white', vertexColors: THREE.VertexColors, shading: THREE.NoShading } );
M
r59  
Mr.doob 已提交
610

M
r66  
Mr.doob 已提交
611
					skyMaterial.side = THREE.BackSide;
M
r59  
Mr.doob 已提交
612

M
r66  
Mr.doob 已提交
613
					skyMaterial.fog = false;
M
r59  
Mr.doob 已提交
614

M
r66  
Mr.doob 已提交
615
					skyMaterial.color = new THREE.Color();
M
r59  
Mr.doob 已提交
616

M
r66  
Mr.doob 已提交
617
					paintFaces( skyGeometry, radius, data.skyAngle, data.skyColor, true );
M
r59  
Mr.doob 已提交
618

M
r66  
Mr.doob 已提交
619
					var sky = new THREE.Mesh( skyGeometry, skyMaterial );
M
r59  
Mr.doob 已提交
620

M
r66  
Mr.doob 已提交
621
					scene.add( sky );
M
r59  
Mr.doob 已提交
622

M
r66  
Mr.doob 已提交
623
					// ground (half sphere):
M
r59  
Mr.doob 已提交
624

M
r66  
Mr.doob 已提交
625
					radius = 1.2e4;
M
r59  
Mr.doob 已提交
626

M
r66  
Mr.doob 已提交
627
					var groundGeometry = new THREE.SphereGeometry( radius, segments, segments, 0, 2 * Math.PI, 0.5 * Math.PI, 1.5 * Math.PI );
M
r59  
Mr.doob 已提交
628

M
r66  
Mr.doob 已提交
629
					var groundMaterial = new THREE.MeshBasicMaterial( { color: 'white', vertexColors: THREE.VertexColors, shading: THREE.NoShading } );
M
r59  
Mr.doob 已提交
630

M
r66  
Mr.doob 已提交
631
					groundMaterial.side = THREE.BackSide;
M
r59  
Mr.doob 已提交
632

M
r66  
Mr.doob 已提交
633
					groundMaterial.fog = false;
M
r59  
Mr.doob 已提交
634

M
r66  
Mr.doob 已提交
635
					groundMaterial.color = new THREE.Color();
M
r59  
Mr.doob 已提交
636

M
r66  
Mr.doob 已提交
637
					paintFaces( groundGeometry, radius, data.groundAngle, data.groundColor, false );
M
r59  
Mr.doob 已提交
638

M
r66  
Mr.doob 已提交
639
					var ground = new THREE.Mesh( groundGeometry, groundMaterial );
M
r59  
Mr.doob 已提交
640

M
r66  
Mr.doob 已提交
641
					scene.add( ground );
M
r59  
Mr.doob 已提交
642

M
r63  
Mr.doob 已提交
643
				} else if ( /geometry/.exec( data.string ) ) {
M
r59  
Mr.doob 已提交
644

M
r63  
Mr.doob 已提交
645
					if ( 'Box' === data.nodeType ) {
M
r59  
Mr.doob 已提交
646

M
r66  
Mr.doob 已提交
647
						var s = data.size;
M
r59  
Mr.doob 已提交
648

M
r66  
Mr.doob 已提交
649
						parent.geometry = new THREE.BoxGeometry( s.x, s.y, s.z );
M
r59  
Mr.doob 已提交
650

M
r63  
Mr.doob 已提交
651
					} else if ( 'Cylinder' === data.nodeType ) {
M
r59  
Mr.doob 已提交
652

M
r63  
Mr.doob 已提交
653
						parent.geometry = new THREE.CylinderGeometry( data.radius, data.radius, data.height );
M
r59  
Mr.doob 已提交
654

M
r63  
Mr.doob 已提交
655
					} else if ( 'Cone' === data.nodeType ) {
M
r59  
Mr.doob 已提交
656

M
r63  
Mr.doob 已提交
657
						parent.geometry = new THREE.CylinderGeometry( data.topRadius, data.bottomRadius, data.height );
M
r59  
Mr.doob 已提交
658

M
r63  
Mr.doob 已提交
659
					} else if ( 'Sphere' === data.nodeType ) {
M
r59  
Mr.doob 已提交
660

M
r63  
Mr.doob 已提交
661
						parent.geometry = new THREE.SphereGeometry( data.radius );
M
r59  
Mr.doob 已提交
662

M
r63  
Mr.doob 已提交
663
					} else if ( 'IndexedFaceSet' === data.nodeType ) {
M
r62  
Mr.doob 已提交
664

M
r66  
Mr.doob 已提交
665
						var geometry = new THREE.Geometry();
M
r62  
Mr.doob 已提交
666

M
r66  
Mr.doob 已提交
667
						var indexes;
M
r62  
Mr.doob 已提交
668

M
r66  
Mr.doob 已提交
669
						for ( var i = 0, j = data.children.length; i < j; i++ ) {
M
r62  
Mr.doob 已提交
670

M
r66  
Mr.doob 已提交
671
							var child = data.children[ i ];
M
r62  
Mr.doob 已提交
672

M
r66  
Mr.doob 已提交
673
							var vec;
M
r62  
Mr.doob 已提交
674

M
r66  
Mr.doob 已提交
675
							if ( 'Coordinate' === child.nodeType ) {
M
r62  
Mr.doob 已提交
676

M
r66  
Mr.doob 已提交
677
								for ( var k = 0, l = child.points.length; k < l; k++ ) {
M
r62  
Mr.doob 已提交
678

M
r66  
Mr.doob 已提交
679
									var point = child.points[ k ];
M
r62  
Mr.doob 已提交
680

M
r66  
Mr.doob 已提交
681
									vec = new THREE.Vector3( point.x, point.y, point.z );
M
r62  
Mr.doob 已提交
682

M
r66  
Mr.doob 已提交
683 684
									geometry.vertices.push( vec );
								}
M
r62  
Mr.doob 已提交
685

M
r66  
Mr.doob 已提交
686 687 688
								break;
							}
						}
M
r62  
Mr.doob 已提交
689

M
r66  
Mr.doob 已提交
690
						var skip = 0;
M
r63  
Mr.doob 已提交
691

M
r66  
Mr.doob 已提交
692 693
						// read this: http://math.hws.edu/eck/cs424/notes2013/16_Threejs_Advanced.html
						for ( var i = 0, j = data.coordIndex.length; i < j; i++ ) {
M
r62  
Mr.doob 已提交
694

M
r66  
Mr.doob 已提交
695
							indexes = data.coordIndex[i];
M
r62  
Mr.doob 已提交
696

M
r66  
Mr.doob 已提交
697 698
							// vrml support multipoint indexed face sets (more then 3 vertices). You must calculate the composing triangles here
							skip = 0;
M
r62  
Mr.doob 已提交
699

M
r66  
Mr.doob 已提交
700
							// todo: this is the time to check if the faces are ordered ccw or not (cw)
M
r62  
Mr.doob 已提交
701

M
r66  
Mr.doob 已提交
702 703
							// Face3 only works with triangles, but IndexedFaceSet allows shapes with more then three vertices, build them of triangles
							while ( indexes.length >= 3 && skip < ( indexes.length -2 ) ) {
M
r62  
Mr.doob 已提交
704

M
r66  
Mr.doob 已提交
705 706 707 708 709 710 711
								var face = new THREE.Face3(
									indexes[0],
									indexes[skip + 1],
									indexes[skip + 2],
									null // normal, will be added later
									// todo: pass in the color, if a color index is present
								);
M
r63  
Mr.doob 已提交
712

M
r66  
Mr.doob 已提交
713
								skip++;
M
r63  
Mr.doob 已提交
714

M
r66  
Mr.doob 已提交
715
								geometry.faces.push( face );
M
r62  
Mr.doob 已提交
716

M
r66  
Mr.doob 已提交
717
							}
M
r62  
Mr.doob 已提交
718 719


M
r66  
Mr.doob 已提交
720
						}
M
r62  
Mr.doob 已提交
721

M
r66  
Mr.doob 已提交
722 723 724
						if ( false === data.solid ) {
							parent.material.side = THREE.DoubleSide;
						}
M
r63  
Mr.doob 已提交
725

M
r66  
Mr.doob 已提交
726 727
						// we need to store it on the geometry for use with defines
						geometry.solid = data.solid;
M
r63  
Mr.doob 已提交
728

M
r66  
Mr.doob 已提交
729 730 731
						geometry.computeFaceNormals();
						//geometry.computeVertexNormals(); // does not show
						geometry.computeBoundingSphere();
M
r62  
Mr.doob 已提交
732

M
r66  
Mr.doob 已提交
733 734 735 736 737
						// see if it's a define
						if ( /DEF/.exec( data.string ) ) {
							geometry.name = /DEF (\w+)/.exec( data.string )[ 1 ];
							defines[ geometry.name ] = geometry;
						}
M
r62  
Mr.doob 已提交
738

M
r66  
Mr.doob 已提交
739
						parent.geometry = geometry;
M
r59  
Mr.doob 已提交
740 741 742 743 744 745 746 747 748 749
					}

					return;

				} else if ( /appearance/.exec( data.string ) ) {

					for ( var i = 0; i < data.children.length; i ++ ) {

						var child = data.children[ i ];

M
r63  
Mr.doob 已提交
750
						if ( 'Material' === child.nodeType ) {
M
r59  
Mr.doob 已提交
751 752
							var material = new THREE.MeshPhongMaterial();

M
r66  
Mr.doob 已提交
753
							if ( undefined !== child.diffuseColor ) {
M
r59  
Mr.doob 已提交
754

M
r66  
Mr.doob 已提交
755
								var d = child.diffuseColor;
M
r59  
Mr.doob 已提交
756

M
r66  
Mr.doob 已提交
757
								material.color.setRGB( d.r, d.g, d.b );
M
r59  
Mr.doob 已提交
758

M
r66  
Mr.doob 已提交
759
							}
M
r59  
Mr.doob 已提交
760

M
r66  
Mr.doob 已提交
761
							if ( undefined !== child.emissiveColor ) {
M
r59  
Mr.doob 已提交
762

M
r66  
Mr.doob 已提交
763
								var e = child.emissiveColor;
M
r59  
Mr.doob 已提交
764

M
r66  
Mr.doob 已提交
765
								material.emissive.setRGB( e.r, e.g, e.b );
M
r59  
Mr.doob 已提交
766

M
r66  
Mr.doob 已提交
767
							}
M
r59  
Mr.doob 已提交
768

M
r66  
Mr.doob 已提交
769
							if ( undefined !== child.specularColor ) {
M
r59  
Mr.doob 已提交
770

M
r66  
Mr.doob 已提交
771
								var s = child.specularColor;
M
r59  
Mr.doob 已提交
772

M
r66  
Mr.doob 已提交
773
								material.specular.setRGB( s.r, s.g, s.b );
M
r59  
Mr.doob 已提交
774

M
r66  
Mr.doob 已提交
775
							}
M
r59  
Mr.doob 已提交
776

M
r66  
Mr.doob 已提交
777
							if ( undefined !== child.transparency ) {
M
r59  
Mr.doob 已提交
778

M
r66  
Mr.doob 已提交
779
								var t = child.transparency;
M
r59  
Mr.doob 已提交
780

M
r66  
Mr.doob 已提交
781 782
								// transparency is opposite of opacity
								material.opacity = Math.abs( 1 - t );
M
r63  
Mr.doob 已提交
783

M
r66  
Mr.doob 已提交
784
								material.transparent = true;
M
r63  
Mr.doob 已提交
785

M
r66  
Mr.doob 已提交
786
							}
M
r59  
Mr.doob 已提交
787 788 789 790

							if ( /DEF/.exec( data.string ) ) {

								material.name = /DEF (\w+)/.exec( data.string )[ 1 ];
M
r62  
Mr.doob 已提交
791

M
r59  
Mr.doob 已提交
792 793 794 795 796 797
								defines[ material.name ] = material;

							}

							parent.material = material;

M
r66  
Mr.doob 已提交
798 799
							// material found, stop looping
							break;
M
r59  
Mr.doob 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
						}

					}

					return;

				}

				for ( var i = 0, l = data.children.length; i < l; i ++ ) {

					var child = data.children[ i ];

					parseNode( data.children[ i ], object );

				}

			}

			parseNode( getTree( lines ), scene );

		};

		var scene = new THREE.Scene();

		var lines = data.split( '\n' );
M
r63  
Mr.doob 已提交
825

M
r59  
Mr.doob 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
		var header = lines.shift();

		if ( /V1.0/.exec( header ) ) {

			parseV1( lines, scene );

		} else if ( /V2.0/.exec( header ) ) {

			parseV2( lines, scene );

		}

		return scene;

	}

};

THREE.EventDispatcher.prototype.apply( THREE.VRMLLoader.prototype );
M
r63  
Mr.doob 已提交
845