VRMLLoader.js 67 KB

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