VRMLLoader.js 22 KB

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