VRMLLoader.js 71 KB

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