VRMLLoader.js 24 KB

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