VRMLLoader.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.VRMLLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. };
  7. THREE.VRMLLoader.prototype = {
  8. constructor: THREE.VRMLLoader,
  9. // for IndexedFaceSet support
  10. isRecordingPoints: false,
  11. isRecordingFaces: false,
  12. points: [],
  13. indexes: [],
  14. // for Background support
  15. isRecordingAngles: false,
  16. isRecordingColors: false,
  17. angles: [],
  18. colors: [],
  19. recordingFieldname: null,
  20. crossOrigin: 'anonymous',
  21. load: function ( url, onLoad, onProgress, onError ) {
  22. var scope = this;
  23. var loader = new THREE.FileLoader( this.manager );
  24. loader.load( url, function ( text ) {
  25. onLoad( scope.parse( text ) );
  26. }, onProgress, onError );
  27. },
  28. setCrossOrigin: function ( value ) {
  29. this.crossOrigin = value;
  30. return this;
  31. },
  32. parse: function ( data ) {
  33. var scope = this;
  34. var texturePath = this.texturePath || '';
  35. var textureLoader = new THREE.TextureLoader( this.manager );
  36. textureLoader.setCrossOrigin( this.crossOrigin );
  37. function parseV2( lines, scene ) {
  38. var defines = {};
  39. var float_pattern = /(\b|\-|\+)([\d\.e]+)/;
  40. var float2_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
  41. var float3_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
  42. /**
  43. * Vertically paints the faces interpolating between the
  44. * specified colors at the specified angels. This is used for the Background
  45. * node, but could be applied to other nodes with multiple faces as well.
  46. *
  47. * When used with the Background node, default is directionIsDown is true if
  48. * interpolating the skyColor down from the Zenith. When interpolationg up from
  49. * the Nadir i.e. interpolating the groundColor, the directionIsDown is false.
  50. *
  51. * The first angle is never specified, it is the Zenith (0 rad). Angles are specified
  52. * in radians. The geometry is thought a sphere, but could be anything. The color interpolation
  53. * is linear along the Y axis in any case.
  54. *
  55. * You must specify one more color than you have angles at the beginning of the colors array.
  56. * This is the color of the Zenith (the top of the shape).
  57. *
  58. * @param geometry
  59. * @param radius
  60. * @param angles
  61. * @param colors
  62. * @param boolean topDown Whether to work top down or bottom up.
  63. */
  64. function paintFaces( geometry, radius, angles, colors, topDown ) {
  65. var direction = ( topDown === true ) ? 1 : - 1;
  66. var coord = [], A = {}, B = {}, applyColor = false;
  67. for ( var k = 0; k < angles.length; k ++ ) {
  68. // push the vector at which the color changes
  69. var vec = {
  70. x: direction * ( Math.cos( angles[ k ] ) * radius ),
  71. y: direction * ( Math.sin( angles[ k ] ) * radius )
  72. };
  73. coord.push( vec );
  74. }
  75. var index = geometry.index;
  76. var positionAttribute = geometry.attributes.position;
  77. var colorAttribute = new THREE.BufferAttribute( new Float32Array( geometry.attributes.position.count * 3 ), 3 );
  78. var position = new THREE.Vector3();
  79. var color = new THREE.Color();
  80. for ( var i = 0; i < index.count; i ++ ) {
  81. var vertexIndex = index.getX( i );
  82. position.fromBufferAttribute( positionAttribute, vertexIndex );
  83. for ( var j = 0; j < colors.length; j ++ ) {
  84. // linear interpolation between aColor and bColor, calculate proportion
  85. // A is previous point (angle)
  86. if ( j === 0 ) {
  87. A.x = 0;
  88. A.y = ( topDown === true ) ? radius : - 1 * radius;
  89. } else {
  90. A.x = coord[ j - 1 ].x;
  91. A.y = coord[ j - 1 ].y;
  92. }
  93. // B is current point (angle)
  94. B = coord[ j ];
  95. if ( B !== undefined ) {
  96. // p has to be between the points A and B which we interpolate
  97. applyColor = ( topDown === true ) ? ( position.y <= A.y && position.y > B.y ) : ( position.y >= A.y && position.y < B.y );
  98. if ( applyColor === true ) {
  99. var aColor = colors[ j ];
  100. var bColor = colors[ j + 1 ];
  101. // below is simple linear interpolation
  102. var t = Math.abs( position.y - A.y ) / ( A.y - B.y );
  103. // 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
  104. color.copy( aColor ).lerp( bColor, t );
  105. colorAttribute.setXYZ( vertexIndex, color.r, color.g, color.b );
  106. } else {
  107. var colorIndex = ( topDown === true ) ? colors.length - 1 : 0;
  108. var c = colors[ colorIndex ];
  109. colorAttribute.setXYZ( vertexIndex, c.r, c.g, c.b );
  110. }
  111. }
  112. }
  113. }
  114. geometry.addAttribute( 'color', colorAttribute );
  115. }
  116. var index = [];
  117. function parseProperty( node, line ) {
  118. var parts = [], part, property = {}, fieldName;
  119. /**
  120. * Expression for matching relevant information, such as a name or value, but not the separators
  121. * @type {RegExp}
  122. */
  123. var regex = /[^\s,\[\]]+/g;
  124. var point;
  125. while ( null !== ( part = regex.exec( line ) ) ) {
  126. parts.push( part[ 0 ] );
  127. }
  128. fieldName = parts[ 0 ];
  129. // trigger several recorders
  130. switch ( fieldName ) {
  131. case 'skyAngle':
  132. case 'groundAngle':
  133. scope.recordingFieldname = fieldName;
  134. scope.isRecordingAngles = true;
  135. scope.angles = [];
  136. break;
  137. case 'color':
  138. case 'skyColor':
  139. case 'groundColor':
  140. scope.recordingFieldname = fieldName;
  141. scope.isRecordingColors = true;
  142. scope.colors = [];
  143. break;
  144. case 'point':
  145. case 'vector':
  146. scope.recordingFieldname = fieldName;
  147. scope.isRecordingPoints = true;
  148. scope.points = [];
  149. break;
  150. case 'colorIndex':
  151. case 'coordIndex':
  152. case 'normalIndex':
  153. case 'texCoordIndex':
  154. scope.recordingFieldname = fieldName;
  155. scope.isRecordingFaces = true;
  156. scope.indexes = [];
  157. break;
  158. }
  159. if ( scope.isRecordingFaces ) {
  160. // the parts hold the indexes as strings
  161. if ( parts.length > 0 ) {
  162. for ( var ind = 0; ind < parts.length; ind ++ ) {
  163. // the part should either be positive integer or -1
  164. if ( ! /(-?\d+)/.test( parts[ ind ] ) ) {
  165. continue;
  166. }
  167. // end of current face
  168. if ( parts[ ind ] === '-1' ) {
  169. if ( index.length > 0 ) {
  170. scope.indexes.push( index );
  171. }
  172. // start new one
  173. index = [];
  174. } else {
  175. index.push( parseInt( parts[ ind ] ) );
  176. }
  177. }
  178. }
  179. // end
  180. if ( /]/.exec( line ) ) {
  181. if ( index.length > 0 ) {
  182. scope.indexes.push( index );
  183. }
  184. // start new one
  185. index = [];
  186. scope.isRecordingFaces = false;
  187. node[ scope.recordingFieldname ] = scope.indexes;
  188. }
  189. } else if ( scope.isRecordingPoints ) {
  190. if ( node.nodeType == 'Coordinate' ) {
  191. while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
  192. point = {
  193. x: parseFloat( parts[ 1 ] ),
  194. y: parseFloat( parts[ 2 ] ),
  195. z: parseFloat( parts[ 3 ] )
  196. };
  197. scope.points.push( point );
  198. }
  199. }
  200. if ( node.nodeType == 'Normal' ) {
  201. while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
  202. point = {
  203. x: parseFloat( parts[ 1 ] ),
  204. y: parseFloat( parts[ 2 ] ),
  205. z: parseFloat( parts[ 3 ] )
  206. };
  207. scope.points.push( point );
  208. }
  209. }
  210. if ( node.nodeType == 'TextureCoordinate' ) {
  211. while ( null !== ( parts = float2_pattern.exec( line ) ) ) {
  212. point = {
  213. x: parseFloat( parts[ 1 ] ),
  214. y: parseFloat( parts[ 2 ] )
  215. };
  216. scope.points.push( point );
  217. }
  218. }
  219. // end
  220. if ( /]/.exec( line ) ) {
  221. scope.isRecordingPoints = false;
  222. node.points = scope.points;
  223. }
  224. } else if ( scope.isRecordingAngles ) {
  225. // the parts hold the angles as strings
  226. if ( parts.length > 0 ) {
  227. for ( var ind = 0; ind < parts.length; ind ++ ) {
  228. // the part should be a float
  229. if ( ! float_pattern.test( parts[ ind ] ) ) {
  230. continue;
  231. }
  232. scope.angles.push( parseFloat( parts[ ind ] ) );
  233. }
  234. }
  235. // end
  236. if ( /]/.exec( line ) ) {
  237. scope.isRecordingAngles = false;
  238. node[ scope.recordingFieldname ] = scope.angles;
  239. }
  240. } else if ( scope.isRecordingColors ) {
  241. while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
  242. var color = {
  243. r: parseFloat( parts[ 1 ] ),
  244. g: parseFloat( parts[ 2 ] ),
  245. b: parseFloat( parts[ 3 ] )
  246. };
  247. scope.colors.push( color );
  248. }
  249. // end
  250. if ( /]/.exec( line ) ) {
  251. scope.isRecordingColors = false;
  252. node[ scope.recordingFieldname ] = scope.colors;
  253. }
  254. } else if ( parts[ parts.length - 1 ] !== 'NULL' && fieldName !== 'children' ) {
  255. switch ( fieldName ) {
  256. case 'diffuseColor':
  257. case 'emissiveColor':
  258. case 'specularColor':
  259. case 'color':
  260. if ( parts.length !== 4 ) {
  261. console.warn( 'THREE.VRMLLoader: Invalid color format detected for %s.', fieldName );
  262. break;
  263. }
  264. property = {
  265. r: parseFloat( parts[ 1 ] ),
  266. g: parseFloat( parts[ 2 ] ),
  267. b: parseFloat( parts[ 3 ] )
  268. };
  269. break;
  270. case 'location':
  271. case 'direction':
  272. case 'translation':
  273. case 'scale':
  274. case 'size':
  275. if ( parts.length !== 4 ) {
  276. console.warn( 'THREE.VRMLLoader: Invalid vector format detected for %s.', fieldName );
  277. break;
  278. }
  279. property = {
  280. x: parseFloat( parts[ 1 ] ),
  281. y: parseFloat( parts[ 2 ] ),
  282. z: parseFloat( parts[ 3 ] )
  283. };
  284. break;
  285. case 'intensity':
  286. case 'cutOffAngle':
  287. case 'radius':
  288. case 'topRadius':
  289. case 'bottomRadius':
  290. case 'height':
  291. case 'transparency':
  292. case 'shininess':
  293. case 'ambientIntensity':
  294. case 'creaseAngle':
  295. if ( parts.length !== 2 ) {
  296. console.warn( 'THREE.VRMLLoader: Invalid single float value specification detected for %s.', fieldName );
  297. break;
  298. }
  299. property = parseFloat( parts[ 1 ] );
  300. break;
  301. case 'rotation':
  302. if ( parts.length !== 5 ) {
  303. console.warn( 'THREE.VRMLLoader: Invalid quaternion format detected for %s.', fieldName );
  304. break;
  305. }
  306. property = {
  307. x: parseFloat( parts[ 1 ] ),
  308. y: parseFloat( parts[ 2 ] ),
  309. z: parseFloat( parts[ 3 ] ),
  310. w: parseFloat( parts[ 4 ] )
  311. };
  312. break;
  313. case 'on':
  314. case 'ccw':
  315. case 'solid':
  316. case 'colorPerVertex':
  317. case 'convex':
  318. if ( parts.length !== 2 ) {
  319. console.warn( 'THREE.VRMLLoader: Invalid format detected for %s.', fieldName );
  320. break;
  321. }
  322. property = parts[ 1 ] === 'TRUE' ? true : false;
  323. break;
  324. }
  325. node[ fieldName ] = property;
  326. }
  327. return property;
  328. }
  329. function getTree( lines ) {
  330. var tree = { 'string': 'Scene', children: [] };
  331. var current = tree;
  332. var matches;
  333. var specification;
  334. for ( var i = 0; i < lines.length; i ++ ) {
  335. var comment = '';
  336. var line = lines[ i ];
  337. // omit whitespace only lines
  338. if ( null !== ( /^\s+?$/g.exec( line ) ) ) {
  339. continue;
  340. }
  341. line = line.trim();
  342. // skip empty lines
  343. if ( line === '' ) {
  344. continue;
  345. }
  346. if ( /#/.exec( line ) ) {
  347. var parts = line.split( '#' );
  348. // discard everything after the #, it is a comment
  349. line = parts[ 0 ];
  350. // well, let's also keep the comment
  351. comment = parts[ 1 ];
  352. }
  353. if ( matches = /([^\s]*){1}(?:\s+)?{/.exec( line ) ) {
  354. // first subpattern should match the Node name
  355. var block = { 'nodeType': matches[ 1 ], 'string': line, 'parent': current, 'children': [], 'comment': comment };
  356. current.children.push( block );
  357. current = block;
  358. if ( /}/.exec( line ) ) {
  359. // example: geometry Box { size 1 1 1 } # all on the same line
  360. specification = /{(.*)}/.exec( line )[ 1 ];
  361. // todo: remove once new parsing is complete?
  362. block.children.push( specification );
  363. parseProperty( current, specification );
  364. current = current.parent;
  365. }
  366. } else if ( /}/.exec( line ) ) {
  367. current = current.parent;
  368. } else if ( line !== '' ) {
  369. parseProperty( current, line );
  370. // todo: remove once new parsing is complete? we still do not parse geometry and appearance the new way
  371. current.children.push( line );
  372. }
  373. }
  374. return tree;
  375. }
  376. function parseNode( data, parent ) {
  377. var object;
  378. if ( typeof data === 'string' ) {
  379. if ( /USE/.exec( data ) ) {
  380. var defineKey = /USE\s+?([^\s]+)/.exec( data )[ 1 ];
  381. if ( undefined == defines[ defineKey ] ) {
  382. console.warn( 'THREE.VRMLLoader: %s is not defined.', defineKey );
  383. } else {
  384. if ( /appearance/.exec( data ) && defineKey ) {
  385. parent.material = defines[ defineKey ].clone();
  386. } else if ( /geometry/.exec( data ) && defineKey ) {
  387. parent.geometry = defines[ defineKey ].clone();
  388. // the solid property is not cloned with clone(), is only needed for VRML loading, so we need to transfer it
  389. if ( undefined !== defines[ defineKey ].solid && defines[ defineKey ].solid === false ) {
  390. parent.geometry.solid = false;
  391. parent.material.side = THREE.DoubleSide;
  392. }
  393. } else if ( defineKey ) {
  394. object = defines[ defineKey ].clone();
  395. parent.add( object );
  396. }
  397. }
  398. }
  399. return;
  400. }
  401. object = parent;
  402. if ( data.string.indexOf( 'AmbientLight' ) > - 1 && data.nodeType === 'PointLight' ) {
  403. data.nodeType = 'AmbientLight';
  404. }
  405. var l_visible = data.on !== undefined ? data.on : true;
  406. var l_intensity = data.intensity !== undefined ? data.intensity : 1;
  407. var l_color = new THREE.Color();
  408. if ( data.color ) {
  409. l_color.copy( data.color );
  410. }
  411. if ( data.nodeType === 'AmbientLight' ) {
  412. object = new THREE.AmbientLight( l_color, l_intensity );
  413. object.visible = l_visible;
  414. parent.add( object );
  415. } else if ( data.nodeType === 'PointLight' ) {
  416. var l_distance = 0;
  417. if ( data.radius !== undefined && data.radius < 1000 ) {
  418. l_distance = data.radius;
  419. }
  420. object = new THREE.PointLight( l_color, l_intensity, l_distance );
  421. object.visible = l_visible;
  422. parent.add( object );
  423. } else if ( data.nodeType === 'SpotLight' ) {
  424. var l_intensity = 1;
  425. var l_distance = 0;
  426. var l_angle = Math.PI / 3;
  427. var l_penumbra = 0;
  428. var l_visible = true;
  429. if ( data.radius !== undefined && data.radius < 1000 ) {
  430. l_distance = data.radius;
  431. }
  432. if ( data.cutOffAngle !== undefined ) {
  433. l_angle = data.cutOffAngle;
  434. }
  435. object = new THREE.SpotLight( l_color, l_intensity, l_distance, l_angle, l_penumbra );
  436. object.visible = l_visible;
  437. parent.add( object );
  438. } else if ( data.nodeType === 'Transform' || data.nodeType === 'Group' ) {
  439. object = new THREE.Object3D();
  440. if ( /DEF/.exec( data.string ) ) {
  441. object.name = /DEF\s+([^\s]+)/.exec( data.string )[ 1 ];
  442. defines[ object.name ] = object;
  443. }
  444. if ( data.translation !== undefined ) {
  445. var t = data.translation;
  446. object.position.set( t.x, t.y, t.z );
  447. }
  448. if ( data.rotation !== undefined ) {
  449. var r = data.rotation;
  450. object.quaternion.setFromAxisAngle( new THREE.Vector3( r.x, r.y, r.z ), r.w );
  451. }
  452. if ( data.scale !== undefined ) {
  453. var s = data.scale;
  454. object.scale.set( s.x, s.y, s.z );
  455. }
  456. parent.add( object );
  457. } else if ( data.nodeType === 'Shape' ) {
  458. object = new THREE.Mesh();
  459. if ( /DEF/.exec( data.string ) ) {
  460. object.name = /DEF\s+([^\s]+)/.exec( data.string )[ 1 ];
  461. defines[ object.name ] = object;
  462. }
  463. parent.add( object );
  464. } else if ( data.nodeType === 'Background' ) {
  465. var segments = 20;
  466. // sky (full sphere):
  467. var radius = 2e4;
  468. var skyGeometry = new THREE.SphereBufferGeometry( radius, segments, segments );
  469. var skyMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide } );
  470. if ( data.skyColor.length > 1 ) {
  471. paintFaces( skyGeometry, radius, data.skyAngle, data.skyColor, true );
  472. skyMaterial.vertexColors = THREE.VertexColors;
  473. } else {
  474. var color = data.skyColor[ 0 ];
  475. skyMaterial.color.setRGB( color.r, color.b, color.g );
  476. }
  477. scene.add( new THREE.Mesh( skyGeometry, skyMaterial ) );
  478. // ground (half sphere):
  479. if ( data.groundColor !== undefined ) {
  480. radius = 1.2e4;
  481. var groundGeometry = new THREE.SphereBufferGeometry( radius, segments, segments, 0, 2 * Math.PI, 0.5 * Math.PI, 1.5 * Math.PI );
  482. var groundMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide, vertexColors: THREE.VertexColors } );
  483. paintFaces( groundGeometry, radius, data.groundAngle, data.groundColor, false );
  484. scene.add( new THREE.Mesh( groundGeometry, groundMaterial ) );
  485. }
  486. } else if ( /geometry/.exec( data.string ) ) {
  487. if ( data.nodeType === 'Box' ) {
  488. var s = data.size;
  489. parent.geometry = new THREE.BoxBufferGeometry( s.x, s.y, s.z );
  490. } else if ( data.nodeType === 'Cylinder' ) {
  491. parent.geometry = new THREE.CylinderBufferGeometry( data.radius, data.radius, data.height );
  492. } else if ( data.nodeType === 'Cone' ) {
  493. parent.geometry = new THREE.CylinderBufferGeometry( data.topRadius, data.bottomRadius, data.height );
  494. } else if ( data.nodeType === 'Sphere' ) {
  495. parent.geometry = new THREE.SphereBufferGeometry( data.radius );
  496. } else if ( data.nodeType === 'IndexedFaceSet' ) {
  497. var geometry = new THREE.BufferGeometry();
  498. var positions = [];
  499. var colors = [];
  500. var normals = [];
  501. var uvs = [];
  502. var position, color, normal, uv;
  503. var i, il, j, jl;
  504. for ( i = 0, il = data.children.length; i < il; i ++ ) {
  505. var child = data.children[ i ];
  506. // uvs
  507. if ( child.nodeType === 'TextureCoordinate' ) {
  508. if ( child.points ) {
  509. for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
  510. uv = child.points[ j ];
  511. uvs.push( uv.x, uv.y );
  512. }
  513. }
  514. }
  515. // normals
  516. if ( child.nodeType === 'Normal' ) {
  517. if ( child.points ) {
  518. for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
  519. normal = child.points[ j ];
  520. normals.push( normal.x, normal.y, normal.z );
  521. }
  522. }
  523. }
  524. // colors
  525. if ( child.nodeType === 'Color' ) {
  526. if ( child.color ) {
  527. for ( j = 0, jl = child.color.length; j < jl; j ++ ) {
  528. color = child.color[ j ];
  529. colors.push( color.r, color.g, color.b );
  530. }
  531. }
  532. }
  533. // positions
  534. if ( child.nodeType === 'Coordinate' ) {
  535. if ( child.points ) {
  536. for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
  537. position = child.points[ j ];
  538. positions.push( position.x, position.y, position.z );
  539. }
  540. }
  541. if ( child.string.indexOf( 'DEF' ) > - 1 ) {
  542. var name = /DEF\s+([^\s]+)/.exec( child.string )[ 1 ];
  543. defines[ name ] = positions.slice( 0 );
  544. }
  545. if ( child.string.indexOf( 'USE' ) > - 1 ) {
  546. var defineKey = /USE\s+([^\s]+)/.exec( child.string )[ 1 ];
  547. positions = defines[ defineKey ];
  548. }
  549. }
  550. }
  551. var skip = 0;
  552. // some shapes only have vertices for use in other shapes
  553. if ( data.coordIndex ) {
  554. var newPositions = [];
  555. var newColors = [];
  556. var newNormals = [];
  557. var newUvs = [];
  558. position = new THREE.Vector3();
  559. color = new THREE.Color();
  560. normal = new THREE.Vector3();
  561. uv = new THREE.Vector2();
  562. for ( i = 0, il = data.coordIndex.length; i < il; i ++ ) {
  563. var indexes = data.coordIndex[ i ];
  564. // VRML support multipoint indexed face sets (more then 3 vertices). You must calculate the composing triangles here
  565. skip = 0;
  566. while ( indexes.length >= 3 && skip < ( indexes.length - 2 ) ) {
  567. if ( data.ccw === undefined ) data.ccw = true; // ccw is true by default
  568. var i1 = indexes[ 0 ];
  569. var i2 = indexes[ skip + ( data.ccw ? 1 : 2 ) ];
  570. var i3 = indexes[ skip + ( data.ccw ? 2 : 1 ) ];
  571. // create non indexed geometry, necessary for face normal generation
  572. position.fromArray( positions, i1 * 3 );
  573. newPositions.push( position.x, position.y, position.z );
  574. position.fromArray( positions, i2 * 3 );
  575. newPositions.push( position.x, position.y, position.z );
  576. position.fromArray( positions, i3 * 3 );
  577. newPositions.push( position.x, position.y, position.z );
  578. if ( colors.length > 0 ) {
  579. color.fromArray( colors, i1 * 3 );
  580. newColors.push( color.r, color.g, color.b );
  581. color.fromArray( colors, i2 * 3 );
  582. newColors.push( color.r, color.g, color.b );
  583. color.fromArray( colors, i3 * 3 );
  584. newColors.push( color.r, color.g, color.b );
  585. }
  586. if ( uvs.length > 0 ) {
  587. uv.fromArray( uvs, i1 * 2 );
  588. newUvs.push( uv.x, uv.y );
  589. uv.fromArray( uvs, i2 * 2 );
  590. newUvs.push( uv.x, uv.y );
  591. uv.fromArray( uvs, i3 * 2 );
  592. newUvs.push( uv.x, uv.y );
  593. }
  594. if ( normals.length > 0 ) {
  595. normal.fromArray( normals, i1 * 3 );
  596. newNormals.push( normal.x, normal.y, normal.z );
  597. normal.fromArray( normals, i2 * 3 );
  598. newNormals.push( normal.x, normal.y, normal.z );
  599. normal.fromArray( normals, i3 * 3 );
  600. newNormals.push( normal.x, normal.y, normal.z );
  601. }
  602. skip ++;
  603. }
  604. }
  605. positions = newPositions;
  606. colors = newColors;
  607. normals = newNormals;
  608. uvs = newUvs;
  609. } else {
  610. // do not add dummy mesh to the scene
  611. parent.parent.remove( parent );
  612. }
  613. if ( false === data.solid ) {
  614. parent.material.side = THREE.DoubleSide;
  615. }
  616. // we need to store it on the geometry for use with defines
  617. geometry.solid = data.solid;
  618. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  619. if ( colors.length > 0 ) {
  620. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  621. }
  622. if ( uvs.length > 0 ) {
  623. geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  624. }
  625. if ( normals.length > 0 ) {
  626. geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  627. } else {
  628. geometry.computeVertexNormals();
  629. }
  630. geometry.computeBoundingSphere();
  631. // see if it's a define
  632. if ( /DEF/.exec( data.string ) ) {
  633. geometry.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
  634. defines[ geometry.name ] = geometry;
  635. }
  636. parent.geometry = geometry;
  637. }
  638. return;
  639. } else if ( /appearance/.exec( data.string ) ) {
  640. for ( var i = 0; i < data.children.length; i ++ ) {
  641. var child = data.children[ i ];
  642. if ( child.nodeType === 'Material' ) {
  643. var material = new THREE.MeshPhongMaterial();
  644. if ( child.diffuseColor !== undefined ) {
  645. var d = child.diffuseColor;
  646. material.color.setRGB( d.r, d.g, d.b );
  647. }
  648. if ( child.emissiveColor !== undefined ) {
  649. var e = child.emissiveColor;
  650. material.emissive.setRGB( e.r, e.g, e.b );
  651. }
  652. if ( child.specularColor !== undefined ) {
  653. var s = child.specularColor;
  654. material.specular.setRGB( s.r, s.g, s.b );
  655. }
  656. if ( child.transparency !== undefined ) {
  657. var t = child.transparency;
  658. // transparency is opposite of opacity
  659. material.opacity = Math.abs( 1 - t );
  660. material.transparent = true;
  661. }
  662. if ( /DEF/.exec( data.string ) ) {
  663. material.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
  664. defines[ material.name ] = material;
  665. }
  666. parent.material = material;
  667. }
  668. if ( child.nodeType === 'ImageTexture' ) {
  669. var textureName = /"([^"]+)"/.exec( child.children[ 0 ] );
  670. if ( textureName ) {
  671. parent.material.name = textureName[ 1 ];
  672. parent.material.map = textureLoader.load( texturePath + textureName[ 1 ] );
  673. }
  674. }
  675. }
  676. return;
  677. }
  678. for ( var i = 0, l = data.children.length; i < l; i ++ ) {
  679. parseNode( data.children[ i ], object );
  680. }
  681. }
  682. parseNode( getTree( lines ), scene );
  683. }
  684. var scene = new THREE.Scene();
  685. var lines = data.split( '\n' );
  686. // some lines do not have breaks
  687. for ( var i = lines.length - 1; i > - 1; i -- ) {
  688. var line = lines[ i ];
  689. // The # symbol indicates that all subsequent text, until the end of the line is a comment,
  690. // and should be ignored. (see http://gun.teipir.gr/VRML-amgem/spec/part1/grammar.html)
  691. line = line.replace( /(#.*)/, '' );
  692. // split lines with {..{ or {..[ - some have both
  693. if ( /{.*[{\[]/.test( line ) ) {
  694. var parts = line.split( '{' ).join( '{\n' ).split( '\n' );
  695. parts.unshift( 1 );
  696. parts.unshift( i );
  697. lines.splice.apply( lines, parts );
  698. } else if ( /\].*}/.test( line ) ) {
  699. // split lines with ]..}
  700. var parts = line.split( ']' ).join( ']\n' ).split( '\n' );
  701. parts.unshift( 1 );
  702. parts.unshift( i );
  703. lines.splice.apply( lines, parts );
  704. }
  705. if ( /}.*}/.test( line ) ) {
  706. // split lines with }..}
  707. var parts = line.split( '}' ).join( '}\n' ).split( '\n' );
  708. parts.unshift( 1 );
  709. parts.unshift( i );
  710. lines.splice.apply( lines, parts );
  711. }
  712. if ( /^\b[^\s]+\b$/.test( line.trim() ) ) {
  713. // prevent lines with single words like "coord" or "geometry", see #12209
  714. lines[ i + 1 ] = line + ' ' + lines[ i + 1 ].trim();
  715. lines.splice( i, 1 );
  716. } else if ( ( line.indexOf( 'coord' ) > - 1 ) && ( line.indexOf( '[' ) < 0 ) && ( line.indexOf( '{' ) < 0 ) ) {
  717. // force the parser to create Coordinate node for empty coords
  718. // coord USE something -> coord USE something Coordinate {}
  719. lines[ i ] += ' Coordinate {}';
  720. }
  721. }
  722. var header = lines.shift();
  723. if ( /V1.0/.exec( header ) ) {
  724. console.warn( 'THREE.VRMLLoader: V1.0 not supported yet.' );
  725. } else if ( /V2.0/.exec( header ) ) {
  726. parseV2( lines, scene );
  727. }
  728. return scene;
  729. }
  730. };