VRMLLoader.js 73 KB

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