VRMLLoader.js 72 KB

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