VRMLLoader.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601
  1. /**
  2. * @author Mugen87 / https://github.com/Mugen87
  3. */
  4. import {
  5. BackSide,
  6. BoxBufferGeometry,
  7. BufferAttribute,
  8. BufferGeometry,
  9. ClampToEdgeWrapping,
  10. Color,
  11. ConeBufferGeometry,
  12. CylinderBufferGeometry,
  13. DefaultLoadingManager,
  14. DoubleSide,
  15. FileLoader,
  16. Float32BufferAttribute,
  17. FrontSide,
  18. Group,
  19. LineBasicMaterial,
  20. LineSegments,
  21. LoaderUtils,
  22. Mesh,
  23. MeshBasicMaterial,
  24. MeshPhongMaterial,
  25. Object3D,
  26. Points,
  27. PointsMaterial,
  28. RepeatWrapping,
  29. Scene,
  30. SphereBufferGeometry,
  31. TextureLoader,
  32. Vector2,
  33. Vector3,
  34. VertexColors
  35. } from "../../../build/three.module.js";
  36. /* global chevrotain */
  37. var VRMLLoader = ( function () {
  38. // dependency check
  39. if ( typeof chevrotain === 'undefined' ) {
  40. throw Error( 'THREE.VRMLLoader: External library chevrotain.min.js required.' );
  41. }
  42. // class definitions
  43. function VRMLLoader( manager ) {
  44. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  45. }
  46. VRMLLoader.prototype = {
  47. constructor: VRMLLoader,
  48. crossOrigin: 'anonymous',
  49. load: function ( url, onLoad, onProgress, onError ) {
  50. var scope = this;
  51. var path = ( scope.path === undefined ) ? LoaderUtils.extractUrlBase( url ) : scope.path;
  52. var loader = new FileLoader( this.manager );
  53. loader.setPath( scope.path );
  54. loader.load( url, function ( text ) {
  55. onLoad( scope.parse( text, path ) );
  56. }, onProgress, onError );
  57. },
  58. setPath: function ( value ) {
  59. this.path = value;
  60. return this;
  61. },
  62. setResourcePath: function ( value ) {
  63. this.resourcePath = value;
  64. return this;
  65. },
  66. setCrossOrigin: function ( value ) {
  67. this.crossOrigin = value;
  68. return this;
  69. },
  70. parse: function ( data, path ) {
  71. var nodeMap = {};
  72. function generateVRMLTree( data ) {
  73. // create lexer, parser and visitor
  74. var tokenData = createTokens();
  75. var lexer = new VRMLLexer( tokenData.tokens );
  76. var parser = new VRMLParser( tokenData.tokenVocabulary );
  77. var visitor = createVisitor( parser.getBaseCstVisitorConstructor() );
  78. // lexing
  79. var lexingResult = lexer.lex( data );
  80. parser.input = lexingResult.tokens;
  81. // parsing
  82. var cstOutput = parser.vrml();
  83. if ( parser.errors.length > 0 ) {
  84. console.error( parser.errors );
  85. throw Error( 'THREE.VRMLLoader: Parsing errors detected.' );
  86. }
  87. // actions
  88. var ast = visitor.visit( cstOutput );
  89. return ast;
  90. }
  91. function createTokens() {
  92. var createToken = chevrotain.createToken;
  93. // from http://gun.teipir.gr/VRML-amgem/spec/part1/concepts.html#SyntaxBasics
  94. 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]*/ } );
  95. 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 } );
  96. // from http://gun.teipir.gr/VRML-amgem/spec/part1/nodesRef.html
  97. var nodeTypes = [
  98. 'Anchor', 'Billboard', 'Collision', 'Group', 'Transform', // grouping nodes
  99. 'Inline', 'LOD', 'Switch', // special groups
  100. 'AudioClip', 'DirectionalLight', 'PointLight', 'Script', 'Shape', 'Sound', 'SpotLight', 'WorldInfo', // common nodes
  101. 'CylinderSensor', 'PlaneSensor', 'ProximitySensor', 'SphereSensor', 'TimeSensor', 'TouchSensor', 'VisibilitySensor', // sensors
  102. 'Box', 'Cone', 'Cylinder', 'ElevationGrid', 'Extrusion', 'IndexedFaceSet', 'IndexedLineSet', 'PointSet', 'Sphere', // geometries
  103. 'Color', 'Coordinate', 'Normal', 'TextureCoordinate', // geometric properties
  104. 'Appearance', 'FontStyle', 'ImageTexture', 'Material', 'MovieTexture', 'PixelTexture', 'TextureTransform', // appearance
  105. 'ColorInterpolator', 'CoordinateInterpolator', 'NormalInterpolator', 'OrientationInterpolator', 'PositionInterpolator', 'ScalarInterpolator', // interpolators
  106. 'Background', 'Fog', 'NavigationInfo', 'Viewpoint', // bindable nodes
  107. 'Text' // Text must be placed at the end of the regex so there are no matches for TextureTransform and TextureCoordinate
  108. ];
  109. //
  110. var Version = createToken( {
  111. name: 'Version',
  112. pattern: /#VRML.*/,
  113. longer_alt: Identifier
  114. } );
  115. var NodeName = createToken( {
  116. name: 'NodeName',
  117. pattern: new RegExp( nodeTypes.join( '|' ) ),
  118. longer_alt: Identifier
  119. } );
  120. var DEF = createToken( {
  121. name: 'DEF',
  122. pattern: /DEF/,
  123. longer_alt: Identifier
  124. } );
  125. var USE = createToken( {
  126. name: 'USE',
  127. pattern: /USE/,
  128. longer_alt: Identifier
  129. } );
  130. var ROUTE = createToken( {
  131. name: 'ROUTE',
  132. pattern: /ROUTE/,
  133. longer_alt: Identifier
  134. } );
  135. var TO = createToken( {
  136. name: 'TO',
  137. pattern: /TO/,
  138. longer_alt: Identifier
  139. } );
  140. //
  141. var StringLiteral = createToken( { name: "StringLiteral", pattern: /"(:?[^\\"\n\r]+|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/ } );
  142. var NumberLiteral = createToken( { name: 'NumberLiteral', pattern: /[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/ } );
  143. var BooleanLiteral = createToken( { name: 'BooleanLiteral', pattern: /TRUE|FALSE/ } );
  144. var NullLiteral = createToken( { name: 'NullLiteral', pattern: /NULL/ } );
  145. var LSquare = createToken( { name: 'LSquare', pattern: /\[/ } );
  146. var RSquare = createToken( { name: 'RSquare', pattern: /]/ } );
  147. var LCurly = createToken( { name: 'LCurly', pattern: /{/ } );
  148. var RCurly = createToken( { name: 'RCurly', pattern: /}/ } );
  149. var Comment = createToken( {
  150. name: 'Comment',
  151. pattern: /#.*/,
  152. group: chevrotain.Lexer.SKIPPED
  153. } );
  154. // commas, blanks, tabs, newlines and carriage returns are whitespace characters wherever they appear outside of string fields
  155. var WhiteSpace = createToken( {
  156. name: 'WhiteSpace',
  157. pattern: /[ ,\s]/,
  158. group: chevrotain.Lexer.SKIPPED
  159. } );
  160. var tokens = [
  161. WhiteSpace,
  162. // keywords appear before the Identifier
  163. NodeName,
  164. DEF,
  165. USE,
  166. ROUTE,
  167. TO,
  168. BooleanLiteral,
  169. NullLiteral,
  170. // the Identifier must appear after the keywords because all keywords are valid identifiers
  171. Version,
  172. Identifier,
  173. RouteIdentifier,
  174. StringLiteral,
  175. NumberLiteral,
  176. LSquare,
  177. RSquare,
  178. LCurly,
  179. RCurly,
  180. Comment
  181. ];
  182. var tokenVocabulary = {};
  183. for ( var i = 0, l = tokens.length; i < l; i ++ ) {
  184. var token = tokens[ i ];
  185. tokenVocabulary[ token.name ] = token;
  186. }
  187. return { tokens: tokens, tokenVocabulary: tokenVocabulary };
  188. }
  189. function createVisitor( BaseVRMLVisitor ) {
  190. // the visitor is created dynmaically based on the given base class
  191. function VRMLToASTVisitor() {
  192. BaseVRMLVisitor.call( this );
  193. this.validateVisitor();
  194. }
  195. VRMLToASTVisitor.prototype = Object.assign( Object.create( BaseVRMLVisitor.prototype ), {
  196. constructor: VRMLToASTVisitor,
  197. vrml: function ( ctx ) {
  198. var data = {
  199. version: this.visit( ctx.version ),
  200. nodes: [],
  201. routes: []
  202. };
  203. for ( var i = 0, l = ctx.node.length; i < l; i ++ ) {
  204. var node = ctx.node[ i ];
  205. data.nodes.push( this.visit( node ) );
  206. }
  207. if ( ctx.route ) {
  208. for ( var i = 0, l = ctx.route.length; i < l; i ++ ) {
  209. var route = ctx.route[ i ];
  210. data.routes.push( this.visit( route ) );
  211. }
  212. }
  213. return data;
  214. },
  215. version: function ( ctx ) {
  216. return ctx.Version[ 0 ].image;
  217. },
  218. node: function ( ctx ) {
  219. var data = {
  220. name: ctx.NodeName[ 0 ].image,
  221. fields: []
  222. };
  223. if ( ctx.field ) {
  224. for ( var i = 0, l = ctx.field.length; i < l; i ++ ) {
  225. var field = ctx.field[ i ];
  226. data.fields.push( this.visit( field ) );
  227. }
  228. }
  229. // DEF
  230. if ( ctx.def ) {
  231. data.DEF = this.visit( ctx.def[ 0 ] );
  232. }
  233. return data;
  234. },
  235. field: function ( ctx ) {
  236. var data = {
  237. name: ctx.Identifier[ 0 ].image,
  238. type: null,
  239. values: null
  240. };
  241. var result;
  242. // SFValue
  243. if ( ctx.singleFieldValue ) {
  244. result = this.visit( ctx.singleFieldValue[ 0 ] );
  245. }
  246. // MFValue
  247. if ( ctx.multiFieldValue ) {
  248. result = this.visit( ctx.multiFieldValue[ 0 ] );
  249. }
  250. data.type = result.type;
  251. data.values = result.values;
  252. return data;
  253. },
  254. def: function ( ctx ) {
  255. return ctx.Identifier[ 0 ].image;
  256. },
  257. use: function ( ctx ) {
  258. return { USE: ctx.Identifier[ 0 ].image };
  259. },
  260. singleFieldValue: function ( ctx ) {
  261. return processField( this, ctx );
  262. },
  263. multiFieldValue: function ( ctx ) {
  264. return processField( this, ctx );
  265. },
  266. route: function ( ctx ) {
  267. var data = {
  268. FROM: ctx.RouteIdentifier[ 0 ].image,
  269. TO: ctx.RouteIdentifier[ 1 ].image
  270. };
  271. return data;
  272. }
  273. } );
  274. function processField( scope, ctx ) {
  275. var field = {
  276. type: null,
  277. values: []
  278. };
  279. if ( ctx.node ) {
  280. field.type = 'node';
  281. for ( var i = 0, l = ctx.node.length; i < l; i ++ ) {
  282. var node = ctx.node[ i ];
  283. field.values.push( scope.visit( node ) );
  284. }
  285. }
  286. if ( ctx.use ) {
  287. field.type = 'use';
  288. for ( var i = 0, l = ctx.use.length; i < l; i ++ ) {
  289. var use = ctx.use[ i ];
  290. field.values.push( scope.visit( use ) );
  291. }
  292. }
  293. if ( ctx.StringLiteral ) {
  294. field.type = 'string';
  295. for ( var i = 0, l = ctx.StringLiteral.length; i < l; i ++ ) {
  296. var stringLiteral = ctx.StringLiteral[ i ];
  297. field.values.push( stringLiteral.image.replace( /'|"/g, '' ) );
  298. }
  299. }
  300. if ( ctx.NumberLiteral ) {
  301. field.type = 'number';
  302. for ( var i = 0, l = ctx.NumberLiteral.length; i < l; i ++ ) {
  303. var numberLiteral = ctx.NumberLiteral[ i ];
  304. field.values.push( parseFloat( numberLiteral.image ) );
  305. }
  306. }
  307. if ( ctx.BooleanLiteral ) {
  308. field.type = 'boolean';
  309. for ( var i = 0, l = ctx.BooleanLiteral.length; i < l; i ++ ) {
  310. var booleanLiteral = ctx.BooleanLiteral[ i ];
  311. field.values.push( booleanLiteral.image === 'TRUE' );
  312. }
  313. }
  314. if ( ctx.NullLiteral ) {
  315. field.type = 'null';
  316. ctx.NullLiteral.forEach( function () {
  317. field.values.push( null );
  318. } );
  319. }
  320. return field;
  321. }
  322. return new VRMLToASTVisitor();
  323. }
  324. function parseTree( tree ) {
  325. // console.log( JSON.stringify( tree, null, 2 ) );
  326. var nodes = tree.nodes;
  327. var scene = new Scene();
  328. // first iteration: build nodemap based on DEF statements
  329. for ( var i = 0, l = nodes.length; i < l; i ++ ) {
  330. var node = nodes[ i ];
  331. buildNodeMap( node );
  332. }
  333. // second iteration: build nodes
  334. for ( var i = 0, l = nodes.length; i < l; i ++ ) {
  335. var node = nodes[ i ];
  336. var object = getNode( node );
  337. if ( object instanceof Object3D ) scene.add( object );
  338. }
  339. return scene;
  340. }
  341. function buildNodeMap( node ) {
  342. if ( node.DEF ) {
  343. nodeMap[ node.DEF ] = node;
  344. }
  345. var fields = node.fields;
  346. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  347. var field = fields[ i ];
  348. if ( field.type === 'node' ) {
  349. var fieldValues = field.values;
  350. for ( var j = 0, jl = fieldValues.length; j < jl; j ++ ) {
  351. buildNodeMap( fieldValues[ j ] );
  352. }
  353. }
  354. }
  355. }
  356. function getNode( node ) {
  357. // handle case where a node refers to a different one
  358. if ( node.USE ) {
  359. return resolveUSE( node.USE );
  360. }
  361. if ( node.build !== undefined ) return node.build;
  362. node.build = buildNode( node );
  363. return node.build;
  364. }
  365. // node builder
  366. function buildNode( node ) {
  367. var nodeName = node.name;
  368. var build;
  369. switch ( nodeName ) {
  370. case 'Group':
  371. case 'Transform':
  372. build = buildGroupingNode( node );
  373. break;
  374. case 'Background':
  375. build = buildBackgroundNode( node );
  376. break;
  377. case 'Shape':
  378. build = buildShapeNode( node );
  379. break;
  380. case 'Appearance':
  381. build = buildApperanceNode( node );
  382. break;
  383. case 'Material':
  384. build = buildMaterialNode( node );
  385. break;
  386. case 'ImageTexture':
  387. build = buildImageTextureNode( node );
  388. break;
  389. case 'TextureTransform':
  390. build = buildTextureTransformNode( node );
  391. break;
  392. case 'IndexedFaceSet':
  393. build = buildIndexedFaceSetNode( node );
  394. break;
  395. case 'IndexedLineSet':
  396. build = buildIndexedLineSetNode( node );
  397. break;
  398. case 'PointSet':
  399. build = buildPointSetNode( node );
  400. break;
  401. case 'Box':
  402. build = buildBoxNode( node );
  403. break;
  404. case 'Cone':
  405. build = buildConeNode( node );
  406. break;
  407. case 'Cylinder':
  408. build = buildCylinderNode( node );
  409. break;
  410. case 'Sphere':
  411. build = buildSphereNode( node );
  412. break;
  413. case 'Color':
  414. case 'Coordinate':
  415. case 'Normal':
  416. case 'TextureCoordinate':
  417. build = buildGeometricNode( node );
  418. break;
  419. case 'Anchor':
  420. case 'Billboard':
  421. case 'Collision':
  422. case 'Inline':
  423. case 'LOD':
  424. case 'Switch':
  425. case 'AudioClip':
  426. case 'DirectionalLight':
  427. case 'PointLight':
  428. case 'Script':
  429. case 'Sound':
  430. case 'SpotLight':
  431. case 'WorldInfo':
  432. case 'CylinderSensor':
  433. case 'PlaneSensor':
  434. case 'ProximitySensor':
  435. case 'SphereSensor':
  436. case 'TimeSensor':
  437. case 'TouchSensor':
  438. case 'VisibilitySensor':
  439. case 'ElevationGrid':
  440. case 'Extrusion':
  441. case 'Text':
  442. case 'FontStyle':
  443. case 'MovieTexture':
  444. case 'PixelTexture':
  445. case 'ColorInterpolator':
  446. case 'CoordinateInterpolator':
  447. case 'NormalInterpolator':
  448. case 'OrientationInterpolator':
  449. case 'PositionInterpolator':
  450. case 'ScalarInterpolator':
  451. case 'Fog':
  452. case 'NavigationInfo':
  453. case 'Viewpoint':
  454. // node not supported yet
  455. break;
  456. default:
  457. console.warn( 'THREE.VRMLLoader: Unknown node:', nodeName );
  458. break;
  459. }
  460. return build;
  461. }
  462. function buildGroupingNode( node ) {
  463. var object = new Group();
  464. //
  465. var fields = node.fields;
  466. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  467. var field = fields[ i ];
  468. var fieldName = field.name;
  469. var fieldValues = field.values;
  470. switch ( fieldName ) {
  471. case 'center':
  472. // field not supported
  473. break;
  474. case 'children':
  475. parseFieldChildren( fieldValues, object );
  476. break;
  477. case 'rotation':
  478. var axis = new Vector3( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  479. var angle = fieldValues[ 3 ];
  480. object.quaternion.setFromAxisAngle( axis, angle );
  481. break;
  482. case 'scale':
  483. object.scale.set( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  484. break;
  485. case 'scaleOrientation':
  486. // field not supported
  487. break;
  488. case 'translation':
  489. object.position.set( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  490. break;
  491. case 'bboxCenter':
  492. // field not supported
  493. break;
  494. case 'bboxSize':
  495. // field not supported
  496. break;
  497. default:
  498. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  499. break;
  500. }
  501. }
  502. return object;
  503. }
  504. function buildBackgroundNode( node ) {
  505. var group = new Group();
  506. var groundAngle, groundColor;
  507. var skyAngle, skyColor;
  508. var fields = node.fields;
  509. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  510. var field = fields[ i ];
  511. var fieldName = field.name;
  512. var fieldValues = field.values;
  513. switch ( fieldName ) {
  514. case 'groundAngle':
  515. groundAngle = fieldValues;
  516. break;
  517. case 'groundColor':
  518. groundColor = fieldValues;
  519. break;
  520. case 'backUrl':
  521. // field not supported
  522. break;
  523. case 'bottomUrl':
  524. // field not supported
  525. break;
  526. case 'frontUrl':
  527. // field not supported
  528. break;
  529. case 'leftUrl':
  530. // field not supported
  531. break;
  532. case 'rightUrl':
  533. // field not supported
  534. break;
  535. case 'topUrl':
  536. // field not supported
  537. break;
  538. case 'skyAngle':
  539. skyAngle = fieldValues;
  540. break;
  541. case 'skyColor':
  542. skyColor = fieldValues;
  543. break;
  544. default:
  545. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  546. break;
  547. }
  548. }
  549. // sky
  550. if ( skyColor ) {
  551. var radius = 10000;
  552. var skyGeometry = new SphereBufferGeometry( radius, 32, 16 );
  553. var skyMaterial = new MeshBasicMaterial( { fog: false, side: BackSide, depthWrite: false, depthTest: false } );
  554. if ( skyColor.length > 3 ) {
  555. paintFaces( skyGeometry, radius, skyAngle, toColorArray( skyColor ), true );
  556. skyMaterial.vertexColors = VertexColors;
  557. } else {
  558. skyMaterial.color.setRGB( skyColor[ 0 ], skyColor[ 1 ], skyColor[ 2 ] );
  559. }
  560. var sky = new Mesh( skyGeometry, skyMaterial );
  561. group.add( sky );
  562. }
  563. // ground
  564. if ( groundColor ) {
  565. if ( groundColor.length > 0 ) {
  566. var groundGeometry = new SphereBufferGeometry( radius, 32, 16, 0, 2 * Math.PI, 0.5 * Math.PI, 1.5 * Math.PI );
  567. var groundMaterial = new MeshBasicMaterial( { fog: false, side: BackSide, vertexColors: VertexColors, depthWrite: false, depthTest: false } );
  568. paintFaces( groundGeometry, radius, groundAngle, toColorArray( groundColor ), false );
  569. var ground = new Mesh( groundGeometry, groundMaterial );
  570. group.add( ground );
  571. }
  572. }
  573. // render background group first
  574. group.renderOrder = - Infinity;
  575. return group;
  576. }
  577. function buildShapeNode( node ) {
  578. var fields = node.fields;
  579. // if the appearance field is NULL or unspecified, lighting is off and the unlit object color is (0, 0, 0)
  580. var material = new MeshBasicMaterial( { color: 0x000000 } );
  581. var geometry;
  582. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  583. var field = fields[ i ];
  584. var fieldName = field.name;
  585. var fieldValues = field.values;
  586. switch ( fieldName ) {
  587. case 'appearance':
  588. if ( fieldValues[ 0 ] !== null ) {
  589. material = getNode( fieldValues[ 0 ] );
  590. }
  591. break;
  592. case 'geometry':
  593. if ( fieldValues[ 0 ] !== null ) {
  594. geometry = getNode( fieldValues[ 0 ] );
  595. }
  596. break;
  597. default:
  598. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  599. break;
  600. }
  601. }
  602. // build 3D object
  603. var object;
  604. if ( geometry ) {
  605. var type = geometry._type;
  606. if ( type === 'points' ) { // points
  607. var pointsMaterial = new PointsMaterial( { color: 0xffffff } );
  608. if ( geometry.attributes.color !== undefined ) {
  609. pointsMaterial.vertexColors = VertexColors;
  610. } else {
  611. // 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
  612. if ( material.isMeshPhongMaterial ) {
  613. pointsMaterial.color.copy( material.emissive );
  614. }
  615. }
  616. object = new Points( geometry, pointsMaterial );
  617. } else if ( type === 'line' ) { // lines
  618. var lineMaterial = new LineBasicMaterial( { color: 0xffffff } );
  619. if ( geometry.attributes.color !== undefined ) {
  620. lineMaterial.vertexColors = VertexColors;
  621. } else {
  622. // 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
  623. if ( material.isMeshPhongMaterial ) {
  624. lineMaterial.color.copy( material.emissive );
  625. }
  626. }
  627. object = new LineSegments( geometry, lineMaterial );
  628. } else { // consider meshes
  629. // check "solid" hint (it's placed in the geometry but affects the material)
  630. if ( geometry._solid !== undefined ) {
  631. material.side = ( geometry._solid ) ? FrontSide : DoubleSide;
  632. }
  633. // check for vertex colors
  634. if ( geometry.attributes.color !== undefined ) {
  635. material.vertexColors = VertexColors;
  636. }
  637. object = new Mesh( geometry, material );
  638. }
  639. } else {
  640. object = new Object3D();
  641. // if the geometry field is NULL the object is not drawn
  642. object.visible = false;
  643. }
  644. return object;
  645. }
  646. function buildApperanceNode( node ) {
  647. var material = new MeshPhongMaterial();
  648. var transformData;
  649. var fields = node.fields;
  650. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  651. var field = fields[ i ];
  652. var fieldName = field.name;
  653. var fieldValues = field.values;
  654. switch ( fieldName ) {
  655. case 'material':
  656. if ( fieldValues[ 0 ] !== null ) {
  657. var materialData = getNode( fieldValues[ 0 ] );
  658. if ( materialData.diffuseColor ) material.color.copy( materialData.diffuseColor );
  659. if ( materialData.emissiveColor ) material.emissive.copy( materialData.emissiveColor );
  660. if ( materialData.shininess ) material.shininess = materialData.shininess;
  661. if ( materialData.specularColor ) material.specular.copy( materialData.specularColor );
  662. if ( materialData.transparency ) material.opacity = 1 - materialData.transparency;
  663. if ( materialData.transparency > 0 ) material.transparent = true;
  664. } else {
  665. // if the material field is NULL or unspecified, lighting is off and the unlit object color is (0, 0, 0)
  666. material = new MeshBasicMaterial( { color: 0x000000 } );
  667. }
  668. break;
  669. case 'texture':
  670. var textureNode = fieldValues[ 0 ];
  671. if ( textureNode !== null ) {
  672. if ( textureNode.name === 'ImageTexture' ) {
  673. material.map = getNode( textureNode );
  674. } else {
  675. // MovieTexture and PixelTexture not supported yet
  676. }
  677. }
  678. break;
  679. case 'textureTransform':
  680. if ( fieldValues[ 0 ] !== null ) {
  681. transformData = getNode( fieldValues[ 0 ] );
  682. }
  683. break;
  684. default:
  685. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  686. break;
  687. }
  688. }
  689. // only apply texture transform data if a texture was defined
  690. if ( material.map && transformData ) {
  691. material.map.center.copy( transformData.center );
  692. material.map.rotation = transformData.rotation;
  693. material.map.repeat.copy( transformData.scale );
  694. material.map.offset.copy( transformData.translation );
  695. }
  696. return material;
  697. }
  698. function buildMaterialNode( node ) {
  699. var materialData = {};
  700. var fields = node.fields;
  701. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  702. var field = fields[ i ];
  703. var fieldName = field.name;
  704. var fieldValues = field.values;
  705. switch ( fieldName ) {
  706. case 'ambientIntensity':
  707. // field not supported
  708. break;
  709. case 'diffuseColor':
  710. materialData.diffuseColor = new Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  711. break;
  712. case 'emissiveColor':
  713. materialData.emissiveColor = new Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  714. break;
  715. case 'shininess':
  716. materialData.shininess = fieldValues[ 0 ];
  717. break;
  718. case 'specularColor':
  719. materialData.emissiveColor = new Color( fieldValues[ 0 ], fieldValues[ 1 ], fieldValues[ 2 ] );
  720. break;
  721. case 'transparency':
  722. materialData.transparency = fieldValues[ 0 ];
  723. break;
  724. default:
  725. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  726. break;
  727. }
  728. }
  729. return materialData;
  730. }
  731. function buildImageTextureNode( node ) {
  732. var texture;
  733. var wrapS = RepeatWrapping;
  734. var wrapT = RepeatWrapping;
  735. var fields = node.fields;
  736. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  737. var field = fields[ i ];
  738. var fieldName = field.name;
  739. var fieldValues = field.values;
  740. switch ( fieldName ) {
  741. case 'url':
  742. var url = fieldValues[ 0 ];
  743. if ( url ) texture = textureLoader.load( url );
  744. break;
  745. case 'repeatS':
  746. if ( fieldValues[ 0 ] === false ) wrapS = ClampToEdgeWrapping;
  747. break;
  748. case 'repeatT':
  749. if ( fieldValues[ 0 ] === false ) wrapT = ClampToEdgeWrapping;
  750. break;
  751. default:
  752. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  753. break;
  754. }
  755. }
  756. if ( texture ) {
  757. texture.wrapS = wrapS;
  758. texture.wrapT = wrapT;
  759. }
  760. return texture;
  761. }
  762. function buildTextureTransformNode( node ) {
  763. var transformData = {
  764. center: new Vector2(),
  765. rotation: new Vector2(),
  766. scale: new Vector2(),
  767. translation: new Vector2()
  768. };
  769. var fields = node.fields;
  770. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  771. var field = fields[ i ];
  772. var fieldName = field.name;
  773. var fieldValues = field.values;
  774. switch ( fieldName ) {
  775. case 'center':
  776. transformData.center.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  777. break;
  778. case 'rotation':
  779. transformData.rotation = fieldValues[ 0 ];
  780. break;
  781. case 'scale':
  782. transformData.scale.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  783. break;
  784. case 'translation':
  785. transformData.translation.set( fieldValues[ 0 ], fieldValues[ 1 ] );
  786. break;
  787. default:
  788. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  789. break;
  790. }
  791. }
  792. return transformData;
  793. }
  794. function buildGeometricNode( node ) {
  795. return node.fields[ 0 ].values;
  796. }
  797. function buildIndexedFaceSetNode( node ) {
  798. var color, coord, normal, texCoord;
  799. var ccw = true, solid = true, creaseAngle;
  800. var colorIndex, coordIndex, normalIndex, texCoordIndex;
  801. var colorPerVertex = true, normalPerVertex = true;
  802. var fields = node.fields;
  803. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  804. var field = fields[ i ];
  805. var fieldName = field.name;
  806. var fieldValues = field.values;
  807. switch ( fieldName ) {
  808. case 'color':
  809. var colorNode = fieldValues[ 0 ];
  810. if ( colorNode !== null ) {
  811. color = getNode( colorNode );
  812. }
  813. break;
  814. case 'coord':
  815. var coordNode = fieldValues[ 0 ];
  816. if ( coordNode !== null ) {
  817. coord = getNode( coordNode );
  818. }
  819. break;
  820. case 'normal':
  821. var normalNode = fieldValues[ 0 ];
  822. if ( normalNode !== null ) {
  823. normal = getNode( normalNode );
  824. }
  825. break;
  826. case 'texCoord':
  827. var texCoordNode = fieldValues[ 0 ];
  828. if ( texCoordNode !== null ) {
  829. texCoord = getNode( texCoordNode );
  830. }
  831. break;
  832. case 'ccw':
  833. ccw = fieldValues[ 0 ];
  834. break;
  835. case 'colorIndex':
  836. colorIndex = fieldValues;
  837. break;
  838. case 'colorPerVertex':
  839. colorPerVertex = fieldValues[ 0 ];
  840. break;
  841. case 'convex':
  842. // field not supported
  843. break;
  844. case 'coordIndex':
  845. coordIndex = fieldValues;
  846. break;
  847. case 'creaseAngle':
  848. creaseAngle = fieldValues[ 0 ];
  849. break;
  850. case 'normalIndex':
  851. normalIndex = fieldValues;
  852. break;
  853. case 'normalPerVertex':
  854. normalPerVertex = fieldValues[ 0 ];
  855. break;
  856. case 'solid':
  857. solid = fieldValues[ 0 ];
  858. break;
  859. case 'texCoordIndex':
  860. texCoordIndex = fieldValues;
  861. break;
  862. default:
  863. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  864. break;
  865. }
  866. }
  867. var triangulatedCoordIndex = triangulateFaceIndex( coordIndex, ccw );
  868. var positionAttribute;
  869. var colorAttribute;
  870. var normalAttribute;
  871. var uvAttribute;
  872. if ( color ) {
  873. if ( colorPerVertex === true ) {
  874. if ( colorIndex.length > 0 ) {
  875. // if the colorIndex field is not empty, then it is used to choose colors for each vertex of the IndexedFaceSet.
  876. var triangulatedColorIndex = triangulateFaceIndex( colorIndex, ccw );
  877. colorAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedColorIndex, color, 3 );
  878. } else {
  879. // if the colorIndex field is empty, then the coordIndex field is used to choose colors from the Color node
  880. colorAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new Float32BufferAttribute( color, 3 ) );
  881. }
  882. } else {
  883. if ( colorIndex.length > 0 ) {
  884. // if the colorIndex field is not empty, then they are used to choose one color for each face of the IndexedFaceSet
  885. var flattenFaceColors = flattenData( color, colorIndex );
  886. var triangulatedFaceColors = triangulateFaceData( flattenFaceColors, coordIndex );
  887. colorAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceColors );
  888. } else {
  889. // if the colorIndex field is empty, then the color are applied to each face of the IndexedFaceSet in order
  890. var triangulatedFaceColors = triangulateFaceData( color, coordIndex );
  891. colorAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceColors );
  892. }
  893. }
  894. }
  895. if ( normal ) {
  896. if ( normalPerVertex === true ) {
  897. // consider vertex normals
  898. if ( normalIndex.length > 0 ) {
  899. // if the normalIndex field is not empty, then it is used to choose normals for each vertex of the IndexedFaceSet.
  900. var triangulatedNormalIndex = triangulateFaceIndex( normalIndex, ccw );
  901. normalAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedNormalIndex, normal, 3 );
  902. } else {
  903. // if the normalIndex field is empty, then the coordIndex field is used to choose normals from the Normal node
  904. normalAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new Float32BufferAttribute( normal, 3 ) );
  905. }
  906. } else {
  907. // consider face normals
  908. if ( normalIndex.length > 0 ) {
  909. // if the normalIndex field is not empty, then they are used to choose one normal for each face of the IndexedFaceSet
  910. var flattenFaceNormals = flattenData( normal, normalIndex );
  911. var triangulatedFaceNormals = triangulateFaceData( flattenFaceNormals, coordIndex );
  912. normalAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceNormals );
  913. } else {
  914. // if the normalIndex field is empty, then the normals are applied to each face of the IndexedFaceSet in order
  915. var triangulatedFaceNormals = triangulateFaceData( normal, coordIndex );
  916. normalAttribute = computeAttributeFromFaceData( triangulatedCoordIndex, triangulatedFaceNormals );
  917. }
  918. }
  919. } else {
  920. // 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
  921. normalAttribute = computeNormalAttribute( triangulatedCoordIndex, coord, creaseAngle );
  922. }
  923. if ( texCoord ) {
  924. // texture coordinates are always defined on vertex level
  925. if ( texCoordIndex.length > 0 ) {
  926. // if the texCoordIndex field is not empty, then it is used to choose texture coordinates for each vertex of the IndexedFaceSet.
  927. var triangulatedTexCoordIndex = triangulateFaceIndex( texCoordIndex, ccw );
  928. uvAttribute = computeAttributeFromIndexedData( triangulatedCoordIndex, triangulatedTexCoordIndex, texCoord, 2 );
  929. } else {
  930. // if the texCoordIndex field is empty, then the coordIndex array is used to choose texture coordinates from the TextureCoordinate node
  931. uvAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new Float32BufferAttribute( texCoord, 2 ) );
  932. }
  933. }
  934. var geometry = new BufferGeometry();
  935. positionAttribute = toNonIndexedAttribute( triangulatedCoordIndex, new Float32BufferAttribute( coord, 3 ) );
  936. geometry.addAttribute( 'position', positionAttribute );
  937. geometry.addAttribute( 'normal', normalAttribute );
  938. // optional attributes
  939. if ( colorAttribute ) geometry.addAttribute( 'color', colorAttribute );
  940. if ( uvAttribute ) geometry.addAttribute( 'uv', uvAttribute );
  941. // "solid" influences the material so let's store it for later use
  942. geometry._solid = solid;
  943. geometry._type = 'mesh';
  944. return geometry;
  945. }
  946. function buildIndexedLineSetNode( node ) {
  947. var color, coord;
  948. var colorIndex, coordIndex;
  949. var colorPerVertex = true;
  950. var fields = node.fields;
  951. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  952. var field = fields[ i ];
  953. var fieldName = field.name;
  954. var fieldValues = field.values;
  955. switch ( fieldName ) {
  956. case 'color':
  957. var colorNode = fieldValues[ 0 ];
  958. if ( colorNode !== null ) {
  959. color = getNode( colorNode );
  960. }
  961. break;
  962. case 'coord':
  963. var coordNode = fieldValues[ 0 ];
  964. if ( coordNode !== null ) {
  965. coord = getNode( coordNode );
  966. }
  967. break;
  968. case 'colorIndex':
  969. colorIndex = fieldValues;
  970. break;
  971. case 'colorPerVertex':
  972. colorPerVertex = fieldValues[ 0 ];
  973. break;
  974. case 'coordIndex':
  975. coordIndex = fieldValues;
  976. break;
  977. default:
  978. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  979. break;
  980. }
  981. }
  982. // build lines
  983. var colorAttribute;
  984. var expandedLineIndex = expandLineIndex( coordIndex ); // create an index for three.js's linesegment primitive
  985. if ( color ) {
  986. if ( colorPerVertex === true ) {
  987. if ( colorIndex.length > 0 ) {
  988. // if the colorIndex field is not empty, then one color is used for each polyline of the IndexedLineSet.
  989. var expandedColorIndex = expandLineIndex( colorIndex ); // compute colors for each line segment (rendering primitve)
  990. colorAttribute = computeAttributeFromIndexedData( expandedLineIndex, expandedColorIndex, color, 3 ); // compute data on vertex level
  991. } else {
  992. // if the colorIndex field is empty, then the colors are applied to each polyline of the IndexedLineSet in order.
  993. colorAttribute = toNonIndexedAttribute( expandedLineIndex, new Float32BufferAttribute( color, 3 ) );
  994. }
  995. } else {
  996. if ( colorIndex.length > 0 ) {
  997. // if the colorIndex field is not empty, then colors are applied to each vertex of the IndexedLineSet
  998. var flattenLineColors = flattenData( color, colorIndex ); // compute colors for each VRML primitve
  999. var expandedLineColors = expandLineData( flattenLineColors, coordIndex ); // compute colors for each line segment (rendering primitve)
  1000. colorAttribute = computeAttributeFromLineData( expandedLineIndex, expandedLineColors ); // compute data on vertex level
  1001. } else {
  1002. // if the colorIndex field is empty, then the coordIndex field is used to choose colors from the Color node
  1003. var expandedLineColors = expandLineData( color, coordIndex ); // compute colors for each line segment (rendering primitve)
  1004. colorAttribute = computeAttributeFromLineData( expandedLineIndex, expandedLineColors ); // compute data on vertex level
  1005. }
  1006. }
  1007. }
  1008. //
  1009. var geometry = new BufferGeometry();
  1010. var positionAttribute = toNonIndexedAttribute( expandedLineIndex, new Float32BufferAttribute( coord, 3 ) );
  1011. geometry.addAttribute( 'position', positionAttribute );
  1012. if ( colorAttribute ) geometry.addAttribute( 'color', colorAttribute );
  1013. geometry._type = 'line';
  1014. return geometry;
  1015. }
  1016. function buildPointSetNode( node ) {
  1017. var geometry;
  1018. var color, coord;
  1019. var fields = node.fields;
  1020. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1021. var field = fields[ i ];
  1022. var fieldName = field.name;
  1023. var fieldValues = field.values;
  1024. switch ( fieldName ) {
  1025. case 'color':
  1026. var colorNode = fieldValues[ 0 ];
  1027. if ( colorNode !== null ) {
  1028. color = getNode( colorNode );
  1029. }
  1030. break;
  1031. case 'coord':
  1032. var coordNode = fieldValues[ 0 ];
  1033. if ( coordNode !== null ) {
  1034. coord = getNode( coordNode );
  1035. }
  1036. break;
  1037. default:
  1038. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1039. break;
  1040. }
  1041. }
  1042. var geometry = new BufferGeometry();
  1043. geometry.addAttribute( 'position', new Float32BufferAttribute( coord, 3 ) );
  1044. if ( color ) geometry.addAttribute( 'color', new Float32BufferAttribute( color, 3 ) );
  1045. geometry._type = 'points';
  1046. return geometry;
  1047. }
  1048. function buildBoxNode( node ) {
  1049. var size = new Vector3( 2, 2, 2 );
  1050. var fields = node.fields;
  1051. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1052. var field = fields[ i ];
  1053. var fieldName = field.name;
  1054. var fieldValues = field.values;
  1055. switch ( fieldName ) {
  1056. case 'size':
  1057. size.x = fieldValues[ 0 ];
  1058. size.y = fieldValues[ 1 ];
  1059. size.z = fieldValues[ 2 ];
  1060. break;
  1061. default:
  1062. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1063. break;
  1064. }
  1065. }
  1066. var geometry = new BoxBufferGeometry( size.x, size.y, size.z );
  1067. return geometry;
  1068. }
  1069. function buildConeNode( node ) {
  1070. var radius = 1, height = 2, openEnded = false;
  1071. var fields = node.fields;
  1072. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1073. var field = fields[ i ];
  1074. var fieldName = field.name;
  1075. var fieldValues = field.values;
  1076. switch ( fieldName ) {
  1077. case 'bottom':
  1078. openEnded = ! fieldValues[ 0 ];
  1079. break;
  1080. case 'bottomRadius':
  1081. radius = fieldValues[ 0 ];
  1082. break;
  1083. case 'height':
  1084. height = fieldValues[ 0 ];
  1085. break;
  1086. case 'side':
  1087. // field not supported
  1088. break;
  1089. default:
  1090. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1091. break;
  1092. }
  1093. }
  1094. var geometry = new ConeBufferGeometry( radius, height, 16, 1, openEnded );
  1095. return geometry;
  1096. }
  1097. function buildCylinderNode( node ) {
  1098. var radius = 1, height = 2;
  1099. var fields = node.fields;
  1100. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1101. var field = fields[ i ];
  1102. var fieldName = field.name;
  1103. var fieldValues = field.values;
  1104. switch ( fieldName ) {
  1105. case 'bottom':
  1106. // field not supported
  1107. break;
  1108. case 'radius':
  1109. radius = fieldValues[ 0 ];
  1110. break;
  1111. case 'height':
  1112. height = fieldValues[ 0 ];
  1113. break;
  1114. case 'side':
  1115. // field not supported
  1116. break;
  1117. case 'top':
  1118. // field not supported
  1119. break;
  1120. default:
  1121. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1122. break;
  1123. }
  1124. }
  1125. var geometry = new CylinderBufferGeometry( radius, radius, height, 16, 1 );
  1126. return geometry;
  1127. }
  1128. function buildSphereNode( node ) {
  1129. var radius = 1;
  1130. var fields = node.fields;
  1131. for ( var i = 0, l = fields.length; i < l; i ++ ) {
  1132. var field = fields[ i ];
  1133. var fieldName = field.name;
  1134. var fieldValues = field.values;
  1135. switch ( fieldName ) {
  1136. case 'radius':
  1137. radius = fieldValues[ 0 ];
  1138. break;
  1139. default:
  1140. console.warn( 'THREE.VRMLLoader: Unknown field:', fieldName );
  1141. break;
  1142. }
  1143. }
  1144. var geometry = new SphereBufferGeometry( radius, 16, 16 );
  1145. return geometry;
  1146. }
  1147. // helper functions
  1148. function resolveUSE( identifier ) {
  1149. var node = nodeMap[ identifier ];
  1150. var build = getNode( node );
  1151. // because the same 3D objects can have different transformations, it's necessary to clone them.
  1152. // materials can be influenced by the geometry (e.g. vertex normals). cloning is necessary to avoid
  1153. // any side effects
  1154. return ( build.isObject3D || build.isMaterial ) ? build.clone() : build;
  1155. }
  1156. function parseFieldChildren( children, owner ) {
  1157. for ( var i = 0, l = children.length; i < l; i ++ ) {
  1158. var object = getNode( children[ i ] );
  1159. if ( object instanceof Object3D ) owner.add( object );
  1160. }
  1161. }
  1162. function triangulateFaceIndex( index, ccw ) {
  1163. var indices = [];
  1164. // since face defintions can have more than three vertices, it's necessary to
  1165. // perform a simple triangulation
  1166. var start = 0;
  1167. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1168. var i1 = index[ start ];
  1169. var i2 = index[ i + ( ccw ? 1 : 2 ) ];
  1170. var i3 = index[ i + ( ccw ? 2 : 1 ) ];
  1171. indices.push( i1, i2, i3 );
  1172. // an index of -1 indicates that the current face has ended and the next one begins
  1173. if ( index[ i + 3 ] === - 1 || i + 3 >= l ) {
  1174. i += 3;
  1175. start = i + 1;
  1176. }
  1177. }
  1178. return indices;
  1179. }
  1180. function triangulateFaceData( data, index ) {
  1181. var triangulatedData = [];
  1182. var start = 0;
  1183. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1184. var stride = start * 3;
  1185. var x = data[ stride ];
  1186. var y = data[ stride + 1 ];
  1187. var z = data[ stride + 2 ];
  1188. triangulatedData.push( x, y, z );
  1189. // an index of -1 indicates that the current face has ended and the next one begins
  1190. if ( index[ i + 3 ] === - 1 || i + 3 >= l ) {
  1191. i += 3;
  1192. start ++;
  1193. }
  1194. }
  1195. return triangulatedData;
  1196. }
  1197. function flattenData( data, index ) {
  1198. var flattenData = [];
  1199. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1200. var i1 = index[ i ];
  1201. var stride = i1 * 3;
  1202. var x = data[ stride ];
  1203. var y = data[ stride + 1 ];
  1204. var z = data[ stride + 2 ];
  1205. flattenData.push( x, y, z );
  1206. }
  1207. return flattenData;
  1208. }
  1209. function expandLineIndex( index ) {
  1210. var indices = [];
  1211. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1212. var i1 = index[ i ];
  1213. var i2 = index[ i + 1 ];
  1214. indices.push( i1, i2 );
  1215. // an index of -1 indicates that the current line has ended and the next one begins
  1216. if ( index[ i + 2 ] === - 1 || i + 2 >= l ) {
  1217. i += 2;
  1218. }
  1219. }
  1220. return indices;
  1221. }
  1222. function expandLineData( data, index ) {
  1223. var triangulatedData = [];
  1224. var start = 0;
  1225. for ( var i = 0, l = index.length; i < l; i ++ ) {
  1226. var stride = start * 3;
  1227. var x = data[ stride ];
  1228. var y = data[ stride + 1 ];
  1229. var z = data[ stride + 2 ];
  1230. triangulatedData.push( x, y, z );
  1231. // an index of -1 indicates that the current line has ended and the next one begins
  1232. if ( index[ i + 2 ] === - 1 || i + 2 >= l ) {
  1233. i += 2;
  1234. start ++;
  1235. }
  1236. }
  1237. return triangulatedData;
  1238. }
  1239. var vA = new Vector3();
  1240. var vB = new Vector3();
  1241. var vC = new Vector3();
  1242. var uvA = new Vector2();
  1243. var uvB = new Vector2();
  1244. var uvC = new Vector2();
  1245. function computeAttributeFromIndexedData( coordIndex, index, data, itemSize ) {
  1246. var array = [];
  1247. // we use the coordIndex.length as delimiter since normalIndex must contain at least as many indices
  1248. for ( var i = 0, l = coordIndex.length; i < l; i += 3 ) {
  1249. var a = index[ i ];
  1250. var b = index[ i + 1 ];
  1251. var c = index[ i + 2 ];
  1252. if ( itemSize === 2 ) {
  1253. uvA.fromArray( data, a * itemSize );
  1254. uvB.fromArray( data, b * itemSize );
  1255. uvC.fromArray( data, c * itemSize );
  1256. array.push( uvA.x, uvA.y );
  1257. array.push( uvB.x, uvB.y );
  1258. array.push( uvC.x, uvC.y );
  1259. } else {
  1260. vA.fromArray( data, a * itemSize );
  1261. vB.fromArray( data, b * itemSize );
  1262. vC.fromArray( data, c * itemSize );
  1263. array.push( vA.x, vA.y, vA.z );
  1264. array.push( vB.x, vB.y, vB.z );
  1265. array.push( vC.x, vC.y, vC.z );
  1266. }
  1267. }
  1268. return new Float32BufferAttribute( array, itemSize );
  1269. }
  1270. function computeAttributeFromFaceData( index, faceData ) {
  1271. var array = [];
  1272. for ( var i = 0, j = 0, l = index.length; i < l; i += 3, j ++ ) {
  1273. vA.fromArray( faceData, j * 3 );
  1274. array.push( vA.x, vA.y, vA.z );
  1275. array.push( vA.x, vA.y, vA.z );
  1276. array.push( vA.x, vA.y, vA.z );
  1277. }
  1278. return new Float32BufferAttribute( array, 3 );
  1279. }
  1280. function computeAttributeFromLineData( index, lineData ) {
  1281. var array = [];
  1282. for ( var i = 0, j = 0, l = index.length; i < l; i += 2, j ++ ) {
  1283. vA.fromArray( lineData, j * 3 );
  1284. array.push( vA.x, vA.y, vA.z );
  1285. array.push( vA.x, vA.y, vA.z );
  1286. }
  1287. return new Float32BufferAttribute( array, 3 );
  1288. }
  1289. function toNonIndexedAttribute( indices, attribute ) {
  1290. var array = attribute.array;
  1291. var itemSize = attribute.itemSize;
  1292. var array2 = new array.constructor( indices.length * itemSize );
  1293. var index = 0, index2 = 0;
  1294. for ( var i = 0, l = indices.length; i < l; i ++ ) {
  1295. index = indices[ i ] * itemSize;
  1296. for ( var j = 0; j < itemSize; j ++ ) {
  1297. array2[ index2 ++ ] = array[ index ++ ];
  1298. }
  1299. }
  1300. return new Float32BufferAttribute( array2, itemSize );
  1301. }
  1302. var ab = new Vector3();
  1303. var cb = new Vector3();
  1304. function computeNormalAttribute( index, coord, creaseAngle ) {
  1305. var faces = [];
  1306. var vertexNormals = {};
  1307. // prepare face and raw vertex normals
  1308. for ( var i = 0, l = index.length; i < l; i += 3 ) {
  1309. var a = index[ i ];
  1310. var b = index[ i + 1 ];
  1311. var c = index[ i + 2 ];
  1312. var face = new Face( a, b, c );
  1313. vA.fromArray( coord, a * 3 );
  1314. vB.fromArray( coord, b * 3 );
  1315. vC.fromArray( coord, c * 3 );
  1316. cb.subVectors( vC, vB );
  1317. ab.subVectors( vA, vB );
  1318. cb.cross( ab );
  1319. cb.normalize();
  1320. face.normal.copy( cb );
  1321. if ( vertexNormals[ a ] === undefined ) vertexNormals[ a ] = [];
  1322. if ( vertexNormals[ b ] === undefined ) vertexNormals[ b ] = [];
  1323. if ( vertexNormals[ c ] === undefined ) vertexNormals[ c ] = [];
  1324. vertexNormals[ a ].push( face.normal );
  1325. vertexNormals[ b ].push( face.normal );
  1326. vertexNormals[ c ].push( face.normal );
  1327. faces.push( face );
  1328. }
  1329. // compute vertex normals and build final geometry
  1330. var normals = [];
  1331. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  1332. var face = faces[ i ];
  1333. var nA = weightedNormal( vertexNormals[ face.a ], face.normal, creaseAngle );
  1334. var nB = weightedNormal( vertexNormals[ face.b ], face.normal, creaseAngle );
  1335. var nC = weightedNormal( vertexNormals[ face.c ], face.normal, creaseAngle );
  1336. vA.fromArray( coord, face.a * 3 );
  1337. vB.fromArray( coord, face.b * 3 );
  1338. vC.fromArray( coord, face.c * 3 );
  1339. normals.push( nA.x, nA.y, nA.z );
  1340. normals.push( nB.x, nB.y, nB.z );
  1341. normals.push( nC.x, nC.y, nC.z );
  1342. }
  1343. return new Float32BufferAttribute( normals, 3 );
  1344. }
  1345. function weightedNormal( normals, vector, creaseAngle ) {
  1346. var normal = vector.clone();
  1347. for ( var i = 0, l = normals.length; i < l; i ++ ) {
  1348. if ( normals[ i ].angleTo( vector ) < creaseAngle ) {
  1349. normal.add( normals[ i ] );
  1350. }
  1351. }
  1352. return normal.normalize();
  1353. }
  1354. function toColorArray( colors ) {
  1355. var array = [];
  1356. for ( var i = 0, l = colors.length; i < l; i += 3 ) {
  1357. array.push( new Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );
  1358. }
  1359. return array;
  1360. }
  1361. /**
  1362. * Vertically paints the faces interpolating between the
  1363. * specified colors at the specified angels. This is used for the Background
  1364. * node, but could be applied to other nodes with multiple faces as well.
  1365. *
  1366. * When used with the Background node, default is directionIsDown is true if
  1367. * interpolating the skyColor down from the Zenith. When interpolationg up from
  1368. * the Nadir i.e. interpolating the groundColor, the directionIsDown is false.
  1369. *
  1370. * The first angle is never specified, it is the Zenith (0 rad). Angles are specified
  1371. * in radians. The geometry is thought a sphere, but could be anything. The color interpolation
  1372. * is linear along the Y axis in any case.
  1373. *
  1374. * You must specify one more color than you have angles at the beginning of the colors array.
  1375. * This is the color of the Zenith (the top of the shape).
  1376. *
  1377. * @param {BufferGeometry} geometry
  1378. * @param {number} radius
  1379. * @param {array} angles
  1380. * @param {array} colors
  1381. * @param {boolean} topDown - Whether to work top down or bottom up.
  1382. */
  1383. function paintFaces( geometry, radius, angles, colors, topDown ) {
  1384. var direction = ( topDown === true ) ? 1 : - 1;
  1385. var coord = [], A = {}, B = {}, applyColor = false;
  1386. for ( var k = 0; k < angles.length; k ++ ) {
  1387. // push the vector at which the color changes
  1388. var vec = {
  1389. x: direction * ( Math.cos( angles[ k ] ) * radius ),
  1390. y: direction * ( Math.sin( angles[ k ] ) * radius )
  1391. };
  1392. coord.push( vec );
  1393. }
  1394. var index = geometry.index;
  1395. var positionAttribute = geometry.attributes.position;
  1396. var colorAttribute = new BufferAttribute( new Float32Array( geometry.attributes.position.count * 3 ), 3 );
  1397. var position = new Vector3();
  1398. var color = new Color();
  1399. for ( var i = 0; i < index.count; i ++ ) {
  1400. var vertexIndex = index.getX( i );
  1401. position.fromBufferAttribute( positionAttribute, vertexIndex );
  1402. for ( var j = 0; j < colors.length; j ++ ) {
  1403. // linear interpolation between aColor and bColor, calculate proportion
  1404. // A is previous point (angle)
  1405. if ( j === 0 ) {
  1406. A.x = 0;
  1407. A.y = ( topDown === true ) ? radius : - 1 * radius;
  1408. } else {
  1409. A.x = coord[ j - 1 ].x;
  1410. A.y = coord[ j - 1 ].y;
  1411. }
  1412. // B is current point (angle)
  1413. B = coord[ j ];
  1414. if ( B !== undefined ) {
  1415. // p has to be between the points A and B which we interpolate
  1416. applyColor = ( topDown === true ) ? ( position.y <= A.y && position.y > B.y ) : ( position.y >= A.y && position.y < B.y );
  1417. if ( applyColor === true ) {
  1418. var aColor = colors[ j ];
  1419. var bColor = colors[ j + 1 ];
  1420. // below is simple linear interpolation
  1421. var t = Math.abs( position.y - A.y ) / ( A.y - B.y );
  1422. // 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
  1423. color.copy( aColor ).lerp( bColor, t );
  1424. colorAttribute.setXYZ( vertexIndex, color.r, color.g, color.b );
  1425. } else {
  1426. var colorIndex = ( topDown === true ) ? colors.length - 1 : 0;
  1427. var c = colors[ colorIndex ];
  1428. colorAttribute.setXYZ( vertexIndex, c.r, c.g, c.b );
  1429. }
  1430. }
  1431. }
  1432. }
  1433. geometry.addAttribute( 'color', colorAttribute );
  1434. }
  1435. //
  1436. var textureLoader = new TextureLoader( this.manager );
  1437. textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
  1438. // create JSON representing the tree structure of the VRML asset
  1439. var tree = generateVRMLTree( data );
  1440. // check version (only 2.0 is supported)
  1441. if ( tree.version.indexOf( 'V2.0' ) === - 1 ) {
  1442. throw Error( 'THREE.VRMLLexer: Version of VRML asset not supported.' );
  1443. }
  1444. // parse the tree structure to a three.js scene
  1445. var scene = parseTree( tree );
  1446. return scene;
  1447. }
  1448. };
  1449. function VRMLLexer( tokens ) {
  1450. this.lexer = new chevrotain.Lexer( tokens );
  1451. }
  1452. VRMLLexer.prototype = {
  1453. constructor: VRMLLexer,
  1454. lex: function ( inputText ) {
  1455. var lexingResult = this.lexer.tokenize( inputText );
  1456. if ( lexingResult.errors.length > 0 ) {
  1457. console.error( lexingResult.errors );
  1458. throw Error( 'THREE.VRMLLexer: Lexing errors detected.' );
  1459. }
  1460. return lexingResult;
  1461. }
  1462. };
  1463. function VRMLParser( tokenVocabulary ) {
  1464. chevrotain.Parser.call( this, tokenVocabulary );
  1465. var $ = this;
  1466. var Version = tokenVocabulary[ 'Version' ];
  1467. var LCurly = tokenVocabulary[ 'LCurly' ];
  1468. var RCurly = tokenVocabulary[ 'RCurly' ];
  1469. var LSquare = tokenVocabulary[ 'LSquare' ];
  1470. var RSquare = tokenVocabulary[ 'RSquare' ];
  1471. var Identifier = tokenVocabulary[ 'Identifier' ];
  1472. var RouteIdentifier = tokenVocabulary[ 'RouteIdentifier' ];
  1473. var StringLiteral = tokenVocabulary[ 'StringLiteral' ];
  1474. var NumberLiteral = tokenVocabulary[ 'NumberLiteral' ];
  1475. var BooleanLiteral = tokenVocabulary[ 'BooleanLiteral' ];
  1476. var NullLiteral = tokenVocabulary[ 'NullLiteral' ];
  1477. var DEF = tokenVocabulary[ 'DEF' ];
  1478. var USE = tokenVocabulary[ 'USE' ];
  1479. var ROUTE = tokenVocabulary[ 'ROUTE' ];
  1480. var TO = tokenVocabulary[ 'TO' ];
  1481. var NodeName = tokenVocabulary[ 'NodeName' ];
  1482. $.RULE( 'vrml', function () {
  1483. $.SUBRULE( $.version );
  1484. $.AT_LEAST_ONE( function () {
  1485. $.SUBRULE( $.node );
  1486. } );
  1487. $.MANY( function () {
  1488. $.SUBRULE( $.route );
  1489. } );
  1490. } );
  1491. $.RULE( 'version', function () {
  1492. $.CONSUME( Version );
  1493. } );
  1494. $.RULE( 'node', function () {
  1495. $.OPTION( function () {
  1496. $.SUBRULE( $.def );
  1497. } );
  1498. $.CONSUME( NodeName );
  1499. $.CONSUME( LCurly );
  1500. $.MANY( function () {
  1501. $.SUBRULE( $.field );
  1502. } );
  1503. $.CONSUME( RCurly );
  1504. } );
  1505. $.RULE( 'field', function () {
  1506. $.CONSUME( Identifier );
  1507. $.OR2( [
  1508. { ALT: function () {
  1509. $.SUBRULE( $.singleFieldValue );
  1510. } },
  1511. { ALT: function () {
  1512. $.SUBRULE( $.multiFieldValue );
  1513. } }
  1514. ] );
  1515. } );
  1516. $.RULE( 'def', function () {
  1517. $.CONSUME( DEF );
  1518. $.CONSUME( Identifier );
  1519. } );
  1520. $.RULE( 'use', function () {
  1521. $.CONSUME( USE );
  1522. $.CONSUME( Identifier );
  1523. } );
  1524. $.RULE( 'singleFieldValue', function () {
  1525. $.AT_LEAST_ONE( function () {
  1526. $.OR( [
  1527. { ALT: function () {
  1528. $.SUBRULE( $.node );
  1529. } },
  1530. { ALT: function () {
  1531. $.SUBRULE( $.use );
  1532. } },
  1533. { ALT: function () {
  1534. $.CONSUME( StringLiteral );
  1535. } },
  1536. { ALT: function () {
  1537. $.CONSUME( NumberLiteral );
  1538. } },
  1539. { ALT: function () {
  1540. $.CONSUME( BooleanLiteral );
  1541. } },
  1542. { ALT: function () {
  1543. $.CONSUME( NullLiteral );
  1544. } }
  1545. ] );
  1546. } );
  1547. } );
  1548. $.RULE( 'multiFieldValue', function () {
  1549. $.CONSUME( LSquare );
  1550. $.MANY( function () {
  1551. $.OR( [
  1552. { ALT: function () {
  1553. $.SUBRULE( $.node );
  1554. } },
  1555. { ALT: function () {
  1556. $.SUBRULE( $.use );
  1557. } },
  1558. { ALT: function () {
  1559. $.CONSUME( StringLiteral );
  1560. } },
  1561. { ALT: function () {
  1562. $.CONSUME( NumberLiteral );
  1563. } },
  1564. { ALT: function () {
  1565. $.CONSUME( NullLiteral );
  1566. } }
  1567. ] );
  1568. } );
  1569. $.CONSUME( RSquare );
  1570. } );
  1571. $.RULE( 'route', function () {
  1572. $.CONSUME( ROUTE );
  1573. $.CONSUME( RouteIdentifier );
  1574. $.CONSUME( TO );
  1575. $.CONSUME2( RouteIdentifier );
  1576. } );
  1577. this.performSelfAnalysis();
  1578. }
  1579. VRMLParser.prototype = Object.create( chevrotain.Parser.prototype );
  1580. VRMLParser.prototype.constructor = VRMLParser;
  1581. function Face( a, b, c ) {
  1582. this.a = a;
  1583. this.b = b;
  1584. this.c = c;
  1585. this.normal = new Vector3();
  1586. }
  1587. return VRMLLoader;
  1588. } )();
  1589. export { VRMLLoader };