VRMLLoader.js 60 KB

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