VRMLLoader.js 60 KB

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