VRMLLoader.js 72 KB

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