VRMLLoader.js 24 KB

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