VRMLLoader.js 73 KB

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