VRMLLoader.js 72 KB

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