VRMLLoader.js 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458
  1. console.warn( "THREE.VRMLLoader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /* global chevrotain */
  3. THREE.VRMLLoader = ( function () {
  4. // dependency check
  5. if ( typeof chevrotain === 'undefined' ) {
  6. throw Error( 'THREE.VRMLLoader: External library chevrotain.min.js required.' );
  7. }
  8. // class definitions
  9. function VRMLLoader( manager ) {
  10. THREE.Loader.call( this, manager );
  11. }
  12. VRMLLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  13. constructor: VRMLLoader,
  14. load: function ( url, onLoad, onProgress, onError ) {
  15. var scope = this;
  16. var path = ( scope.path === '' ) ? THREE.LoaderUtils.extractUrlBase( url ) : scope.path;
  17. var loader = new THREE.FileLoader( scope.manager );
  18. loader.setPath( scope.path );
  19. loader.setRequestHeader( scope.requestHeader );
  20. loader.setWithCredentials( scope.withCredentials );
  21. loader.load( url, function ( text ) {
  22. try {
  23. onLoad( scope.parse( text, path ) );
  24. } catch ( e ) {
  25. if ( onError ) {
  26. onError( e );
  27. } else {
  28. console.error( e );
  29. }
  30. scope.manager.itemError( url );
  31. }
  32. }, onProgress, onError );
  33. },
  34. parse: function ( data, path ) {
  35. var nodeMap = {};
  36. function generateVRMLTree( data ) {
  37. // create lexer, parser and visitor
  38. var tokenData = createTokens();
  39. var lexer = new VRMLLexer( tokenData.tokens );
  40. var parser = new VRMLParser( tokenData.tokenVocabulary );
  41. var visitor = createVisitor( parser.getBaseCstVisitorConstructor() );
  42. // lexing
  43. var lexingResult = lexer.lex( data );
  44. parser.input = lexingResult.tokens;
  45. // parsing
  46. var cstOutput = parser.vrml();
  47. if ( parser.errors.length > 0 ) {
  48. console.error( parser.errors );
  49. throw Error( 'THREE.VRMLLoader: Parsing errors detected.' );
  50. }
  51. // actions
  52. var ast = visitor.visit( cstOutput );
  53. return ast;
  54. }
  55. function createTokens() {
  56. var createToken = chevrotain.createToken;
  57. // from http://gun.teipir.gr/VRML-amgem/spec/part1/concepts.html#SyntaxBasics
  58. var RouteIdentifier = createToken( { name: 'RouteIdentifier', pattern: /[^\x30-\x39\0-\x20\x22\x27\x23\x2b\x2c\x2d\x2e\x5b\x5d\x5c\x7b\x7d][^\0-\x20\x22\x27\x23\x2b\x2c\x2d\x2e\x5b\x5d\x5c\x7b\x7d]*[\.][^\x30-\x39\0-\x20\x22\x27\x23\x2b\x2c\x2d\x2e\x5b\x5d\x5c\x7b\x7d][^\0-\x20\x22\x27\x23\x2b\x2c\x2d\x2e\x5b\x5d\x5c\x7b\x7d]*/ } );
  59. var Identifier = createToken( { name: 'Identifier', pattern: /[^\x30-\x39\0-\x20\x22\x27\x23\x2b\x2c\x2d\x2e\x5b\x5d\x5c\x7b\x7d][^\0-\x20\x22\x27\x23\x2b\x2c\x2d\x2e\x5b\x5d\x5c\x7b\x7d]*/, longer_alt: RouteIdentifier } );
  60. // from http://gun.teipir.gr/VRML-amgem/spec/part1/nodesRef.html
  61. var nodeTypes = [
  62. 'Anchor', 'Billboard', 'Collision', 'Group', 'Transform', // grouping nodes
  63. 'Inline', 'LOD', 'Switch', // special groups
  64. 'AudioClip', 'DirectionalLight', 'PointLight', 'Script', 'Shape', 'Sound', 'SpotLight', 'WorldInfo', // common nodes
  65. 'CylinderSensor', 'PlaneSensor', 'ProximitySensor', 'SphereSensor', 'TimeSensor', 'TouchSensor', 'VisibilitySensor', // sensors
  66. 'Box', 'Cone', 'Cylinder', 'ElevationGrid', 'Extrusion', 'IndexedFaceSet', 'IndexedLineSet', 'PointSet', 'Sphere', // geometries
  67. 'Color', 'Coordinate', 'Normal', 'TextureCoordinate', // geometric properties
  68. 'Appearance', 'FontStyle', 'ImageTexture', 'Material', 'MovieTexture', 'PixelTexture', 'TextureTransform', // appearance
  69. 'ColorInterpolator', 'CoordinateInterpolator', 'NormalInterpolator', 'OrientationInterpolator', 'PositionInterpolator', 'ScalarInterpolator', // interpolators
  70. 'Background', 'Fog', 'NavigationInfo', 'Viewpoint', // bindable nodes
  71. 'Text' // Text must be placed at the end of the regex so there are no matches for TextureTransform and TextureCoordinate
  72. ];
  73. //
  74. var Version = createToken( {
  75. name: 'Version',
  76. pattern: /#VRML.*/,
  77. longer_alt: Identifier
  78. } );
  79. var NodeName = createToken( {
  80. name: 'NodeName',
  81. pattern: new RegExp( nodeTypes.join( '|' ) ),
  82. longer_alt: Identifier
  83. } );
  84. var DEF = createToken( {
  85. name: 'DEF',
  86. pattern: /DEF/,
  87. longer_alt: Identifier
  88. } );
  89. var USE = createToken( {
  90. name: 'USE',
  91. pattern: /USE/,
  92. longer_alt: Identifier
  93. } );
  94. var ROUTE = createToken( {
  95. name: 'ROUTE',
  96. pattern: /ROUTE/,
  97. longer_alt: Identifier
  98. } );
  99. var TO = createToken( {
  100. name: 'TO',
  101. pattern: /TO/,
  102. longer_alt: Identifier
  103. } );
  104. //
  105. var StringLiteral = createToken( { name: "StringLiteral", pattern: /"(:?[^\\"\n\r]+|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/ } );
  106. var HexLiteral = createToken( { name: 'HexLiteral', pattern: /0[xX][0-9a-fA-F]+/ } );
  107. var NumberLiteral = createToken( { name: 'NumberLiteral', pattern: /[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/ } );
  108. var TrueLiteral = createToken( { name: 'TrueLiteral', pattern: /TRUE/ } );
  109. var FalseLiteral = createToken( { name: 'FalseLiteral', pattern: /FALSE/ } );
  110. var NullLiteral = createToken( { name: 'NullLiteral', pattern: /NULL/ } );
  111. var LSquare = createToken( { name: 'LSquare', pattern: /\[/ } );
  112. var RSquare = createToken( { name: 'RSquare', pattern: /]/ } );
  113. var LCurly = createToken( { name: 'LCurly', pattern: /{/ } );
  114. var RCurly = createToken( { name: 'RCurly', pattern: /}/ } );
  115. var Comment = createToken( {
  116. name: 'Comment',
  117. pattern: /#.*/,
  118. group: chevrotain.Lexer.SKIPPED
  119. } );
  120. // commas, blanks, tabs, newlines and carriage returns are whitespace characters wherever they appear outside of string fields
  121. var WhiteSpace = createToken( {
  122. name: 'WhiteSpace',
  123. pattern: /[ ,\s]/,
  124. group: chevrotain.Lexer.SKIPPED
  125. } );
  126. var tokens = [
  127. WhiteSpace,
  128. // keywords appear before the Identifier
  129. NodeName,
  130. DEF,
  131. USE,
  132. ROUTE,
  133. TO,
  134. TrueLiteral,
  135. FalseLiteral,
  136. NullLiteral,
  137. // the Identifier must appear after the keywords because all keywords are valid identifiers
  138. Version,
  139. Identifier,
  140. RouteIdentifier,
  141. StringLiteral,
  142. HexLiteral,
  143. NumberLiteral,
  144. LSquare,
  145. RSquare,
  146. LCurly,
  147. RCurly,
  148. Comment
  149. ];
  150. var tokenVocabulary = {};
  151. for ( var i = 0, l = tokens.length; i < l; i ++ ) {
  152. var token = tokens[ i ];
  153. tokenVocabulary[ token.name ] = token;
  154. }
  155. return { tokens: tokens, tokenVocabulary: tokenVocabulary };
  156. }
  157. function createVisitor( BaseVRMLVisitor ) {
  158. // the visitor is created dynmaically based on the given base class
  159. function VRMLToASTVisitor() {
  160. BaseVRMLVisitor.call( this );
  161. this.validateVisitor();
  162. }
  163. VRMLToASTVisitor.prototype = Object.assign( Object.create( BaseVRMLVisitor.prototype ), {
  164. constructor: VRMLToASTVisitor,
  165. vrml: function ( ctx ) {
  166. var data = {
  167. version: this.visit( ctx.version ),
  168. nodes: [],
  169. routes: []
  170. };
  171. for ( var i = 0, l = ctx.node.length; i < l; i ++ ) {
  172. var node = ctx.node[ i ];
  173. data.nodes.push( this.visit( node ) );
  174. }
  175. if ( ctx.route ) {
  176. for ( var i = 0, l = ctx.route.length; i < l; i ++ ) {
  177. var route = ctx.route[ i ];
  178. data.routes.push( this.visit( route ) );
  179. }
  180. }
  181. return data;
  182. },
  183. version: function ( ctx ) {
  184. return ctx.Version[ 0 ].image;
  185. },
  186. node: function ( ctx ) {
  187. var data = {
  188. name: ctx.NodeName[ 0 ].image,
  189. fields: []
  190. };
  191. if ( ctx.field ) {
  192. for ( var i = 0, l = ctx.field.length; i < l; i ++ ) {
  193. var field = ctx.field[ i ];
  194. data.fields.push( this.visit( field ) );
  195. }
  196. }
  197. // DEF
  198. if ( ctx.def ) {
  199. data.DEF = this.visit( ctx.def[ 0 ] );
  200. }
  201. return data;
  202. },
  203. field: function ( ctx ) {
  204. var data = {
  205. name: ctx.Identifier[ 0 ].image,
  206. type: null,
  207. values: null
  208. };
  209. var result;
  210. // SFValue
  211. if ( ctx.singleFieldValue ) {
  212. result = this.visit( ctx.singleFieldValue[ 0 ] );
  213. }
  214. // MFValue
  215. if ( ctx.multiFieldValue ) {
  216. result = this.visit( ctx.multiFieldValue[ 0 ] );
  217. }
  218. data.type = result.type;
  219. data.values = result.values;
  220. return data;
  221. },
  222. def: function ( ctx ) {
  223. return ( ctx.Identifier || ctx.NodeName )[ 0 ].image;
  224. },
  225. use: function ( ctx ) {
  226. return { USE: ( ctx.Identifier || ctx.NodeName )[ 0 ].image };
  227. },
  228. singleFieldValue: function ( ctx ) {
  229. return processField( this, ctx );
  230. },
  231. multiFieldValue: function ( ctx ) {
  232. return processField( this, ctx );
  233. },
  234. route: function ( ctx ) {
  235. var data = {
  236. FROM: ctx.RouteIdentifier[ 0 ].image,
  237. TO: ctx.RouteIdentifier[ 1 ].image
  238. };
  239. return data;
  240. }
  241. } );
  242. function processField( scope, ctx ) {
  243. var field = {
  244. type: null,
  245. values: []
  246. };
  247. if ( ctx.node ) {
  248. field.type = 'node';
  249. for ( var i = 0, l = ctx.node.length; i < l; i ++ ) {
  250. var node = ctx.node[ i ];
  251. field.values.push( scope.visit( node ) );
  252. }
  253. }
  254. if ( ctx.use ) {
  255. field.type = 'use';
  256. for ( var i = 0, l = ctx.use.length; i < l; i ++ ) {
  257. var use = ctx.use[ i ];
  258. field.values.push( scope.visit( use ) );
  259. }
  260. }
  261. if ( ctx.StringLiteral ) {
  262. field.type = 'string';
  263. for ( var i = 0, l = ctx.StringLiteral.length; i < l; i ++ ) {
  264. var stringLiteral = ctx.StringLiteral[ i ];
  265. field.values.push( stringLiteral.image.replace( /'|"/g, '' ) );
  266. }
  267. }
  268. if ( ctx.NumberLiteral ) {
  269. field.type = 'number';
  270. for ( var i = 0, l = ctx.NumberLiteral.length; i < l; i ++ ) {
  271. var numberLiteral = ctx.NumberLiteral[ i ];
  272. field.values.push( parseFloat( numberLiteral.image ) );
  273. }
  274. }
  275. if ( ctx.HexLiteral ) {
  276. field.type = 'hex';
  277. for ( var i = 0, l = ctx.HexLiteral.length; i < l; i ++ ) {
  278. var hexLiteral = ctx.HexLiteral[ i ];
  279. field.values.push( hexLiteral.image );
  280. }
  281. }
  282. if ( ctx.TrueLiteral ) {
  283. field.type = 'boolean';
  284. for ( var i = 0, l = ctx.TrueLiteral.length; i < l; i ++ ) {
  285. var trueLiteral = ctx.TrueLiteral[ i ];
  286. if ( trueLiteral.image === 'TRUE' ) field.values.push( true );
  287. }
  288. }
  289. if ( ctx.FalseLiteral ) {
  290. field.type = 'boolean';
  291. for ( var i = 0, l = ctx.FalseLiteral.length; i < l; i ++ ) {
  292. var falseLiteral = ctx.FalseLiteral[ i ];
  293. if ( falseLiteral.image === 'FALSE' ) field.values.push( false );
  294. }
  295. }
  296. if ( ctx.NullLiteral ) {
  297. field.type = 'null';
  298. ctx.NullLiteral.forEach( function () {
  299. field.values.push( null );
  300. } );
  301. }
  302. return field;
  303. }
  304. return new VRMLToASTVisitor();
  305. }
  306. function parseTree( tree ) {
  307. // console.log( JSON.stringify( tree, null, 2 ) );
  308. var nodes = tree.nodes;
  309. var scene = new THREE.Scene();
  310. // first iteration: build nodemap based on DEF statements
  311. for ( var i = 0, l = nodes.length; i < l; i ++ ) {
  312. var node = nodes[ i ];
  313. buildNodeMap( node );
  314. }
  315. // second iteration: build nodes
  316. for ( var i = 0, l = nodes.length; i < l; i ++ ) {
  317. var node = nodes[ i ];
  318. var object = getNode( node );
  319. if ( object instanceof THREE.Object3D ) scene.add( object );
  320. if ( node.name === 'WorldInfo' ) scene.userData.worldInfo = object;
  321. }
  322. return scene;
  323. }
  324. function buildNodeMap( node ) {
  325. if ( node.DEF ) {
  326. nodeMap[ node.DEF ] = node;
  327. }
  328. var fields = node.fields;
  329. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  330. var field = fields[ i ];
  331. if ( field.type === 'node' ) {
  332. var fieldValues = field.values;
  333. for ( var j = 0, jl = fieldValues.length; j < jl; j ++ ) {
  334. buildNodeMap( fieldValues[ j ] );
  335. }
  336. }
  337. }
  338. }
  339. function getNode( node ) {
  340. // handle case where a node refers to a different one
  341. if ( node.USE ) {
  342. return resolveUSE( node.USE );
  343. }
  344. if ( node.build !== undefined ) return node.build;
  345. node.build = buildNode( node );
  346. return node.build;
  347. }
  348. // node builder
  349. function buildNode( node ) {
  350. var nodeName = node.name;
  351. var build;
  352. switch ( nodeName ) {
  353. case 'Group':
  354. case 'Transform':
  355. case 'Collision':
  356. build = buildGroupingNode( node );
  357. break;
  358. case 'Background':
  359. build = buildBackgroundNode( node );
  360. break;
  361. case 'Shape':
  362. build = buildShapeNode( node );
  363. break;
  364. case 'Appearance':
  365. build = buildAppearanceNode( node );
  366. break;
  367. case 'Material':
  368. build = buildMaterialNode( node );
  369. break;
  370. case 'ImageTexture':
  371. build = buildImageTextureNode( node );
  372. break;
  373. case 'PixelTexture':
  374. build = buildPixelTextureNode( node );
  375. break;
  376. case 'TextureTransform':
  377. build = buildTextureTransformNode( node );
  378. break;
  379. case 'IndexedFaceSet':
  380. build = buildIndexedFaceSetNode( node );
  381. break;
  382. case 'IndexedLineSet':
  383. build = buildIndexedLineSetNode( node );
  384. break;
  385. case 'PointSet':
  386. build = buildPointSetNode( node );
  387. break;
  388. case 'Box':
  389. build = buildBoxNode( node );
  390. break;
  391. case 'Cone':
  392. build = buildConeNode( node );
  393. break;
  394. case 'Cylinder':
  395. build = buildCylinderNode( node );
  396. break;
  397. case 'Sphere':
  398. build = buildSphereNode( node );
  399. break;
  400. case 'ElevationGrid':
  401. build = buildElevationGridNode( node );
  402. break;
  403. case 'Extrusion':
  404. build = buildExtrusionNode( node );
  405. break;
  406. case 'Color':
  407. case 'Coordinate':
  408. case 'Normal':
  409. case 'TextureCoordinate':
  410. build = buildGeometricNode( node );
  411. break;
  412. case 'WorldInfo':
  413. build = buildWorldInfoNode( node );
  414. break;
  415. case 'Anchor':
  416. case 'Billboard':
  417. case 'Inline':
  418. case 'LOD':
  419. case 'Switch':
  420. case 'AudioClip':
  421. case 'DirectionalLight':
  422. case 'PointLight':
  423. case 'Script':
  424. case 'Sound':
  425. case 'SpotLight':
  426. case 'CylinderSensor':
  427. case 'PlaneSensor':
  428. case 'ProximitySensor':
  429. case 'SphereSensor':
  430. case 'TimeSensor':
  431. case 'TouchSensor':
  432. case 'VisibilitySensor':
  433. case 'Text':
  434. case 'FontStyle':
  435. case 'MovieTexture':
  436. case 'ColorInterpolator':
  437. case 'CoordinateInterpolator':
  438. case 'NormalInterpolator':
  439. case 'OrientationInterpolator':
  440. case 'PositionInterpolator':
  441. case 'ScalarInterpolator':
  442. case 'Fog':
  443. case 'NavigationInfo':
  444. case 'Viewpoint':
  445. // node not supported yet
  446. break;
  447. default:
  448. console.warn( 'THREE.VRMLLoader: Unknown node:', nodeName );
  449. break;
  450. }
  451. return build;
  452. }
  453. function buildGroupingNode( node ) {
  454. var object = new THREE.Group();
  455. //
  456. var fields = node.fields;
  457. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  458. var field = fields[ i ];
  459. var fieldName = field.name;
  460. var fieldValues = field.values;
  461. switch ( fieldName ) {
  462. case 'bboxCenter':
  463. // field not supported
  464. break;
  465. case 'bboxSize':
  466. // field not supported
  467. break;
  468. case 'center':
  469. // field not supported
  470. break;
  471. case 'children':
  472. parseFieldChildren( fieldValues, object );
  473. break;
  474. case 'collide':
  475. // field not supported
  476. break;
  477. case 'rotation':
  478. var axis = new THREE.Vector3( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  479. var angle = fieldValues[ 3 ];
  480. object.quaternion.setFromAxisAngle( axis, angle );
  481. break;
  482. case 'scale':
  483. object.scale.set( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  484. break;
  485. case 'scaleOrientation':
  486. // field not supported
  487. break;
  488. case 'translation':
  489. object.position.set( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  490. break;
  491. case 'proxy':
  492. // field not supported
  493. break;
  494. default:
  495. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  496. break;
  497. }
  498. }
  499. return object;
  500. }
  501. function buildBackgroundNode( node ) {
  502. var group = new THREE.Group();
  503. var groundAngle, groundColor;
  504. var skyAngle, skyColor;
  505. var fields = node.fields;
  506. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  507. var field = fields[ i ];
  508. var fieldName = field.name;
  509. var fieldValues = field.values;
  510. switch ( fieldName ) {
  511. case 'groundAngle':
  512. groundAngle = fieldValues;
  513. break;
  514. case 'groundColor':
  515. groundColor = fieldValues;
  516. break;
  517. case 'backUrl':
  518. // field not supported
  519. break;
  520. case 'bottomUrl':
  521. // field not supported
  522. break;
  523. case 'frontUrl':
  524. // field not supported
  525. break;
  526. case 'leftUrl':
  527. // field not supported
  528. break;
  529. case 'rightUrl':
  530. // field not supported
  531. break;
  532. case 'topUrl':
  533. // field not supported
  534. break;
  535. case 'skyAngle':
  536. skyAngle = fieldValues;
  537. break;
  538. case 'skyColor':
  539. skyColor = fieldValues;
  540. break;
  541. default:
  542. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  543. break;
  544. }
  545. }
  546. var radius = 10000;
  547. // sky
  548. if ( skyColor ) {
  549. var skyGeometry = new THREE.SphereBufferGeometry( radius, 32, 16 );
  550. var skyMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide, depthWrite: false, depthTest: false } );
  551. if ( skyColor.length > 3 ) {
  552. paintFaces( skyGeometry, radius, skyAngle, toColorArray( skyColor ), true );
  553. skyMaterial.vertexColors = true;
  554. } else {
  555. skyMaterial.color.setRGB( skyColor[ 0 ], skyColor[ 1 ], skyColor[ 2 ] );
  556. }
  557. var sky = new THREE.Mesh( skyGeometry, skyMaterial );
  558. group.add( sky );
  559. }
  560. // ground
  561. if ( groundColor ) {
  562. if ( groundColor.length > 0 ) {
  563. var groundGeometry = new THREE.SphereBufferGeometry( radius, 32, 16, 0, 2 * Math.PI, 0.5 * Math.PI, 1.5 * Math.PI );
  564. var groundMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide, vertexColors: true, depthWrite: false, depthTest: false } );
  565. paintFaces( groundGeometry, radius, groundAngle, toColorArray( groundColor ), false );
  566. var ground = new THREE.Mesh( groundGeometry, groundMaterial );
  567. group.add( ground );
  568. }
  569. }
  570. // render background group first
  571. group.renderOrder = - Infinity;
  572. return group;
  573. }
  574. function buildShapeNode( node ) {
  575. var fields = node.fields;
  576. // if the appearance field is NULL or unspecified, lighting is off and the unlit object color is (0, 0, 0)
  577. var material = new THREE.MeshBasicMaterial( { color: 0x000000 } );
  578. var geometry;
  579. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  580. var field = fields[ i ];
  581. var fieldName = field.name;
  582. var fieldValues = field.values;
  583. switch ( fieldName ) {
  584. case 'appearance':
  585. if ( fieldValues[ 0 ] !== null ) {
  586. material = getNode( fieldValues[ 0 ] );
  587. }
  588. break;
  589. case 'geometry':
  590. if ( fieldValues[ 0 ] !== null ) {
  591. geometry = getNode( fieldValues[ 0 ] );
  592. }
  593. break;
  594. default:
  595. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  596. break;
  597. }
  598. }
  599. // build 3D object
  600. var object;
  601. if ( geometry && geometry.attributes.position ) {
  602. var type = geometry._type;
  603. if ( type === 'points' ) { // points
  604. var pointsMaterial = new THREE.PointsMaterial( { color: 0xffffff } );
  605. if ( geometry.attributes.color !== undefined ) {
  606. pointsMaterial.vertexColors = true;
  607. } else {
  608. // if the color field is NULL and there is a material defined for the appearance affecting this PointSet, then use the emissiveColor of the material to draw the points
  609. if ( material.isMeshPhongMaterial ) {
  610. pointsMaterial.color.copy( material.emissive );
  611. }
  612. }
  613. object = new THREE.Points( geometry, pointsMaterial );
  614. } else if ( type === 'line' ) { // lines
  615. var lineMaterial = new THREE.LineBasicMaterial( { color: 0xffffff } );
  616. if ( geometry.attributes.color !== undefined ) {
  617. lineMaterial.vertexColors = true;
  618. } else {
  619. // if the color field is NULL and there is a material defined for the appearance affecting this IndexedLineSet, then use the emissiveColor of the material to draw the lines
  620. if ( material.isMeshPhongMaterial ) {
  621. lineMaterial.color.copy( material.emissive );
  622. }
  623. }
  624. object = new THREE.LineSegments( geometry, lineMaterial );
  625. } else { // consider meshes
  626. // check "solid" hint (it's placed in the geometry but affects the material)
  627. if ( geometry._solid !== undefined ) {
  628. material.side = ( geometry._solid ) ? THREE.FrontSide : THREE.DoubleSide;
  629. }
  630. // check for vertex colors
  631. if ( geometry.attributes.color !== undefined ) {
  632. material.vertexColors = true;
  633. }
  634. object = new THREE.Mesh( geometry, material );
  635. }
  636. } else {
  637. object = new THREE.Object3D();
  638. // if the geometry field is NULL or no vertices are defined the object is not drawn
  639. object.visible = false;
  640. }
  641. return object;
  642. }
  643. function buildAppearanceNode( node ) {
  644. var material = new THREE.MeshPhongMaterial();
  645. var transformData;
  646. var fields = node.fields;
  647. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  648. var field = fields[ i ];
  649. var fieldName = field.name;
  650. var fieldValues = field.values;
  651. switch ( fieldName ) {
  652. case 'material':
  653. if ( fieldValues[ 0 ] !== null ) {
  654. var materialData = getNode( fieldValues[ 0 ] );
  655. if ( materialData.diffuseColor ) material.color.copy( materialData.diffuseColor );
  656. if ( materialData.emissiveColor ) material.emissive.copy( materialData.emissiveColor );
  657. if ( materialData.shininess ) material.shininess = materialData.shininess;
  658. if ( materialData.specularColor ) material.specular.copy( materialData.specularColor );
  659. if ( materialData.transparency ) material.opacity = 1 - materialData.transparency;
  660. if ( materialData.transparency > 0 ) material.transparent = true;
  661. } else {
  662. // if the material field is NULL or unspecified, lighting is off and the unlit object color is (0, 0, 0)
  663. material = new THREE.MeshBasicMaterial( { color: 0x000000 } );
  664. }
  665. break;
  666. case 'texture':
  667. var textureNode = fieldValues[ 0 ];
  668. if ( textureNode !== null ) {
  669. if ( textureNode.name === 'ImageTexture' || textureNode.name === 'PixelTexture' ) {
  670. material.map = getNode( textureNode );
  671. } else {
  672. // MovieTexture not supported yet
  673. }
  674. }
  675. break;
  676. case 'textureTransform':
  677. if ( fieldValues[ 0 ] !== null ) {
  678. transformData = getNode( fieldValues[ 0 ] );
  679. }
  680. break;
  681. default:
  682. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  683. break;
  684. }
  685. }
  686. // only apply texture transform data if a texture was defined
  687. if ( material.map ) {
  688. // respect VRML lighting model
  689. if ( material.map.__type ) {
  690. switch ( material.map.__type ) {
  691. case TEXTURE_TYPE.INTENSITY_ALPHA:
  692. material.opacity = 1; // ignore transparency
  693. break;
  694. case TEXTURE_TYPE.RGB:
  695. material.color.set( 0xffffff ); // ignore material color
  696. break;
  697. case TEXTURE_TYPE.RGBA:
  698. material.color.set( 0xffffff ); // ignore material color
  699. material.opacity = 1; // ignore transparency
  700. break;
  701. default:
  702. }
  703. delete material.map.__type;
  704. }
  705. // apply texture transform
  706. if ( transformData ) {
  707. material.map.center.copy( transformData.center );
  708. material.map.rotation = transformData.rotation;
  709. material.map.repeat.copy( transformData.scale );
  710. material.map.offset.copy( transformData.translation );
  711. }
  712. }
  713. return material;
  714. }
  715. function buildMaterialNode( node ) {
  716. var materialData = {};
  717. var fields = node.fields;
  718. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  719. var field = fields[ i ];
  720. var fieldName = field.name;
  721. var fieldValues = field.values;
  722. switch ( fieldName ) {
  723. case 'ambientIntensity':
  724. // field not supported
  725. break;
  726. case 'diffuseColor':
  727. materialData.diffuseColor = new THREE.Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  728. break;
  729. case 'emissiveColor':
  730. materialData.emissiveColor = new THREE.Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  731. break;
  732. case 'shininess':
  733. materialData.shininess = fieldValues[ 0 ];
  734. break;
  735. case 'specularColor':
  736. materialData.emissiveColor = new THREE.Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  737. break;
  738. case 'transparency':
  739. materialData.transparency = fieldValues[ 0 ];
  740. break;
  741. default:
  742. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  743. break;
  744. }
  745. }
  746. return materialData;
  747. }
  748. function parseHexColor( hex, textureType, color ) {
  749. switch ( textureType ) {
  750. case TEXTURE_TYPE.INTENSITY:
  751. // Intensity texture: A one-component image specifies one-byte hexadecimal or integer values representing the intensity of the image
  752. var value = parseInt( hex );
  753. color.r = value;
  754. color.g = value;
  755. color.b = value;
  756. break;
  757. case TEXTURE_TYPE.INTENSITY_ALPHA:
  758. // Intensity+Alpha texture: A two-component image specifies the intensity in the first (high) byte and the alpha opacity in the second (low) byte.
  759. var value = parseInt( "0x" + hex.substring( 2, 4 ) );
  760. color.r = value;
  761. color.g = value;
  762. color.b = value;
  763. color.a = parseInt( "0x" + hex.substring( 4, 6 ) );
  764. break;
  765. case TEXTURE_TYPE.RGB:
  766. // RGB texture: Pixels in a three-component image specify the red component in the first (high) byte, followed by the green and blue components
  767. color.r = parseInt( "0x" + hex.substring( 2, 4 ) );
  768. color.g = parseInt( "0x" + hex.substring( 4, 6 ) );
  769. color.b = parseInt( "0x" + hex.substring( 6, 8 ) );
  770. break;
  771. case TEXTURE_TYPE.RGBA:
  772. // RGBA texture: Four-component images specify the alpha opacity byte after red/green/blue
  773. color.r = parseInt( "0x" + hex.substring( 2, 4 ) );
  774. color.g = parseInt( "0x" + hex.substring( 4, 6 ) );
  775. color.b = parseInt( "0x" + hex.substring( 6, 8 ) );
  776. color.a = parseInt( "0x" + hex.substring( 8, 10 ) );
  777. break;
  778. default:
  779. }
  780. }
  781. function getTextureType( num_components ) {
  782. var type;
  783. switch ( num_components ) {
  784. case 1:
  785. type = TEXTURE_TYPE.INTENSITY;
  786. break;
  787. case 2:
  788. type = TEXTURE_TYPE.INTENSITY_ALPHA;
  789. break;
  790. case 3:
  791. type = TEXTURE_TYPE.RGB;
  792. break;
  793. case 4:
  794. type = TEXTURE_TYPE.RGBA;
  795. break;
  796. default:
  797. }
  798. return type;
  799. }
  800. function buildPixelTextureNode( node ) {
  801. var texture;
  802. var wrapS = THREE.RepeatWrapping;
  803. var wrapT = THREE.RepeatWrapping;
  804. var fields = node.fields;
  805. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  806. var field = fields[ i ];
  807. var fieldName = field.name;
  808. var fieldValues = field.values;
  809. switch ( fieldName ) {
  810. case 'image':
  811. var width = fieldValues[ 0 ];
  812. var height = fieldValues[ 1 ];
  813. var num_components = fieldValues[ 2 ];
  814. var useAlpha = ( num_components === 2 || num_components === 4 );
  815. var textureType = getTextureType( num_components );
  816. var size = ( ( useAlpha === true ) ? 4 : 3 ) * ( width * height );
  817. var data = new Uint8Array( size );
  818. var color = { r: 0, g: 0, b: 0, a: 0 };
  819. for ( var j = 3, k = 0, jl = fieldValues.length; j < jl; j ++, k ++ ) {
  820. parseHexColor( fieldValues[ j ], textureType, color );
  821. if ( useAlpha === true ) {
  822. var stride = k * 4;
  823. data[ stride + 0 ] = color.r;
  824. data[ stride + 1 ] = color.g;
  825. data[ stride + 2 ] = color.b;
  826. data[ stride + 3 ] = color.a;
  827. } else {
  828. var stride = k * 3;
  829. data[ stride + 0 ] = color.r;
  830. data[ stride + 1 ] = color.g;
  831. data[ stride + 2 ] = color.b;
  832. }
  833. }
  834. texture = new THREE.DataTexture( data, width, height, ( useAlpha === true ) ? THREE.RGBAFormat : THREE.RGBFormat );
  835. texture.__type = textureType; // needed for material modifications
  836. break;
  837. case 'repeatS':
  838. if ( fieldValues[ 0 ] === false ) wrapS = THREE.ClampToEdgeWrapping;
  839. break;
  840. case 'repeatT':
  841. if ( fieldValues[ 0 ] === false ) wrapT = THREE.ClampToEdgeWrapping;
  842. break;
  843. default:
  844. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  845. break;
  846. }
  847. }
  848. if ( texture ) {
  849. texture.wrapS = wrapS;
  850. texture.wrapT = wrapT;
  851. }
  852. return texture;
  853. }
  854. function buildImageTextureNode( node ) {
  855. var texture;
  856. var wrapS = THREE.RepeatWrapping;
  857. var wrapT = THREE.RepeatWrapping;
  858. var fields = node.fields;
  859. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  860. var field = fields[ i ];
  861. var fieldName = field.name;
  862. var fieldValues = field.values;
  863. switch ( fieldName ) {
  864. case 'url':
  865. var url = fieldValues[ 0 ];
  866. if ( url ) texture = textureLoader.load( url );
  867. break;
  868. case 'repeatS':
  869. if ( fieldValues[ 0 ] === false ) wrapS = THREE.ClampToEdgeWrapping;
  870. break;
  871. case 'repeatT':
  872. if ( fieldValues[ 0 ] === false ) wrapT = THREE.ClampToEdgeWrapping;
  873. break;
  874. default:
  875. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  876. break;
  877. }
  878. }
  879. if ( texture ) {
  880. texture.wrapS = wrapS;
  881. texture.wrapT = wrapT;
  882. }
  883. return texture;
  884. }
  885. function buildTextureTransformNode( node ) {
  886. var transformData = {
  887. center: new THREE.Vector2(),
  888. rotation: new THREE.Vector2(),
  889. scale: new THREE.Vector2(),
  890. translation: new THREE.Vector2()
  891. };
  892. var fields = node.fields;
  893. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  894. var field = fields[ i ];
  895. var fieldName = field.name;
  896. var fieldValues = field.values;
  897. switch ( fieldName ) {
  898. case 'center':
  899. transformData.center.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  900. break;
  901. case 'rotation':
  902. transformData.rotation = fieldValues[ 0 ];
  903. break;
  904. case 'scale':
  905. transformData.scale.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  906. break;
  907. case 'translation':
  908. transformData.translation.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  909. break;
  910. default:
  911. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  912. break;
  913. }
  914. }
  915. return transformData;
  916. }
  917. function buildGeometricNode( node ) {
  918. return node.fields[ 0 ].values;
  919. }
  920. function buildWorldInfoNode( node ) {
  921. var worldInfo = {};
  922. var fields = node.fields;
  923. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  924. var field = fields[ i ];
  925. var fieldName = field.name;
  926. var fieldValues = field.values;
  927. switch ( fieldName ) {
  928. case 'title':
  929. worldInfo.title = fieldValues[ 0 ];
  930. break;
  931. case 'info':
  932. worldInfo.info = fieldValues;
  933. break;
  934. default:
  935. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  936. break;
  937. }
  938. }
  939. return worldInfo;
  940. }
  941. function buildIndexedFaceSetNode( node ) {
  942. var color, coord, normal, texCoord;
  943. var ccw = true, solid = true, creaseAngle = 0;
  944. var colorIndex, coordIndex, normalIndex, texCoordIndex;
  945. var colorPerVertex = true, normalPerVertex = true;
  946. var fields = node.fields;
  947. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  948. var field = fields[ i ];
  949. var fieldName = field.name;
  950. var fieldValues = field.values;
  951. switch ( fieldName ) {
  952. case 'color':
  953. var colorNode = fieldValues[ 0 ];
  954. if ( colorNode !== null ) {
  955. color = getNode( colorNode );
  956. }
  957. break;
  958. case 'coord':
  959. var coordNode = fieldValues[ 0 ];
  960. if ( coordNode !== null ) {
  961. coord = getNode( coordNode );
  962. }
  963. break;
  964. case 'normal':
  965. var normalNode = fieldValues[ 0 ];
  966. if ( normalNode !== null ) {
  967. normal = getNode( normalNode );
  968. }
  969. break;
  970. case 'texCoord':
  971. var texCoordNode = fieldValues[ 0 ];
  972. if ( texCoordNode !== null ) {
  973. texCoord = getNode( texCoordNode );
  974. }
  975. break;
  976. case 'ccw':
  977. ccw = fieldValues[ 0 ];
  978. break;
  979. case 'colorIndex':
  980. colorIndex = fieldValues;
  981. break;
  982. case 'colorPerVertex':
  983. colorPerVertex = fieldValues[ 0 ];
  984. break;
  985. case 'convex':
  986. // field not supported
  987. break;
  988. case 'coordIndex':
  989. coordIndex = fieldValues;
  990. break;
  991. case 'creaseAngle':
  992. creaseAngle = fieldValues[ 0 ];
  993. break;
  994. case 'normalIndex':
  995. normalIndex = fieldValues;
  996. break;
  997. case 'normalPerVertex':
  998. normalPerVertex = fieldValues[ 0 ];
  999. break;
  1000. case 'solid':
  1001. solid = fieldValues[ 0 ];
  1002. break;
  1003. case 'texCoordIndex':
  1004. texCoordIndex = fieldValues;
  1005. break;
  1006. default:
  1007. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1008. break;
  1009. }
  1010. }
  1011. if ( coordIndex === undefined ) {
  1012. console.warn( 'THREE.VRMLLoader: Missing coordIndex.' );
  1013. return new THREE.BufferGeometry(); // handle VRML files with incomplete geometry definition
  1014. }
  1015. var triangulatedCoordIndex = triangulateFaceIndex( coordIndex, ccw );
  1016. var positionAttribute;
  1017. var colorAttribute;
  1018. var normalAttribute;
  1019. var uvAttribute;
  1020. if ( color ) {
  1021. if ( colorPerVertex === true ) {
  1022. if ( colorIndex && colorIndex.length > 0 ) {
  1023. // if the colorIndex field is not empty, then it is used to choose colors for each vertex of the IndexedFaceSet.
  1024. var triangulatedColorIndex = triangulateFaceIndex( colorIndex, ccw );
  1025. colorAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedColorIndex, color, 3 );
  1026. } else {
  1027. // if the colorIndex field is empty, then the coordIndex field is used to choose colors from the Color node
  1028. colorAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new THREE.Float32BufferAttribute( color, 3 ) );
  1029. }
  1030. } else {
  1031. if ( colorIndex && colorIndex.length > 0 ) {
  1032. // if the colorIndex field is not empty, then they are used to choose one color for each face of the IndexedFaceSet
  1033. var flattenFaceColors = flattenData( color, colorIndex );
  1034. var triangulatedFaceColors = triangulateFaceData( flattenFaceColors, coordIndex );
  1035. colorAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceColors );
  1036. } else {
  1037. // if the colorIndex field is empty, then the color are applied to each face of the IndexedFaceSet in order
  1038. var triangulatedFaceColors = triangulateFaceData( color, coordIndex );
  1039. colorAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceColors );
  1040. }
  1041. }
  1042. }
  1043. if ( normal ) {
  1044. if ( normalPerVertex === true ) {
  1045. // consider vertex normals
  1046. if ( normalIndex && normalIndex.length > 0 ) {
  1047. // if the normalIndex field is not empty, then it is used to choose normals for each vertex of the IndexedFaceSet.
  1048. var triangulatedNormalIndex = triangulateFaceIndex( normalIndex, ccw );
  1049. normalAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedNormalIndex, normal, 3 );
  1050. } else {
  1051. // if the normalIndex field is empty, then the coordIndex field is used to choose normals from the Normal node
  1052. normalAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new THREE.Float32BufferAttribute( normal, 3 ) );
  1053. }
  1054. } else {
  1055. // consider face normals
  1056. if ( normalIndex && normalIndex.length > 0 ) {
  1057. // if the normalIndex field is not empty, then they are used to choose one normal for each face of the IndexedFaceSet
  1058. var flattenFaceNormals = flattenData( normal, normalIndex );
  1059. var triangulatedFaceNormals = triangulateFaceData( flattenFaceNormals, coordIndex );
  1060. normalAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceNormals );
  1061. } else {
  1062. // if the normalIndex field is empty, then the normals are applied to each face of the IndexedFaceSet in order
  1063. var triangulatedFaceNormals = triangulateFaceData( normal, coordIndex );
  1064. normalAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceNormals );
  1065. }
  1066. }
  1067. } else {
  1068. // if the normal field is NULL, then the loader should automatically generate normals, using creaseAngle to determine if and how normals are smoothed across shared vertices
  1069. normalAttribute = computeNormalAttribute( triangulatedCoordIndex, coord, creaseAngle );
  1070. }
  1071. if ( texCoord ) {
  1072. // texture coordinates are always defined on vertex level
  1073. if ( texCoordIndex && texCoordIndex.length > 0 ) {
  1074. // if the texCoordIndex field is not empty, then it is used to choose texture coordinates for each vertex of the IndexedFaceSet.
  1075. var triangulatedTexCoordIndex = triangulateFaceIndex( texCoordIndex, ccw );
  1076. uvAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedTexCoordIndex, texCoord, 2 );
  1077. } else {
  1078. // if the texCoordIndex field is empty, then the coordIndex array is used to choose texture coordinates from the TextureCoordinate node
  1079. uvAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new THREE.Float32BufferAttribute( texCoord, 2 ) );
  1080. }
  1081. }
  1082. var geometry = new THREE.BufferGeometry();
  1083. positionAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new THREE.Float32BufferAttribute( coord, 3 ) );
  1084. geometry.setAttribute( 'position', positionAttribute );
  1085. geometry.setAttribute( 'normal', normalAttribute );
  1086. // optional attributes
  1087. if ( colorAttribute ) geometry.setAttribute( 'color', colorAttribute );
  1088. if ( uvAttribute ) geometry.setAttribute( 'uv', uvAttribute );
  1089. // "solid" influences the material so let's store it for later use
  1090. geometry._solid = solid;
  1091. geometry._type = 'mesh';
  1092. return geometry;
  1093. }
  1094. function buildIndexedLineSetNode( node ) {
  1095. var color, coord;
  1096. var colorIndex, coordIndex;
  1097. var colorPerVertex = true;
  1098. var fields = node.fields;
  1099. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1100. var field = fields[ i ];
  1101. var fieldName = field.name;
  1102. var fieldValues = field.values;
  1103. switch ( fieldName ) {
  1104. case 'color':
  1105. var colorNode = fieldValues[ 0 ];
  1106. if ( colorNode !== null ) {
  1107. color = getNode( colorNode );
  1108. }
  1109. break;
  1110. case 'coord':
  1111. var coordNode = fieldValues[ 0 ];
  1112. if ( coordNode !== null ) {
  1113. coord = getNode( coordNode );
  1114. }
  1115. break;
  1116. case 'colorIndex':
  1117. colorIndex = fieldValues;
  1118. break;
  1119. case 'colorPerVertex':
  1120. colorPerVertex = fieldValues[ 0 ];
  1121. break;
  1122. case 'coordIndex':
  1123. coordIndex = fieldValues;
  1124. break;
  1125. default:
  1126. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1127. break;
  1128. }
  1129. }
  1130. // build lines
  1131. var colorAttribute;
  1132. var expandedLineIndex = expandLineIndex( coordIndex ); // create an index for three.js's linesegment primitive
  1133. if ( color ) {
  1134. if ( colorPerVertex === true ) {
  1135. if ( colorIndex.length > 0 ) {
  1136. // if the colorIndex field is not empty, then one color is used for each polyline of the IndexedLineSet.
  1137. var expandedColorIndex = expandLineIndex( colorIndex ); // compute colors for each line segment (rendering primitve)
  1138. colorAttribute = computeAttributeFromIndexedData( expandedLineIndex, expandedColorIndex, color, 3 ); // compute data on vertex level
  1139. } else {
  1140. // if the colorIndex field is empty, then the colors are applied to each polyline of the IndexedLineSet in order.
  1141. colorAttribute = toNonIndexedAttribute( expandedLineIndex, new THREE.Float32BufferAttribute( color, 3 ) );
  1142. }
  1143. } else {
  1144. if ( colorIndex.length > 0 ) {
  1145. // if the colorIndex field is not empty, then colors are applied to each vertex of the IndexedLineSet
  1146. var flattenLineColors = flattenData( color, colorIndex ); // compute colors for each VRML primitve
  1147. var expandedLineColors = expandLineData( flattenLineColors, coordIndex ); // compute colors for each line segment (rendering primitve)
  1148. colorAttribute = computeAttributeFromLineData( expandedLineIndex, expandedLineColors ); // compute data on vertex level
  1149. } else {
  1150. // if the colorIndex field is empty, then the coordIndex field is used to choose colors from the Color node
  1151. var expandedLineColors = expandLineData( color, coordIndex ); // compute colors for each line segment (rendering primitve)
  1152. colorAttribute = computeAttributeFromLineData( expandedLineIndex, expandedLineColors ); // compute data on vertex level
  1153. }
  1154. }
  1155. }
  1156. //
  1157. var geometry = new THREE.BufferGeometry();
  1158. var positionAttribute = toNonIndexedAttribute( expandedLineIndex, new THREE.Float32BufferAttribute( coord, 3 ) );
  1159. geometry.setAttribute( 'position', positionAttribute );
  1160. if ( colorAttribute ) geometry.setAttribute( 'color', colorAttribute );
  1161. geometry._type = 'line';
  1162. return geometry;
  1163. }
  1164. function buildPointSetNode( node ) {
  1165. var geometry;
  1166. var color, coord;
  1167. var fields = node.fields;
  1168. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1169. var field = fields[ i ];
  1170. var fieldName = field.name;
  1171. var fieldValues = field.values;
  1172. switch ( fieldName ) {
  1173. case 'color':
  1174. var colorNode = fieldValues[ 0 ];
  1175. if ( colorNode !== null ) {
  1176. color = getNode( colorNode );
  1177. }
  1178. break;
  1179. case 'coord':
  1180. var coordNode = fieldValues[ 0 ];
  1181. if ( coordNode !== null ) {
  1182. coord = getNode( coordNode );
  1183. }
  1184. break;
  1185. default:
  1186. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1187. break;
  1188. }
  1189. }
  1190. var geometry = new THREE.BufferGeometry();
  1191. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( coord, 3 ) );
  1192. if ( color ) geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( color, 3 ) );
  1193. geometry._type = 'points';
  1194. return geometry;
  1195. }
  1196. function buildBoxNode( node ) {
  1197. var size = new THREE.Vector3( 2, 2, 2 );
  1198. var fields = node.fields;
  1199. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1200. var field = fields[ i ];
  1201. var fieldName = field.name;
  1202. var fieldValues = field.values;
  1203. switch ( fieldName ) {
  1204. case 'size':
  1205. size.x = fieldValues[ 0 ];
  1206. size.y = fieldValues[ 1 ];
  1207. size.z = fieldValues[ 2 ];
  1208. break;
  1209. default:
  1210. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1211. break;
  1212. }
  1213. }
  1214. var geometry = new THREE.BoxBufferGeometry( size.x, size.y, size.z );
  1215. return geometry;
  1216. }
  1217. function buildConeNode( node ) {
  1218. var radius = 1, height = 2, openEnded = false;
  1219. var fields = node.fields;
  1220. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1221. var field = fields[ i ];
  1222. var fieldName = field.name;
  1223. var fieldValues = field.values;
  1224. switch ( fieldName ) {
  1225. case 'bottom':
  1226. openEnded = ! fieldValues[ 0 ];
  1227. break;
  1228. case 'bottomRadius':
  1229. radius = fieldValues[ 0 ];
  1230. break;
  1231. case 'height':
  1232. height = fieldValues[ 0 ];
  1233. break;
  1234. case 'side':
  1235. // field not supported
  1236. break;
  1237. default:
  1238. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1239. break;
  1240. }
  1241. }
  1242. var geometry = new THREE.ConeBufferGeometry( radius, height, 16, 1, openEnded );
  1243. return geometry;
  1244. }
  1245. function buildCylinderNode( node ) {
  1246. var radius = 1, height = 2;
  1247. var fields = node.fields;
  1248. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1249. var field = fields[ i ];
  1250. var fieldName = field.name;
  1251. var fieldValues = field.values;
  1252. switch ( fieldName ) {
  1253. case 'bottom':
  1254. // field not supported
  1255. break;
  1256. case 'radius':
  1257. radius = fieldValues[ 0 ];
  1258. break;
  1259. case 'height':
  1260. height = fieldValues[ 0 ];
  1261. break;
  1262. case 'side':
  1263. // field not supported
  1264. break;
  1265. case 'top':
  1266. // field not supported
  1267. break;
  1268. default:
  1269. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1270. break;
  1271. }
  1272. }
  1273. var geometry = new THREE.CylinderBufferGeometry( radius, radius, height, 16, 1 );
  1274. return geometry;
  1275. }
  1276. function buildSphereNode( node ) {
  1277. var radius = 1;
  1278. var fields = node.fields;
  1279. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1280. var field = fields[ i ];
  1281. var fieldName = field.name;
  1282. var fieldValues = field.values;
  1283. switch ( fieldName ) {
  1284. case 'radius':
  1285. radius = fieldValues[ 0 ];
  1286. break;
  1287. default:
  1288. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1289. break;
  1290. }
  1291. }
  1292. var geometry = new THREE.SphereBufferGeometry( radius, 16, 16 );
  1293. return geometry;
  1294. }
  1295. function buildElevationGridNode( node ) {
  1296. var color;
  1297. var normal;
  1298. var texCoord;
  1299. var height;
  1300. var colorPerVertex = true;
  1301. var normalPerVertex = true;
  1302. var solid = true;
  1303. var ccw = true;
  1304. var creaseAngle = 0;
  1305. var xDimension = 2;
  1306. var zDimension = 2;
  1307. var xSpacing = 1;
  1308. var zSpacing = 1;
  1309. var fields = node.fields;
  1310. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1311. var field = fields[ i ];
  1312. var fieldName = field.name;
  1313. var fieldValues = field.values;
  1314. switch ( fieldName ) {
  1315. case 'color':
  1316. var colorNode = fieldValues[ 0 ];
  1317. if ( colorNode !== null ) {
  1318. color = getNode( colorNode );
  1319. }
  1320. break;
  1321. case 'normal':
  1322. var normalNode = fieldValues[ 0 ];
  1323. if ( normalNode !== null ) {
  1324. normal = getNode( normalNode );
  1325. }
  1326. break;
  1327. case 'texCoord':
  1328. var texCoordNode = fieldValues[ 0 ];
  1329. if ( texCoordNode !== null ) {
  1330. texCoord = getNode( texCoordNode );
  1331. }
  1332. break;
  1333. case 'height':
  1334. height = fieldValues;
  1335. break;
  1336. case 'ccw':
  1337. ccw = fieldValues[ 0 ];
  1338. break;
  1339. case 'colorPerVertex':
  1340. colorPerVertex = fieldValues[ 0 ];
  1341. break;
  1342. case 'creaseAngle':
  1343. creaseAngle = fieldValues[ 0 ];
  1344. break;
  1345. case 'normalPerVertex':
  1346. normalPerVertex = fieldValues[ 0 ];
  1347. break;
  1348. case 'solid':
  1349. solid = fieldValues[ 0 ];
  1350. break;
  1351. case 'xDimension':
  1352. xDimension = fieldValues[ 0 ];
  1353. break;
  1354. case 'xSpacing':
  1355. xSpacing = fieldValues[ 0 ];
  1356. break;
  1357. case 'zDimension':
  1358. zDimension = fieldValues[ 0 ];
  1359. break;
  1360. case 'zSpacing':
  1361. zSpacing = fieldValues[ 0 ];
  1362. break;
  1363. default:
  1364. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1365. break;
  1366. }
  1367. }
  1368. // vertex data
  1369. var vertices = [];
  1370. var normals = [];
  1371. var colors = [];
  1372. var uvs = [];
  1373. for ( var i = 0; i < zDimension; i ++ ) {
  1374. for ( var j = 0; j < xDimension; j ++ ) {
  1375. // compute a row major index
  1376. var index = ( i * xDimension ) + j;
  1377. // vertices
  1378. var x = xSpacing * i;
  1379. var y = height[ index ];
  1380. var z = zSpacing * j;
  1381. vertices.push( x, y, z );
  1382. // colors
  1383. if ( color && colorPerVertex === true ) {
  1384. var r = color[ index * 3 + 0 ];
  1385. var g = color[ index * 3 + 1 ];
  1386. var b = color[ index * 3 + 2 ];
  1387. colors.push( r, g, b );
  1388. }
  1389. // normals
  1390. if ( normal && normalPerVertex === true ) {
  1391. var xn = normal[ index * 3 + 0 ];
  1392. var yn = normal[ index * 3 + 1 ];
  1393. var zn = normal[ index * 3 + 2 ];
  1394. normals.push( xn, yn, zn );
  1395. }
  1396. // uvs
  1397. if ( texCoord ) {
  1398. var s = texCoord[ index * 2 + 0 ];
  1399. var t = texCoord[ index * 2 + 1 ];
  1400. uvs.push( s, t );
  1401. } else {
  1402. uvs.push( i / ( xDimension - 1 ), j / ( zDimension - 1 ) );
  1403. }
  1404. }
  1405. }
  1406. // indices
  1407. var indices = [];
  1408. for ( var i = 0; i < xDimension - 1; i ++ ) {
  1409. for ( var j = 0; j < zDimension - 1; j ++ ) {
  1410. // from https://tecfa.unige.ch/guides/vrml/vrml97/spec/part1/nodesRef.html#ElevationGrid
  1411. var a = i + j * xDimension;
  1412. var b = i + ( j + 1 ) * xDimension;
  1413. var c = ( i + 1 ) + ( j + 1 ) * xDimension;
  1414. var d = ( i + 1 ) + j * xDimension;
  1415. // faces
  1416. if ( ccw === true ) {
  1417. indices.push( a, c, b );
  1418. indices.push( c, a, d );
  1419. } else {
  1420. indices.push( a, b, c );
  1421. indices.push( c, d, a );
  1422. }
  1423. }
  1424. }
  1425. //
  1426. var positionAttribute = toNonIndexedAttribute( indices, new THREE.Float32BufferAttribute( vertices, 3 ) );
  1427. var uvAttribute = toNonIndexedAttribute( indices, new THREE.Float32BufferAttribute( uvs, 2 ) );
  1428. var colorAttribute;
  1429. var normalAttribute;
  1430. // color attribute
  1431. if ( color ) {
  1432. if ( colorPerVertex === false ) {
  1433. for ( var i = 0; i < xDimension - 1; i ++ ) {
  1434. for ( var j = 0; j < zDimension - 1; j ++ ) {
  1435. var index = i + j * ( xDimension - 1 );
  1436. var r = color[ index * 3 + 0 ];
  1437. var g = color[ index * 3 + 1 ];
  1438. var b = color[ index * 3 + 2 ];
  1439. // one color per quad
  1440. colors.push( r, g, b ); colors.push( r, g, b ); colors.push( r, g, b );
  1441. colors.push( r, g, b ); colors.push( r, g, b ); colors.push( r, g, b );
  1442. }
  1443. }
  1444. colorAttribute = new THREE.Float32BufferAttribute( colors, 3 );
  1445. } else {
  1446. colorAttribute = toNonIndexedAttribute( indices, new THREE.Float32BufferAttribute( colors, 3 ) );
  1447. }
  1448. }
  1449. // normal attribute
  1450. if ( normal ) {
  1451. if ( normalPerVertex === false ) {
  1452. for ( var i = 0; i < xDimension - 1; i ++ ) {
  1453. for ( var j = 0; j < zDimension - 1; j ++ ) {
  1454. var index = i + j * ( xDimension - 1 );
  1455. var xn = normal[ index * 3 + 0 ];
  1456. var yn = normal[ index * 3 + 1 ];
  1457. var zn = normal[ index * 3 + 2 ];
  1458. // one normal per quad
  1459. normals.push( xn, yn, zn ); normals.push( xn, yn, zn ); normals.push( xn, yn, zn );
  1460. normals.push( xn, yn, zn ); normals.push( xn, yn, zn ); normals.push( xn, yn, zn );
  1461. }
  1462. }
  1463. normalAttribute = new THREE.Float32BufferAttribute( normals, 3 );
  1464. } else {
  1465. normalAttribute = toNonIndexedAttribute( indices, new THREE.Float32BufferAttribute( normals, 3 ) );
  1466. }
  1467. } else {
  1468. normalAttribute = computeNormalAttribute( indices, vertices, creaseAngle );
  1469. }
  1470. // build geometry
  1471. var geometry = new THREE.BufferGeometry();
  1472. geometry.setAttribute( 'position', positionAttribute );
  1473. geometry.setAttribute( 'normal', normalAttribute );
  1474. geometry.setAttribute( 'uv', uvAttribute );
  1475. if ( colorAttribute ) geometry.setAttribute( 'color', colorAttribute );
  1476. // "solid" influences the material so let's store it for later use
  1477. geometry._solid = solid;
  1478. geometry._type = 'mesh';
  1479. return geometry;
  1480. }
  1481. function buildExtrusionNode( node ) {
  1482. var crossSection = [ 1, 1, 1, - 1, - 1, - 1, - 1, 1, 1, 1 ];
  1483. var spine = [ 0, 0, 0, 0, 1, 0 ];
  1484. var scale;
  1485. var orientation;
  1486. var beginCap = true;
  1487. var ccw = true;
  1488. var creaseAngle = 0;
  1489. var endCap = true;
  1490. var solid = true;
  1491. var fields = node.fields;
  1492. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1493. var field = fields[ i ];
  1494. var fieldName = field.name;
  1495. var fieldValues = field.values;
  1496. switch ( fieldName ) {
  1497. case 'beginCap':
  1498. beginCap = fieldValues[ 0 ];
  1499. break;
  1500. case 'ccw':
  1501. ccw = fieldValues[ 0 ];
  1502. break;
  1503. case 'convex':
  1504. // field not supported
  1505. break;
  1506. case 'creaseAngle':
  1507. creaseAngle = fieldValues[ 0 ];
  1508. break;
  1509. case 'crossSection':
  1510. crossSection = fieldValues;
  1511. break;
  1512. case 'endCap':
  1513. endCap = fieldValues[ 0 ];
  1514. break;
  1515. case 'orientation':
  1516. orientation = fieldValues;
  1517. break;
  1518. case 'scale':
  1519. scale = fieldValues;
  1520. break;
  1521. case 'solid':
  1522. solid = fieldValues[ 0 ];
  1523. break;
  1524. case 'spine':
  1525. spine = fieldValues; // only extrusion along the Y-axis are supported so far
  1526. break;
  1527. default:
  1528. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1529. break;
  1530. }
  1531. }
  1532. var crossSectionClosed = ( crossSection[ 0 ] === crossSection[ crossSection.length - 2 ] && crossSection[ 1 ] === crossSection[ crossSection.length - 1 ] );
  1533. // vertices
  1534. var vertices = [];
  1535. var spineVector = new THREE.Vector3();
  1536. var scaling = new THREE.Vector3();
  1537. var axis = new THREE.Vector3();
  1538. var vertex = new THREE.Vector3();
  1539. var quaternion = new THREE.Quaternion();
  1540. for ( var i = 0, j = 0, o = 0, il = spine.length; i < il; i += 3, j += 2, o += 4 ) {
  1541. spineVector.fromArray( spine, i );
  1542. scaling.x = scale ? scale[ j + 0 ] : 1;
  1543. scaling.y = 1;
  1544. scaling.z = scale ? scale[ j + 1 ] : 1;
  1545. axis.x = orientation ? orientation[ o + 0 ] : 0;
  1546. axis.y = orientation ? orientation[ o + 1 ] : 0;
  1547. axis.z = orientation ? orientation[ o + 2 ] : 1;
  1548. var angle = orientation ? orientation[ o + 3 ] : 0;
  1549. for ( var k = 0, kl = crossSection.length; k < kl; k += 2 ) {
  1550. vertex.x = crossSection[ k + 0 ];
  1551. vertex.y = 0;
  1552. vertex.z = crossSection[ k + 1 ];
  1553. // scale
  1554. vertex.multiply( scaling );
  1555. // rotate
  1556. quaternion.setFromAxisAngle( axis, angle );
  1557. vertex.applyQuaternion( quaternion );
  1558. // translate
  1559. vertex.add( spineVector );
  1560. vertices.push( vertex.x, vertex.y, vertex.z );
  1561. }
  1562. }
  1563. // indices
  1564. var indices = [];
  1565. var spineCount = spine.length / 3;
  1566. var crossSectionCount = crossSection.length / 2;
  1567. for ( var i = 0; i < spineCount - 1; i ++ ) {
  1568. for ( var j = 0; j < crossSectionCount - 1; j ++ ) {
  1569. var a = j + i * crossSectionCount;
  1570. var b = ( j + 1 ) + i * crossSectionCount;
  1571. var c = j + ( i + 1 ) * crossSectionCount;
  1572. var d = ( j + 1 ) + ( i + 1 ) * crossSectionCount;
  1573. if ( ( j === crossSectionCount - 2 ) && ( crossSectionClosed === true ) ) {
  1574. b = i * crossSectionCount;
  1575. d = ( i + 1 ) * crossSectionCount;
  1576. }
  1577. if ( ccw === true ) {
  1578. indices.push( a, b, c );
  1579. indices.push( c, b, d );
  1580. } else {
  1581. indices.push( a, c, b );
  1582. indices.push( c, d, b );
  1583. }
  1584. }
  1585. }
  1586. // triangulate cap
  1587. if ( beginCap === true || endCap === true ) {
  1588. var contour = [];
  1589. for ( var i = 0, l = crossSection.length; i < l; i += 2 ) {
  1590. contour.push( new THREE.Vector2( crossSection[ i ], crossSection[ i + 1 ] ) );
  1591. }
  1592. var faces = THREE.ShapeUtils.triangulateShape( contour, [] );
  1593. var capIndices = [];
  1594. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  1595. var face = faces[ i ];
  1596. capIndices.push( face[ 0 ], face[ 1 ], face[ 2 ] );
  1597. }
  1598. // begin cap
  1599. if ( beginCap === true ) {
  1600. for ( var i = 0, l = capIndices.length; i < l; i += 3 ) {
  1601. if ( ccw === true ) {
  1602. indices.push( capIndices[ i + 0 ], capIndices[ i + 1 ], capIndices[ i + 2 ] );
  1603. } else {
  1604. indices.push( capIndices[ i + 0 ], capIndices[ i + 2 ], capIndices[ i + 1 ] );
  1605. }
  1606. }
  1607. }
  1608. // end cap
  1609. if ( endCap === true ) {
  1610. var indexOffset = crossSectionCount * ( spineCount - 1 ); // references to the first vertex of the last cross section
  1611. for ( var i = 0, l = capIndices.length; i < l; i += 3 ) {
  1612. if ( ccw === true ) {
  1613. indices.push( indexOffset + capIndices[ i + 0 ], indexOffset + capIndices[ i + 2 ], indexOffset + capIndices[ i + 1 ] );
  1614. } else {
  1615. indices.push( indexOffset + capIndices[ i + 0 ], indexOffset + capIndices[ i + 1 ], indexOffset + capIndices[ i + 2 ] );
  1616. }
  1617. }
  1618. }
  1619. }
  1620. var positionAttribute = toNonIndexedAttribute( indices, new THREE.Float32BufferAttribute( vertices, 3 ) );
  1621. var normalAttribute = computeNormalAttribute( indices, vertices, creaseAngle );
  1622. var geometry = new THREE.BufferGeometry();
  1623. geometry.setAttribute( 'position', positionAttribute );
  1624. geometry.setAttribute( 'normal', normalAttribute );
  1625. // no uvs yet
  1626. // "solid" influences the material so let's store it for later use
  1627. geometry._solid = solid;
  1628. geometry._type = 'mesh';
  1629. return geometry;
  1630. }
  1631. // helper functions
  1632. function resolveUSE( identifier ) {
  1633. var node = nodeMap[ identifier ];
  1634. var build = getNode( node );
  1635. // because the same 3D objects can have different transformations, it's necessary to clone them.
  1636. // materials can be influenced by the geometry (e.g. vertex normals). cloning is necessary to avoid
  1637. // any side effects
  1638. return ( build.isObject3D || build.isMaterial ) ? build.clone() : build;
  1639. }
  1640. function parseFieldChildren( children, owner ) {
  1641. for ( var i = 0, l = children.length; i < l; i ++ ) {
  1642. var object = getNode( children[ i ] );
  1643. if ( object instanceof THREE.Object3D ) owner.add( object );
  1644. }
  1645. }
  1646. function triangulateFaceIndex( index, ccw ) {
  1647. var indices = [];
  1648. // since face defintions can have more than three vertices, it's necessary to
  1649. // perform a simple triangulation
  1650. var start = 0;
  1651. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1652. var i1 = index[ start ];
  1653. var i2 = index[ i + ( ccw ? 1 : 2 ) ];
  1654. var i3 = index[ i + ( ccw ? 2 : 1 ) ];
  1655. indices.push( i1, i2, i3 );
  1656. // an index of -1 indicates that the current face has ended and the next one begins
  1657. if ( index[ i + 3 ] === - 1 || i + 3 >= l ) {
  1658. i += 3;
  1659. start = i + 1;
  1660. }
  1661. }
  1662. return indices;
  1663. }
  1664. function triangulateFaceData( data, index ) {
  1665. var triangulatedData = [];
  1666. var start = 0;
  1667. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1668. var stride = start * 3;
  1669. var x = data[ stride ];
  1670. var y = data[ stride + 1 ];
  1671. var z = data[ stride + 2 ];
  1672. triangulatedData.push( x, y, z );
  1673. // an index of -1 indicates that the current face has ended and the next one begins
  1674. if ( index[ i + 3 ] === - 1 || i + 3 >= l ) {
  1675. i += 3;
  1676. start ++;
  1677. }
  1678. }
  1679. return triangulatedData;
  1680. }
  1681. function flattenData( data, index ) {
  1682. var flattenData = [];
  1683. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1684. var i1 = index[ i ];
  1685. var stride = i1 * 3;
  1686. var x = data[ stride ];
  1687. var y = data[ stride + 1 ];
  1688. var z = data[ stride + 2 ];
  1689. flattenData.push( x, y, z );
  1690. }
  1691. return flattenData;
  1692. }
  1693. function expandLineIndex( index ) {
  1694. var indices = [];
  1695. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1696. var i1 = index[ i ];
  1697. var i2 = index[ i + 1 ];
  1698. indices.push( i1, i2 );
  1699. // an index of -1 indicates that the current line has ended and the next one begins
  1700. if ( index[ i + 2 ] === - 1 || i + 2 >= l ) {
  1701. i += 2;
  1702. }
  1703. }
  1704. return indices;
  1705. }
  1706. function expandLineData( data, index ) {
  1707. var triangulatedData = [];
  1708. var start = 0;
  1709. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1710. var stride = start * 3;
  1711. var x = data[ stride ];
  1712. var y = data[ stride + 1 ];
  1713. var z = data[ stride + 2 ];
  1714. triangulatedData.push( x, y, z );
  1715. // an index of -1 indicates that the current line has ended and the next one begins
  1716. if ( index[ i + 2 ] === - 1 || i + 2 >= l ) {
  1717. i += 2;
  1718. start ++;
  1719. }
  1720. }
  1721. return triangulatedData;
  1722. }
  1723. var vA = new THREE.Vector3();
  1724. var vB = new THREE.Vector3();
  1725. var vC = new THREE.Vector3();
  1726. var uvA = new THREE.Vector2();
  1727. var uvB = new THREE.Vector2();
  1728. var uvC = new THREE.Vector2();
  1729. function computeAttributeFromIndexedData( coordIndex, index, data, itemSize ) {
  1730. var array = [];
  1731. // we use the coordIndex.length as delimiter since normalIndex must contain at least as many indices
  1732. for ( var i = 0, l = coordIndex.length; i < l; i += 3 ) {
  1733. var a = index[ i ];
  1734. var b = index[ i + 1 ];
  1735. var c = index[ i + 2 ];
  1736. if ( itemSize === 2 ) {
  1737. uvA.fromArray( data, a * itemSize );
  1738. uvB.fromArray( data, b * itemSize );
  1739. uvC.fromArray( data, c * itemSize );
  1740. array.push( uvA.x, uvA.y );
  1741. array.push( uvB.x, uvB.y );
  1742. array.push( uvC.x, uvC.y );
  1743. } else {
  1744. vA.fromArray( data, a * itemSize );
  1745. vB.fromArray( data, b * itemSize );
  1746. vC.fromArray( data, c * itemSize );
  1747. array.push( vA.x, vA.y, vA.z );
  1748. array.push( vB.x, vB.y, vB.z );
  1749. array.push( vC.x, vC.y, vC.z );
  1750. }
  1751. }
  1752. return new THREE.Float32BufferAttribute( array, itemSize );
  1753. }
  1754. function computeAttributeFromFaceData( index, faceData ) {
  1755. var array = [];
  1756. for ( var i = 0, j = 0, l = index.length; i < l; i += 3, j ++ ) {
  1757. vA.fromArray( faceData, j * 3 );
  1758. array.push( vA.x, vA.y, vA.z );
  1759. array.push( vA.x, vA.y, vA.z );
  1760. array.push( vA.x, vA.y, vA.z );
  1761. }
  1762. return new THREE.Float32BufferAttribute( array, 3 );
  1763. }
  1764. function computeAttributeFromLineData( index, lineData ) {
  1765. var array = [];
  1766. for ( var i = 0, j = 0, l = index.length; i < l; i += 2, j ++ ) {
  1767. vA.fromArray( lineData, j * 3 );
  1768. array.push( vA.x, vA.y, vA.z );
  1769. array.push( vA.x, vA.y, vA.z );
  1770. }
  1771. return new THREE.Float32BufferAttribute( array, 3 );
  1772. }
  1773. function toNonIndexedAttribute( indices, attribute ) {
  1774. var array = attribute.array;
  1775. var itemSize = attribute.itemSize;
  1776. var array2 = new array.constructor( indices.length * itemSize );
  1777. var index = 0, index2 = 0;
  1778. for ( var i = 0, l = indices.length; i < l; i ++ ) {
  1779. index = indices[ i ] * itemSize;
  1780. for ( var j = 0; j < itemSize; j ++ ) {
  1781. array2[ index2 ++ ] = array[ index ++ ];
  1782. }
  1783. }
  1784. return new THREE.Float32BufferAttribute( array2, itemSize );
  1785. }
  1786. var ab = new THREE.Vector3();
  1787. var cb = new THREE.Vector3();
  1788. function computeNormalAttribute( index, coord, creaseAngle ) {
  1789. var faces = [];
  1790. var vertexNormals = {};
  1791. // prepare face and raw vertex normals
  1792. for ( var i = 0, l = index.length; i < l; i += 3 ) {
  1793. var a = index[ i ];
  1794. var b = index[ i + 1 ];
  1795. var c = index[ i + 2 ];
  1796. var face = new Face( a, b, c );
  1797. vA.fromArray( coord, a * 3 );
  1798. vB.fromArray( coord, b * 3 );
  1799. vC.fromArray( coord, c * 3 );
  1800. cb.subVectors( vC, vB );
  1801. ab.subVectors( vA, vB );
  1802. cb.cross( ab );
  1803. cb.normalize();
  1804. face.normal.copy( cb );
  1805. if ( vertexNormals[ a ] === undefined ) vertexNormals[ a ] = [];
  1806. if ( vertexNormals[ b ] === undefined ) vertexNormals[ b ] = [];
  1807. if ( vertexNormals[ c ] === undefined ) vertexNormals[ c ] = [];
  1808. vertexNormals[ a ].push( face.normal );
  1809. vertexNormals[ b ].push( face.normal );
  1810. vertexNormals[ c ].push( face.normal );
  1811. faces.push( face );
  1812. }
  1813. // compute vertex normals and build final geometry
  1814. var normals = [];
  1815. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  1816. var face = faces[ i ];
  1817. var nA = weightedNormal( vertexNormals[ face.a ], face.normal, creaseAngle );
  1818. var nB = weightedNormal( vertexNormals[ face.b ], face.normal, creaseAngle );
  1819. var nC = weightedNormal( vertexNormals[ face.c ], face.normal, creaseAngle );
  1820. vA.fromArray( coord, face.a * 3 );
  1821. vB.fromArray( coord, face.b * 3 );
  1822. vC.fromArray( coord, face.c * 3 );
  1823. normals.push( nA.x, nA.y, nA.z );
  1824. normals.push( nB.x, nB.y, nB.z );
  1825. normals.push( nC.x, nC.y, nC.z );
  1826. }
  1827. return new THREE.Float32BufferAttribute( normals, 3 );
  1828. }
  1829. function weightedNormal( normals, vector, creaseAngle ) {
  1830. var normal = new THREE.Vector3();
  1831. if ( creaseAngle === 0 ) {
  1832. normal.copy( vector );
  1833. } else {
  1834. for ( var i = 0, l = normals.length; i < l; i ++ ) {
  1835. if ( normals[ i ].angleTo( vector ) < creaseAngle ) {
  1836. normal.add( normals[ i ] );
  1837. }
  1838. }
  1839. }
  1840. return normal.normalize();
  1841. }
  1842. function toColorArray( colors ) {
  1843. var array = [];
  1844. for ( var i = 0, l = colors.length; i < l; i += 3 ) {
  1845. array.push( new THREE.Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );
  1846. }
  1847. return array;
  1848. }
  1849. /**
  1850. * Vertically paints the faces interpolating between the
  1851. * specified colors at the specified angels. This is used for the Background
  1852. * node, but could be applied to other nodes with multiple faces as well.
  1853. *
  1854. * When used with the Background node, default is directionIsDown is true if
  1855. * interpolating the skyColor down from the Zenith. When interpolationg up from
  1856. * the Nadir i.e. interpolating the groundColor, the directionIsDown is false.
  1857. *
  1858. * The first angle is never specified, it is the Zenith (0 rad). Angles are specified
  1859. * in radians. The geometry is thought a sphere, but could be anything. The color interpolation
  1860. * is linear along the Y axis in any case.
  1861. *
  1862. * You must specify one more color than you have angles at the beginning of the colors array.
  1863. * This is the color of the Zenith (the top of the shape).
  1864. *
  1865. * @param {BufferGeometry} geometry
  1866. * @param {number} radius
  1867. * @param {array} angles
  1868. * @param {array} colors
  1869. * @param {boolean} topDown - Whether to work top down or bottom up.
  1870. */
  1871. function paintFaces( geometry, radius, angles, colors, topDown ) {
  1872. // compute threshold values
  1873. var thresholds = [];
  1874. var startAngle = ( topDown === true ) ? 0 : Math.PI;
  1875. for ( var i = 0, l = colors.length; i < l; i ++ ) {
  1876. var angle = ( i === 0 ) ? 0 : angles[ i - 1 ];
  1877. angle = ( topDown === true ) ? angle : ( startAngle - angle );
  1878. var point = new THREE.Vector3();
  1879. point.setFromSphericalCoords( radius, angle, 0 );
  1880. thresholds.push( point );
  1881. }
  1882. // generate vertex colors
  1883. var indices = geometry.index;
  1884. var positionAttribute = geometry.attributes.position;
  1885. var colorAttribute = new THREE.BufferAttribute( new Float32Array( geometry.attributes.position.count * 3 ), 3 );
  1886. var position = new THREE.Vector3();
  1887. var color = new THREE.Color();
  1888. for ( var i = 0; i < indices.count; i ++ ) {
  1889. var index = indices.getX( i );
  1890. position.fromBufferAttribute( positionAttribute, index );
  1891. var thresholdIndexA, thresholdIndexB;
  1892. var t = 1;
  1893. for ( var j = 1; j < thresholds.length; j ++ ) {
  1894. thresholdIndexA = j - 1;
  1895. thresholdIndexB = j;
  1896. var thresholdA = thresholds[ thresholdIndexA ];
  1897. var thresholdB = thresholds[ thresholdIndexB ];
  1898. if ( topDown === true ) {
  1899. // interpolation for sky color
  1900. if ( position.y <= thresholdA.y && position.y > thresholdB.y ) {
  1901. t = Math.abs( thresholdA.y - position.y ) / Math.abs( thresholdA.y - thresholdB.y );
  1902. break;
  1903. }
  1904. } else {
  1905. // interpolation for ground color
  1906. if ( position.y >= thresholdA.y && position.y < thresholdB.y ) {
  1907. t = Math.abs( thresholdA.y - position.y ) / Math.abs( thresholdA.y - thresholdB.y );
  1908. break;
  1909. }
  1910. }
  1911. }
  1912. var colorA = colors[ thresholdIndexA ];
  1913. var colorB = colors[ thresholdIndexB ];
  1914. color.copy( colorA ).lerp( colorB, t );
  1915. colorAttribute.setXYZ( index, color.r, color.g, color.b );
  1916. }
  1917. geometry.setAttribute( 'color', colorAttribute );
  1918. }
  1919. //
  1920. var textureLoader = new THREE.TextureLoader( this.manager );
  1921. textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
  1922. // check version (only 2.0 is supported)
  1923. if ( data.indexOf( '#VRML V2.0' ) === - 1 ) {
  1924. throw Error( 'THREE.VRMLLexer: Version of VRML asset not supported.' );
  1925. }
  1926. // create JSON representing the tree structure of the VRML asset
  1927. var tree = generateVRMLTree( data );
  1928. // parse the tree structure to a three.js scene
  1929. var scene = parseTree( tree );
  1930. return scene;
  1931. }
  1932. } );
  1933. function VRMLLexer( tokens ) {
  1934. this.lexer = new chevrotain.Lexer( tokens );
  1935. }
  1936. VRMLLexer.prototype = {
  1937. constructor: VRMLLexer,
  1938. lex: function ( inputText ) {
  1939. var lexingResult = this.lexer.tokenize( inputText );
  1940. if ( lexingResult.errors.length > 0 ) {
  1941. console.error( lexingResult.errors );
  1942. throw Error( 'THREE.VRMLLexer: Lexing errors detected.' );
  1943. }
  1944. return lexingResult;
  1945. }
  1946. };
  1947. function VRMLParser( tokenVocabulary ) {
  1948. chevrotain.Parser.call( this, tokenVocabulary );
  1949. var $ = this;
  1950. var Version = tokenVocabulary[ 'Version' ];
  1951. var LCurly = tokenVocabulary[ 'LCurly' ];
  1952. var RCurly = tokenVocabulary[ 'RCurly' ];
  1953. var LSquare = tokenVocabulary[ 'LSquare' ];
  1954. var RSquare = tokenVocabulary[ 'RSquare' ];
  1955. var Identifier = tokenVocabulary[ 'Identifier' ];
  1956. var RouteIdentifier = tokenVocabulary[ 'RouteIdentifier' ];
  1957. var StringLiteral = tokenVocabulary[ 'StringLiteral' ];
  1958. var HexLiteral = tokenVocabulary[ 'HexLiteral' ];
  1959. var NumberLiteral = tokenVocabulary[ 'NumberLiteral' ];
  1960. var TrueLiteral = tokenVocabulary[ 'TrueLiteral' ];
  1961. var FalseLiteral = tokenVocabulary[ 'FalseLiteral' ];
  1962. var NullLiteral = tokenVocabulary[ 'NullLiteral' ];
  1963. var DEF = tokenVocabulary[ 'DEF' ];
  1964. var USE = tokenVocabulary[ 'USE' ];
  1965. var ROUTE = tokenVocabulary[ 'ROUTE' ];
  1966. var TO = tokenVocabulary[ 'TO' ];
  1967. var NodeName = tokenVocabulary[ 'NodeName' ];
  1968. $.RULE( 'vrml', function () {
  1969. $.SUBRULE( $.version );
  1970. $.AT_LEAST_ONE( function () {
  1971. $.SUBRULE( $.node );
  1972. } );
  1973. $.MANY( function () {
  1974. $.SUBRULE( $.route );
  1975. } );
  1976. } );
  1977. $.RULE( 'version', function () {
  1978. $.CONSUME( Version );
  1979. } );
  1980. $.RULE( 'node', function () {
  1981. $.OPTION( function () {
  1982. $.SUBRULE( $.def );
  1983. } );
  1984. $.CONSUME( NodeName );
  1985. $.CONSUME( LCurly );
  1986. $.MANY( function () {
  1987. $.SUBRULE( $.field );
  1988. } );
  1989. $.CONSUME( RCurly );
  1990. } );
  1991. $.RULE( 'field', function () {
  1992. $.CONSUME( Identifier );
  1993. $.OR2( [
  1994. { ALT: function () {
  1995. $.SUBRULE( $.singleFieldValue );
  1996. } },
  1997. { ALT: function () {
  1998. $.SUBRULE( $.multiFieldValue );
  1999. } }
  2000. ] );
  2001. } );
  2002. $.RULE( 'def', function () {
  2003. $.CONSUME( DEF );
  2004. $.OR( [
  2005. { ALT: function () {
  2006. $.CONSUME( Identifier );
  2007. } },
  2008. { ALT: function () {
  2009. $.CONSUME( NodeName );
  2010. } }
  2011. ] );
  2012. } );
  2013. $.RULE( 'use', function () {
  2014. $.CONSUME( USE );
  2015. $.OR( [
  2016. { ALT: function () {
  2017. $.CONSUME( Identifier );
  2018. } },
  2019. { ALT: function () {
  2020. $.CONSUME( NodeName );
  2021. } }
  2022. ] );
  2023. } );
  2024. $.RULE( 'singleFieldValue', function () {
  2025. $.AT_LEAST_ONE( function () {
  2026. $.OR( [
  2027. { ALT: function () {
  2028. $.SUBRULE( $.node );
  2029. } },
  2030. { ALT: function () {
  2031. $.SUBRULE( $.use );
  2032. } },
  2033. { ALT: function () {
  2034. $.CONSUME( StringLiteral );
  2035. } },
  2036. { ALT: function () {
  2037. $.CONSUME( HexLiteral );
  2038. } },
  2039. { ALT: function () {
  2040. $.CONSUME( NumberLiteral );
  2041. } },
  2042. { ALT: function () {
  2043. $.CONSUME( TrueLiteral );
  2044. } },
  2045. { ALT: function () {
  2046. $.CONSUME( FalseLiteral );
  2047. } },
  2048. { ALT: function () {
  2049. $.CONSUME( NullLiteral );
  2050. } }
  2051. ] );
  2052. } );
  2053. } );
  2054. $.RULE( 'multiFieldValue', function () {
  2055. $.CONSUME( LSquare );
  2056. $.MANY( function () {
  2057. $.OR( [
  2058. { ALT: function () {
  2059. $.SUBRULE( $.node );
  2060. } },
  2061. { ALT: function () {
  2062. $.SUBRULE( $.use );
  2063. } },
  2064. { ALT: function () {
  2065. $.CONSUME( StringLiteral );
  2066. } },
  2067. { ALT: function () {
  2068. $.CONSUME( HexLiteral );
  2069. } },
  2070. { ALT: function () {
  2071. $.CONSUME( NumberLiteral );
  2072. } },
  2073. { ALT: function () {
  2074. $.CONSUME( NullLiteral );
  2075. } }
  2076. ] );
  2077. } );
  2078. $.CONSUME( RSquare );
  2079. } );
  2080. $.RULE( 'route', function () {
  2081. $.CONSUME( ROUTE );
  2082. $.CONSUME( RouteIdentifier );
  2083. $.CONSUME( TO );
  2084. $.CONSUME2( RouteIdentifier );
  2085. } );
  2086. this.performSelfAnalysis();
  2087. }
  2088. VRMLParser.prototype = Object.create( chevrotain.Parser.prototype );
  2089. VRMLParser.prototype.constructor = VRMLParser;
  2090. function Face( a, b, c ) {
  2091. this.a = a;
  2092. this.b = b;
  2093. this.c = c;
  2094. this.normal = new THREE.Vector3();
  2095. }
  2096. var TEXTURE_TYPE = {
  2097. INTENSITY: 1,
  2098. INTENSITY_ALPHA: 2,
  2099. RGB: 3,
  2100. RGBA: 4
  2101. };
  2102. return VRMLLoader;
  2103. } )();