VRMLLoader.js 73 KB

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