VRMLLoader.js 73 KB

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