VRMLLoader.js 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808
  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[ 0 ].image;
  215. },
  216. use: function ( ctx ) {
  217. return { USE: ctx.Identifier[ 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. }
  312. return scene;
  313. }
  314. function buildNodeMap( node ) {
  315. if ( node.DEF ) {
  316. nodeMap[ node.DEF ] = node;
  317. }
  318. var fields = node.fields;
  319. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  320. var field = fields[ i ];
  321. if ( field.type === 'node' ) {
  322. var fieldValues = field.values;
  323. for ( var j = 0, jl = fieldValues.length; j < jl; j ++ ) {
  324. buildNodeMap( fieldValues[ j ] );
  325. }
  326. }
  327. }
  328. }
  329. function getNode( node ) {
  330. // handle case where a node refers to a different one
  331. if ( node.USE ) {
  332. return resolveUSE( node.USE );
  333. }
  334. if ( node.build !== undefined ) return node.build;
  335. node.build = buildNode( node );
  336. return node.build;
  337. }
  338. // node builder
  339. function buildNode( node ) {
  340. var nodeName = node.name;
  341. var build;
  342. switch ( nodeName ) {
  343. case 'Group':
  344. case 'Transform':
  345. build = buildGroupingNode( node );
  346. break;
  347. case 'Background':
  348. build = buildBackgroundNode( node );
  349. break;
  350. case 'Shape':
  351. build = buildShapeNode( node );
  352. break;
  353. case 'Appearance':
  354. build = buildAppearanceNode( node );
  355. break;
  356. case 'Material':
  357. build = buildMaterialNode( node );
  358. break;
  359. case 'ImageTexture':
  360. build = buildImageTextureNode( node );
  361. break;
  362. case 'PixelTexture':
  363. build = buildPixelTextureNode( node );
  364. break;
  365. case 'TextureTransform':
  366. build = buildTextureTransformNode( node );
  367. break;
  368. case 'IndexedFaceSet':
  369. build = buildIndexedFaceSetNode( node );
  370. break;
  371. case 'IndexedLineSet':
  372. build = buildIndexedLineSetNode( node );
  373. break;
  374. case 'PointSet':
  375. build = buildPointSetNode( node );
  376. break;
  377. case 'Box':
  378. build = buildBoxNode( node );
  379. break;
  380. case 'Cone':
  381. build = buildConeNode( node );
  382. break;
  383. case 'Cylinder':
  384. build = buildCylinderNode( node );
  385. break;
  386. case 'Sphere':
  387. build = buildSphereNode( node );
  388. break;
  389. case 'Color':
  390. case 'Coordinate':
  391. case 'Normal':
  392. case 'TextureCoordinate':
  393. build = buildGeometricNode( node );
  394. break;
  395. case 'Anchor':
  396. case 'Billboard':
  397. case 'Collision':
  398. case 'Inline':
  399. case 'LOD':
  400. case 'Switch':
  401. case 'AudioClip':
  402. case 'DirectionalLight':
  403. case 'PointLight':
  404. case 'Script':
  405. case 'Sound':
  406. case 'SpotLight':
  407. case 'WorldInfo':
  408. case 'CylinderSensor':
  409. case 'PlaneSensor':
  410. case 'ProximitySensor':
  411. case 'SphereSensor':
  412. case 'TimeSensor':
  413. case 'TouchSensor':
  414. case 'VisibilitySensor':
  415. case 'ElevationGrid':
  416. case 'Extrusion':
  417. case 'Text':
  418. case 'FontStyle':
  419. case 'MovieTexture':
  420. case 'ColorInterpolator':
  421. case 'CoordinateInterpolator':
  422. case 'NormalInterpolator':
  423. case 'OrientationInterpolator':
  424. case 'PositionInterpolator':
  425. case 'ScalarInterpolator':
  426. case 'Fog':
  427. case 'NavigationInfo':
  428. case 'Viewpoint':
  429. // node not supported yet
  430. break;
  431. default:
  432. console.warn( 'THREE.VRMLLoader: Unknown node:', nodeName );
  433. break;
  434. }
  435. return build;
  436. }
  437. function buildGroupingNode( node ) {
  438. var object = new THREE.Group();
  439. //
  440. var fields = node.fields;
  441. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  442. var field = fields[ i ];
  443. var fieldName = field.name;
  444. var fieldValues = field.values;
  445. switch ( fieldName ) {
  446. case 'center':
  447. // field not supported
  448. break;
  449. case 'children':
  450. parseFieldChildren( fieldValues, object );
  451. break;
  452. case 'rotation':
  453. var axis = new THREE.Vector3( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  454. var angle = fieldValues[ 3 ];
  455. object.quaternion.setFromAxisAngle( axis, angle );
  456. break;
  457. case 'scale':
  458. object.scale.set( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  459. break;
  460. case 'scaleOrientation':
  461. // field not supported
  462. break;
  463. case 'translation':
  464. object.position.set( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  465. break;
  466. case 'bboxCenter':
  467. // field not supported
  468. break;
  469. case 'bboxSize':
  470. // field not supported
  471. break;
  472. default:
  473. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  474. break;
  475. }
  476. }
  477. return object;
  478. }
  479. function buildBackgroundNode( node ) {
  480. var group = new THREE.Group();
  481. var groundAngle, groundColor;
  482. var skyAngle, skyColor;
  483. var fields = node.fields;
  484. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  485. var field = fields[ i ];
  486. var fieldName = field.name;
  487. var fieldValues = field.values;
  488. switch ( fieldName ) {
  489. case 'groundAngle':
  490. groundAngle = fieldValues;
  491. break;
  492. case 'groundColor':
  493. groundColor = fieldValues;
  494. break;
  495. case 'backUrl':
  496. // field not supported
  497. break;
  498. case 'bottomUrl':
  499. // field not supported
  500. break;
  501. case 'frontUrl':
  502. // field not supported
  503. break;
  504. case 'leftUrl':
  505. // field not supported
  506. break;
  507. case 'rightUrl':
  508. // field not supported
  509. break;
  510. case 'topUrl':
  511. // field not supported
  512. break;
  513. case 'skyAngle':
  514. skyAngle = fieldValues;
  515. break;
  516. case 'skyColor':
  517. skyColor = fieldValues;
  518. break;
  519. default:
  520. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  521. break;
  522. }
  523. }
  524. // sky
  525. if ( skyColor ) {
  526. var radius = 10000;
  527. var skyGeometry = new THREE.SphereBufferGeometry( radius, 32, 16 );
  528. var skyMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide, depthWrite: false, depthTest: false } );
  529. if ( skyColor.length > 3 ) {
  530. paintFaces( skyGeometry, radius, skyAngle, toColorArray( skyColor ), true );
  531. skyMaterial.vertexColors = THREE.VertexColors;
  532. } else {
  533. skyMaterial.color.setRGB( skyColor[ 0 ], skyColor[ 1 ], skyColor[ 2 ] );
  534. }
  535. var sky = new THREE.Mesh( skyGeometry, skyMaterial );
  536. group.add( sky );
  537. }
  538. // ground
  539. if ( groundColor ) {
  540. if ( groundColor.length > 0 ) {
  541. var groundGeometry = new THREE.SphereBufferGeometry( radius, 32, 16, 0, 2 * Math.PI, 0.5 * Math.PI, 1.5 * Math.PI );
  542. var groundMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide, vertexColors: THREE.VertexColors, depthWrite: false, depthTest: false } );
  543. paintFaces( groundGeometry, radius, groundAngle, toColorArray( groundColor ), false );
  544. var ground = new THREE.Mesh( groundGeometry, groundMaterial );
  545. group.add( ground );
  546. }
  547. }
  548. // render background group first
  549. group.renderOrder = - Infinity;
  550. return group;
  551. }
  552. function buildShapeNode( node ) {
  553. var fields = node.fields;
  554. // if the appearance field is NULL or unspecified, lighting is off and the unlit object color is (0, 0, 0)
  555. var material = new THREE.MeshBasicMaterial( { color: 0x000000 } );
  556. var geometry;
  557. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  558. var field = fields[ i ];
  559. var fieldName = field.name;
  560. var fieldValues = field.values;
  561. switch ( fieldName ) {
  562. case 'appearance':
  563. if ( fieldValues[ 0 ] !== null ) {
  564. material = getNode( fieldValues[ 0 ] );
  565. }
  566. break;
  567. case 'geometry':
  568. if ( fieldValues[ 0 ] !== null ) {
  569. geometry = getNode( fieldValues[ 0 ] );
  570. }
  571. break;
  572. default:
  573. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  574. break;
  575. }
  576. }
  577. // build 3D object
  578. var object;
  579. if ( geometry && geometry.attributes.position ) {
  580. var type = geometry._type;
  581. if ( type === 'points' ) { // points
  582. var pointsMaterial = new THREE.PointsMaterial( { color: 0xffffff } );
  583. if ( geometry.attributes.color !== undefined ) {
  584. pointsMaterial.vertexColors = THREE.VertexColors;
  585. } else {
  586. // 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
  587. if ( material.isMeshPhongMaterial ) {
  588. pointsMaterial.color.copy( material.emissive );
  589. }
  590. }
  591. object = new THREE.Points( geometry, pointsMaterial );
  592. } else if ( type === 'line' ) { // lines
  593. var lineMaterial = new THREE.LineBasicMaterial( { color: 0xffffff } );
  594. if ( geometry.attributes.color !== undefined ) {
  595. lineMaterial.vertexColors = THREE.VertexColors;
  596. } else {
  597. // 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
  598. if ( material.isMeshPhongMaterial ) {
  599. lineMaterial.color.copy( material.emissive );
  600. }
  601. }
  602. object = new THREE.LineSegments( geometry, lineMaterial );
  603. } else { // consider meshes
  604. // check "solid" hint (it's placed in the geometry but affects the material)
  605. if ( geometry._solid !== undefined ) {
  606. material.side = ( geometry._solid ) ? THREE.FrontSide : THREE.DoubleSide;
  607. }
  608. // check for vertex colors
  609. if ( geometry.attributes.color !== undefined ) {
  610. material.vertexColors = THREE.VertexColors;
  611. }
  612. object = new THREE.Mesh( geometry, material );
  613. }
  614. } else {
  615. object = new THREE.Object3D();
  616. // if the geometry field is NULL or no vertices are defined the object is not drawn
  617. object.visible = false;
  618. }
  619. return object;
  620. }
  621. function buildAppearanceNode( node ) {
  622. var material = new THREE.MeshPhongMaterial();
  623. var transformData;
  624. var fields = node.fields;
  625. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  626. var field = fields[ i ];
  627. var fieldName = field.name;
  628. var fieldValues = field.values;
  629. switch ( fieldName ) {
  630. case 'material':
  631. if ( fieldValues[ 0 ] !== null ) {
  632. var materialData = getNode( fieldValues[ 0 ] );
  633. if ( materialData.diffuseColor ) material.color.copy( materialData.diffuseColor );
  634. if ( materialData.emissiveColor ) material.emissive.copy( materialData.emissiveColor );
  635. if ( materialData.shininess ) material.shininess = materialData.shininess;
  636. if ( materialData.specularColor ) material.specular.copy( materialData.specularColor );
  637. if ( materialData.transparency ) material.opacity = 1 - materialData.transparency;
  638. if ( materialData.transparency > 0 ) material.transparent = true;
  639. } else {
  640. // if the material field is NULL or unspecified, lighting is off and the unlit object color is (0, 0, 0)
  641. material = new THREE.MeshBasicMaterial( { color: 0x000000 } );
  642. }
  643. break;
  644. case 'texture':
  645. var textureNode = fieldValues[ 0 ];
  646. if ( textureNode !== null ) {
  647. if ( textureNode.name === 'ImageTexture' || textureNode.name === 'PixelTexture' ) {
  648. material.map = getNode( textureNode );
  649. } else {
  650. // MovieTexture not supported yet
  651. }
  652. }
  653. break;
  654. case 'textureTransform':
  655. if ( fieldValues[ 0 ] !== null ) {
  656. transformData = getNode( fieldValues[ 0 ] );
  657. }
  658. break;
  659. default:
  660. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  661. break;
  662. }
  663. }
  664. // only apply texture transform data if a texture was defined
  665. if ( material.map ) {
  666. // respect VRML lighting model
  667. if ( material.map.__type ) {
  668. switch ( material.map.__type ) {
  669. case TEXTURE_TYPE.INTENSITY_ALPHA:
  670. material.opacity = 1; // ignore transparency
  671. break;
  672. case TEXTURE_TYPE.RGB:
  673. material.color.set( 0xffffff ); // ignore material color
  674. break;
  675. case TEXTURE_TYPE.RGBA:
  676. material.color.set( 0xffffff ); // ignore material color
  677. material.opacity = 1; // ignore transparency
  678. break;
  679. default:
  680. }
  681. delete material.map.__type;
  682. }
  683. // apply texture transform
  684. if ( transformData ) {
  685. material.map.center.copy( transformData.center );
  686. material.map.rotation = transformData.rotation;
  687. material.map.repeat.copy( transformData.scale );
  688. material.map.offset.copy( transformData.translation );
  689. }
  690. }
  691. return material;
  692. }
  693. function buildMaterialNode( node ) {
  694. var materialData = {};
  695. var fields = node.fields;
  696. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  697. var field = fields[ i ];
  698. var fieldName = field.name;
  699. var fieldValues = field.values;
  700. switch ( fieldName ) {
  701. case 'ambientIntensity':
  702. // field not supported
  703. break;
  704. case 'diffuseColor':
  705. materialData.diffuseColor = new THREE.Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  706. break;
  707. case 'emissiveColor':
  708. materialData.emissiveColor = new THREE.Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  709. break;
  710. case 'shininess':
  711. materialData.shininess = fieldValues[ 0 ];
  712. break;
  713. case 'specularColor':
  714. materialData.emissiveColor = new THREE.Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  715. break;
  716. case 'transparency':
  717. materialData.transparency = fieldValues[ 0 ];
  718. break;
  719. default:
  720. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  721. break;
  722. }
  723. }
  724. return materialData;
  725. }
  726. function parseHexColor( hex, textureType, color ) {
  727. switch ( textureType ) {
  728. case TEXTURE_TYPE.INTENSITY:
  729. // Intensity texture: A one-component image specifies one-byte hexadecimal or integer values representing the intensity of the image
  730. var value = parseInt( hex );
  731. color.r = value;
  732. color.g = value;
  733. color.b = value;
  734. break;
  735. case TEXTURE_TYPE.INTENSITY_ALPHA:
  736. // Intensity+Alpha texture: A two-component image specifies the intensity in the first (high) byte and the alpha opacity in the second (low) byte.
  737. var value = parseInt( "0x" + hex.substring( 2, 4 ) );
  738. color.r = value;
  739. color.g = value;
  740. color.b = value;
  741. color.a = parseInt( "0x" + hex.substring( 4, 6 ) );
  742. break;
  743. case TEXTURE_TYPE.RGB:
  744. // RGB texture: Pixels in a three-component image specify the red component in the first (high) byte, followed by the green and blue components
  745. color.r = parseInt( "0x" + hex.substring( 2, 4 ) );
  746. color.g = parseInt( "0x" + hex.substring( 4, 6 ) );
  747. color.b = parseInt( "0x" + hex.substring( 6, 8 ) );
  748. break;
  749. case TEXTURE_TYPE.RGBA:
  750. // RGBA texture: Four-component images specify the alpha opacity byte after red/green/blue
  751. color.r = parseInt( "0x" + hex.substring( 2, 4 ) );
  752. color.g = parseInt( "0x" + hex.substring( 4, 6 ) );
  753. color.b = parseInt( "0x" + hex.substring( 6, 8 ) );
  754. color.a = parseInt( "0x" + hex.substring( 8, 10 ) );
  755. break;
  756. default:
  757. }
  758. }
  759. function getTextureType( num_components ) {
  760. var type;
  761. switch ( num_components ) {
  762. case 1:
  763. type = TEXTURE_TYPE.INTENSITY;
  764. break;
  765. case 2:
  766. type = TEXTURE_TYPE.INTENSITY_ALPHA;
  767. break;
  768. case 3:
  769. type = TEXTURE_TYPE.RGB;
  770. break;
  771. case 4:
  772. type = TEXTURE_TYPE.RGBA;
  773. break;
  774. default:
  775. }
  776. return type;
  777. }
  778. function buildPixelTextureNode( node ) {
  779. var texture;
  780. var wrapS = THREE.RepeatWrapping;
  781. var wrapT = THREE.RepeatWrapping;
  782. var fields = node.fields;
  783. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  784. var field = fields[ i ];
  785. var fieldName = field.name;
  786. var fieldValues = field.values;
  787. switch ( fieldName ) {
  788. case 'image':
  789. var width = fieldValues[ 0 ];
  790. var height = fieldValues[ 1 ];
  791. var num_components = fieldValues[ 2 ];
  792. var useAlpha = ( num_components === 2 || num_components === 4 );
  793. var textureType = getTextureType( num_components );
  794. var size = ( ( useAlpha === true ) ? 4 : 3 ) * ( width * height );
  795. var data = new Uint8Array( size );
  796. var color = { r: 0, g: 0, b: 0, a: 0 };
  797. for ( var j = 3, k = 0, jl = fieldValues.length; j < jl; j ++, k ++ ) {
  798. parseHexColor( fieldValues[ j ], textureType, color );
  799. if ( useAlpha === true ) {
  800. var stride = k * 4;
  801. data[ stride + 0 ] = color.r;
  802. data[ stride + 1 ] = color.g;
  803. data[ stride + 2 ] = color.b;
  804. data[ stride + 3 ] = color.a;
  805. } else {
  806. var stride = k * 3;
  807. data[ stride + 0 ] = color.r;
  808. data[ stride + 1 ] = color.g;
  809. data[ stride + 2 ] = color.b;
  810. }
  811. }
  812. texture = new THREE.DataTexture( data, width, height, ( useAlpha === true ) ? THREE.RGBAFormat : THREE.RGBFormat );
  813. texture.__type = textureType; // needed for material modifications
  814. break;
  815. case 'repeatS':
  816. if ( fieldValues[ 0 ] === false ) wrapS = THREE.ClampToEdgeWrapping;
  817. break;
  818. case 'repeatT':
  819. if ( fieldValues[ 0 ] === false ) wrapT = THREE.ClampToEdgeWrapping;
  820. break;
  821. default:
  822. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  823. break;
  824. }
  825. }
  826. if ( texture ) {
  827. texture.wrapS = wrapS;
  828. texture.wrapT = wrapT;
  829. }
  830. return texture;
  831. }
  832. function buildImageTextureNode( node ) {
  833. var texture;
  834. var wrapS = THREE.RepeatWrapping;
  835. var wrapT = THREE.RepeatWrapping;
  836. var fields = node.fields;
  837. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  838. var field = fields[ i ];
  839. var fieldName = field.name;
  840. var fieldValues = field.values;
  841. switch ( fieldName ) {
  842. case 'url':
  843. var url = fieldValues[ 0 ];
  844. if ( url ) texture = textureLoader.load( url );
  845. break;
  846. case 'repeatS':
  847. if ( fieldValues[ 0 ] === false ) wrapS = THREE.ClampToEdgeWrapping;
  848. break;
  849. case 'repeatT':
  850. if ( fieldValues[ 0 ] === false ) wrapT = THREE.ClampToEdgeWrapping;
  851. break;
  852. default:
  853. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  854. break;
  855. }
  856. }
  857. if ( texture ) {
  858. texture.wrapS = wrapS;
  859. texture.wrapT = wrapT;
  860. }
  861. return texture;
  862. }
  863. function buildTextureTransformNode( node ) {
  864. var transformData = {
  865. center: new THREE.Vector2(),
  866. rotation: new THREE.Vector2(),
  867. scale: new THREE.Vector2(),
  868. translation: new THREE.Vector2()
  869. };
  870. var fields = node.fields;
  871. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  872. var field = fields[ i ];
  873. var fieldName = field.name;
  874. var fieldValues = field.values;
  875. switch ( fieldName ) {
  876. case 'center':
  877. transformData.center.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  878. break;
  879. case 'rotation':
  880. transformData.rotation = fieldValues[ 0 ];
  881. break;
  882. case 'scale':
  883. transformData.scale.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  884. break;
  885. case 'translation':
  886. transformData.translation.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  887. break;
  888. default:
  889. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  890. break;
  891. }
  892. }
  893. return transformData;
  894. }
  895. function buildGeometricNode( node ) {
  896. return node.fields[ 0 ].values;
  897. }
  898. function buildIndexedFaceSetNode( node ) {
  899. var color, coord, normal, texCoord;
  900. var ccw = true, solid = true, creaseAngle = 0;
  901. var colorIndex, coordIndex, normalIndex, texCoordIndex;
  902. var colorPerVertex = true, normalPerVertex = true;
  903. var fields = node.fields;
  904. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  905. var field = fields[ i ];
  906. var fieldName = field.name;
  907. var fieldValues = field.values;
  908. switch ( fieldName ) {
  909. case 'color':
  910. var colorNode = fieldValues[ 0 ];
  911. if ( colorNode !== null ) {
  912. color = getNode( colorNode );
  913. }
  914. break;
  915. case 'coord':
  916. var coordNode = fieldValues[ 0 ];
  917. if ( coordNode !== null ) {
  918. coord = getNode( coordNode );
  919. }
  920. break;
  921. case 'normal':
  922. var normalNode = fieldValues[ 0 ];
  923. if ( normalNode !== null ) {
  924. normal = getNode( normalNode );
  925. }
  926. break;
  927. case 'texCoord':
  928. var texCoordNode = fieldValues[ 0 ];
  929. if ( texCoordNode !== null ) {
  930. texCoord = getNode( texCoordNode );
  931. }
  932. break;
  933. case 'ccw':
  934. ccw = fieldValues[ 0 ];
  935. break;
  936. case 'colorIndex':
  937. colorIndex = fieldValues;
  938. break;
  939. case 'colorPerVertex':
  940. colorPerVertex = fieldValues[ 0 ];
  941. break;
  942. case 'convex':
  943. // field not supported
  944. break;
  945. case 'coordIndex':
  946. coordIndex = fieldValues;
  947. break;
  948. case 'creaseAngle':
  949. creaseAngle = fieldValues[ 0 ];
  950. break;
  951. case 'normalIndex':
  952. normalIndex = fieldValues;
  953. break;
  954. case 'normalPerVertex':
  955. normalPerVertex = fieldValues[ 0 ];
  956. break;
  957. case 'solid':
  958. solid = fieldValues[ 0 ];
  959. break;
  960. case 'texCoordIndex':
  961. texCoordIndex = fieldValues;
  962. break;
  963. default:
  964. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  965. break;
  966. }
  967. }
  968. if ( coordIndex === undefined ) {
  969. console.warn( 'THREE.VRMLLoader: Missing coordIndex.' );
  970. return new THREE.BufferGeometry(); // handle VRML files with incomplete geometry definition
  971. }
  972. var triangulatedCoordIndex = triangulateFaceIndex( coordIndex, ccw );
  973. var positionAttribute;
  974. var colorAttribute;
  975. var normalAttribute;
  976. var uvAttribute;
  977. if ( color ) {
  978. if ( colorPerVertex === true ) {
  979. if ( colorIndex && colorIndex.length > 0 ) {
  980. // if the colorIndex field is not empty, then it is used to choose colors for each vertex of the IndexedFaceSet.
  981. var triangulatedColorIndex = triangulateFaceIndex( colorIndex, ccw );
  982. colorAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedColorIndex, color, 3 );
  983. } else {
  984. // if the colorIndex field is empty, then the coordIndex field is used to choose colors from the Color node
  985. colorAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new THREE.Float32BufferAttribute( color, 3 ) );
  986. }
  987. } else {
  988. if ( colorIndex && colorIndex.length > 0 ) {
  989. // if the colorIndex field is not empty, then they are used to choose one color for each face of the IndexedFaceSet
  990. var flattenFaceColors = flattenData( color, colorIndex );
  991. var triangulatedFaceColors = triangulateFaceData( flattenFaceColors, coordIndex );
  992. colorAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceColors );
  993. } else {
  994. // if the colorIndex field is empty, then the color are applied to each face of the IndexedFaceSet in order
  995. var triangulatedFaceColors = triangulateFaceData( color, coordIndex );
  996. colorAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceColors );
  997. }
  998. }
  999. }
  1000. if ( normal ) {
  1001. if ( normalPerVertex === true ) {
  1002. // consider vertex normals
  1003. if ( normalIndex && normalIndex.length > 0 ) {
  1004. // if the normalIndex field is not empty, then it is used to choose normals for each vertex of the IndexedFaceSet.
  1005. var triangulatedNormalIndex = triangulateFaceIndex( normalIndex, ccw );
  1006. normalAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedNormalIndex, normal, 3 );
  1007. } else {
  1008. // if the normalIndex field is empty, then the coordIndex field is used to choose normals from the Normal node
  1009. normalAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new THREE.Float32BufferAttribute( normal, 3 ) );
  1010. }
  1011. } else {
  1012. // consider face normals
  1013. if ( normalIndex && normalIndex.length > 0 ) {
  1014. // if the normalIndex field is not empty, then they are used to choose one normal for each face of the IndexedFaceSet
  1015. var flattenFaceNormals = flattenData( normal, normalIndex );
  1016. var triangulatedFaceNormals = triangulateFaceData( flattenFaceNormals, coordIndex );
  1017. normalAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceNormals );
  1018. } else {
  1019. // if the normalIndex field is empty, then the normals are applied to each face of the IndexedFaceSet in order
  1020. var triangulatedFaceNormals = triangulateFaceData( normal, coordIndex );
  1021. normalAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceNormals );
  1022. }
  1023. }
  1024. } else {
  1025. // 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
  1026. normalAttribute = computeNormalAttribute( triangulatedCoordIndex, coord, creaseAngle );
  1027. }
  1028. if ( texCoord ) {
  1029. // texture coordinates are always defined on vertex level
  1030. if ( texCoordIndex && texCoordIndex.length > 0 ) {
  1031. // if the texCoordIndex field is not empty, then it is used to choose texture coordinates for each vertex of the IndexedFaceSet.
  1032. var triangulatedTexCoordIndex = triangulateFaceIndex( texCoordIndex, ccw );
  1033. uvAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedTexCoordIndex, texCoord, 2 );
  1034. } else {
  1035. // if the texCoordIndex field is empty, then the coordIndex array is used to choose texture coordinates from the TextureCoordinate node
  1036. uvAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new THREE.Float32BufferAttribute( texCoord, 2 ) );
  1037. }
  1038. }
  1039. var geometry = new THREE.BufferGeometry();
  1040. positionAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new THREE.Float32BufferAttribute( coord, 3 ) );
  1041. geometry.setAttribute( 'position', positionAttribute );
  1042. geometry.setAttribute( 'normal', normalAttribute );
  1043. // optional attributes
  1044. if ( colorAttribute ) geometry.setAttribute( 'color', colorAttribute );
  1045. if ( uvAttribute ) geometry.setAttribute( 'uv', uvAttribute );
  1046. // "solid" influences the material so let's store it for later use
  1047. geometry._solid = solid;
  1048. geometry._type = 'mesh';
  1049. return geometry;
  1050. }
  1051. function buildIndexedLineSetNode( node ) {
  1052. var color, coord;
  1053. var colorIndex, coordIndex;
  1054. var colorPerVertex = true;
  1055. var fields = node.fields;
  1056. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1057. var field = fields[ i ];
  1058. var fieldName = field.name;
  1059. var fieldValues = field.values;
  1060. switch ( fieldName ) {
  1061. case 'color':
  1062. var colorNode = fieldValues[ 0 ];
  1063. if ( colorNode !== null ) {
  1064. color = getNode( colorNode );
  1065. }
  1066. break;
  1067. case 'coord':
  1068. var coordNode = fieldValues[ 0 ];
  1069. if ( coordNode !== null ) {
  1070. coord = getNode( coordNode );
  1071. }
  1072. break;
  1073. case 'colorIndex':
  1074. colorIndex = fieldValues;
  1075. break;
  1076. case 'colorPerVertex':
  1077. colorPerVertex = fieldValues[ 0 ];
  1078. break;
  1079. case 'coordIndex':
  1080. coordIndex = fieldValues;
  1081. break;
  1082. default:
  1083. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1084. break;
  1085. }
  1086. }
  1087. // build lines
  1088. var colorAttribute;
  1089. var expandedLineIndex = expandLineIndex( coordIndex ); // create an index for three.js's linesegment primitive
  1090. if ( color ) {
  1091. if ( colorPerVertex === true ) {
  1092. if ( colorIndex.length > 0 ) {
  1093. // if the colorIndex field is not empty, then one color is used for each polyline of the IndexedLineSet.
  1094. var expandedColorIndex = expandLineIndex( colorIndex ); // compute colors for each line segment (rendering primitve)
  1095. colorAttribute = computeAttributeFromIndexedData( expandedLineIndex, expandedColorIndex, color, 3 ); // compute data on vertex level
  1096. } else {
  1097. // if the colorIndex field is empty, then the colors are applied to each polyline of the IndexedLineSet in order.
  1098. colorAttribute = toNonIndexedAttribute( expandedLineIndex, new THREE.Float32BufferAttribute( color, 3 ) );
  1099. }
  1100. } else {
  1101. if ( colorIndex.length > 0 ) {
  1102. // if the colorIndex field is not empty, then colors are applied to each vertex of the IndexedLineSet
  1103. var flattenLineColors = flattenData( color, colorIndex ); // compute colors for each VRML primitve
  1104. var expandedLineColors = expandLineData( flattenLineColors, coordIndex ); // compute colors for each line segment (rendering primitve)
  1105. colorAttribute = computeAttributeFromLineData( expandedLineIndex, expandedLineColors ); // compute data on vertex level
  1106. } else {
  1107. // if the colorIndex field is empty, then the coordIndex field is used to choose colors from the Color node
  1108. var expandedLineColors = expandLineData( color, coordIndex ); // compute colors for each line segment (rendering primitve)
  1109. colorAttribute = computeAttributeFromLineData( expandedLineIndex, expandedLineColors ); // compute data on vertex level
  1110. }
  1111. }
  1112. }
  1113. //
  1114. var geometry = new THREE.BufferGeometry();
  1115. var positionAttribute = toNonIndexedAttribute( expandedLineIndex, new THREE.Float32BufferAttribute( coord, 3 ) );
  1116. geometry.setAttribute( 'position', positionAttribute );
  1117. if ( colorAttribute ) geometry.setAttribute( 'color', colorAttribute );
  1118. geometry._type = 'line';
  1119. return geometry;
  1120. }
  1121. function buildPointSetNode( node ) {
  1122. var geometry;
  1123. var color, coord;
  1124. var fields = node.fields;
  1125. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1126. var field = fields[ i ];
  1127. var fieldName = field.name;
  1128. var fieldValues = field.values;
  1129. switch ( fieldName ) {
  1130. case 'color':
  1131. var colorNode = fieldValues[ 0 ];
  1132. if ( colorNode !== null ) {
  1133. color = getNode( colorNode );
  1134. }
  1135. break;
  1136. case 'coord':
  1137. var coordNode = fieldValues[ 0 ];
  1138. if ( coordNode !== null ) {
  1139. coord = getNode( coordNode );
  1140. }
  1141. break;
  1142. default:
  1143. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1144. break;
  1145. }
  1146. }
  1147. var geometry = new THREE.BufferGeometry();
  1148. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( coord, 3 ) );
  1149. if ( color ) geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( color, 3 ) );
  1150. geometry._type = 'points';
  1151. return geometry;
  1152. }
  1153. function buildBoxNode( node ) {
  1154. var size = new THREE.Vector3( 2, 2, 2 );
  1155. var fields = node.fields;
  1156. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1157. var field = fields[ i ];
  1158. var fieldName = field.name;
  1159. var fieldValues = field.values;
  1160. switch ( fieldName ) {
  1161. case 'size':
  1162. size.x = fieldValues[ 0 ];
  1163. size.y = fieldValues[ 1 ];
  1164. size.z = fieldValues[ 2 ];
  1165. break;
  1166. default:
  1167. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1168. break;
  1169. }
  1170. }
  1171. var geometry = new THREE.BoxBufferGeometry( size.x, size.y, size.z );
  1172. return geometry;
  1173. }
  1174. function buildConeNode( node ) {
  1175. var radius = 1, height = 2, openEnded = false;
  1176. var fields = node.fields;
  1177. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1178. var field = fields[ i ];
  1179. var fieldName = field.name;
  1180. var fieldValues = field.values;
  1181. switch ( fieldName ) {
  1182. case 'bottom':
  1183. openEnded = ! fieldValues[ 0 ];
  1184. break;
  1185. case 'bottomRadius':
  1186. radius = fieldValues[ 0 ];
  1187. break;
  1188. case 'height':
  1189. height = fieldValues[ 0 ];
  1190. break;
  1191. case 'side':
  1192. // field not supported
  1193. break;
  1194. default:
  1195. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1196. break;
  1197. }
  1198. }
  1199. var geometry = new THREE.ConeBufferGeometry( radius, height, 16, 1, openEnded );
  1200. return geometry;
  1201. }
  1202. function buildCylinderNode( node ) {
  1203. var radius = 1, height = 2;
  1204. var fields = node.fields;
  1205. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1206. var field = fields[ i ];
  1207. var fieldName = field.name;
  1208. var fieldValues = field.values;
  1209. switch ( fieldName ) {
  1210. case 'bottom':
  1211. // field not supported
  1212. break;
  1213. case 'radius':
  1214. radius = fieldValues[ 0 ];
  1215. break;
  1216. case 'height':
  1217. height = fieldValues[ 0 ];
  1218. break;
  1219. case 'side':
  1220. // field not supported
  1221. break;
  1222. case 'top':
  1223. // field not supported
  1224. break;
  1225. default:
  1226. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1227. break;
  1228. }
  1229. }
  1230. var geometry = new THREE.CylinderBufferGeometry( radius, radius, height, 16, 1 );
  1231. return geometry;
  1232. }
  1233. function buildSphereNode( node ) {
  1234. var radius = 1;
  1235. var fields = node.fields;
  1236. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1237. var field = fields[ i ];
  1238. var fieldName = field.name;
  1239. var fieldValues = field.values;
  1240. switch ( fieldName ) {
  1241. case 'radius':
  1242. radius = fieldValues[ 0 ];
  1243. break;
  1244. default:
  1245. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1246. break;
  1247. }
  1248. }
  1249. var geometry = new THREE.SphereBufferGeometry( radius, 16, 16 );
  1250. return geometry;
  1251. }
  1252. // helper functions
  1253. function resolveUSE( identifier ) {
  1254. var node = nodeMap[ identifier ];
  1255. var build = getNode( node );
  1256. // because the same 3D objects can have different transformations, it's necessary to clone them.
  1257. // materials can be influenced by the geometry (e.g. vertex normals). cloning is necessary to avoid
  1258. // any side effects
  1259. return ( build.isObject3D || build.isMaterial ) ? build.clone() : build;
  1260. }
  1261. function parseFieldChildren( children, owner ) {
  1262. for ( var i = 0, l = children.length; i < l; i ++ ) {
  1263. var object = getNode( children[ i ] );
  1264. if ( object instanceof THREE.Object3D ) owner.add( object );
  1265. }
  1266. }
  1267. function triangulateFaceIndex( index, ccw ) {
  1268. var indices = [];
  1269. // since face defintions can have more than three vertices, it's necessary to
  1270. // perform a simple triangulation
  1271. var start = 0;
  1272. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1273. var i1 = index[ start ];
  1274. var i2 = index[ i + ( ccw ? 1 : 2 ) ];
  1275. var i3 = index[ i + ( ccw ? 2 : 1 ) ];
  1276. indices.push( i1, i2, i3 );
  1277. // an index of -1 indicates that the current face has ended and the next one begins
  1278. if ( index[ i + 3 ] === - 1 || i + 3 >= l ) {
  1279. i += 3;
  1280. start = i + 1;
  1281. }
  1282. }
  1283. return indices;
  1284. }
  1285. function triangulateFaceData( data, index ) {
  1286. var triangulatedData = [];
  1287. var start = 0;
  1288. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1289. var stride = start * 3;
  1290. var x = data[ stride ];
  1291. var y = data[ stride + 1 ];
  1292. var z = data[ stride + 2 ];
  1293. triangulatedData.push( x, y, z );
  1294. // an index of -1 indicates that the current face has ended and the next one begins
  1295. if ( index[ i + 3 ] === - 1 || i + 3 >= l ) {
  1296. i += 3;
  1297. start ++;
  1298. }
  1299. }
  1300. return triangulatedData;
  1301. }
  1302. function flattenData( data, index ) {
  1303. var flattenData = [];
  1304. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1305. var i1 = index[ i ];
  1306. var stride = i1 * 3;
  1307. var x = data[ stride ];
  1308. var y = data[ stride + 1 ];
  1309. var z = data[ stride + 2 ];
  1310. flattenData.push( x, y, z );
  1311. }
  1312. return flattenData;
  1313. }
  1314. function expandLineIndex( index ) {
  1315. var indices = [];
  1316. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1317. var i1 = index[ i ];
  1318. var i2 = index[ i + 1 ];
  1319. indices.push( i1, i2 );
  1320. // an index of -1 indicates that the current line has ended and the next one begins
  1321. if ( index[ i + 2 ] === - 1 || i + 2 >= l ) {
  1322. i += 2;
  1323. }
  1324. }
  1325. return indices;
  1326. }
  1327. function expandLineData( data, index ) {
  1328. var triangulatedData = [];
  1329. var start = 0;
  1330. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1331. var stride = start * 3;
  1332. var x = data[ stride ];
  1333. var y = data[ stride + 1 ];
  1334. var z = data[ stride + 2 ];
  1335. triangulatedData.push( x, y, z );
  1336. // an index of -1 indicates that the current line has ended and the next one begins
  1337. if ( index[ i + 2 ] === - 1 || i + 2 >= l ) {
  1338. i += 2;
  1339. start ++;
  1340. }
  1341. }
  1342. return triangulatedData;
  1343. }
  1344. var vA = new THREE.Vector3();
  1345. var vB = new THREE.Vector3();
  1346. var vC = new THREE.Vector3();
  1347. var uvA = new THREE.Vector2();
  1348. var uvB = new THREE.Vector2();
  1349. var uvC = new THREE.Vector2();
  1350. function computeAttributeFromIndexedData( coordIndex, index, data, itemSize ) {
  1351. var array = [];
  1352. // we use the coordIndex.length as delimiter since normalIndex must contain at least as many indices
  1353. for ( var i = 0, l = coordIndex.length; i < l; i += 3 ) {
  1354. var a = index[ i ];
  1355. var b = index[ i + 1 ];
  1356. var c = index[ i + 2 ];
  1357. if ( itemSize === 2 ) {
  1358. uvA.fromArray( data, a * itemSize );
  1359. uvB.fromArray( data, b * itemSize );
  1360. uvC.fromArray( data, c * itemSize );
  1361. array.push( uvA.x, uvA.y );
  1362. array.push( uvB.x, uvB.y );
  1363. array.push( uvC.x, uvC.y );
  1364. } else {
  1365. vA.fromArray( data, a * itemSize );
  1366. vB.fromArray( data, b * itemSize );
  1367. vC.fromArray( data, c * itemSize );
  1368. array.push( vA.x, vA.y, vA.z );
  1369. array.push( vB.x, vB.y, vB.z );
  1370. array.push( vC.x, vC.y, vC.z );
  1371. }
  1372. }
  1373. return new THREE.Float32BufferAttribute( array, itemSize );
  1374. }
  1375. function computeAttributeFromFaceData( index, faceData ) {
  1376. var array = [];
  1377. for ( var i = 0, j = 0, l = index.length; i < l; i += 3, j ++ ) {
  1378. vA.fromArray( faceData, j * 3 );
  1379. array.push( vA.x, vA.y, vA.z );
  1380. array.push( vA.x, vA.y, vA.z );
  1381. array.push( vA.x, vA.y, vA.z );
  1382. }
  1383. return new THREE.Float32BufferAttribute( array, 3 );
  1384. }
  1385. function computeAttributeFromLineData( index, lineData ) {
  1386. var array = [];
  1387. for ( var i = 0, j = 0, l = index.length; i < l; i += 2, j ++ ) {
  1388. vA.fromArray( lineData, j * 3 );
  1389. array.push( vA.x, vA.y, vA.z );
  1390. array.push( vA.x, vA.y, vA.z );
  1391. }
  1392. return new THREE.Float32BufferAttribute( array, 3 );
  1393. }
  1394. function toNonIndexedAttribute( indices, attribute ) {
  1395. var array = attribute.array;
  1396. var itemSize = attribute.itemSize;
  1397. var array2 = new array.constructor( indices.length * itemSize );
  1398. var index = 0, index2 = 0;
  1399. for ( var i = 0, l = indices.length; i < l; i ++ ) {
  1400. index = indices[ i ] * itemSize;
  1401. for ( var j = 0; j < itemSize; j ++ ) {
  1402. array2[ index2 ++ ] = array[ index ++ ];
  1403. }
  1404. }
  1405. return new THREE.Float32BufferAttribute( array2, itemSize );
  1406. }
  1407. var ab = new THREE.Vector3();
  1408. var cb = new THREE.Vector3();
  1409. function computeNormalAttribute( index, coord, creaseAngle ) {
  1410. var faces = [];
  1411. var vertexNormals = {};
  1412. // prepare face and raw vertex normals
  1413. for ( var i = 0, l = index.length; i < l; i += 3 ) {
  1414. var a = index[ i ];
  1415. var b = index[ i + 1 ];
  1416. var c = index[ i + 2 ];
  1417. var face = new Face( a, b, c );
  1418. vA.fromArray( coord, a * 3 );
  1419. vB.fromArray( coord, b * 3 );
  1420. vC.fromArray( coord, c * 3 );
  1421. cb.subVectors( vC, vB );
  1422. ab.subVectors( vA, vB );
  1423. cb.cross( ab );
  1424. cb.normalize();
  1425. face.normal.copy( cb );
  1426. if ( vertexNormals[ a ] === undefined ) vertexNormals[ a ] = [];
  1427. if ( vertexNormals[ b ] === undefined ) vertexNormals[ b ] = [];
  1428. if ( vertexNormals[ c ] === undefined ) vertexNormals[ c ] = [];
  1429. vertexNormals[ a ].push( face.normal );
  1430. vertexNormals[ b ].push( face.normal );
  1431. vertexNormals[ c ].push( face.normal );
  1432. faces.push( face );
  1433. }
  1434. // compute vertex normals and build final geometry
  1435. var normals = [];
  1436. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  1437. var face = faces[ i ];
  1438. var nA = weightedNormal( vertexNormals[ face.a ], face.normal, creaseAngle );
  1439. var nB = weightedNormal( vertexNormals[ face.b ], face.normal, creaseAngle );
  1440. var nC = weightedNormal( vertexNormals[ face.c ], face.normal, creaseAngle );
  1441. vA.fromArray( coord, face.a * 3 );
  1442. vB.fromArray( coord, face.b * 3 );
  1443. vC.fromArray( coord, face.c * 3 );
  1444. normals.push( nA.x, nA.y, nA.z );
  1445. normals.push( nB.x, nB.y, nB.z );
  1446. normals.push( nC.x, nC.y, nC.z );
  1447. }
  1448. return new THREE.Float32BufferAttribute( normals, 3 );
  1449. }
  1450. function weightedNormal( normals, vector, creaseAngle ) {
  1451. var normal = new THREE.Vector3();
  1452. if ( creaseAngle === 0 ) {
  1453. normal.copy( vector );
  1454. } else {
  1455. for ( var i = 0, l = normals.length; i < l; i ++ ) {
  1456. if ( normals[ i ].angleTo( vector ) < creaseAngle ) {
  1457. normal.add( normals[ i ] );
  1458. }
  1459. }
  1460. }
  1461. return normal.normalize();
  1462. }
  1463. function toColorArray( colors ) {
  1464. var array = [];
  1465. for ( var i = 0, l = colors.length; i < l; i += 3 ) {
  1466. array.push( new THREE.Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );
  1467. }
  1468. return array;
  1469. }
  1470. /**
  1471. * Vertically paints the faces interpolating between the
  1472. * specified colors at the specified angels. This is used for the Background
  1473. * node, but could be applied to other nodes with multiple faces as well.
  1474. *
  1475. * When used with the Background node, default is directionIsDown is true if
  1476. * interpolating the skyColor down from the Zenith. When interpolationg up from
  1477. * the Nadir i.e. interpolating the groundColor, the directionIsDown is false.
  1478. *
  1479. * The first angle is never specified, it is the Zenith (0 rad). Angles are specified
  1480. * in radians. The geometry is thought a sphere, but could be anything. The color interpolation
  1481. * is linear along the Y axis in any case.
  1482. *
  1483. * You must specify one more color than you have angles at the beginning of the colors array.
  1484. * This is the color of the Zenith (the top of the shape).
  1485. *
  1486. * @param {BufferGeometry} geometry
  1487. * @param {number} radius
  1488. * @param {array} angles
  1489. * @param {array} colors
  1490. * @param {boolean} topDown - Whether to work top down or bottom up.
  1491. */
  1492. function paintFaces( geometry, radius, angles, colors, topDown ) {
  1493. var direction = ( topDown === true ) ? 1 : - 1;
  1494. var coord = [], A = {}, B = {}, applyColor = false;
  1495. for ( var k = 0; k < angles.length; k ++ ) {
  1496. // push the vector at which the color changes
  1497. var vec = {
  1498. x: direction * ( Math.cos( angles[ k ] ) * radius ),
  1499. y: direction * ( Math.sin( angles[ k ] ) * radius )
  1500. };
  1501. coord.push( vec );
  1502. }
  1503. var index = geometry.index;
  1504. var positionAttribute = geometry.attributes.position;
  1505. var colorAttribute = new THREE.BufferAttribute( new Float32Array( geometry.attributes.position.count * 3 ), 3 );
  1506. var position = new THREE.Vector3();
  1507. var color = new THREE.Color();
  1508. for ( var i = 0; i < index.count; i ++ ) {
  1509. var vertexIndex = index.getX( i );
  1510. position.fromBufferAttribute( positionAttribute, vertexIndex );
  1511. for ( var j = 0; j < colors.length; j ++ ) {
  1512. // linear interpolation between aColor and bColor, calculate proportion
  1513. // A is previous point (angle)
  1514. if ( j === 0 ) {
  1515. A.x = 0;
  1516. A.y = ( topDown === true ) ? radius : - 1 * radius;
  1517. } else {
  1518. A.x = coord[ j - 1 ].x;
  1519. A.y = coord[ j - 1 ].y;
  1520. }
  1521. // B is current point (angle)
  1522. B = coord[ j ];
  1523. if ( B !== undefined ) {
  1524. // p has to be between the points A and B which we interpolate
  1525. applyColor = ( topDown === true ) ? ( position.y <= A.y && position.y > B.y ) : ( position.y >= A.y && position.y < B.y );
  1526. if ( applyColor === true ) {
  1527. var aColor = colors[ j ];
  1528. var bColor = colors[ j + 1 ];
  1529. // below is simple linear interpolation
  1530. var t = Math.abs( position.y - A.y ) / ( A.y - B.y );
  1531. // to make it faster, you can only calculate this if the y coord changes, the color is the same for points with the same y
  1532. color.copy( aColor ).lerp( bColor, t );
  1533. colorAttribute.setXYZ( vertexIndex, color.r, color.g, color.b );
  1534. } else {
  1535. var colorIndex = ( topDown === true ) ? colors.length - 1 : 0;
  1536. var c = colors[ colorIndex ];
  1537. colorAttribute.setXYZ( vertexIndex, c.r, c.g, c.b );
  1538. }
  1539. }
  1540. }
  1541. }
  1542. geometry.setAttribute( 'color', colorAttribute );
  1543. }
  1544. //
  1545. var textureLoader = new THREE.TextureLoader( this.manager );
  1546. textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
  1547. // create JSON representing the tree structure of the VRML asset
  1548. var tree = generateVRMLTree( data );
  1549. // check version (only 2.0 is supported)
  1550. if ( tree.version.indexOf( 'V2.0' ) === - 1 ) {
  1551. throw Error( 'THREE.VRMLLexer: Version of VRML asset not supported.' );
  1552. }
  1553. // parse the tree structure to a three.js scene
  1554. var scene = parseTree( tree );
  1555. return scene;
  1556. }
  1557. } );
  1558. function VRMLLexer( tokens ) {
  1559. this.lexer = new chevrotain.Lexer( tokens );
  1560. }
  1561. VRMLLexer.prototype = {
  1562. constructor: VRMLLexer,
  1563. lex: function ( inputText ) {
  1564. var lexingResult = this.lexer.tokenize( inputText );
  1565. if ( lexingResult.errors.length > 0 ) {
  1566. console.error( lexingResult.errors );
  1567. throw Error( 'THREE.VRMLLexer: Lexing errors detected.' );
  1568. }
  1569. return lexingResult;
  1570. }
  1571. };
  1572. function VRMLParser( tokenVocabulary ) {
  1573. chevrotain.Parser.call( this, tokenVocabulary );
  1574. var $ = this;
  1575. var Version = tokenVocabulary[ 'Version' ];
  1576. var LCurly = tokenVocabulary[ 'LCurly' ];
  1577. var RCurly = tokenVocabulary[ 'RCurly' ];
  1578. var LSquare = tokenVocabulary[ 'LSquare' ];
  1579. var RSquare = tokenVocabulary[ 'RSquare' ];
  1580. var Identifier = tokenVocabulary[ 'Identifier' ];
  1581. var RouteIdentifier = tokenVocabulary[ 'RouteIdentifier' ];
  1582. var StringLiteral = tokenVocabulary[ 'StringLiteral' ];
  1583. var HexLiteral = tokenVocabulary[ 'HexLiteral' ];
  1584. var NumberLiteral = tokenVocabulary[ 'NumberLiteral' ];
  1585. var TrueLiteral = tokenVocabulary[ 'TrueLiteral' ];
  1586. var FalseLiteral = tokenVocabulary[ 'FalseLiteral' ];
  1587. var NullLiteral = tokenVocabulary[ 'NullLiteral' ];
  1588. var DEF = tokenVocabulary[ 'DEF' ];
  1589. var USE = tokenVocabulary[ 'USE' ];
  1590. var ROUTE = tokenVocabulary[ 'ROUTE' ];
  1591. var TO = tokenVocabulary[ 'TO' ];
  1592. var NodeName = tokenVocabulary[ 'NodeName' ];
  1593. $.RULE( 'vrml', function () {
  1594. $.SUBRULE( $.version );
  1595. $.AT_LEAST_ONE( function () {
  1596. $.SUBRULE( $.node );
  1597. } );
  1598. $.MANY( function () {
  1599. $.SUBRULE( $.route );
  1600. } );
  1601. } );
  1602. $.RULE( 'version', function () {
  1603. $.CONSUME( Version );
  1604. } );
  1605. $.RULE( 'node', function () {
  1606. $.OPTION( function () {
  1607. $.SUBRULE( $.def );
  1608. } );
  1609. $.CONSUME( NodeName );
  1610. $.CONSUME( LCurly );
  1611. $.MANY( function () {
  1612. $.SUBRULE( $.field );
  1613. } );
  1614. $.CONSUME( RCurly );
  1615. } );
  1616. $.RULE( 'field', function () {
  1617. $.CONSUME( Identifier );
  1618. $.OR2( [
  1619. { ALT: function () {
  1620. $.SUBRULE( $.singleFieldValue );
  1621. } },
  1622. { ALT: function () {
  1623. $.SUBRULE( $.multiFieldValue );
  1624. } }
  1625. ] );
  1626. } );
  1627. $.RULE( 'def', function () {
  1628. $.CONSUME( DEF );
  1629. $.CONSUME( Identifier );
  1630. } );
  1631. $.RULE( 'use', function () {
  1632. $.CONSUME( USE );
  1633. $.CONSUME( Identifier );
  1634. } );
  1635. $.RULE( 'singleFieldValue', function () {
  1636. $.AT_LEAST_ONE( function () {
  1637. $.OR( [
  1638. { ALT: function () {
  1639. $.SUBRULE( $.node );
  1640. } },
  1641. { ALT: function () {
  1642. $.SUBRULE( $.use );
  1643. } },
  1644. { ALT: function () {
  1645. $.CONSUME( StringLiteral );
  1646. } },
  1647. { ALT: function () {
  1648. $.CONSUME( HexLiteral );
  1649. } },
  1650. { ALT: function () {
  1651. $.CONSUME( NumberLiteral );
  1652. } },
  1653. { ALT: function () {
  1654. $.CONSUME( TrueLiteral );
  1655. } },
  1656. { ALT: function () {
  1657. $.CONSUME( FalseLiteral );
  1658. } },
  1659. { ALT: function () {
  1660. $.CONSUME( NullLiteral );
  1661. } }
  1662. ] );
  1663. } );
  1664. } );
  1665. $.RULE( 'multiFieldValue', function () {
  1666. $.CONSUME( LSquare );
  1667. $.MANY( function () {
  1668. $.OR( [
  1669. { ALT: function () {
  1670. $.SUBRULE( $.node );
  1671. } },
  1672. { ALT: function () {
  1673. $.SUBRULE( $.use );
  1674. } },
  1675. { ALT: function () {
  1676. $.CONSUME( StringLiteral );
  1677. } },
  1678. { ALT: function () {
  1679. $.CONSUME( HexLiteral );
  1680. } },
  1681. { ALT: function () {
  1682. $.CONSUME( NumberLiteral );
  1683. } },
  1684. { ALT: function () {
  1685. $.CONSUME( NullLiteral );
  1686. } }
  1687. ] );
  1688. } );
  1689. $.CONSUME( RSquare );
  1690. } );
  1691. $.RULE( 'route', function () {
  1692. $.CONSUME( ROUTE );
  1693. $.CONSUME( RouteIdentifier );
  1694. $.CONSUME( TO );
  1695. $.CONSUME2( RouteIdentifier );
  1696. } );
  1697. this.performSelfAnalysis();
  1698. }
  1699. VRMLParser.prototype = Object.create( chevrotain.Parser.prototype );
  1700. VRMLParser.prototype.constructor = VRMLParser;
  1701. function Face( a, b, c ) {
  1702. this.a = a;
  1703. this.b = b;
  1704. this.c = c;
  1705. this.normal = new THREE.Vector3();
  1706. }
  1707. var TEXTURE_TYPE = {
  1708. INTENSITY: 1,
  1709. INTENSITY_ALPHA: 2,
  1710. RGB: 3,
  1711. RGBA: 4
  1712. };
  1713. return VRMLLoader;
  1714. } )();