VRMLLoader.js 24 KB

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