VRMLLoader.js 73 KB

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