VRMLLoader.js 54 KB

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