VRMLLoader.js 60 KB

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