VRMLLoader.js 71 KB

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