VRMLLoader.js 28.2 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
	// for IndexedFaceSet support
	isRecordingPoints: false,
	isRecordingFaces: false,
	points: [],
M
r88  
Mr.doob 已提交
19
	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
r94  
Mr.doob 已提交
29
	crossOrigin: 'anonymous',
M
r87  
Mr.doob 已提交
30

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

		var scope = this;

M
r97  
Mr.doob 已提交
35 36
		var path = ( scope.path === undefined ) ? THREE.LoaderUtils.extractUrlBase( url ) : scope.path;

M
r83  
Mr.doob 已提交
37
		var loader = new THREE.FileLoader( this.manager );
M
r97  
Mr.doob 已提交
38
		loader.setPath( scope.path );
M
r72  
Mr.doob 已提交
39
		loader.load( url, function ( text ) {
M
r66  
Mr.doob 已提交
40

M
r97  
Mr.doob 已提交
41
			onLoad( scope.parse( text, path ) );
M
r59  
Mr.doob 已提交
42

M
r72  
Mr.doob 已提交
43
		}, onProgress, onError );
M
r59  
Mr.doob 已提交
44

M
r72  
Mr.doob 已提交
45
	},
M
r59  
Mr.doob 已提交
46

M
r97  
Mr.doob 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
	setPath: function ( value ) {

		this.path = value;
		return this;

	},

	setResourcePath: function ( value ) {

		this.resourcePath = value;
		return this;

	},

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

M
r72  
Mr.doob 已提交
63
		this.crossOrigin = value;
M
r94  
Mr.doob 已提交
64
		return this;
M
r59  
Mr.doob 已提交
65 66 67

	},

