VRMLLoader.js 72 KB

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