VRMLLoader.js 71 KB

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