M
r97  
Mr.doob 已提交
68
	parse: function ( data, path ) {
M
r59  
Mr.doob 已提交
69

M
r96  
Mr.doob 已提交
70
		var scope = this;
M
r73  
Mr.doob 已提交
71

M
r74  
Mr.doob 已提交
72
		var textureLoader = new THREE.TextureLoader( this.manager );
M
r97  
Mr.doob 已提交
73
		textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
M
r74  
Mr.doob 已提交
74

M
r88  
Mr.doob 已提交
75
		function parseV2( lines, scene ) {
M
r69  
Mr.doob 已提交
76

M
r66  
Mr.doob 已提交
77 78
			var defines = {};
			var float_pattern = /(\b|\-|\+)([\d\.e]+)/;
M
r73  
Mr.doob 已提交
79
			var float2_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
M
r69  
Mr.doob 已提交
80
			var float3_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
M
r66  
Mr.doob 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

			/**
			 * 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
M
r88  
Mr.doob 已提交
102
			 * @param boolean topDown Whether to work top down or bottom up.
M
r66  
Mr.doob 已提交
103
			 */
M
r88  
Mr.doob 已提交
104
			function paintFaces( geometry, radius, angles, colors, topDown ) {
M
r66  
Mr.doob 已提交
105

M
r88  
Mr.doob 已提交
106
				var direction = ( topDown === true ) ? 1 : - 1;
M
r66  
Mr.doob 已提交
107

M
r86  
Mr.doob 已提交
108
				var coord = [], A = {}, B = {}, applyColor = false;
M
r66  
Mr.doob 已提交
109

M
r71  
Mr.doob 已提交
110
				for ( var k = 0; k < angles.length; k ++ ) {
M
r66  
Mr.doob 已提交
111 112 113

					// push the vector at which the color changes

M
r86  
Mr.doob 已提交
114 115 116 117
					var vec = {
						x: direction * ( Math.cos( angles[ k ] ) * radius ),
						y: direction * ( Math.sin( angles[ k ] ) * radius )
					};
M
r66  
Mr.doob 已提交
118 119 120 121 122

					coord.push( vec );

				}

M
r88  
Mr.doob 已提交
123 124 125
				var index = geometry.index;
				var positionAttribute = geometry.attributes.position;
				var colorAttribute = new THREE.BufferAttribute( new Float32Array( geometry.attributes.position.count * 3 ), 3 );
M
r66  
Mr.doob 已提交
126

M
r88  
Mr.doob 已提交
127 128
				var position = new THREE.Vector3();
				var color = new THREE.Color();
M
r66  
Mr.doob 已提交
129

M
r88  
Mr.doob 已提交
130
				for ( var i = 0; i < index.count; i ++ ) {
M
r66  
Mr.doob 已提交
131

M
r88  
Mr.doob 已提交
132
					var vertexIndex = index.getX( i );
M
r66  
Mr.doob 已提交
133

M
r88  
Mr.doob 已提交
134
					position.fromBufferAttribute( positionAttribute, vertexIndex );
M
r66  
Mr.doob 已提交
135

M
r88  
Mr.doob 已提交
136
					for ( var j = 0; j < colors.length; j ++ ) {
M
r66  
Mr.doob 已提交
137

M
r88  
Mr.doob 已提交
138 139
						// linear interpolation between aColor and bColor, calculate proportion
						// A is previous point (angle)
M
r66  
Mr.doob 已提交
140

M
r88  
Mr.doob 已提交
141
						if ( j === 0 ) {
M
r66  
Mr.doob 已提交
142

M
r88  
Mr.doob 已提交
143 144
							A.x = 0;
							A.y = ( topDown === true ) ? radius : - 1 * radius;
M
r66  
Mr.doob 已提交
145

M
r88  
Mr.doob 已提交
146
						} else {
M
r66  
Mr.doob 已提交
147

M
r88  
Mr.doob 已提交
148 149
							A.x = coord[ j - 1 ].x;
							A.y = coord[ j - 1 ].y;
M
r66  
Mr.doob 已提交
150

M
r88  
Mr.doob 已提交
151
						}
M
r63  
Mr.doob 已提交
152

M
r88  
Mr.doob 已提交
153
						// B is current point (angle)
M
r63  
Mr.doob 已提交
154

M
r88  
Mr.doob 已提交
155
						B = coord[ j ];
M
r72  
Mr.doob 已提交
156

M
r88  
Mr.doob 已提交
157
						if ( B !== undefined ) {
M
r63  
Mr.doob 已提交
158

M
r88  
Mr.doob 已提交
159
							// p has to be between the points A and B which we interpolate
M
r63  
Mr.doob 已提交
160

M
r88  
Mr.doob 已提交
161
							applyColor = ( topDown === true ) ? ( position.y <= A.y && position.y > B.y ) : ( position.y >= A.y && position.y < B.y );
M
r63  
Mr.doob 已提交
162

M
r88  
Mr.doob 已提交
163
							if ( applyColor === true ) {
M
r63  
Mr.doob 已提交
164

M
r88  
Mr.doob 已提交
165 166
								var aColor = colors[ j ];
								var bColor = colors[ j + 1 ];
M
r63  
Mr.doob 已提交
167

M
r88  
Mr.doob 已提交
168
								// below is simple linear interpolation
M
r72  
Mr.doob 已提交
169

M
r88  
Mr.doob 已提交
170 171 172
								var t = Math.abs( position.y - A.y ) / ( A.y - B.y );

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

M
r88  
Mr.doob 已提交
174
								color.copy( aColor ).lerp( bColor, t );
M
r72  
Mr.doob 已提交
175

M
r88  
Mr.doob 已提交
176 177 178 179 180 181 182
								colorAttribute.setXYZ( vertexIndex, color.r, color.g, color.b );

							} else {

								var colorIndex = ( topDown === true ) ? colors.length - 1 : 0;
								var c = colors[ colorIndex ];
								colorAttribute.setXYZ( vertexIndex, c.r, c.g, c.b );
M
r63  
Mr.doob 已提交
183

M
r66  
Mr.doob 已提交
184
							}
M
r72  
Mr.doob 已提交
185

M
r66  
Mr.doob 已提交
186 187 188
						}

					}
M
r63  
Mr.doob 已提交
189

M
r66  
Mr.doob 已提交
190
				}
M
r72  
Mr.doob 已提交
191

M
r88  
Mr.doob 已提交
192 193 194
				geometry.addAttribute( 'color', colorAttribute );

			}
M
r66  
Mr.doob 已提交
195

M
Mr.doob 已提交
196 197
			var index = [];

M
r88  
Mr.doob 已提交
198
			function parseProperty( node, line ) {
M
r63  
Mr.doob 已提交
199

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

M
r66  
Mr.doob 已提交
202 203 204 205 206
				/**
				 * 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 已提交
207

M
r88  
Mr.doob 已提交
208
				var point;
M
r63  
Mr.doob 已提交
209

M
r88  
Mr.doob 已提交
210
				while ( null !== ( part = regex.exec( line ) ) ) {
M
r72  
Mr.doob 已提交
211 212 213

					parts.push( part[ 0 ] );

M
r66  
Mr.doob 已提交
214
				}
M
r63  
Mr.doob 已提交
215

M
r72  
Mr.doob 已提交
216
				fieldName = parts[ 0 ];
M
r66  
Mr.doob 已提交
217 218 219


				// trigger several recorders
M
r72  
Mr.doob 已提交
220
				switch ( fieldName ) {
M
r88  
Mr.doob 已提交
221

M
r66  
Mr.doob 已提交
222 223
					case 'skyAngle':
					case 'groundAngle':
M
r96  
Mr.doob 已提交
224 225 226
						scope.recordingFieldname = fieldName;
						scope.isRecordingAngles = true;
						scope.angles = [];
M
r66  
Mr.doob 已提交
227
						break;
M
r88  
Mr.doob 已提交
228

M
r97  
Mr.doob 已提交
229
					case 'color':
M
r66  
Mr.doob 已提交
230 231
					case 'skyColor':
					case 'groundColor':
M
r96  
Mr.doob 已提交
232 233 234
						scope.recordingFieldname = fieldName;
						scope.isRecordingColors = true;
						scope.colors = [];
M
r66  
Mr.doob 已提交
235
						break;
M
r88  
Mr.doob 已提交
236

M
r66  
Mr.doob 已提交
237
					case 'point':
M
r97  
Mr.doob 已提交
238
					case 'vector':
M
r96  
Mr.doob 已提交
239 240 241
						scope.recordingFieldname = fieldName;
						scope.isRecordingPoints = true;
						scope.points = [];
M
r66  
Mr.doob 已提交
242
						break;
M
r88  
Mr.doob 已提交
243

M
r97  
Mr.doob 已提交
244
					case 'colorIndex':
M
r66  
Mr.doob 已提交
245
					case 'coordIndex':
M
r97  
Mr.doob 已提交
246
					case 'normalIndex':
M
r73  
Mr.doob 已提交
247
					case 'texCoordIndex':
M
r96  
Mr.doob 已提交
248 249 250
						scope.recordingFieldname = fieldName;
						scope.isRecordingFaces = true;
						scope.indexes = [];
M
r88  
Mr.doob 已提交
251 252
						break;

M
r66  
Mr.doob 已提交
253
				}
M
r63  
Mr.doob 已提交
254

M
r96  
Mr.doob 已提交
255
				if ( scope.isRecordingFaces ) {
M
r63  
Mr.doob 已提交
256

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

						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
r88  
Mr.doob 已提交
270
							if ( parts[ ind ] === '-1' ) {
M
r72  
Mr.doob 已提交
271 272 273

								if ( index.length > 0 ) {

M
r96  
Mr.doob 已提交
274
									scope.indexes.push( index );
M
r72  
Mr.doob 已提交
275

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
Mr.doob 已提交
294 295
						if ( index.length > 0 ) {

M
r96  
Mr.doob 已提交
296
							scope.indexes.push( index );
M
Mr.doob 已提交
297 298 299 300 301 302

						}

						// start new one
						index = [];

M
r96  
Mr.doob 已提交
303 304
						scope.isRecordingFaces = false;
						node[ scope.recordingFieldname ] = scope.indexes;
M
r72  
Mr.doob 已提交
305

M
r66  
Mr.doob 已提交
306
					}
M
r63  
Mr.doob 已提交
307

M
r96  
Mr.doob 已提交
308
				} else if ( scope.isRecordingPoints ) {
M
r72  
Mr.doob 已提交
309

M
r88  
Mr.doob 已提交
310
					if ( node.nodeType == 'Coordinate' ) {
M
r73  
Mr.doob 已提交
311

M
r88  
Mr.doob 已提交
312
						while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
M
r63  
Mr.doob 已提交
313

M
r88  
Mr.doob 已提交
314 315 316 317 318
							point = {
								x: parseFloat( parts[ 1 ] ),
								y: parseFloat( parts[ 2 ] ),
								z: parseFloat( parts[ 3 ] )
							};
M
r63  
Mr.doob 已提交
319

M
r96  
Mr.doob 已提交
320
							scope.points.push( point );
M
r88  
Mr.doob 已提交
321 322

						}
M
r72  
Mr.doob 已提交
323

M
r66  
Mr.doob 已提交
324
					}
M
r63  
Mr.doob 已提交
325

M
r97  
Mr.doob 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
					if ( node.nodeType == 'Normal' ) {

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

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

							scope.points.push( point );

						}

					}

M
r88  
Mr.doob 已提交
342
					if ( node.nodeType == 'TextureCoordinate' ) {
M
r73  
Mr.doob 已提交
343

M
r88  
Mr.doob 已提交
344
						while ( null !== ( parts = float2_pattern.exec( line ) ) ) {
M
r73  
Mr.doob 已提交
345

M
r88  
Mr.doob 已提交
346 347 348 349
							point = {
								x: parseFloat( parts[ 1 ] ),
								y: parseFloat( parts[ 2 ] )
							};
M
r73  
Mr.doob 已提交
350

M
r96  
Mr.doob 已提交
351
							scope.points.push( point );
M
r88  
Mr.doob 已提交
352 353

						}
M
r73  
Mr.doob 已提交
354 355 356

					}

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

M
r96  
Mr.doob 已提交
360 361
						scope.isRecordingPoints = false;
						node.points = scope.points;
M
r72  
Mr.doob 已提交
362

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

M
r96  
Mr.doob 已提交
365
				} else if ( scope.isRecordingAngles ) {
M
r63  
Mr.doob 已提交
366

M
r66  
Mr.doob 已提交
367 368
					// the parts hold the angles as strings
					if ( parts.length > 0 ) {
M
r63  
Mr.doob 已提交
369

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

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

M
r66  
Mr.doob 已提交
375
								continue;
M
r72  
Mr.doob 已提交
376

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

M
r96  
Mr.doob 已提交
379
							scope.angles.push( parseFloat( parts[ ind ] ) );
M
r72  
Mr.doob 已提交
380

M
r66  
Mr.doob 已提交
381
						}
M
r63  
Mr.doob 已提交
382

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

M
r66  
Mr.doob 已提交
385
					// end
M
r72  
Mr.doob 已提交
386 387
					if ( /]/.exec( line ) ) {

M
r96  
Mr.doob 已提交
388 389
						scope.isRecordingAngles = false;
						node[ scope.recordingFieldname ] = scope.angles;
M
r72  
Mr.doob 已提交
390

M
r66  
Mr.doob 已提交
391
					}
M
r63  
Mr.doob 已提交
392

M
r96  
Mr.doob 已提交
393
				} else if ( scope.isRecordingColors ) {
M
r63  
Mr.doob 已提交
394

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

M
r86  
Mr.doob 已提交
397
						var color = {
M
r72  
Mr.doob 已提交
398 399 400
							r: parseFloat( parts[ 1 ] ),
							g: parseFloat( parts[ 2 ] ),
							b: parseFloat( parts[ 3 ] )
M
r66  
Mr.doob 已提交
401
						};
M
r63  
Mr.doob 已提交
402

M
r96  
Mr.doob 已提交
403
						scope.colors.push( color );
M
r63  
Mr.doob 已提交
404

M
r66  
Mr.doob 已提交
405
					}
M
r63  
Mr.doob 已提交
406

M
r66  
Mr.doob 已提交
407
					// end
M
r72  
Mr.doob 已提交
408 409
					if ( /]/.exec( line ) ) {

M
r96  
Mr.doob 已提交
410 411
						scope.isRecordingColors = false;
						node[ scope.recordingFieldname ] = scope.colors;
M
r72  
Mr.doob 已提交
412

M
r66  
Mr.doob 已提交
413
					}
M
r63  
Mr.doob 已提交
414

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

M
r72  
Mr.doob 已提交
417
					switch ( fieldName ) {
M
r63  
Mr.doob 已提交
418

M
r66  
Mr.doob 已提交
419 420 421 422
						case 'diffuseColor':
						case 'emissiveColor':
						case 'specularColor':
						case 'color':
M
r63  
Mr.doob 已提交
423

M
r88  
Mr.doob 已提交
424
							if ( parts.length !== 4 ) {
M
r72  
Mr.doob 已提交
425

M
r88  
Mr.doob 已提交
426
								console.warn( 'THREE.VRMLLoader: Invalid color format detected for %s.', fieldName );
M
r66  
Mr.doob 已提交
427
								break;
M
r72  
Mr.doob 已提交
428

M
r66  
Mr.doob 已提交
429
							}
M
r63  
Mr.doob 已提交
430

M
r66  
Mr.doob 已提交
431
							property = {
M
r72  
Mr.doob 已提交
432 433 434 435
								r: parseFloat( parts[ 1 ] ),
								g: parseFloat( parts[ 2 ] ),
								b: parseFloat( parts[ 3 ] )
							};
M
r63  
Mr.doob 已提交
436

M
r66  
Mr.doob 已提交
437
							break;
M
r86  
Mr.doob 已提交
438

M
r83  
Mr.doob 已提交
439 440
						case 'location':
						case 'direction':
M
r66  
Mr.doob 已提交
441 442 443
						case 'translation':
						case 'scale':
						case 'size':
M
r88  
Mr.doob 已提交
444
							if ( parts.length !== 4 ) {
M
r72  
Mr.doob 已提交
445

M
r88  
Mr.doob 已提交
446
								console.warn( 'THREE.VRMLLoader: Invalid vector format detected for %s.', fieldName );
M
r66  
Mr.doob 已提交
447
								break;
M
r72  
Mr.doob 已提交
448

M
r66  
Mr.doob 已提交
449
							}
M
r63  
Mr.doob 已提交
450

M
r66  
Mr.doob 已提交
451
							property = {
M
r72  
Mr.doob 已提交
452 453 454 455
								x: parseFloat( parts[ 1 ] ),
								y: parseFloat( parts[ 2 ] ),
								z: parseFloat( parts[ 3 ] )
							};
M
r63  
Mr.doob 已提交
456

M
r66  
Mr.doob 已提交
457 458
							break;

M
r86  
Mr.doob 已提交
459
						case 'intensity':
M
r83  
Mr.doob 已提交
460
						case 'cutOffAngle':
M
r66  
Mr.doob 已提交
461 462 463 464 465 466 467
						case 'radius':
						case 'topRadius':
						case 'bottomRadius':
						case 'height':
						case 'transparency':
						case 'shininess':
						case 'ambientIntensity':
M
r97  
Mr.doob 已提交
468
						case 'creaseAngle':
M
r88  
Mr.doob 已提交
469
							if ( parts.length !== 2 ) {
M
r72  
Mr.doob 已提交
470

M
r88  
Mr.doob 已提交
471
								console.warn( 'THREE.VRMLLoader: Invalid single float value specification detected for %s.', fieldName );
M
r66  
Mr.doob 已提交
472
								break;
M
r72  
Mr.doob 已提交
473

M
r66  
Mr.doob 已提交
474
							}
M
r63  
Mr.doob 已提交
475

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

M
r66  
Mr.doob 已提交
478
							break;
M
r63  
Mr.doob 已提交
479

M
r66  
Mr.doob 已提交
480
						case 'rotation':
M
r88  
Mr.doob 已提交
481
							if ( parts.length !== 5 ) {
M
r72  
Mr.doob 已提交
482

M
r88  
Mr.doob 已提交
483
								console.warn( 'THREE.VRMLLoader: Invalid quaternion format detected for %s.', fieldName );
M
r66  
Mr.doob 已提交
484
								break;
M
r72  
Mr.doob 已提交
485

M
r66  
Mr.doob 已提交
486
							}
M
r63  
Mr.doob 已提交
487

M
r66  
Mr.doob 已提交
488
							property = {
M
r72  
Mr.doob 已提交
489 490 491 492 493
								x: parseFloat( parts[ 1 ] ),
								y: parseFloat( parts[ 2 ] ),
								z: parseFloat( parts[ 3 ] ),
								w: parseFloat( parts[ 4 ] )
							};
M
r63  
Mr.doob 已提交
494

M
r66  
Mr.doob 已提交
495
							break;
M
r86  
Mr.doob 已提交
496

M
r83  
Mr.doob 已提交
497
						case 'on':
M
r66  
Mr.doob 已提交
498 499 500 501
						case 'ccw':
						case 'solid':
						case 'colorPerVertex':
						case 'convex':
M
r88  
Mr.doob 已提交
502
							if ( parts.length !== 2 ) {
M
r72  
Mr.doob 已提交
503

M
r88  
Mr.doob 已提交
504
								console.warn( 'THREE.VRMLLoader: Invalid format detected for %s.', fieldName );
M
r66  
Mr.doob 已提交
505
								break;
M
r72  
Mr.doob 已提交
506

M
r66  
Mr.doob 已提交
507
							}
M
r63  
Mr.doob 已提交
508

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

M
r66  
Mr.doob 已提交
511
							break;
M
r88  
Mr.doob 已提交
512

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

M
r72  
Mr.doob 已提交
515 516
					node[ fieldName ] = property;

M
r66  
Mr.doob 已提交
517
				}
M
r63  
Mr.doob 已提交
518

M
r66  
Mr.doob 已提交
519
				return property;
M
r72  
Mr.doob 已提交
520

M
r88  
Mr.doob 已提交
521
			}
M
r59  
Mr.doob 已提交
522

M
r88  
Mr.doob 已提交
523
			function getTree( lines ) {
M
r59  
Mr.doob 已提交
524 525 526

				var tree = { 'string': 'Scene', children: [] };
				var current = tree;
M
r66  
Mr.doob 已提交
527 528
				var matches;
				var specification;
M
r59  
Mr.doob 已提交
529 530 531

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

M
r66  
Mr.doob 已提交
532
					var comment = '';
M
r62  
Mr.doob 已提交
533

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

M
r66  
Mr.doob 已提交
536
					// omit whitespace only lines
M
r88  
Mr.doob 已提交
537
					if ( null !== ( /^\s+?$/g.exec( line ) ) ) {
M
r72  
Mr.doob 已提交
538

M
r66  
Mr.doob 已提交
539
						continue;
M
r72  
Mr.doob 已提交
540

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

M
r66  
Mr.doob 已提交
543
					line = line.trim();
M
r63  
Mr.doob 已提交
544

M
r66  
Mr.doob 已提交
545
					// skip empty lines
M
r72  
Mr.doob 已提交
546 547
					if ( line === '' ) {

M
r66  
Mr.doob 已提交
548
						continue;
M
r72  
Mr.doob 已提交
549

M
r66  
Mr.doob 已提交
550
					}
M
r63  
Mr.doob 已提交
551

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

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

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

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

M
r62  
Mr.doob 已提交
562
					}
M
r63  
Mr.doob 已提交
563

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

M
r72  
Mr.doob 已提交
566 567
						// first subpattern should match the Node name

M
r88  
Mr.doob 已提交
568
						var block = { 'nodeType': matches[ 1 ], 'string': line, 'parent': current, 'children': [], 'comment': comment };
M
r59  
Mr.doob 已提交
569 570 571 572
						current.children.push( block );
						current = block;

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

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

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

M
r72  
Mr.doob 已提交
580
							parseProperty( current, specification );
M
r59  
Mr.doob 已提交
581 582 583 584 585 586 587 588 589 590 591

							current = current.parent;

						}

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

						current = current.parent;

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

M
r72  
Mr.doob 已提交
592
						parseProperty( current, line );
M
r66  
Mr.doob 已提交
593 594
						// 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 已提交
595 596 597 598 599

					}

				}

M
r66  
Mr.doob 已提交
600
				return tree;
M
r72  
Mr.doob 已提交
601

M
r88  
Mr.doob 已提交
602
			}
M
r59  
Mr.doob 已提交
603

M
r88  
Mr.doob 已提交
604
			function parseNode( data, parent ) {
M
r59  
Mr.doob 已提交
605

M
r88  
Mr.doob 已提交
606
				var object;
M
r59  
Mr.doob 已提交
607 608 609 610 611

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

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

M
r75  
Mr.doob 已提交
612
						var defineKey = /USE\s+?([^\s]+)/.exec( data )[ 1 ];
M
r59  
Mr.doob 已提交
613

M
r72  
Mr.doob 已提交
614 615
						if ( undefined == defines[ defineKey ] ) {

M
r88  
Mr.doob 已提交
616
							console.warn( 'THREE.VRMLLoader: %s is not defined.', defineKey );
M
r72  
Mr.doob 已提交
617

M
r66  
Mr.doob 已提交
618
						} else {
M
r59  
Mr.doob 已提交
619

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

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

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

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

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

M
r66  
Mr.doob 已提交
631 632
									parent.geometry.solid = false;
									parent.material.side = THREE.DoubleSide;
M
r72  
Mr.doob 已提交
633

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

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

M
r88  
Mr.doob 已提交
638
								object = defines[ defineKey ].clone();
M
r66  
Mr.doob 已提交
639
								parent.add( object );
M
r62  
Mr.doob 已提交
640

M
r66  
Mr.doob 已提交
641
							}
M
r62  
Mr.doob 已提交
642

M
r66  
Mr.doob 已提交
643
						}
M
r59  
Mr.doob 已提交
644 645 646 647 648 649 650

					}

					return;

				}

M
r88  
Mr.doob 已提交
651 652 653 654 655
				object = parent;

				if ( data.string.indexOf( 'AmbientLight' ) > - 1 && data.nodeType === 'PointLight' ) {

					data.nodeType = 'AmbientLight';
M
r59  
Mr.doob 已提交
656

M
r83  
Mr.doob 已提交
657
				}
M
r86  
Mr.doob 已提交
658 659 660 661 662

				var l_visible = data.on !== undefined ? data.on : true;
				var l_intensity = data.intensity !== undefined ? data.intensity : 1;
				var l_color = new THREE.Color();

M
r88  
Mr.doob 已提交
663
				if ( data.color ) {
M
r86  
Mr.doob 已提交
664

M
r88  
Mr.doob 已提交
665
					l_color.copy( data.color );
M
r86  
Mr.doob 已提交
666

M
r83  
Mr.doob 已提交
667
				}
M
r86  
Mr.doob 已提交
668

M
r88  
Mr.doob 已提交
669
				if ( data.nodeType === 'AmbientLight' ) {
M
r86  
Mr.doob 已提交
670

M
r88  
Mr.doob 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
					object = new THREE.AmbientLight( l_color, l_intensity );
					object.visible = l_visible;

					parent.add( object );

				} else if ( data.nodeType === 'PointLight' ) {

					var l_distance = 0;

					if ( data.radius !== undefined && data.radius < 1000 ) {

						l_distance = data.radius;

					}

					object = new THREE.PointLight( l_color, l_intensity, l_distance );
					object.visible = l_visible;

					parent.add( object );
M
r86  
Mr.doob 已提交
690

M
r88  
Mr.doob 已提交
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
				} else if ( data.nodeType === 'SpotLight' ) {

					var l_intensity = 1;
					var l_distance = 0;
					var l_angle = Math.PI / 3;
					var l_penumbra = 0;
					var l_visible = true;

					if ( data.radius !== undefined && data.radius < 1000 ) {

						l_distance = data.radius;

					}

					if ( data.cutOffAngle !== undefined ) {

						l_angle = data.cutOffAngle;

					}

					object = new THREE.SpotLight( l_color, l_intensity, l_distance, l_angle, l_penumbra );
					object.visible = l_visible;

					parent.add( object );

				} else if ( data.nodeType === 'Transform' || data.nodeType === 'Group' ) {
M
r63  
Mr.doob 已提交
717

M
r59  
Mr.doob 已提交
718 719 720
					object = new THREE.Object3D();

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

M
r75  
Mr.doob 已提交
722
						object.name = /DEF\s+([^\s]+)/.exec( data.string )[ 1 ];
M
r59  
Mr.doob 已提交
723
						defines[ object.name ] = object;
M
r72  
Mr.doob 已提交
724

M
r59  
Mr.doob 已提交
725 726
					}

M
r88  
Mr.doob 已提交
727
					if ( data.translation !== undefined ) {
M
r59  
Mr.doob 已提交
728

M
r66  
Mr.doob 已提交
729
						var t = data.translation;
M
r59  
Mr.doob 已提交
730

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

M
r66  
Mr.doob 已提交
733
					}
M
r59  
Mr.doob 已提交
734

M
r88  
Mr.doob 已提交
735
					if ( data.rotation !== undefined ) {
M
r59  
Mr.doob 已提交
736

M
r66  
Mr.doob 已提交
737
						var r = data.rotation;
M
r59  
Mr.doob 已提交
738

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

M
r66  
Mr.doob 已提交
741
					}
M
r59  
Mr.doob 已提交
742

M
r88  
Mr.doob 已提交
743
					if ( data.scale !== undefined ) {
M
r59  
Mr.doob 已提交
744

M
r66  
Mr.doob 已提交
745
						var s = data.scale;
M
r59  
Mr.doob 已提交
746

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

M
r66  
Mr.doob 已提交
749
					}
M
r59  
Mr.doob 已提交
750 751 752

					parent.add( object );

M
r88  
Mr.doob 已提交
753
				} else if ( data.nodeType === 'Shape' ) {
M
r59  
Mr.doob 已提交
754 755 756 757

					object = new THREE.Mesh();

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

M
r75  
Mr.doob 已提交
759
						object.name = /DEF\s+([^\s]+)/.exec( data.string )[ 1 ];
M
r63  
Mr.doob 已提交
760

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

M
r59  
Mr.doob 已提交
763 764 765 766
					}

					parent.add( object );

M
r88  
Mr.doob 已提交
767
				} else if ( data.nodeType === 'Background' ) {
M
r59  
Mr.doob 已提交
768

M
r66  
Mr.doob 已提交
769
					var segments = 20;
M
r59  
Mr.doob 已提交
770

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

M
r66  
Mr.doob 已提交
773
					var radius = 2e4;
M
r59  
Mr.doob 已提交
774

M
r88  
Mr.doob 已提交
775
					var skyGeometry = new THREE.SphereBufferGeometry( radius, segments, segments );
M
r69  
Mr.doob 已提交
776
					var skyMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide } );
M
r59  
Mr.doob 已提交
777

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

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

M
r88  
Mr.doob 已提交
782
						skyMaterial.vertexColors = THREE.VertexColors;
M
r59  
Mr.doob 已提交
783

M
r69  
Mr.doob 已提交
784
					} else {
M
r59  
Mr.doob 已提交
785

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

M
r69  
Mr.doob 已提交
789
					}
M
r59  
Mr.doob 已提交
790

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

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

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

M
r69  
Mr.doob 已提交
797
						radius = 1.2e4;
M
r59  
Mr.doob 已提交
798

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

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

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

M
r69  
Mr.doob 已提交
806
					}
M
r59  
Mr.doob 已提交
807

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

M
r88  
Mr.doob 已提交
810
					if ( data.nodeType === 'Box' ) {
M
r59  
Mr.doob 已提交
811

M
r66  
Mr.doob 已提交
812
						var s = data.size;
M
r59  
Mr.doob 已提交
813

M
r88  
Mr.doob 已提交
814 815 816 817 818
						parent.geometry = new THREE.BoxBufferGeometry( s.x, s.y, s.z );

					} else if ( data.nodeType === 'Cylinder' ) {

						parent.geometry = new THREE.CylinderBufferGeometry( data.radius, data.radius, data.height );
M
r59  
Mr.doob 已提交
819

M
r88  
Mr.doob 已提交
820
					} else if ( data.nodeType === 'Cone' ) {
M
r59  
Mr.doob 已提交
821

M
r88  
Mr.doob 已提交
822
						parent.geometry = new THREE.CylinderBufferGeometry( data.topRadius, data.bottomRadius, data.height );
M
r59  
Mr.doob 已提交
823

M
r88  
Mr.doob 已提交
824
					} else if ( data.nodeType === 'Sphere' ) {
M
r59  
Mr.doob 已提交
825

M
r88  
Mr.doob 已提交
826
						parent.geometry = new THREE.SphereBufferGeometry( data.radius );
M
r59  
Mr.doob 已提交
827

M
r88  
Mr.doob 已提交
828
					} else if ( data.nodeType === 'IndexedFaceSet' ) {
M
r59  
Mr.doob 已提交
829

M
r88  
Mr.doob 已提交
830
						var geometry = new THREE.BufferGeometry();
M
r59  
Mr.doob 已提交
831

M
r88  
Mr.doob 已提交
832
						var positions = [];
M
r97  
Mr.doob 已提交
833 834
						var colors = [];
						var normals = [];
M
r88  
Mr.doob 已提交
835
						var uvs = [];
M
r62  
Mr.doob 已提交
836

M
r97  
Mr.doob 已提交
837
						var position, color, normal, uv;
M
r62  
Mr.doob 已提交
838

M
r88  
Mr.doob 已提交
839
						var i, il, j, jl;
M
r62  
Mr.doob 已提交
840

M
r88  
Mr.doob 已提交
841
						for ( i = 0, il = data.children.length; i < il; i ++ ) {
M
r62  
Mr.doob 已提交
842

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

M
r88  
Mr.doob 已提交
845
							// uvs
M
r62  
Mr.doob 已提交
846

M
r88  
Mr.doob 已提交
847
							if ( child.nodeType === 'TextureCoordinate' ) {
M
r73  
Mr.doob 已提交
848

M
r88  
Mr.doob 已提交
849
								if ( child.points ) {
M
r73  
Mr.doob 已提交
850

M
r88  
Mr.doob 已提交
851
									for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
M
r73  
Mr.doob 已提交
852

M
r88  
Mr.doob 已提交
853 854
										uv = child.points[ j ];
										uvs.push( uv.x, uv.y );
M
r73  
Mr.doob 已提交
855

M
r88  
Mr.doob 已提交
856
									}
M
r62  
Mr.doob 已提交
857

M
r88  
Mr.doob 已提交
858 859 860
								}

							}
M
r73  
Mr.doob 已提交
861

M
r97  
Mr.doob 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
							// normals

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

								if ( child.points ) {

									for ( j = 0, jl = child.points.length; j < jl; j ++ ) {

										normal = child.points[ j ];
										normals.push( normal.x, normal.y, normal.z );

									}

								}

							}

							// colors

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

								if ( child.color ) {

									for ( j = 0, jl = child.color.length; j < jl; j ++ ) {

										color = child.color[ j ];
										colors.push( color.r, color.g, color.b );

									}

								}

							}

M
r88  
Mr.doob 已提交
896
							// positions
M
r62  
Mr.doob 已提交
897

M
r88  
Mr.doob 已提交
898
							if ( child.nodeType === 'Coordinate' ) {
M
r62  
Mr.doob 已提交
899

M
r88  
Mr.doob 已提交
900
								if ( child.points ) {
M
r62  
Mr.doob 已提交
901

M
r88  
Mr.doob 已提交
902 903 904 905
									for ( j = 0, jl = child.points.length; j < jl; j ++ ) {

										position = child.points[ j ];
										positions.push( position.x, position.y, position.z );
M
r73  
Mr.doob 已提交
906 907

									}
M
r72  
Mr.doob 已提交
908

M
r66  
Mr.doob 已提交
909
								}
M
r62  
Mr.doob 已提交
910

M
r88  
Mr.doob 已提交
911
								if ( child.string.indexOf( 'DEF' ) > - 1 ) {
M
r72  
Mr.doob 已提交
912

M
r75  
Mr.doob 已提交
913
									var name = /DEF\s+([^\s]+)/.exec( child.string )[ 1 ];
M
r72  
Mr.doob 已提交
914

M
r88  
Mr.doob 已提交
915
									defines[ name ] = positions.slice( 0 );
M
r62  
Mr.doob 已提交
916

M
r73  
Mr.doob 已提交
917 918
								}

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

M
r75  
Mr.doob 已提交
921
									var defineKey = /USE\s+([^\s]+)/.exec( child.string )[ 1 ];
M
r62  
Mr.doob 已提交
922

M
r88  
Mr.doob 已提交
923 924
									positions = defines[ defineKey ];

M
r73  
Mr.doob 已提交
925
								}
M
r62  
Mr.doob 已提交
926

M
r73  
Mr.doob 已提交
927
							}
M
r62  
Mr.doob 已提交
928

M
r73  
Mr.doob 已提交
929
						}
M
r62  
Mr.doob 已提交
930

M
r73  
Mr.doob 已提交
931
						// some shapes only have vertices for use in other shapes
M
r88  
Mr.doob 已提交
932

M
r73  
Mr.doob 已提交
933 934
						if ( data.coordIndex ) {

M
r102  
Mr.doob 已提交
935
							function triangulateIndexArray( indexArray, ccw, colorPerVertex ) {
M
r97  
Mr.doob 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948

								if ( ccw === undefined ) {

									// ccw is true by default
									ccw = true;

								}

								var triangulatedIndexArray = [];
								var skip = 0;

								for ( i = 0, il = indexArray.length; i < il; i ++ ) {

M
r102  
Mr.doob 已提交
949
									if ( colorPerVertex === false ) {
M
r97  
Mr.doob 已提交
950

M
r102  
Mr.doob 已提交
951
										var colorIndices = indexArray[ i ];
M
r97  
Mr.doob 已提交
952

M
r102  
Mr.doob 已提交
953
										for ( j = 0, jl = colorIndices.length; j < jl; j ++ ) {
M
r97  
Mr.doob 已提交
954

M
r102  
Mr.doob 已提交
955
											var index = colorIndices[ j ];
M
r97  
Mr.doob 已提交
956

M
r102  
Mr.doob 已提交
957
											triangulatedIndexArray.push( index, index, index );
M
r97  
Mr.doob 已提交
958

M
r102  
Mr.doob 已提交
959
										}
M
r97  
Mr.doob 已提交
960

M
r102  
Mr.doob 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
									} else {

										var indexedFace = indexArray[ i ];

										// VRML support multipoint indexed face sets (more then 3 vertices). You must calculate the composing triangles here

										skip = 0;

										while ( indexedFace.length >= 3 && skip < ( indexedFace.length - 2 ) ) {

											var i1 = indexedFace[ 0 ];
											var i2 = indexedFace[ skip + ( ccw ? 1 : 2 ) ];
											var i3 = indexedFace[ skip + ( ccw ? 2 : 1 ) ];

											triangulatedIndexArray.push( i1, i2, i3 );

											skip ++;

										}
M
r97  
Mr.doob 已提交
980 981 982 983 984 985 986 987 988 989 990

									}

								}

								return triangulatedIndexArray;

							}

							var positionIndexes = data.coordIndex ? triangulateIndexArray( data.coordIndex, data.ccw ) : [];
							var normalIndexes = data.normalIndex ? triangulateIndexArray( data.normalIndex, data.ccw ) : positionIndexes;
M
r102.1  
Mr.doob 已提交
991
							var colorIndexes = data.colorIndex ? triangulateIndexArray( data.colorIndex, data.ccw, data.colorPerVertex ) : [];
M
r97  
Mr.doob 已提交
992 993 994
							var uvIndexes = data.texCoordIndex ? triangulateIndexArray( data.texCoordIndex, data.ccw ) : positionIndexes;

							var newIndexes = [];
M
r88  
Mr.doob 已提交
995
							var newPositions = [];
M
r97  
Mr.doob 已提交
996 997
							var newNormals = [];
							var newColors = [];
M
r88  
Mr.doob 已提交
998 999
							var newUvs = [];

M
r97  
Mr.doob 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
							// if any other index array does not match the coordinate indexes, split any points that differ

							var pointMap = Object.create( null );

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

								var pointAttributes = [];

								var positionIndex = positionIndexes[ i ];
								var normalIndex = normalIndexes[ i ];
								var colorIndex = colorIndexes[ i ];
								var uvIndex = uvIndexes[ i ];

								var base = 10; // which base to use to represent each value

								pointAttributes.push( positionIndex.toString( base ) );

								if ( normalIndex !== undefined ) {

									pointAttributes.push( normalIndex.toString( base ) );

								}

								if ( colorIndex !== undefined ) {

									pointAttributes.push( colorIndex.toString( base ) );

								}

								if ( uvIndex !== undefined ) {

									pointAttributes.push( uvIndex.toString( base ) );
M
r73  
Mr.doob 已提交
1032

M
r97  
Mr.doob 已提交
1033 1034 1035 1036
								}

								var pointId = pointAttributes.join( ',' );
								var newIndex = pointMap[ pointId ];
M
r88  
Mr.doob 已提交
1037

M
r97  
Mr.doob 已提交
1038
								if ( newIndex === undefined ) {
M
r88  
Mr.doob 已提交
1039

M
r97  
Mr.doob 已提交
1040 1041
									newIndex = newPositions.length / 3;
									pointMap[ pointId ] = newIndex;
M
r73  
Mr.doob 已提交
1042

M
r97  
Mr.doob 已提交
1043 1044 1045 1046 1047
									newPositions.push(
										positions[ positionIndex * 3 ],
										positions[ positionIndex * 3 + 1 ],
										positions[ positionIndex * 3 + 2 ]
									);
M
r73  
Mr.doob 已提交
1048

M
r97  
Mr.doob 已提交
1049
									if ( normalIndex !== undefined && normals.length > 0 ) {
M
r73  
Mr.doob 已提交
1050

M
r97  
Mr.doob 已提交
1051 1052 1053 1054 1055
										newNormals.push(
											normals[ normalIndex * 3 ],
											normals[ normalIndex * 3 + 1 ],
											normals[ normalIndex * 3 + 2 ]
										);
M
r73  
Mr.doob 已提交
1056

M
r97  
Mr.doob 已提交
1057
									}
M
r73  
Mr.doob 已提交
1058

M
r97  
Mr.doob 已提交
1059
									if ( colorIndex !== undefined && colors.length > 0 ) {
M
r63  
Mr.doob 已提交
1060

M
r97  
Mr.doob 已提交
1061 1062 1063 1064 1065
										newColors.push(
											colors[ colorIndex * 3 ],
											colors[ colorIndex * 3 + 1 ],
											colors[ colorIndex * 3 + 2 ]
										);
M
r88  
Mr.doob 已提交
1066

M
r97  
Mr.doob 已提交
1067
									}
M
r88  
Mr.doob 已提交
1068

M
r97  
Mr.doob 已提交
1069
									if ( uvIndex !== undefined && uvs.length > 0 ) {
M
r63  
Mr.doob 已提交
1070

M
r97  
Mr.doob 已提交
1071 1072 1073 1074 1075 1076
										newUvs.push(
											uvs[ uvIndex * 2 ],
											uvs[ uvIndex * 2 + 1 ]
										);

									}
M
r88  
Mr.doob 已提交
1077 1078

								}
M
r62  
Mr.doob 已提交
1079

M
r97  
Mr.doob 已提交
1080 1081
								newIndexes.push( newIndex );

M
r66  
Mr.doob 已提交
1082
							}
M
r62  
Mr.doob 已提交
1083

M
r88  
Mr.doob 已提交
1084
							positions = newPositions;
M
r97  
Mr.doob 已提交
1085
							normals = newNormals;
M
r98  
Mr.doob 已提交
1086
							colors = newColors;
M
r88  
Mr.doob 已提交
1087 1088
							uvs = newUvs;

M
r97  
Mr.doob 已提交
1089 1090
							geometry.setIndex( newIndexes );

M
r73  
Mr.doob 已提交
1091 1092 1093
						} else {

							// do not add dummy mesh to the scene
M
r88  
Mr.doob 已提交
1094

M
r73  
Mr.doob 已提交
1095
							parent.parent.remove( parent );
M
r62  
Mr.doob 已提交
1096

M
r66  
Mr.doob 已提交
1097
						}
M
r62  
Mr.doob 已提交
1098

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

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

M
r66  
Mr.doob 已提交
1103
						}
M
r63  
Mr.doob 已提交
1104

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

M
r88  
Mr.doob 已提交
1108 1109
						geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );

M
r97  
Mr.doob 已提交
1110 1111 1112 1113
						if ( colors.length > 0 ) {

							geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );

M
r102  
Mr.doob 已提交
1114 1115
							parent.material.vertexColors = THREE.VertexColors;

M
r97  
Mr.doob 已提交
1116 1117
						}

M
r88  
Mr.doob 已提交
1118 1119 1120 1121 1122 1123
						if ( uvs.length > 0 ) {

							geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );

						}

M
r97  
Mr.doob 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
						if ( normals.length > 0 ) {

							geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );

						} else {

							// convert geometry to non-indexed to get sharp normals
							geometry = geometry.toNonIndexed();
							geometry.computeVertexNormals();

						}

M
r66  
Mr.doob 已提交
1136
						geometry.computeBoundingSphere();
M
r62  
Mr.doob 已提交
1137

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

M
r75  
Mr.doob 已提交
1141
							geometry.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
M
r66  
Mr.doob 已提交
1142
							defines[ geometry.name ] = geometry;
M
r72  
Mr.doob 已提交
1143

M
r66  
Mr.doob 已提交
1144
						}
M
r62  
Mr.doob 已提交
1145

M
r66  
Mr.doob 已提交
1146
						parent.geometry = geometry;
M
r72  
Mr.doob 已提交
1147

M
r59  
Mr.doob 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
					}

					return;

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

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

						var child = data.children[ i ];

M
r88  
Mr.doob 已提交
1158
						if ( child.nodeType === 'Material' ) {
M
r72  
Mr.doob 已提交
1159

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

M
r88  
Mr.doob 已提交
1162
							if ( child.diffuseColor !== undefined ) {
M
r59  
Mr.doob 已提交
1163

M
r66  
Mr.doob 已提交
1164
								var d = child.diffuseColor;
M
r59  
Mr.doob 已提交
1165

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

M
r66  
Mr.doob 已提交
1168
							}
M
r59  
Mr.doob 已提交
1169

M
r88  
Mr.doob 已提交
1170
							if ( child.emissiveColor !== undefined ) {
M
r59  
Mr.doob 已提交
1171

M
r66  
Mr.doob 已提交
1172
								var e = child.emissiveColor;
M
r59  
Mr.doob 已提交
1173

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

M
r66  
Mr.doob 已提交
1176
							}
M
r59  
Mr.doob 已提交
1177

M
r88  
Mr.doob 已提交
1178
							if ( child.specularColor !== undefined ) {
M
r59  
Mr.doob 已提交
1179

M
r66  
Mr.doob 已提交
1180
								var s = child.specularColor;
M
r59  
Mr.doob 已提交
1181

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

M
r66  
Mr.doob 已提交
1184
							}
M
r59  
Mr.doob 已提交
1185

M
r88  
Mr.doob 已提交
1186
							if ( child.transparency !== undefined ) {
M
r59  
Mr.doob 已提交
1187

M
r66  
Mr.doob 已提交
1188
								var t = child.transparency;
M
r59  
Mr.doob 已提交
1189

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

M
r66  
Mr.doob 已提交
1193
								material.transparent = true;
M
r63  
Mr.doob 已提交
1194

M
r66  
Mr.doob 已提交
1195
							}
M
r59  
Mr.doob 已提交
1196 1197 1198

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

M
r75  
Mr.doob 已提交
1199
								material.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
M
r62  
Mr.doob 已提交
1200

M
r59  
Mr.doob 已提交
1201 1202 1203 1204 1205 1206
								defines[ material.name ] = material;

							}

							parent.material = material;

M
r73  
Mr.doob 已提交
1207 1208
						}

M
r88  
Mr.doob 已提交
1209
						if ( child.nodeType === 'ImageTexture' ) {
M
r73  
Mr.doob 已提交
1210

M
r88  
Mr.doob 已提交
1211
							var textureName = /"([^"]+)"/.exec( child.children[ 0 ] );
M
r73  
Mr.doob 已提交
1212

M
r88  
Mr.doob 已提交
1213
							if ( textureName ) {
M
r73  
Mr.doob 已提交
1214 1215 1216

								parent.material.name = textureName[ 1 ];

M
r97  
Mr.doob 已提交
1217
								parent.material.map = textureLoader.load( textureName[ 1 ] );
M
r73  
Mr.doob 已提交
1218 1219

							}
M
r72  
Mr.doob 已提交
1220

M
r59  
Mr.doob 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
						}

					}

					return;

				}

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

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

				}

M
r88  
Mr.doob 已提交
1235
			}
M
r59  
Mr.doob 已提交
1236 1237 1238

			parseNode( getTree( lines ), scene );

M
r88  
Mr.doob 已提交
1239
		}
M
r59  
Mr.doob 已提交
1240 1241 1242 1243

		var scene = new THREE.Scene();

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

M
r73  
Mr.doob 已提交
1245
		// some lines do not have breaks
M
r88  
Mr.doob 已提交
1246 1247 1248 1249

		for ( var i = lines.length - 1; i > - 1; i -- ) {

			var line = lines[ i ];
M
r73  
Mr.doob 已提交
1250

M
r97  
Mr.doob 已提交
1251 1252 1253 1254
			// The # symbol indicates that all subsequent text, until the end of the line is a comment,
			// and should be ignored. (see http://gun.teipir.gr/VRML-amgem/spec/part1/grammar.html)
			line = line.replace( /(#.*)/, '' );

M
r73  
Mr.doob 已提交
1255
			// split lines with {..{ or {..[ - some have both
M
r88  
Mr.doob 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
			if ( /{.*[{\[]/.test( line ) ) {

				var parts = line.split( '{' ).join( '{\n' ).split( '\n' );
				parts.unshift( 1 );
				parts.unshift( i );
				lines.splice.apply( lines, parts );

			} else if ( /\].*}/.test( line ) ) {

				// split lines with ]..}
				var parts = line.split( ']' ).join( ']\n' ).split( '\n' );
				parts.unshift( 1 );
				parts.unshift( i );
				lines.splice.apply( lines, parts );

M
r73  
Mr.doob 已提交
1271 1272
			}

M
r88  
Mr.doob 已提交
1273 1274 1275 1276 1277 1278 1279 1280
			if ( /}.*}/.test( line ) ) {

				// split lines with }..}
				var parts = line.split( '}' ).join( '}\n' ).split( '\n' );
				parts.unshift( 1 );
				parts.unshift( i );
				lines.splice.apply( lines, parts );

M
r73  
Mr.doob 已提交
1281 1282
			}

M
r88  
Mr.doob 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
			if ( /^\b[^\s]+\b$/.test( line.trim() ) ) {

				// prevent lines with single words like "coord" or "geometry", see #12209
				lines[ i + 1 ] = line + ' ' + lines[ i + 1 ].trim();
				lines.splice( i, 1 );

			} else if ( ( line.indexOf( 'coord' ) > - 1 ) && ( line.indexOf( '[' ) < 0 ) && ( line.indexOf( '{' ) < 0 ) ) {

				// force the parser to create Coordinate node for empty coords
				// coord USE something -> coord USE something Coordinate {}

				lines[ i ] += ' Coordinate {}';

M
r73  
Mr.doob 已提交
1296
			}
M
r88  
Mr.doob 已提交
1297

M
r73  
Mr.doob 已提交
1298 1299
		}

M
r59  
Mr.doob 已提交
1300 1301 1302 1303
		var header = lines.shift();

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

M
r92  
Mr.doob 已提交
1304
			console.warn( 'THREE.VRMLLoader: V1.0 not supported yet.' );
M
r59  
Mr.doob 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

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

			parseV2( lines, scene );

		}

		return scene;

	}

};