VRMLLoader.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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() {
  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;
  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. }
  197. if ( node.nodeType == 'TextureCoordinate' ) {
  198. while ( null !== ( parts = float2_pattern.exec( line ) ) ) {
  199. point = {
  200. x: parseFloat( parts[ 1 ] ),
  201. y: parseFloat( parts[ 2 ] )
  202. };
  203. this.points.push( point );
  204. }
  205. }
  206. // end
  207. if ( /]/.exec( line ) ) {
  208. this.isRecordingPoints = false;
  209. node.points = this.points;
  210. }
  211. } else if ( this.isRecordingAngles ) {
  212. // the parts hold the angles as strings
  213. if ( parts.length > 0 ) {
  214. for ( var ind = 0; ind < parts.length; ind ++ ) {
  215. // the part should be a float
  216. if ( ! float_pattern.test( parts[ ind ] ) ) {
  217. continue;
  218. }
  219. this.angles.push( parseFloat( parts[ ind ] ) );
  220. }
  221. }
  222. // end
  223. if ( /]/.exec( line ) ) {
  224. this.isRecordingAngles = false;
  225. node[ this.recordingFieldname ] = this.angles;
  226. }
  227. } else if ( this.isRecordingColors ) {
  228. while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
  229. var color = {
  230. r: parseFloat( parts[ 1 ] ),
  231. g: parseFloat( parts[ 2 ] ),
  232. b: parseFloat( parts[ 3 ] )
  233. };
  234. this.colors.push( color );
  235. }
  236. // end
  237. if ( /]/.exec( line ) ) {
  238. this.isRecordingColors = false;
  239. node[ this.recordingFieldname ] = this.colors;
  240. }
  241. } else if ( parts[ parts.length - 1 ] !== 'NULL' && fieldName !== 'children' ) {
  242. switch ( fieldName ) {
  243. case 'diffuseColor':
  244. case 'emissiveColor':
  245. case 'specularColor':
  246. case 'color':
  247. if ( parts.length !== 4 ) {
  248. console.warn( 'THREE.VRMLLoader: Invalid color format detected for %s.', fieldName );
  249. break;
  250. }
  251. property = {
  252. r: parseFloat( parts[ 1 ] ),
  253. g: parseFloat( parts[ 2 ] ),
  254. b: parseFloat( parts[ 3 ] )
  255. };
  256. break;
  257. case 'location':
  258. case 'direction':
  259. case 'translation':
  260. case 'scale':
  261. case 'size':
  262. if ( parts.length !== 4 ) {
  263. console.warn( 'THREE.VRMLLoader: Invalid vector format detected for %s.', fieldName );
  264. break;
  265. }
  266. property = {
  267. x: parseFloat( parts[ 1 ] ),
  268. y: parseFloat( parts[ 2 ] ),
  269. z: parseFloat( parts[ 3 ] )
  270. };
  271. break;
  272. case 'intensity':
  273. case 'cutOffAngle':
  274. case 'radius':
  275. case 'topRadius':
  276. case 'bottomRadius':
  277. case 'height':
  278. case 'transparency':
  279. case 'shininess':
  280. case 'ambientIntensity':
  281. if ( parts.length !== 2 ) {
  282. console.warn( 'THREE.VRMLLoader: Invalid single float value specification detected for %s.', fieldName );
  283. break;
  284. }
  285. property = parseFloat( parts[ 1 ] );
  286. break;
  287. case 'rotation':
  288. if ( parts.length !== 5 ) {
  289. console.warn( 'THREE.VRMLLoader: Invalid quaternion format detected for %s.', fieldName );
  290. break;
  291. }
  292. property = {
  293. x: parseFloat( parts[ 1 ] ),
  294. y: parseFloat( parts[ 2 ] ),
  295. z: parseFloat( parts[ 3 ] ),
  296. w: parseFloat( parts[ 4 ] )
  297. };
  298. break;
  299. case 'on':
  300. case 'ccw':
  301. case 'solid':
  302. case 'colorPerVertex':
  303. case 'convex':
  304. if ( parts.length !== 2 ) {
  305. console.warn( 'THREE.VRMLLoader: Invalid format detected for %s.', fieldName );
  306. break;
  307. }
  308. property = parts[ 1 ] === 'TRUE' ? true : false;
  309. break;
  310. }
  311. node[ fieldName ] = property;
  312. }
  313. return property;
  314. }
  315. function getTree( lines ) {
  316. var tree = { 'string': 'Scene', children: [] };
  317. var current = tree;
  318. var matches;
  319. var specification;
  320. for ( var i = 0; i < lines.length; i ++ ) {
  321. var comment = '';
  322. var line = lines[ i ];
  323. // omit whitespace only lines
  324. if ( null !== ( /^\s+?$/g.exec( line ) ) ) {
  325. continue;
  326. }
  327. line = line.trim();
  328. // skip empty lines
  329. if ( line === '' ) {
  330. continue;
  331. }
  332. if ( /#/.exec( line ) ) {
  333. var parts = line.split( '#' );
  334. // discard everything after the #, it is a comment
  335. line = parts[ 0 ];
  336. // well, let's also keep the comment
  337. comment = parts[ 1 ];
  338. }
  339. if ( matches = /([^\s]*){1}(?:\s+)?{/.exec( line ) ) {
  340. // first subpattern should match the Node name
  341. var block = { 'nodeType': matches[ 1 ], 'string': line, 'parent': current, 'children': [], 'comment': comment };
  342. current.children.push( block );
  343. current = block;
  344. if ( /}/.exec( line ) ) {
  345. // example: geometry Box { size 1 1 1 } # all on the same line
  346. specification = /{(.*)}/.exec( line )[ 1 ];
  347. // todo: remove once new parsing is complete?
  348. block.children.push( specification );
  349. parseProperty( current, specification );
  350. current = current.parent;
  351. }
  352. } else if ( /}/.exec( line ) ) {
  353. current = current.parent;
  354. } else if ( line !== '' ) {
  355. parseProperty( current, line );
  356. // todo: remove once new parsing is complete? we still do not parse geometry and appearance the new way
  357. current.children.push( line );
  358. }
  359. }
  360. return tree;
  361. }
  362. function parseNode( data, parent ) {
  363. var object;
  364. if ( typeof data === 'string' ) {
  365. if ( /USE/.exec( data ) ) {
  366. var defineKey = /USE\s+?([^\s]+)/.exec( data )[ 1 ];
  367. if ( undefined == defines[ defineKey ] ) {
  368. console.warn( 'THREE.VRMLLoader: %s is not defined.', defineKey );
  369. } else {
  370. if ( /appearance/.exec( data ) && defineKey ) {
  371. parent.material = defines[ defineKey ].clone();
  372. } else if ( /geometry/.exec( data ) && defineKey ) {
  373. parent.geometry = defines[ defineKey ].clone();
  374. // the solid property is not cloned with clone(), is only needed for VRML loading, so we need to transfer it
  375. if ( undefined !== defines[ defineKey ].solid && defines[ defineKey ].solid === false ) {
  376. parent.geometry.solid = false;
  377. parent.material.side = THREE.DoubleSide;
  378. }
  379. } else if ( defineKey ) {
  380. object = defines[ defineKey ].clone();
  381. parent.add( object );
  382. }
  383. }
  384. }
  385. return;
  386. }
  387. object = parent;
  388. if ( data.string.indexOf( 'AmbientLight' ) > - 1 && data.nodeType === 'PointLight' ) {
  389. data.nodeType = 'AmbientLight';
  390. }
  391. var l_visible = data.on !== undefined ? data.on : true;
  392. var l_intensity = data.intensity !== undefined ? data.intensity : 1;
  393. var l_color = new THREE.Color();
  394. if ( data.color ) {
  395. l_color.copy( data.color );
  396. }
  397. if ( data.nodeType === 'AmbientLight' ) {
  398. object = new THREE.AmbientLight( l_color, l_intensity );
  399. object.visible = l_visible;
  400. parent.add( object );
  401. } else if ( data.nodeType === 'PointLight' ) {
  402. var l_distance = 0;
  403. if ( data.radius !== undefined && data.radius < 1000 ) {
  404. l_distance = data.radius;
  405. }
  406. object = new THREE.PointLight( l_color, l_intensity, l_distance );
  407. object.visible = l_visible;
  408. parent.add( object );
  409. } else if ( data.nodeType === 'SpotLight' ) {
  410. var l_intensity = 1;
  411. var l_distance = 0;
  412. var l_angle = Math.PI / 3;
  413. var l_penumbra = 0;
  414. var l_visible = true;
  415. if ( data.radius !== undefined && data.radius < 1000 ) {
  416. l_distance = data.radius;
  417. }
  418. if ( data.cutOffAngle !== undefined ) {
  419. l_angle = data.cutOffAngle;
  420. }
  421. object = new THREE.SpotLight( l_color, l_intensity, l_distance, l_angle, l_penumbra );
  422. object.visible = l_visible;
  423. parent.add( object );
  424. } else if ( data.nodeType === 'Transform' || data.nodeType === 'Group' ) {
  425. object = new THREE.Object3D();
  426. if ( /DEF/.exec( data.string ) ) {
  427. object.name = /DEF\s+([^\s]+)/.exec( data.string )[ 1 ];
  428. defines[ object.name ] = object;
  429. }
  430. if ( data.translation !== undefined ) {
  431. var t = data.translation;
  432. object.position.set( t.x, t.y, t.z );
  433. }
  434. if ( data.rotation !== undefined ) {
  435. var r = data.rotation;
  436. object.quaternion.setFromAxisAngle( new THREE.Vector3( r.x, r.y, r.z ), r.w );
  437. }
  438. if ( data.scale !== undefined ) {
  439. var s = data.scale;
  440. object.scale.set( s.x, s.y, s.z );
  441. }
  442. parent.add( object );
  443. } else if ( data.nodeType === 'Shape' ) {
  444. object = new THREE.Mesh();
  445. if ( /DEF/.exec( data.string ) ) {
  446. object.name = /DEF\s+([^\s]+)/.exec( data.string )[ 1 ];
  447. defines[ object.name ] = object;
  448. }
  449. parent.add( object );
  450. } else if ( data.nodeType === 'Background' ) {
  451. var segments = 20;
  452. // sky (full sphere):
  453. var radius = 2e4;
  454. var skyGeometry = new THREE.SphereBufferGeometry( radius, segments, segments );
  455. var skyMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide } );
  456. if ( data.skyColor.length > 1 ) {
  457. paintFaces( skyGeometry, radius, data.skyAngle, data.skyColor, true );
  458. skyMaterial.vertexColors = THREE.VertexColors;
  459. } else {
  460. var color = data.skyColor[ 0 ];
  461. skyMaterial.color.setRGB( color.r, color.b, color.g );
  462. }
  463. scene.add( new THREE.Mesh( skyGeometry, skyMaterial ) );
  464. // ground (half sphere):
  465. if ( data.groundColor !== undefined ) {
  466. radius = 1.2e4;
  467. var groundGeometry = new THREE.SphereBufferGeometry( radius, segments, segments, 0, 2 * Math.PI, 0.5 * Math.PI, 1.5 * Math.PI );
  468. var groundMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide, vertexColors: THREE.VertexColors } );
  469. paintFaces( groundGeometry, radius, data.groundAngle, data.groundColor, false );
  470. scene.add( new THREE.Mesh( groundGeometry, groundMaterial ) );
  471. }
  472. } else if ( /geometry/.exec( data.string ) ) {
  473. if ( data.nodeType === 'Box' ) {
  474. var s = data.size;
  475. parent.geometry = new THREE.BoxBufferGeometry( s.x, s.y, s.z );
  476. } else if ( data.nodeType === 'Cylinder' ) {
  477. parent.geometry = new THREE.CylinderBufferGeometry( data.radius, data.radius, data.height );
  478. } else if ( data.nodeType === 'Cone' ) {
  479. parent.geometry = new THREE.CylinderBufferGeometry( data.topRadius, data.bottomRadius, data.height );
  480. } else if ( data.nodeType === 'Sphere' ) {
  481. parent.geometry = new THREE.SphereBufferGeometry( data.radius );
  482. } else if ( data.nodeType === 'IndexedFaceSet' ) {
  483. var geometry = new THREE.BufferGeometry();
  484. var positions = [];
  485. var uvs = [];
  486. var position, uv;
  487. var i, il, j, jl;
  488. for ( i = 0, il = data.children.length; i < il; i ++ ) {
  489. var child = data.children[ i ];
  490. // uvs
  491. if ( child.nodeType === 'TextureCoordinate' ) {
  492. if ( child.points ) {
  493. for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
  494. uv = child.points[ j ];
  495. uvs.push( uv.x, uv.y );
  496. }
  497. }
  498. }
  499. // positions
  500. if ( child.nodeType === 'Coordinate' ) {
  501. if ( child.points ) {
  502. for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
  503. position = child.points[ j ];
  504. positions.push( position.x, position.y, position.z );
  505. }
  506. }
  507. if ( child.string.indexOf( 'DEF' ) > - 1 ) {
  508. var name = /DEF\s+([^\s]+)/.exec( child.string )[ 1 ];
  509. defines[ name ] = positions.slice( 0 );
  510. }
  511. if ( child.string.indexOf( 'USE' ) > - 1 ) {
  512. var defineKey = /USE\s+([^\s]+)/.exec( child.string )[ 1 ];
  513. positions = defines[ defineKey ];
  514. }
  515. }
  516. }
  517. var skip = 0;
  518. // some shapes only have vertices for use in other shapes
  519. if ( data.coordIndex ) {
  520. var newPositions = [];
  521. var newUvs = [];
  522. position = new THREE.Vector3();
  523. uv = new THREE.Vector2();
  524. for ( i = 0, il = data.coordIndex.length; i < il; i ++ ) {
  525. var indexes = data.coordIndex[ i ];
  526. // VRML support multipoint indexed face sets (more then 3 vertices). You must calculate the composing triangles here
  527. skip = 0;
  528. while ( indexes.length >= 3 && skip < ( indexes.length - 2 ) ) {
  529. if ( data.ccw === undefined ) data.ccw = true; // ccw is true by default
  530. var i1 = indexes[ 0 ];
  531. var i2 = indexes[ skip + ( data.ccw ? 1 : 2 ) ];
  532. var i3 = indexes[ skip + ( data.ccw ? 2 : 1 ) ];
  533. // create non indexed geometry, necessary for face normal generation
  534. position.fromArray( positions, i1 * 3 );
  535. uv.fromArray( uvs, i1 * 2 );
  536. newPositions.push( position.x, position.y, position.z );
  537. newUvs.push( uv.x, uv.y );
  538. position.fromArray( positions, i2 * 3 );
  539. uv.fromArray( uvs, i2 * 2 );
  540. newPositions.push( position.x, position.y, position.z );
  541. newUvs.push( uv.x, uv.y );
  542. position.fromArray( positions, i3 * 3 );
  543. uv.fromArray( uvs, i3 * 2 );
  544. newPositions.push( position.x, position.y, position.z );
  545. newUvs.push( uv.x, uv.y );
  546. skip ++;
  547. }
  548. }
  549. positions = newPositions;
  550. uvs = newUvs;
  551. } else {
  552. // do not add dummy mesh to the scene
  553. parent.parent.remove( parent );
  554. }
  555. if ( false === data.solid ) {
  556. parent.material.side = THREE.DoubleSide;
  557. }
  558. // we need to store it on the geometry for use with defines
  559. geometry.solid = data.solid;
  560. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  561. if ( uvs.length > 0 ) {
  562. geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  563. }
  564. geometry.computeVertexNormals();
  565. geometry.computeBoundingSphere();
  566. // see if it's a define
  567. if ( /DEF/.exec( data.string ) ) {
  568. geometry.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
  569. defines[ geometry.name ] = geometry;
  570. }
  571. parent.geometry = geometry;
  572. }
  573. return;
  574. } else if ( /appearance/.exec( data.string ) ) {
  575. for ( var i = 0; i < data.children.length; i ++ ) {
  576. var child = data.children[ i ];
  577. if ( child.nodeType === 'Material' ) {
  578. var material = new THREE.MeshPhongMaterial();
  579. if ( child.diffuseColor !== undefined ) {
  580. var d = child.diffuseColor;
  581. material.color.setRGB( d.r, d.g, d.b );
  582. }
  583. if ( child.emissiveColor !== undefined ) {
  584. var e = child.emissiveColor;
  585. material.emissive.setRGB( e.r, e.g, e.b );
  586. }
  587. if ( child.specularColor !== undefined ) {
  588. var s = child.specularColor;
  589. material.specular.setRGB( s.r, s.g, s.b );
  590. }
  591. if ( child.transparency !== undefined ) {
  592. var t = child.transparency;
  593. // transparency is opposite of opacity
  594. material.opacity = Math.abs( 1 - t );
  595. material.transparent = true;
  596. }
  597. if ( /DEF/.exec( data.string ) ) {
  598. material.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
  599. defines[ material.name ] = material;
  600. }
  601. parent.material = material;
  602. }
  603. if ( child.nodeType === 'ImageTexture' ) {
  604. var textureName = /"([^"]+)"/.exec( child.children[ 0 ] );
  605. if ( textureName ) {
  606. parent.material.name = textureName[ 1 ];
  607. parent.material.map = textureLoader.load( texturePath + textureName[ 1 ] );
  608. }
  609. }
  610. }
  611. return;
  612. }
  613. for ( var i = 0, l = data.children.length; i < l; i ++ ) {
  614. parseNode( data.children[ i ], object );
  615. }
  616. }
  617. parseNode( getTree( lines ), scene );
  618. }
  619. var scene = new THREE.Scene();
  620. var lines = data.split( '\n' );
  621. // some lines do not have breaks
  622. for ( var i = lines.length - 1; i > - 1; i -- ) {
  623. // split lines with {..{ or {..[ - some have both
  624. if ( /{.*[{\[]/.test( lines[ i ] ) ) {
  625. var parts = lines[ i ].split( '{' ).join( '{\n' ).split( '\n' );
  626. parts.unshift( 1 );
  627. parts.unshift( i );
  628. lines.splice.apply( lines, parts );
  629. } else if ( /\].*}/.test( lines[ i ] ) ) {
  630. // split lines with ]..}
  631. var parts = lines[ i ].split( ']' ).join( ']\n' ).split( '\n' );
  632. parts.unshift( 1 );
  633. parts.unshift( i );
  634. lines.splice.apply( lines, parts );
  635. }
  636. if ( /}.*}/.test( lines[ i ] ) ) {
  637. // split lines with }..}
  638. var parts = lines[ i ].split( '}' ).join( '}\n' ).split( '\n' );
  639. parts.unshift( 1 );
  640. parts.unshift( i );
  641. lines.splice.apply( lines, parts );
  642. }
  643. // force the parser to create Coordinate node for empty coords
  644. // coord USE something -> coord USE something Coordinate {}
  645. if ( ( lines[ i ].indexOf( 'coord' ) > - 1 ) && ( lines[ i ].indexOf( '[' ) < 0 ) && ( lines[ i ].indexOf( '{' ) < 0 ) ) {
  646. lines[ i ] += ' Coordinate {}';
  647. }
  648. }
  649. var header = lines.shift();
  650. if ( /V1.0/.exec( header ) ) {
  651. parseV1( lines, scene );
  652. } else if ( /V2.0/.exec( header ) ) {
  653. parseV2( lines, scene );
  654. }
  655. return scene;
  656. }
  657. };