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

M
r72  
Mr.doob 已提交
5 6 7 8 9
THREE.VRMLLoader = function ( manager ) {

	this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;

};
M
r59  
Mr.doob 已提交
10 11 12

THREE.VRMLLoader.prototype = {

M
r62  
Mr.doob 已提交
13
	constructor: THREE.VRMLLoader,
M
r59  
Mr.doob 已提交
14

M
r66  
Mr.doob 已提交
15 16 17 18 19
	// for IndexedFaceSet support
	isRecordingPoints: false,
	isRecordingFaces: false,
	points: [],
	indexes : [],
M
r63  
Mr.doob 已提交
20

M
r66  
Mr.doob 已提交
21 22 23 24 25
	// for Background support
	isRecordingAngles: false,
	isRecordingColors: false,
	angles: [],
	colors: [],
M
r63  
Mr.doob 已提交
26

M
r66  
Mr.doob 已提交
27
	recordingFieldname: null,
M
r63  
Mr.doob 已提交
28

M
r72  
Mr.doob 已提交
29
	load: function ( url, onLoad, onProgress, onError ) {
M
r59  
Mr.doob 已提交
30 31 32

		var scope = this;

M
r72  
Mr.doob 已提交
33 34 35
		var loader = new THREE.XHRLoader( this.manager );
		loader.setCrossOrigin( this.crossOrigin );
		loader.load( url, function ( text ) {
M
r66  
Mr.doob 已提交
36

M
r72  
Mr.doob 已提交
37
			onLoad( scope.parse( text ) );
M
r59  
Mr.doob 已提交
38

M
r72  
Mr.doob 已提交
39
		}, onProgress, onError );
M
r59  
Mr.doob 已提交
40

M
r72  
Mr.doob 已提交
41
	},
M
r59  
Mr.doob 已提交
42

M
r72  
Mr.doob 已提交
43
	setCrossOrigin: function ( value ) {
M
r59  
Mr.doob 已提交
44

M
r72  
Mr.doob 已提交
45
		this.crossOrigin = value;
M
r59  
Mr.doob 已提交
46

M
r73  
Mr.doob 已提交
47 48
		THREE.ImageUtils.crossOrigin = value;

M
r59  
Mr.doob 已提交
49 50 51 52
	},

	parse: function ( data ) {

M
r73  
Mr.doob 已提交
53 54
		var texturePath = this.texturePath || '';

M
r59  
Mr.doob 已提交
55 56 57 58 59 60 61
		var parseV1 = function ( lines, scene ) {

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

		};

		var parseV2 = function ( lines, scene ) {
M
r69  
Mr.doob 已提交
62

M
r66  
Mr.doob 已提交
63 64
			var defines = {};
			var float_pattern = /(\b|\-|\+)([\d\.e]+)/;
M
r73  
Mr.doob 已提交
65
			var float2_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
M
r69  
Mr.doob 已提交
66
			var float3_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
M
r66  
Mr.doob 已提交
67 68 69 70 71 72 73 74 75 76

			/**
			* Interpolates colors a and b following their relative distance
			* expressed by t.
			*
			* @param float a
			* @param float b
			* @param float t
			* @returns {Color}
			*/
M
r72  
Mr.doob 已提交
77 78
			var interpolateColors = function( a, b, t ) {

M
r69  
Mr.doob 已提交
79 80 81
				var deltaR = a.r - b.r;
				var deltaG = a.g - b.g;
				var deltaB = a.b - b.b;
M
r66  
Mr.doob 已提交
82

M
r69  
Mr.doob 已提交
83
				var c = new THREE.Color();
M
r66  
Mr.doob 已提交
84

M
r69  
Mr.doob 已提交
85 86 87
				c.r = a.r - t * deltaR;
				c.g = a.g - t * deltaG;
				c.b = a.b - t * deltaB;
M
r66  
Mr.doob 已提交
88

M
r69  
Mr.doob 已提交
89
				return c;
M
r72  
Mr.doob 已提交
90

M
r66  
Mr.doob 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
			};

			/**
			 * 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.
			 */
M
r72  
Mr.doob 已提交
115
			var paintFaces = function ( geometry, radius, angles, colors, directionIsDown ) {
M
r66  
Mr.doob 已提交
116 117 118

				var f, n, p, vertexIndex, color;

M
r72  
Mr.doob 已提交
119
				var direction = directionIsDown ? 1 : - 1;
M
r66  
Mr.doob 已提交
120 121 122 123 124

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

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

M
r71  
Mr.doob 已提交
125
				for ( var k = 0; k < angles.length; k ++ ) {
M
r66  
Mr.doob 已提交
126 127 128 129

					var vec = { };

					// push the vector at which the color changes
M
r72  
Mr.doob 已提交
130
					vec.y = direction * ( Math.cos( angles[ k ] ) * radius );
M
r66  
Mr.doob 已提交
131

M
r72  
Mr.doob 已提交
132
					vec.x = direction * ( Math.sin( angles[ k ] ) * radius );
M
r66  
Mr.doob 已提交
133 134 135 136 137 138

					coord.push( vec );

				}

				// painting the colors on the faces
M
r71  
Mr.doob 已提交
139
				for ( var i = 0; i < geometry.faces.length ; i ++ ) {
M
r66  
Mr.doob 已提交
140 141 142 143 144

					f  = geometry.faces[ i ];

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

M
r71  
Mr.doob 已提交
145
					for ( var j = 0; j < n; j ++ ) {
M
r66  
Mr.doob 已提交
146 147 148 149 150

						vertexIndex = f[ faceIndices[ j ] ];

						p = geometry.vertices[ vertexIndex ];

M
r71  
Mr.doob 已提交
151
						for ( var index = 0; index < colors.length; index ++ ) {
M
r66  
Mr.doob 已提交
152 153 154 155 156 157

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

								A.x = 0;
M
r72  
Mr.doob 已提交
158
								A.y = directionIsDown ? radius : - 1 * radius;
M
r66  
Mr.doob 已提交
159 160 161

							} else {

M
r71  
Mr.doob 已提交
162 163
								A.x = coord[ index - 1 ].x;
								A.y = coord[ index - 1 ].y;
M
r66  
Mr.doob 已提交
164 165

							}
M
r63  
Mr.doob 已提交
166

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

M
r66  
Mr.doob 已提交
170
							if ( undefined !== B ) {
M
r72  
Mr.doob 已提交
171

M
r66  
Mr.doob 已提交
172 173
								// 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 已提交
174

M
r72  
Mr.doob 已提交
175
								if ( applyColor ) {
M
r63  
Mr.doob 已提交
176

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

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

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

M
r66  
Mr.doob 已提交
184 185
									// 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 已提交
186

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

M
r66  
Mr.doob 已提交
189
								}
M
r63  
Mr.doob 已提交
190

M
r66  
Mr.doob 已提交
191
							} else if ( undefined === f.vertexColors[ j ] ) {
M
r72  
Mr.doob 已提交
192

M
r71  
Mr.doob 已提交
193
								colorIndex = directionIsDown ? colors.length - 1 : 0;
M
r66  
Mr.doob 已提交
194
								f.vertexColors[ j ] = colors[ colorIndex ];
M
r63  
Mr.doob 已提交
195

M
r66  
Mr.doob 已提交
196
							}
M
r72  
Mr.doob 已提交
197

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

					}
M
r63  
Mr.doob 已提交
201

M
r66  
Mr.doob 已提交
202
				}
M
r72  
Mr.doob 已提交
203

M
r66  
Mr.doob 已提交
204 205
			};

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

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

M
r66  
Mr.doob 已提交
210 211 212 213 214
				/**
				 * 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 已提交
215

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

M
r72  
Mr.doob 已提交
218 219 220 221
				while ( null != ( part = regex.exec( line ) ) ) {

					parts.push( part[ 0 ] );

M
r66  
Mr.doob 已提交
222
				}
M
r63  
Mr.doob 已提交
223

M
r72  
Mr.doob 已提交
224
				fieldName = parts[ 0 ];
M
r66  
Mr.doob 已提交
225 226 227


				// trigger several recorders
M
r72  
Mr.doob 已提交
228
				switch ( fieldName ) {
M
r66  
Mr.doob 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
					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':
M
r73  
Mr.doob 已提交
247
					case 'texCoordIndex':
M
r66  
Mr.doob 已提交
248 249 250 251
						this.recordingFieldname = fieldName;
						this.isRecordingFaces = true;
						this.indexes = [];
				}
M
r63  
Mr.doob 已提交
252

M
r72  
Mr.doob 已提交
253
				if ( this.isRecordingFaces ) {
M
r63  
Mr.doob 已提交
254

M
r66  
Mr.doob 已提交
255
					// the parts hold the indexes as strings
M
r72  
Mr.doob 已提交
256 257
					if ( parts.length > 0 ) {

M
r66  
Mr.doob 已提交
258
						index = [];
M
r63  
Mr.doob 已提交
259

M
r72  
Mr.doob 已提交
260
						for ( var ind = 0; ind < parts.length; ind ++ ) {
M
r63  
Mr.doob 已提交
261

M
r66  
Mr.doob 已提交
262
							// the part should either be positive integer or -1
M
r72  
Mr.doob 已提交
263 264
							if ( ! /(-?\d+)/.test( parts[ ind ] ) ) {

M
r66  
Mr.doob 已提交
265
								continue;
M
r72  
Mr.doob 已提交
266

M
r66  
Mr.doob 已提交
267
							}
M
r63  
Mr.doob 已提交
268

M
r66  
Mr.doob 已提交
269
							// end of current face
M
r72  
Mr.doob 已提交
270 271 272 273 274 275
							if ( parts[ ind ] === "-1" ) {

								if ( index.length > 0 ) {

									this.indexes.push( index );

M
r66  
Mr.doob 已提交
276
								}
M
r63  
Mr.doob 已提交
277

M
r66  
Mr.doob 已提交
278 279
								// start new one
								index = [];
M
r72  
Mr.doob 已提交
280

M
r66  
Mr.doob 已提交
281
							} else {
M
r72  
Mr.doob 已提交
282 283 284

								index.push( parseInt( parts[ ind ] ) );

M
r66  
Mr.doob 已提交
285
							}
M
r72  
Mr.doob 已提交
286

M
r66  
Mr.doob 已提交
287
						}
M
r63  
Mr.doob 已提交
288

M
r66  
Mr.doob 已提交
289
					}
M
r63  
Mr.doob 已提交
290

M
r66  
Mr.doob 已提交
291
					// end
M
r72  
Mr.doob 已提交
292 293
					if ( /]/.exec( line ) ) {

M
r66  
Mr.doob 已提交
294
						this.isRecordingFaces = false;
M
r73  
Mr.doob 已提交
295
						node[this.recordingFieldname] = this.indexes;
M
r72  
Mr.doob 已提交
296

M
r66  
Mr.doob 已提交
297
					}
M
r63  
Mr.doob 已提交
298

M
r72  
Mr.doob 已提交
299 300
				} else if ( this.isRecordingPoints ) {

M
r73  
Mr.doob 已提交
301 302
					if ( node.nodeType == 'Coordinate' )

M
r72  
Mr.doob 已提交
303
					while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
M
r63  
Mr.doob 已提交
304

M
r66  
Mr.doob 已提交
305
						point = {
M
r72  
Mr.doob 已提交
306 307 308
							x: parseFloat( parts[ 1 ] ),
							y: parseFloat( parts[ 2 ] ),
							z: parseFloat( parts[ 3 ] )
M
r66  
Mr.doob 已提交
309
						};
M
r63  
Mr.doob 已提交
310

M
r72  
Mr.doob 已提交
311 312
						this.points.push( point );

M
r66  
Mr.doob 已提交
313
					}
M
r63  
Mr.doob 已提交
314

M
r73  
Mr.doob 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327
					if ( node.nodeType == 'TextureCoordinate' )

					while ( null !== ( parts = float2_pattern.exec( line ) ) ) {

						point = {
							x: parseFloat( parts[ 1 ] ),
							y: parseFloat( parts[ 2 ] )
						};

						this.points.push( point );

					}

M
r66  
Mr.doob 已提交
328
					// end
M
r72  
Mr.doob 已提交
329 330
					if ( /]/.exec( line ) ) {

M
r66  
Mr.doob 已提交
331 332
						this.isRecordingPoints = false;
						node.points = this.points;
M
r72  
Mr.doob 已提交
333

M
r66  
Mr.doob 已提交
334
					}
M
r63  
Mr.doob 已提交
335

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

M
r66  
Mr.doob 已提交
338 339
					// the parts hold the angles as strings
					if ( parts.length > 0 ) {
M
r63  
Mr.doob 已提交
340

M
r71  
Mr.doob 已提交
341
						for ( var ind = 0; ind < parts.length; ind ++ ) {
M
r63  
Mr.doob 已提交
342

M
r66  
Mr.doob 已提交
343
							// the part should be a float
M
r72  
Mr.doob 已提交
344 345
							if ( ! float_pattern.test( parts[ ind ] ) ) {

M
r66  
Mr.doob 已提交
346
								continue;
M
r72  
Mr.doob 已提交
347

M
r66  
Mr.doob 已提交
348
							}
M
r63  
Mr.doob 已提交
349

M
r72  
Mr.doob 已提交
350 351
							this.angles.push( parseFloat( parts[ ind ] ) );

M
r66  
Mr.doob 已提交
352
						}
M
r63  
Mr.doob 已提交
353

M
r66  
Mr.doob 已提交
354
					}
M
r63  
Mr.doob 已提交
355

M
r66  
Mr.doob 已提交
356
					// end
M
r72  
Mr.doob 已提交
357 358
					if ( /]/.exec( line ) ) {

M
r66  
Mr.doob 已提交
359
						this.isRecordingAngles = false;
M
r72  
Mr.doob 已提交
360 361
						node[ this.recordingFieldname ] = this.angles;

M
r66  
Mr.doob 已提交
362
					}
M
r63  
Mr.doob 已提交
363

M
r72  
Mr.doob 已提交
364
				} else if ( this.isRecordingColors ) {
M
r63  
Mr.doob 已提交
365

M
r72  
Mr.doob 已提交
366
					while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
M
r63  
Mr.doob 已提交
367

M
r66  
Mr.doob 已提交
368
						color = {
M
r72  
Mr.doob 已提交
369 370 371
							r: parseFloat( parts[ 1 ] ),
							g: parseFloat( parts[ 2 ] ),
							b: parseFloat( parts[ 3 ] )
M
r66  
Mr.doob 已提交
372
						};
M
r63  
Mr.doob 已提交
373

M
r72  
Mr.doob 已提交
374
						this.colors.push( color );
M
r63  
Mr.doob 已提交
375

M
r66  
Mr.doob 已提交
376
					}
M
r63  
Mr.doob 已提交
377

M
r66  
Mr.doob 已提交
378
					// end
M
r72  
Mr.doob 已提交
379 380
					if ( /]/.exec( line ) ) {

M
r66  
Mr.doob 已提交
381
						this.isRecordingColors = false;
M
r72  
Mr.doob 已提交
382 383
						node[ this.recordingFieldname ] = this.colors;

M
r66  
Mr.doob 已提交
384
					}
M
r63  
Mr.doob 已提交
385

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

M
r72  
Mr.doob 已提交
388
					switch ( fieldName ) {
M
r63  
Mr.doob 已提交
389

M
r66  
Mr.doob 已提交
390 391 392 393
						case 'diffuseColor':
						case 'emissiveColor':
						case 'specularColor':
						case 'color':
M
r63  
Mr.doob 已提交
394

M
r72  
Mr.doob 已提交
395 396 397
							if ( parts.length != 4 ) {

								console.warn( 'Invalid color format detected for ' + fieldName );
M
r66  
Mr.doob 已提交
398
								break;
M
r72  
Mr.doob 已提交
399

M
r66  
Mr.doob 已提交
400
							}
M
r63  
Mr.doob 已提交
401

M
r66  
Mr.doob 已提交
402
							property = {
M
r72  
Mr.doob 已提交
403 404 405 406
								r: parseFloat( parts[ 1 ] ),
								g: parseFloat( parts[ 2 ] ),
								b: parseFloat( parts[ 3 ] )
							};
M
r63  
Mr.doob 已提交
407

M
r66  
Mr.doob 已提交
408
							break;
M
r63  
Mr.doob 已提交
409

M
r66  
Mr.doob 已提交
410 411 412
						case 'translation':
						case 'scale':
						case 'size':
M
r72  
Mr.doob 已提交
413 414 415
							if ( parts.length != 4 ) {

								console.warn( 'Invalid vector format detected for ' + fieldName );
M
r66  
Mr.doob 已提交
416
								break;
M
r72  
Mr.doob 已提交
417

M
r66  
Mr.doob 已提交
418
							}
M
r63  
Mr.doob 已提交
419

M
r66  
Mr.doob 已提交
420
							property = {
M
r72  
Mr.doob 已提交
421 422 423 424
								x: parseFloat( parts[ 1 ] ),
								y: parseFloat( parts[ 2 ] ),
								z: parseFloat( parts[ 3 ] )
							};
M
r63  
Mr.doob 已提交
425

M
r66  
Mr.doob 已提交
426 427 428 429 430 431 432 433 434
							break;

						case 'radius':
						case 'topRadius':
						case 'bottomRadius':
						case 'height':
						case 'transparency':
						case 'shininess':
						case 'ambientIntensity':
M
r72  
Mr.doob 已提交
435 436 437
							if ( parts.length != 2 ) {

								console.warn( 'Invalid single float value specification detected for ' + fieldName );
M
r66  
Mr.doob 已提交
438
								break;
M
r72  
Mr.doob 已提交
439

M
r66  
Mr.doob 已提交
440
							}
M
r63  
Mr.doob 已提交
441

M
r72  
Mr.doob 已提交
442
							property = parseFloat( parts[ 1 ] );
M
r63  
Mr.doob 已提交
443

M
r66  
Mr.doob 已提交
444
							break;
M
r63  
Mr.doob 已提交
445

M
r66  
Mr.doob 已提交
446
						case 'rotation':
M
r72  
Mr.doob 已提交
447 448 449
							if ( parts.length != 5 ) {

								console.warn( 'Invalid quaternion format detected for ' + fieldName );
M
r66  
Mr.doob 已提交
450
								break;
M
r72  
Mr.doob 已提交
451

M
r66  
Mr.doob 已提交
452
							}
M
r63  
Mr.doob 已提交
453

M
r66  
Mr.doob 已提交
454
							property = {
M
r72  
Mr.doob 已提交
455 456 457 458 459
								x: parseFloat( parts[ 1 ] ),
								y: parseFloat( parts[ 2 ] ),
								z: parseFloat( parts[ 3 ] ),
								w: parseFloat( parts[ 4 ] )
							};
M
r63  
Mr.doob 已提交
460

M
r66  
Mr.doob 已提交
461
							break;
M
r63  
Mr.doob 已提交
462

M
r66  
Mr.doob 已提交
463 464 465 466
						case 'ccw':
						case 'solid':
						case 'colorPerVertex':
						case 'convex':
M
r72  
Mr.doob 已提交
467 468 469
							if ( parts.length != 2 ) {

								console.warn( 'Invalid format detected for ' + fieldName );
M
r66  
Mr.doob 已提交
470
								break;
M
r72  
Mr.doob 已提交
471

M
r66  
Mr.doob 已提交
472
							}
M
r63  
Mr.doob 已提交
473

M
r72  
Mr.doob 已提交
474
							property = parts[ 1 ] === 'TRUE' ? true : false;
M
r63  
Mr.doob 已提交
475

M
r66  
Mr.doob 已提交
476 477
							break;
					}
M
r63  
Mr.doob 已提交
478

M
r72  
Mr.doob 已提交
479 480
					node[ fieldName ] = property;

M
r66  
Mr.doob 已提交
481
				}
M
r63  
Mr.doob 已提交
482

M
r66  
Mr.doob 已提交
483
				return property;
M
r72  
Mr.doob 已提交
484

M
r66  
Mr.doob 已提交
485
			};
M
r59  
Mr.doob 已提交
486 487 488 489 490

			var getTree = function ( lines ) {

				var tree = { 'string': 'Scene', children: [] };
				var current = tree;
M
r66  
Mr.doob 已提交
491 492
				var matches;
				var specification;
M
r59  
Mr.doob 已提交
493 494 495

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

M
r66  
Mr.doob 已提交
496
					var comment = '';
M
r62  
Mr.doob 已提交
497

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

M
r66  
Mr.doob 已提交
500 501
					// omit whitespace only lines
					if ( null !== ( result = /^\s+?$/g.exec( line ) ) ) {
M
r72  
Mr.doob 已提交
502

M
r66  
Mr.doob 已提交
503
						continue;
M
r72  
Mr.doob 已提交
504

M
r66  
Mr.doob 已提交
505
					}
M
r62  
Mr.doob 已提交
506

M
r66  
Mr.doob 已提交
507
					line = line.trim();
M
r63  
Mr.doob 已提交
508

M
r66  
Mr.doob 已提交
509
					// skip empty lines
M
r72  
Mr.doob 已提交
510 511
					if ( line === '' ) {

M
r66  
Mr.doob 已提交
512
						continue;
M
r72  
Mr.doob 已提交
513

M
r66  
Mr.doob 已提交
514
					}
M
r63  
Mr.doob 已提交
515

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

M
r72  
Mr.doob 已提交
518
						var parts = line.split( '#' );
M
r59  
Mr.doob 已提交
519

M
r66  
Mr.doob 已提交
520
						// discard everything after the #, it is a comment
M
r72  
Mr.doob 已提交
521
						line = parts[ 0 ];
M
r59  
Mr.doob 已提交
522

M
r66  
Mr.doob 已提交
523
						// well, let's also keep the comment
M
r72  
Mr.doob 已提交
524 525
						comment = parts[ 1 ];

M
r62  
Mr.doob 已提交
526
					}
M
r63  
Mr.doob 已提交
527

M
r73  
Mr.doob 已提交
528
					if ( matches = /([^\s]*){1}(?:\s+)?{/.exec( line ) ) {
M
r59  
Mr.doob 已提交
529

M
r72  
Mr.doob 已提交
530 531 532
						// first subpattern should match the Node name

						var block = { 'nodeType' : matches[ 1 ], 'string': line, 'parent': current, 'children': [], 'comment' : comment };
M
r59  
Mr.doob 已提交
533 534 535 536
						current.children.push( block );
						current = block;

						if ( /}/.exec( line ) ) {
M
r72  
Mr.doob 已提交
537

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

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

M
r72  
Mr.doob 已提交
544
							parseProperty( current, specification );
M
r59  
Mr.doob 已提交
545 546 547 548 549 550 551 552 553 554 555

							current = current.parent;

						}

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

						current = current.parent;

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

M
r72  
Mr.doob 已提交
556
						parseProperty( current, line );
M
r66  
Mr.doob 已提交
557 558
						// 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 已提交
559 560 561 562 563

					}

				}

M
r66  
Mr.doob 已提交
564
				return tree;
M
r72  
Mr.doob 已提交
565 566

			};
M
r59  
Mr.doob 已提交
567 568 569 570 571 572 573 574 575

			var parseNode = function ( data, parent ) {

				// console.log( data );

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

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

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

M
r72  
Mr.doob 已提交
578 579 580 581
						if ( undefined == defines[ defineKey ] ) {

							console.warn( defineKey + ' is not defined.' );

M
r66  
Mr.doob 已提交
582
						} else {
M
r59  
Mr.doob 已提交
583

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

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

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

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

M
r66  
Mr.doob 已提交
592
								// the solid property is not cloned with clone(), is only needed for VRML loading, so we need to transfer it
M
r72  
Mr.doob 已提交
593 594
								if ( undefined !== defines[ defineKey ].solid && defines[ defineKey ].solid === false ) {

M
r66  
Mr.doob 已提交
595 596
									parent.geometry.solid = false;
									parent.material.side = THREE.DoubleSide;
M
r72  
Mr.doob 已提交
597

M
r66  
Mr.doob 已提交
598
								}
M
r62  
Mr.doob 已提交
599

M
r72  
Mr.doob 已提交
600
							} else if ( defineKey ) {
M
r63  
Mr.doob 已提交
601

M
r66  
Mr.doob 已提交
602 603
								var object = defines[ defineKey ].clone();
								parent.add( object );
M
r62  
Mr.doob 已提交
604

M
r66  
Mr.doob 已提交
605
							}
M
r62  
Mr.doob 已提交
606

M
r66  
Mr.doob 已提交
607
						}
M
r59  
Mr.doob 已提交
608 609 610 611 612 613 614 615 616

					}

					return;

				}

				var object = parent;

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

M
r59  
Mr.doob 已提交
619 620 621
					object = new THREE.Object3D();

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

M
r62  
Mr.doob 已提交
623
						object.name = /DEF\s+(\w+)/.exec( data.string )[ 1 ];
M
r59  
Mr.doob 已提交
624
						defines[ object.name ] = object;
M
r72  
Mr.doob 已提交
625

M
r59  
Mr.doob 已提交
626 627
					}

M
r72  
Mr.doob 已提交
628
					if ( undefined !== data[ 'translation' ] ) {
M
r59  
Mr.doob 已提交
629

M
r66  
Mr.doob 已提交
630
						var t = data.translation;
M
r59  
Mr.doob 已提交
631

M
r72  
Mr.doob 已提交
632
						object.position.set( t.x, t.y, t.z );
M
r59  
Mr.doob 已提交
633

M
r66  
Mr.doob 已提交
634
					}
M
r59  
Mr.doob 已提交
635

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

M
r66  
Mr.doob 已提交
638
						var r = data.rotation;
M
r59  
Mr.doob 已提交
639

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

M
r66  
Mr.doob 已提交
642
					}
M
r59  
Mr.doob 已提交
643

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

M
r66  
Mr.doob 已提交
646
						var s = data.scale;
M
r59  
Mr.doob 已提交
647

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

M
r66  
Mr.doob 已提交
650
					}
M
r59  
Mr.doob 已提交
651 652 653

					parent.add( object );

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

					object = new THREE.Mesh();

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

M
r73  
Mr.doob 已提交
660
						object.name = /DEF\s+(\w+)/.exec( data.string )[ 1 ];
M
r63  
Mr.doob 已提交
661

M
r59  
Mr.doob 已提交
662
						defines[ object.name ] = object;
M
r72  
Mr.doob 已提交
663

M
r59  
Mr.doob 已提交
664 665 666 667
					}

					parent.add( object );

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

M
r66  
Mr.doob 已提交
670
					var segments = 20;
M
r59  
Mr.doob 已提交
671

M
r66  
Mr.doob 已提交
672
					// sky (full sphere):
M
r69  
Mr.doob 已提交
673

M
r66  
Mr.doob 已提交
674
					var radius = 2e4;
M
r59  
Mr.doob 已提交
675

M
r66  
Mr.doob 已提交
676
					var skyGeometry = new THREE.SphereGeometry( radius, segments, segments );
M
r69  
Mr.doob 已提交
677
					var skyMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide } );
M
r59  
Mr.doob 已提交
678

M
r69  
Mr.doob 已提交
679
					if ( data.skyColor.length > 1 ) {
M
r59  
Mr.doob 已提交
680

M
r69  
Mr.doob 已提交
681
						paintFaces( skyGeometry, radius, data.skyAngle, data.skyColor, true );
M
r59  
Mr.doob 已提交
682

M
r69  
Mr.doob 已提交
683
						skyMaterial.vertexColors = THREE.VertexColors
M
r59  
Mr.doob 已提交
684

M
r69  
Mr.doob 已提交
685
					} else {
M
r59  
Mr.doob 已提交
686

M
r69  
Mr.doob 已提交
687 688
						var color = data.skyColor[ 0 ];
						skyMaterial.color.setRGB( color.r, color.b, color.g );
M
r59  
Mr.doob 已提交
689

M
r69  
Mr.doob 已提交
690
					}
M
r59  
Mr.doob 已提交
691

M
r69  
Mr.doob 已提交
692
					scene.add( new THREE.Mesh( skyGeometry, skyMaterial ) );
M
r59  
Mr.doob 已提交
693

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

M
r69  
Mr.doob 已提交
696
					if ( data.groundColor !== undefined ) {
M
r59  
Mr.doob 已提交
697

M
r69  
Mr.doob 已提交
698
						radius = 1.2e4;
M
r59  
Mr.doob 已提交
699

M
r69  
Mr.doob 已提交
700 701
						var groundGeometry = new THREE.SphereGeometry( radius, segments, segments, 0, 2 * Math.PI, 0.5 * Math.PI, 1.5 * Math.PI );
						var groundMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide, vertexColors: THREE.VertexColors } );
M
r59  
Mr.doob 已提交
702

M
r69  
Mr.doob 已提交
703
						paintFaces( groundGeometry, radius, data.groundAngle, data.groundColor, false );
M
r59  
Mr.doob 已提交
704

M
r69  
Mr.doob 已提交
705
						scene.add( new THREE.Mesh( groundGeometry, groundMaterial ) );
M
r59  
Mr.doob 已提交
706

M
r69  
Mr.doob 已提交
707
					}
M
r59  
Mr.doob 已提交
708

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

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

M
r66  
Mr.doob 已提交
713
						var s = data.size;
M
r59  
Mr.doob 已提交
714

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

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

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

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

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

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

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

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

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

M
r73  
Mr.doob 已提交
733
						var indexes, uvIndexes, uvs;
M
r62  
Mr.doob 已提交
734

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

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

M
r66  
Mr.doob 已提交
739
							var vec;
M
r62  
Mr.doob 已提交
740

M
r73  
Mr.doob 已提交
741 742 743 744 745 746 747
							if ( 'TextureCoordinate' === child.nodeType ) {

								uvs = child.points;

							}


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

M
r73  
Mr.doob 已提交
750 751 752
								if ( child.points ) {

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

M
r73  
Mr.doob 已提交
754
										var point = child.points[ k ];
M
r62  
Mr.doob 已提交
755

M
r73  
Mr.doob 已提交
756
										vec = new THREE.Vector3( point.x, point.y, point.z );
M
r62  
Mr.doob 已提交
757

M
r73  
Mr.doob 已提交
758 759 760
										geometry.vertices.push( vec );

									}
M
r72  
Mr.doob 已提交
761

M
r66  
Mr.doob 已提交
762
								}
M
r62  
Mr.doob 已提交
763

M
r73  
Mr.doob 已提交
764
								if ( child.string.indexOf ( 'DEF' ) > -1 ) {
M
r72  
Mr.doob 已提交
765

M
r73  
Mr.doob 已提交
766
									var name = /DEF\s+(\w+)/.exec( child.string )[ 1 ];
M
r72  
Mr.doob 已提交
767

M
r73  
Mr.doob 已提交
768
									defines[ name ] = geometry.vertices;
M
r62  
Mr.doob 已提交
769

M
r73  
Mr.doob 已提交
770 771 772
								}

								if ( child.string.indexOf ( 'USE' ) > -1 ) {
M
r63  
Mr.doob 已提交
773

M
r73  
Mr.doob 已提交
774
									var defineKey = /USE\s+(\w+)/.exec( child.string )[ 1 ];
M
r62  
Mr.doob 已提交
775

M
r73  
Mr.doob 已提交
776 777
									geometry.vertices = defines[ defineKey ];
								}
M
r62  
Mr.doob 已提交
778

M
r73  
Mr.doob 已提交
779
							}
M
r62  
Mr.doob 已提交
780

M
r73  
Mr.doob 已提交
781
						}
M
r62  
Mr.doob 已提交
782

M
r73  
Mr.doob 已提交
783
						var skip = 0;
M
r62  
Mr.doob 已提交
784

M
r73  
Mr.doob 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 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 825 826
						// some shapes only have vertices for use in other shapes
						if ( data.coordIndex ) {

							// read this: http://math.hws.edu/eck/cs424/notes2013/16_Threejs_Advanced.html
							for ( var i = 0, j = data.coordIndex.length; i < j; i ++ ) {

								indexes = data.coordIndex[ i ]; if ( data.texCoordIndex ) uvIndexes = data.texCoordIndex[ i ];

								// vrml support multipoint indexed face sets (more then 3 vertices). You must calculate the composing triangles here
								skip = 0;

								// 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 ) ) {

									var face = new THREE.Face3(
										indexes[ 0 ],
										indexes[ skip + (data.ccw ? 1 : 2) ],
										indexes[ skip + (data.ccw ? 2 : 1) ],
										null // normal, will be added later
										// todo: pass in the color, if a color index is present
									);

									if ( uvs && uvIndexes ) {
										geometry.faceVertexUvs [0].push( [
											new THREE.Vector2 (
												uvs[ uvIndexes[ 0 ] ].x ,
												uvs[ uvIndexes[ 0 ] ].y
											) ,
											new THREE.Vector2 (
												uvs[ uvIndexes[ skip + (data.ccw ? 1 : 2) ] ].x ,
												uvs[ uvIndexes[ skip + (data.ccw ? 1 : 2) ] ].y
											) ,
											new THREE.Vector2 (
												uvs[ uvIndexes[ skip + (data.ccw ? 2 : 1) ] ].x ,
												uvs[ uvIndexes[ skip + (data.ccw ? 2 : 1) ] ].y
											)
										] );
									}

									skip ++;

									geometry.faces.push( face );
M
r63  
Mr.doob 已提交
827

M
r73  
Mr.doob 已提交
828
								}
M
r63  
Mr.doob 已提交
829

M
r62  
Mr.doob 已提交
830

M
r66  
Mr.doob 已提交
831
							}
M
r62  
Mr.doob 已提交
832

M
r73  
Mr.doob 已提交
833 834 835 836
						} else {

							// do not add dummy mesh to the scene
							parent.parent.remove( parent );
M
r62  
Mr.doob 已提交
837

M
r66  
Mr.doob 已提交
838
						}
M
r62  
Mr.doob 已提交
839

M
r66  
Mr.doob 已提交
840
						if ( false === data.solid ) {
M
r72  
Mr.doob 已提交
841

M
r66  
Mr.doob 已提交
842
							parent.material.side = THREE.DoubleSide;
M
r72  
Mr.doob 已提交
843

M
r66  
Mr.doob 已提交
844
						}
M
r63  
Mr.doob 已提交
845

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

M
r66  
Mr.doob 已提交
849 850 851
						geometry.computeFaceNormals();
						//geometry.computeVertexNormals(); // does not show
						geometry.computeBoundingSphere();
M
r62  
Mr.doob 已提交
852

M
r66  
Mr.doob 已提交
853 854
						// see if it's a define
						if ( /DEF/.exec( data.string ) ) {
M
r72  
Mr.doob 已提交
855

M
r66  
Mr.doob 已提交
856 857
							geometry.name = /DEF (\w+)/.exec( data.string )[ 1 ];
							defines[ geometry.name ] = geometry;
M
r72  
Mr.doob 已提交
858

M
r66  
Mr.doob 已提交
859
						}
M
r62  
Mr.doob 已提交
860

M
r66  
Mr.doob 已提交
861
						parent.geometry = geometry;
M
r72  
Mr.doob 已提交
862

M
r59  
Mr.doob 已提交
863 864 865 866 867 868 869 870 871 872
					}

					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 已提交
873
						if ( 'Material' === child.nodeType ) {
M
r72  
Mr.doob 已提交
874

M
r59  
Mr.doob 已提交
875 876
							var material = new THREE.MeshPhongMaterial();

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

M
r66  
Mr.doob 已提交
879
								var d = child.diffuseColor;
M
r59  
Mr.doob 已提交
880

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

M
r66  
Mr.doob 已提交
883
							}
M
r59  
Mr.doob 已提交
884

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

M
r66  
Mr.doob 已提交
887
								var e = child.emissiveColor;
M
r59  
Mr.doob 已提交
888

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

M
r66  
Mr.doob 已提交
891
							}
M
r59  
Mr.doob 已提交
892

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

M
r66  
Mr.doob 已提交
895
								var s = child.specularColor;
M
r59  
Mr.doob 已提交
896

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

M
r66  
Mr.doob 已提交
899
							}
M
r59  
Mr.doob 已提交
900

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

M
r66  
Mr.doob 已提交
903
								var t = child.transparency;
M
r59  
Mr.doob 已提交
904

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

M
r66  
Mr.doob 已提交
908
								material.transparent = true;
M
r63  
Mr.doob 已提交
909

M
r66  
Mr.doob 已提交
910
							}
M
r59  
Mr.doob 已提交
911 912 913 914

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

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

M
r59  
Mr.doob 已提交
916 917 918 919 920 921
								defines[ material.name ] = material;

							}

							parent.material = material;

M
r73  
Mr.doob 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934
						}

						if ( 'ImageTexture' === child.nodeType ) {

							var textureName = /"([^"]+)"/.exec(child.children[ 0 ]);

							if (textureName) {

								parent.material.name = textureName[ 1 ];

								parent.material.map = THREE.ImageUtils.loadTexture (texturePath + textureName[ 1 ]);

							}
M
r72  
Mr.doob 已提交
935

M
r59  
Mr.doob 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
						}

					}

					return;

				}

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

					var child = data.children[ i ];

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

				}

M
r72  
Mr.doob 已提交
952
			};
M
r59  
Mr.doob 已提交
953 954 955 956 957 958 959 960

			parseNode( getTree( lines ), scene );

		};

		var scene = new THREE.Scene();

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

M
r73  
Mr.doob 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
		// some lines do not have breaks
		for (var i = lines.length -1; i > -1; i--) {

			// split lines with {..{ or {..[ - some have both
			if (/{.*[{\[]/.test (lines[i])) {
				var parts = lines[i].split ('{').join ('{\n').split ('\n');
				parts.unshift(1);
				parts.unshift(i);
				lines.splice.apply(lines, parts);
			} else

			// split lines with ]..}
			if (/\].*}/.test (lines[i])) {
				var parts = lines[i].split (']').join (']\n').split ('\n');
				parts.unshift(1);
				parts.unshift(i);
				lines.splice.apply(lines, parts);
			}

			// split lines with }..}
			if (/}.*}/.test (lines[i])) {
				var parts = lines[i].split ('}').join ('}\n').split ('\n');
				parts.unshift(1);
				parts.unshift(i);
				lines.splice.apply(lines, parts);
			}

			// force the parser to create Coordinate node for empty coords
			// coord USE something -> coord USE something Coordinate {}
			if((lines[i].indexOf ('coord') > -1) && (lines[i].indexOf ('[') < 0) && (lines[i].indexOf ('{') < 0)) {
				lines[i] += ' Coordinate {}';
			}
		}

M
r59  
Mr.doob 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
		var header = lines.shift();

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

			parseV1( lines, scene );

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

			parseV2( lines, scene );

		}

		return scene;

	}

};