VRMLLoader.js 72 KB

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