2
0

VRMLLoader.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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 ( 'AmbientLight' === data.nodeType ) {
  398. object = new THREE.AmbientLight( l_color, l_intensity );
  399. object.visible = l_visible;
  400. parent.add( object );
  401. } else if ( 'PointLight' === data.nodeType ) {
  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 ( 'SpotLight' === data.nodeType ) {
  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 ( 'Transform' === data.nodeType || 'Group' === data.nodeType ) {
  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 ( undefined !== data[ 'translation' ] ) {
  431. var t = data.translation;
  432. object.position.set( t.x, t.y, t.z );
  433. }
  434. if ( undefined !== data.rotation ) {
  435. var r = data.rotation;
  436. object.quaternion.setFromAxisAngle( new THREE.Vector3( r.x, r.y, r.z ), r.w );
  437. }
  438. if ( undefined !== data.scale ) {
  439. var s = data.scale;
  440. object.scale.set( s.x, s.y, s.z );
  441. }
  442. parent.add( object );
  443. } else if ( 'Shape' === data.nodeType ) {
  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 ( 'Background' === data.nodeType ) {
  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 ( 'Box' === data.nodeType ) {
  474. var s = data.size;
  475. parent.geometry = new THREE.BoxBufferGeometry( s.x, s.y, s.z );
  476. } else if ( 'Cylinder' === data.nodeType ) {
  477. parent.geometry = new THREE.CylinderBufferGeometry( data.radius, data.radius, data.height );
  478. } else if ( 'Cone' === data.nodeType ) {
  479. parent.geometry = new THREE.CylinderBufferGeometry( data.topRadius, data.bottomRadius, data.height );
  480. } else if ( 'Sphere' === data.nodeType ) {
  481. parent.geometry = new THREE.SphereBufferGeometry( data.radius );
  482. } else if ( 'IndexedFaceSet' === data.nodeType ) {
  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 ( 'TextureCoordinate' === child.nodeType ) {
  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 ( 'Coordinate' === child.nodeType ) {
  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. var i1 = indexes[ 0 ];
  530. var i2 = indexes[ skip + ( data.ccw ? 1 : 2 ) ];
  531. var i3 = indexes[ skip + ( data.ccw ? 2 : 1 ) ];
  532. // create non indexed geometry, necessary for face normal generation
  533. position.fromArray( positions, i1 * 3 );
  534. uv.fromArray( uvs, i1 * 2 );
  535. newPositions.push( position.x, position.y, position.z );
  536. newUvs.push( uv.x, uv.y );
  537. position.fromArray( positions, i2 * 3 );
  538. uv.fromArray( uvs, i2 * 2 );
  539. newPositions.push( position.x, position.y, position.z );
  540. newUvs.push( uv.x, uv.y );
  541. position.fromArray( positions, i3 * 3 );
  542. uv.fromArray( uvs, i3 * 2 );
  543. newPositions.push( position.x, position.y, position.z );
  544. newUvs.push( uv.x, uv.y );
  545. skip ++;
  546. }
  547. }
  548. positions = newPositions;
  549. uvs = newUvs;
  550. } else {
  551. // do not add dummy mesh to the scene
  552. parent.parent.remove( parent );
  553. }
  554. if ( false === data.solid ) {
  555. parent.material.side = THREE.DoubleSide;
  556. }
  557. // we need to store it on the geometry for use with defines
  558. geometry.solid = data.solid;
  559. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  560. if ( uvs.length > 0 ) {
  561. geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  562. }
  563. geometry.computeVertexNormals();
  564. geometry.computeBoundingSphere();
  565. // see if it's a define
  566. if ( /DEF/.exec( data.string ) ) {
  567. geometry.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
  568. defines[ geometry.name ] = geometry;
  569. }
  570. parent.geometry = geometry;
  571. }
  572. return;
  573. } else if ( /appearance/.exec( data.string ) ) {
  574. for ( var i = 0; i < data.children.length; i ++ ) {
  575. var child = data.children[ i ];
  576. if ( 'Material' === child.nodeType ) {
  577. var material = new THREE.MeshPhongMaterial();
  578. if ( undefined !== child.diffuseColor ) {
  579. var d = child.diffuseColor;
  580. material.color.setRGB( d.r, d.g, d.b );
  581. }
  582. if ( undefined !== child.emissiveColor ) {
  583. var e = child.emissiveColor;
  584. material.emissive.setRGB( e.r, e.g, e.b );
  585. }
  586. if ( undefined !== child.specularColor ) {
  587. var s = child.specularColor;
  588. material.specular.setRGB( s.r, s.g, s.b );
  589. }
  590. if ( undefined !== child.transparency ) {
  591. var t = child.transparency;
  592. // transparency is opposite of opacity
  593. material.opacity = Math.abs( 1 - t );
  594. material.transparent = true;
  595. }
  596. if ( /DEF/.exec( data.string ) ) {
  597. material.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
  598. defines[ material.name ] = material;
  599. }
  600. parent.material = material;
  601. }
  602. if ( 'ImageTexture' === child.nodeType ) {
  603. var textureName = /"([^"]+)"/.exec( child.children[ 0 ] );
  604. if ( textureName ) {
  605. parent.material.name = textureName[ 1 ];
  606. parent.material.map = textureLoader.load( texturePath + textureName[ 1 ] );
  607. }
  608. }
  609. }
  610. return;
  611. }
  612. for ( var i = 0, l = data.children.length; i < l; i ++ ) {
  613. parseNode( data.children[ i ], object );
  614. }
  615. }
  616. parseNode( getTree( lines ), scene );
  617. }
  618. var scene = new THREE.Scene();
  619. var lines = data.split( '\n' );
  620. // some lines do not have breaks
  621. for ( var i = lines.length - 1; i > - 1; i -- ) {
  622. // split lines with {..{ or {..[ - some have both
  623. if ( /{.*[{\[]/.test( lines[ i ] ) ) {
  624. var parts = lines[ i ].split( '{' ).join( '{\n' ).split( '\n' );
  625. parts.unshift( 1 );
  626. parts.unshift( i );
  627. lines.splice.apply( lines, parts );
  628. } else if ( /\].*}/.test( lines[ i ] ) ) {
  629. // split lines with ]..}
  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. if ( /}.*}/.test( lines[ i ] ) ) {
  636. // split lines with }..}
  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. };