VRMLLoader.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.VRMLLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. };
  7. THREE.VRMLLoader.prototype = {
  8. constructor: THREE.VRMLLoader,
  9. // for IndexedFaceSet support
  10. isRecordingPoints: false,
  11. isRecordingFaces: false,
  12. points: [],
  13. indexes: [],
  14. // for Background support
  15. isRecordingAngles: false,
  16. isRecordingColors: false,
  17. angles: [],
  18. colors: [],
  19. recordingFieldname: null,
  20. crossOrigin: 'anonymous',
  21. load: function ( url, onLoad, onProgress, onError ) {
  22. var scope = this;
  23. var loader = new THREE.FileLoader( this.manager );
  24. loader.load( url, function ( text ) {
  25. onLoad( scope.parse( text ) );
  26. }, onProgress, onError );
  27. },
  28. setCrossOrigin: function ( value ) {
  29. this.crossOrigin = value;
  30. return this;
  31. },
  32. parse: function ( data ) {
  33. var texturePath = this.texturePath || '';
  34. var textureLoader = new THREE.TextureLoader( this.manager );
  35. textureLoader.setCrossOrigin( this.crossOrigin );
  36. function parseV2( lines, scene ) {
  37. var defines = {};
  38. var float_pattern = /(\b|\-|\+)([\d\.e]+)/;
  39. var float2_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
  40. var float3_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
  41. /**
  42. * Vertically paints the faces interpolating between the
  43. * specified colors at the specified angels. This is used for the Background
  44. * node, but could be applied to other nodes with multiple faces as well.
  45. *
  46. * When used with the Background node, default is directionIsDown is true if
  47. * interpolating the skyColor down from the Zenith. When interpolationg up from
  48. * the Nadir i.e. interpolating the groundColor, the directionIsDown is false.
  49. *
  50. * The first angle is never specified, it is the Zenith (0 rad). Angles are specified
  51. * in radians. The geometry is thought a sphere, but could be anything. The color interpolation
  52. * is linear along the Y axis in any case.
  53. *
  54. * You must specify one more color than you have angles at the beginning of the colors array.
  55. * This is the color of the Zenith (the top of the shape).
  56. *
  57. * @param geometry
  58. * @param radius
  59. * @param angles
  60. * @param colors
  61. * @param boolean topDown Whether to work top down or bottom up.
  62. */
  63. function paintFaces( geometry, radius, angles, colors, topDown ) {
  64. var direction = ( topDown === true ) ? 1 : - 1;
  65. var coord = [], A = {}, B = {}, applyColor = false;
  66. for ( var k = 0; k < angles.length; k ++ ) {
  67. // push the vector at which the color changes
  68. var vec = {
  69. x: direction * ( Math.cos( angles[ k ] ) * radius ),
  70. y: direction * ( Math.sin( angles[ k ] ) * radius )
  71. };
  72. coord.push( vec );
  73. }
  74. var index = geometry.index;
  75. var positionAttribute = geometry.attributes.position;
  76. var colorAttribute = new THREE.BufferAttribute( new Float32Array( geometry.attributes.position.count * 3 ), 3 );
  77. var position = new THREE.Vector3();
  78. var color = new THREE.Color();
  79. for ( var i = 0; i < index.count; i ++ ) {
  80. var vertexIndex = index.getX( i );
  81. position.fromBufferAttribute( positionAttribute, vertexIndex );
  82. for ( var j = 0; j < colors.length; j ++ ) {
  83. // linear interpolation between aColor and bColor, calculate proportion
  84. // A is previous point (angle)
  85. if ( j === 0 ) {
  86. A.x = 0;
  87. A.y = ( topDown === true ) ? radius : - 1 * radius;
  88. } else {
  89. A.x = coord[ j - 1 ].x;
  90. A.y = coord[ j - 1 ].y;
  91. }
  92. // B is current point (angle)
  93. B = coord[ j ];
  94. if ( B !== undefined ) {
  95. // p has to be between the points A and B which we interpolate
  96. applyColor = ( topDown === true ) ? ( position.y <= A.y && position.y > B.y ) : ( position.y >= A.y && position.y < B.y );
  97. if ( applyColor === true ) {
  98. var aColor = colors[ j ];
  99. var bColor = colors[ j + 1 ];
  100. // below is simple linear interpolation
  101. var t = Math.abs( position.y - A.y ) / ( A.y - B.y );
  102. // 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
  103. color.copy( aColor ).lerp( bColor, t );
  104. colorAttribute.setXYZ( vertexIndex, color.r, color.g, color.b );
  105. } else {
  106. var colorIndex = ( topDown === true ) ? colors.length - 1 : 0;
  107. var c = colors[ colorIndex ];
  108. colorAttribute.setXYZ( vertexIndex, c.r, c.g, c.b );
  109. }
  110. }
  111. }
  112. }
  113. geometry.addAttribute( 'color', colorAttribute );
  114. }
  115. var index = [];
  116. function parseProperty( node, line ) {
  117. var parts = [], part, property = {}, fieldName;
  118. /**
  119. * Expression for matching relevant information, such as a name or value, but not the separators
  120. * @type {RegExp}
  121. */
  122. var regex = /[^\s,\[\]]+/g;
  123. var point;
  124. while ( null !== ( part = regex.exec( line ) ) ) {
  125. parts.push( part[ 0 ] );
  126. }
  127. fieldName = parts[ 0 ];
  128. // trigger several recorders
  129. switch ( fieldName ) {
  130. case 'skyAngle':
  131. case 'groundAngle':
  132. this.recordingFieldname = fieldName;
  133. this.isRecordingAngles = true;
  134. this.angles = [];
  135. break;
  136. case 'skyColor':
  137. case 'groundColor':
  138. this.recordingFieldname = fieldName;
  139. this.isRecordingColors = true;
  140. this.colors = [];
  141. break;
  142. case 'point':
  143. this.recordingFieldname = fieldName;
  144. this.isRecordingPoints = true;
  145. this.points = [];
  146. break;
  147. case 'coordIndex':
  148. case 'texCoordIndex':
  149. this.recordingFieldname = fieldName;
  150. this.isRecordingFaces = true;
  151. this.indexes = [];
  152. break;
  153. }
  154. if ( this.isRecordingFaces ) {
  155. // the parts hold the indexes as strings
  156. if ( parts.length > 0 ) {
  157. for ( var ind = 0; ind < parts.length; ind ++ ) {
  158. // the part should either be positive integer or -1
  159. if ( ! /(-?\d+)/.test( parts[ ind ] ) ) {
  160. continue;
  161. }
  162. // end of current face
  163. if ( parts[ ind ] === '-1' ) {
  164. if ( index.length > 0 ) {
  165. this.indexes.push( index );
  166. }
  167. // start new one
  168. index = [];
  169. } else {
  170. index.push( parseInt( parts[ ind ] ) );
  171. }
  172. }
  173. }
  174. // end
  175. if ( /]/.exec( line ) ) {
  176. if ( index.length > 0 ) {
  177. this.indexes.push( index );
  178. }
  179. // start new one
  180. index = [];
  181. this.isRecordingFaces = false;
  182. node[ this.recordingFieldname ] = this.indexes;
  183. }
  184. } else if ( this.isRecordingPoints ) {
  185. if ( node.nodeType == 'Coordinate' ) {
  186. while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
  187. point = {
  188. x: parseFloat( parts[ 1 ] ),
  189. y: parseFloat( parts[ 2 ] ),
  190. z: parseFloat( parts[ 3 ] )
  191. };
  192. this.points.push( point );
  193. }
  194. }
  195. if ( node.nodeType == 'TextureCoordinate' ) {
  196. while ( null !== ( parts = float2_pattern.exec( line ) ) ) {
  197. point = {
  198. x: parseFloat( parts[ 1 ] ),
  199. y: parseFloat( parts[ 2 ] )
  200. };
  201. this.points.push( point );
  202. }
  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 !== ( /^\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 ( data.nodeType === 'AmbientLight' ) {
  396. object = new THREE.AmbientLight( l_color, l_intensity );
  397. object.visible = l_visible;
  398. parent.add( object );
  399. } else if ( data.nodeType === 'PointLight' ) {
  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 ( data.nodeType === 'SpotLight' ) {
  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 ( data.nodeType === 'Transform' || data.nodeType === 'Group' ) {
  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 ( data.translation !== undefined ) {
  429. var t = data.translation;
  430. object.position.set( t.x, t.y, t.z );
  431. }
  432. if ( data.rotation !== undefined ) {
  433. var r = data.rotation;
  434. object.quaternion.setFromAxisAngle( new THREE.Vector3( r.x, r.y, r.z ), r.w );
  435. }
  436. if ( data.scale !== undefined ) {
  437. var s = data.scale;
  438. object.scale.set( s.x, s.y, s.z );
  439. }
  440. parent.add( object );
  441. } else if ( data.nodeType === 'Shape' ) {
  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 ( data.nodeType === 'Background' ) {
  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 ( data.nodeType === 'Box' ) {
  472. var s = data.size;
  473. parent.geometry = new THREE.BoxBufferGeometry( s.x, s.y, s.z );
  474. } else if ( data.nodeType === 'Cylinder' ) {
  475. parent.geometry = new THREE.CylinderBufferGeometry( data.radius, data.radius, data.height );
  476. } else if ( data.nodeType === 'Cone' ) {
  477. parent.geometry = new THREE.CylinderBufferGeometry( data.topRadius, data.bottomRadius, data.height );
  478. } else if ( data.nodeType === 'Sphere' ) {
  479. parent.geometry = new THREE.SphereBufferGeometry( data.radius );
  480. } else if ( data.nodeType === 'IndexedFaceSet' ) {
  481. var geometry = new THREE.BufferGeometry();
  482. var positions = [];
  483. var uvs = [];
  484. var position, uv;
  485. var i, il, j, jl;
  486. for ( i = 0, il = data.children.length; i < il; i ++ ) {
  487. var child = data.children[ i ];
  488. // uvs
  489. if ( child.nodeType === 'TextureCoordinate' ) {
  490. if ( child.points ) {
  491. for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
  492. uv = child.points[ j ];
  493. uvs.push( uv.x, uv.y );
  494. }
  495. }
  496. }
  497. // positions
  498. if ( child.nodeType === 'Coordinate' ) {
  499. if ( child.points ) {
  500. for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
  501. position = child.points[ j ];
  502. positions.push( position.x, position.y, position.z );
  503. }
  504. }
  505. if ( child.string.indexOf( 'DEF' ) > - 1 ) {
  506. var name = /DEF\s+([^\s]+)/.exec( child.string )[ 1 ];
  507. defines[ name ] = positions.slice( 0 );
  508. }
  509. if ( child.string.indexOf( 'USE' ) > - 1 ) {
  510. var defineKey = /USE\s+([^\s]+)/.exec( child.string )[ 1 ];
  511. positions = defines[ defineKey ];
  512. }
  513. }
  514. }
  515. var skip = 0;
  516. // some shapes only have vertices for use in other shapes
  517. if ( data.coordIndex ) {
  518. var newPositions = [];
  519. var newUvs = [];
  520. position = new THREE.Vector3();
  521. uv = new THREE.Vector2();
  522. for ( i = 0, il = data.coordIndex.length; i < il; i ++ ) {
  523. var indexes = data.coordIndex[ i ];
  524. // VRML support multipoint indexed face sets (more then 3 vertices). You must calculate the composing triangles here
  525. skip = 0;
  526. while ( indexes.length >= 3 && skip < ( indexes.length - 2 ) ) {
  527. if ( data.ccw === undefined ) data.ccw = true; // ccw is true by default
  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 ( child.nodeType === 'Material' ) {
  576. var material = new THREE.MeshPhongMaterial();
  577. if ( child.diffuseColor !== undefined ) {
  578. var d = child.diffuseColor;
  579. material.color.setRGB( d.r, d.g, d.b );
  580. }
  581. if ( child.emissiveColor !== undefined ) {
  582. var e = child.emissiveColor;
  583. material.emissive.setRGB( e.r, e.g, e.b );
  584. }
  585. if ( child.specularColor !== undefined ) {
  586. var s = child.specularColor;
  587. material.specular.setRGB( s.r, s.g, s.b );
  588. }
  589. if ( child.transparency !== undefined ) {
  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 ( child.nodeType === 'ImageTexture' ) {
  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. var line = lines[ i ];
  622. // split lines with {..{ or {..[ - some have both
  623. if ( /{.*[{\[]/.test( line ) ) {
  624. var parts = line.split( '{' ).join( '{\n' ).split( '\n' );
  625. parts.unshift( 1 );
  626. parts.unshift( i );
  627. lines.splice.apply( lines, parts );
  628. } else if ( /\].*}/.test( line ) ) {
  629. // split lines with ]..}
  630. var parts = line.split( ']' ).join( ']\n' ).split( '\n' );
  631. parts.unshift( 1 );
  632. parts.unshift( i );
  633. lines.splice.apply( lines, parts );
  634. }
  635. if ( /}.*}/.test( line ) ) {
  636. // split lines with }..}
  637. var parts = line.split( '}' ).join( '}\n' ).split( '\n' );
  638. parts.unshift( 1 );
  639. parts.unshift( i );
  640. lines.splice.apply( lines, parts );
  641. }
  642. if ( /^\b[^\s]+\b$/.test( line.trim() ) ) {
  643. // prevent lines with single words like "coord" or "geometry", see #12209
  644. lines[ i + 1 ] = line + ' ' + lines[ i + 1 ].trim();
  645. lines.splice( i, 1 );
  646. } else if ( ( line.indexOf( 'coord' ) > - 1 ) && ( line.indexOf( '[' ) < 0 ) && ( line.indexOf( '{' ) < 0 ) ) {
  647. // force the parser to create Coordinate node for empty coords
  648. // coord USE something -> coord USE something Coordinate {}
  649. lines[ i ] += ' Coordinate {}';
  650. }
  651. }
  652. var header = lines.shift();
  653. if ( /V1.0/.exec( header ) ) {
  654. console.warn( 'THREE.VRMLLoader: V1.0 not supported yet.' );
  655. } else if ( /V2.0/.exec( header ) ) {
  656. parseV2( lines, scene );
  657. }
  658. return scene;
  659. }
  660. };