LDrawLoader.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author yomboprime / https://github.com/yomboprime/
  4. *
  5. *
  6. */
  7. THREE.LDrawLoader = ( function () {
  8. var tempVec0 = new THREE.Vector3();
  9. var tempVec1 = new THREE.Vector3();
  10. function smoothNormals( triangles, lineSegments ) {
  11. function hashVertex( v ) {
  12. var x = ~ ~ ( v.x * 1e6 );
  13. var y = ~ ~ ( v.y * 1e6 );
  14. var z = ~ ~ ( v.z * 1e6 );
  15. return `${ x },${ y },${ z }`;
  16. }
  17. function hashEdge( v0, v1 ) {
  18. return `${ hashVertex( v0 ) }_${ hashVertex( v1 ) }`;
  19. }
  20. var hardEdges = new Set();
  21. var halfEdgeList = {};
  22. var fullHalfEdgeList = {};
  23. var normals = [];
  24. // Save the list of hard edges by hash
  25. for ( var i = 0, l = lineSegments.length; i < l; i ++ ) {
  26. var ls = lineSegments[ i ];
  27. var v0 = ls.v0;
  28. var v1 = ls.v1;
  29. hardEdges.add( hashEdge( v0, v1 ) );
  30. hardEdges.add( hashEdge( v1, v0 ) );
  31. }
  32. // track the half edges associated with each triangle
  33. for ( var i = 0, l = triangles.length; i < l; i ++ ) {
  34. var tri = triangles[ i ];
  35. for ( var i2 = 0, l2 = 3; i2 < l2; i2 ++ ) {
  36. var index = i2;
  37. var next = ( i2 + 1 ) % 3;
  38. var v0 = tri[ `v${ index }` ];
  39. var v1 = tri[ `v${ next }` ];
  40. var hash = hashEdge( v0, v1 );
  41. // don't add the triangle if the edge is supposed to be hard
  42. if ( hardEdges.has( hash ) ) continue;
  43. halfEdgeList[ hash ] = tri;
  44. fullHalfEdgeList[ hash ] = tri;
  45. }
  46. }
  47. // Iterate until we've tried to connect all triangles to share normals
  48. while ( true ) {
  49. // Stop if there are no more triangles left
  50. var halfEdges = Object.keys( halfEdgeList );
  51. if ( halfEdges.length === 0 ) break;
  52. // Exhaustively find all connected triangles
  53. var i = 0;
  54. var queue = [ fullHalfEdgeList[ halfEdges[ 0 ] ] ];
  55. while ( i < queue.length ) {
  56. // initialize all vertex normals in this triangle
  57. var tri = queue[ i ];
  58. i ++;
  59. var faceNormal = tri.faceNormal;
  60. if ( tri.n0 === null ) {
  61. tri.n0 = faceNormal.clone();
  62. normals.push( tri.n0 );
  63. }
  64. if ( tri.n1 === null ) {
  65. tri.n1 = faceNormal.clone();
  66. normals.push( tri.n1 );
  67. }
  68. if ( tri.n2 === null ) {
  69. tri.n2 = faceNormal.clone();
  70. normals.push( tri.n2 );
  71. }
  72. // Check if any edge is connected to another triangle edge
  73. for ( var i2 = 0, l2 = 3; i2 < l2; i2 ++ ) {
  74. var index = i2;
  75. var next = ( i2 + 1 ) % 3;
  76. var v0 = tri[ `v${ index }` ];
  77. var v1 = tri[ `v${ next }` ];
  78. // delete this triangle from the list so it won't be found again
  79. var hash = hashEdge( v0, v1 );
  80. delete halfEdgeList[ hash ];
  81. var reverseHash = hashEdge( v1, v0 );
  82. var otherTri = fullHalfEdgeList[ reverseHash ];
  83. if ( otherTri ) {
  84. // if this triangle has already been traversed then it won't be in
  85. // the halfEdgeList. If it has not then add it to the queue and delete
  86. // it so it won't be found again.
  87. if ( reverseHash in halfEdgeList ) {
  88. queue.push( otherTri );
  89. delete halfEdgeList[ reverseHash ];
  90. }
  91. // Find the matching edge in this triangle and copy the normal vector over
  92. for ( var i3 = 0, l3 = 3; i3 < l3; i3 ++ ) {
  93. var otherIndex = i3;
  94. var otherNext = ( i3 + 1 ) % 3;
  95. var otherV0 = otherTri[ `v${ otherIndex }` ];
  96. var otherV1 = otherTri[ `v${ otherNext }` ];
  97. var otherHash = hashEdge( otherV0, otherV1 );
  98. if ( otherHash === reverseHash ) {
  99. otherTri[ `n${ otherIndex }` ] = tri[ `n${ next }` ];
  100. otherTri[ `n${ otherNext }` ] = tri[ `n${ index }` ];
  101. tri[ `n${ next }` ].add( otherTri.faceNormal );
  102. tri[ `n${ index }` ].add( otherTri.faceNormal );
  103. break;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. // The normals of each face have been added up so now we average them by normalizing the vector.
  111. for ( var i = 0, l = normals.length; i < l; i ++ ) {
  112. normals[ i ].normalize();
  113. }
  114. }
  115. function isPrimitiveType( type ) {
  116. return /primitive/i.test( type ) || type === 'Subpart';
  117. }
  118. function LineParser( line, lineNumber ) {
  119. this.line = line;
  120. this.lineLength = line.length;
  121. this.currentCharIndex = 0;
  122. this.currentChar = ' ';
  123. this.lineNumber = lineNumber;
  124. }
  125. LineParser.prototype = {
  126. constructor: LineParser,
  127. seekNonSpace: function () {
  128. while ( this.currentCharIndex < this.lineLength ) {
  129. this.currentChar = this.line.charAt( this.currentCharIndex );
  130. if ( this.currentChar !== ' ' && this.currentChar !== '\t' ) {
  131. return;
  132. }
  133. this.currentCharIndex ++;
  134. }
  135. },
  136. getToken: function () {
  137. var pos0 = this.currentCharIndex ++;
  138. // Seek space
  139. while ( this.currentCharIndex < this.lineLength ) {
  140. this.currentChar = this.line.charAt( this.currentCharIndex );
  141. if ( this.currentChar === ' ' || this.currentChar === '\t' ) {
  142. break;
  143. }
  144. this.currentCharIndex ++;
  145. }
  146. var pos1 = this.currentCharIndex;
  147. this.seekNonSpace();
  148. return this.line.substring( pos0, pos1 );
  149. },
  150. getRemainingString: function () {
  151. return this.line.substring( this.currentCharIndex, this.lineLength );
  152. },
  153. isAtTheEnd: function () {
  154. return this.currentCharIndex >= this.lineLength;
  155. },
  156. setToEnd: function () {
  157. this.currentCharIndex = this.lineLength;
  158. },
  159. getLineNumberString: function () {
  160. return this.lineNumber >= 0 ? " at line " + this.lineNumber : "";
  161. }
  162. };
  163. function sortByMaterial( a, b ) {
  164. if ( a.colourCode === b.colourCode ) {
  165. return 0;
  166. }
  167. if ( a.colourCode < b.colourCode ) {
  168. return - 1;
  169. }
  170. return 1;
  171. }
  172. function createObject( elements, elementSize ) {
  173. // Creates a THREE.LineSegments (elementSize = 2) or a THREE.Mesh (elementSize = 3 )
  174. // With per face / segment material, implemented with mesh groups and materials array
  175. // Sort the triangles or line segments by colour code to make later the mesh groups
  176. elements.sort( sortByMaterial );
  177. var positions = [];
  178. var normals = [];
  179. var materials = [];
  180. var bufferGeometry = new THREE.BufferGeometry();
  181. var prevMaterial = null;
  182. var index0 = 0;
  183. var numGroupVerts = 0;
  184. for ( var iElem = 0, nElem = elements.length; iElem < nElem; iElem ++ ) {
  185. var elem = elements[ iElem ];
  186. var v0 = elem.v0;
  187. var v1 = elem.v1;
  188. // Note that LDraw coordinate system is rotated 180 deg. in the X axis w.r.t. Three.js's one
  189. positions.push( v0.x, v0.y, v0.z, v1.x, v1.y, v1.z );
  190. if ( elementSize === 3 ) {
  191. positions.push( elem.v2.x, elem.v2.y, elem.v2.z );
  192. var n0 = elem.n0 || elem.faceNormal;
  193. var n1 = elem.n1 || elem.faceNormal;
  194. var n2 = elem.n2 || elem.faceNormal;
  195. normals.push( n0.x, n0.y, n0.z );
  196. normals.push( n1.x, n1.y, n1.z );
  197. normals.push( n2.x, n2.y, n2.z );
  198. }
  199. if ( prevMaterial !== elem.material ) {
  200. if ( prevMaterial !== null ) {
  201. bufferGeometry.addGroup( index0, numGroupVerts, materials.length - 1 );
  202. }
  203. materials.push( elem.material );
  204. prevMaterial = elem.material;
  205. index0 = iElem * elementSize;
  206. numGroupVerts = elementSize;
  207. } else {
  208. numGroupVerts += elementSize;
  209. }
  210. }
  211. if ( numGroupVerts > 0 ) {
  212. bufferGeometry.addGroup( index0, Infinity, materials.length - 1 );
  213. }
  214. bufferGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  215. if ( elementSize === 3 ) {
  216. bufferGeometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  217. }
  218. var object3d = null;
  219. if ( elementSize === 2 ) {
  220. object3d = new THREE.LineSegments( bufferGeometry, materials );
  221. } else if ( elementSize === 3 ) {
  222. object3d = new THREE.Mesh( bufferGeometry, materials );
  223. }
  224. return object3d;
  225. }
  226. //
  227. function LDrawLoader( manager ) {
  228. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  229. // This is a stack of 'parse scopes' with one level per subobject loaded file.
  230. // Each level contains a material lib and also other runtime variables passed between parent and child subobjects
  231. // When searching for a material code, the stack is read from top of the stack to bottom
  232. // Each material library is an object map keyed by colour codes.
  233. this.parseScopesStack = null;
  234. this.path = '';
  235. // Array of THREE.Material
  236. this.materials = [];
  237. // Not using THREE.Cache here because it returns the previous HTML error response instead of calling onError()
  238. // This also allows to handle the embedded text files ("0 FILE" lines)
  239. this.subobjectCache = {};
  240. // This object is a map from file names to paths. It agilizes the paths search. If it is not set then files will be searched by trial and error.
  241. this.fileMap = null;
  242. // Add default main triangle and line edge materials (used in piecess that can be coloured with a main color)
  243. this.setMaterials( [
  244. this.parseColourMetaDirective( new LineParser( "Main_Colour CODE 16 VALUE #FF8080 EDGE #333333" ) ),
  245. this.parseColourMetaDirective( new LineParser( "Edge_Colour CODE 24 VALUE #A0A0A0 EDGE #333333" ) )
  246. ] );
  247. // If this flag is set to true, each subobject will be a THREE.Object.
  248. // If not (the default), only one object which contains all the merged primitives will be created.
  249. this.separateObjects = false;
  250. // If this flag is set to true the vertex normals will be smoothed.
  251. this.smoothNormals = true;
  252. }
  253. // Special surface finish tag types.
  254. // Note: "MATERIAL" tag (e.g. GLITTER, SPECKLE) is not implemented
  255. LDrawLoader.FINISH_TYPE_DEFAULT = 0;
  256. LDrawLoader.FINISH_TYPE_CHROME = 1;
  257. LDrawLoader.FINISH_TYPE_PEARLESCENT = 2;
  258. LDrawLoader.FINISH_TYPE_RUBBER = 3;
  259. LDrawLoader.FINISH_TYPE_MATTE_METALLIC = 4;
  260. LDrawLoader.FINISH_TYPE_METAL = 5;
  261. // State machine to search a subobject path.
  262. // The LDraw standard establishes these various possible subfolders.
  263. LDrawLoader.FILE_LOCATION_AS_IS = 0;
  264. LDrawLoader.FILE_LOCATION_TRY_PARTS = 1;
  265. LDrawLoader.FILE_LOCATION_TRY_P = 2;
  266. LDrawLoader.FILE_LOCATION_TRY_MODELS = 3;
  267. LDrawLoader.FILE_LOCATION_TRY_RELATIVE = 4;
  268. LDrawLoader.FILE_LOCATION_TRY_ABSOLUTE = 5;
  269. LDrawLoader.FILE_LOCATION_NOT_FOUND = 6;
  270. LDrawLoader.prototype = {
  271. constructor: LDrawLoader,
  272. load: function ( url, onLoad, onProgress, onError ) {
  273. if ( ! this.fileMap ) {
  274. this.fileMap = {};
  275. }
  276. var scope = this;
  277. var fileLoader = new THREE.FileLoader( this.manager );
  278. fileLoader.setPath( this.path );
  279. fileLoader.load( url, function ( text ) {
  280. processObject( text, onLoad );
  281. }, onProgress, onError );
  282. function processObject( text, onProcessed, subobject ) {
  283. var parseScope = scope.newParseScopeLevel();
  284. parseScope.url = url;
  285. var parentParseScope = scope.getParentParseScope();
  286. // Set current matrix
  287. if ( subobject ) {
  288. parseScope.currentMatrix.multiplyMatrices( parentParseScope.currentMatrix, subobject.matrix );
  289. parseScope.matrix.copy( subobject.matrix );
  290. parseScope.inverted = subobject.inverted;
  291. }
  292. // Add to cache
  293. var currentFileName = parentParseScope.currentFileName;
  294. if ( currentFileName !== null ) {
  295. currentFileName = parentParseScope.currentFileName.toLowerCase();
  296. }
  297. if ( scope.subobjectCache[ currentFileName ] === undefined ) {
  298. scope.subobjectCache[ currentFileName ] = text;
  299. }
  300. // Parse the object (returns a THREE.Group)
  301. scope.parse( text );
  302. var finishedCount = 0;
  303. onSubobjectFinish();
  304. function onSubobjectFinish() {
  305. finishedCount ++;
  306. if ( finishedCount === parseScope.subobjects.length + 1 ) {
  307. finalizeObject();
  308. } else {
  309. // Once the previous subobject has finished we can start processing the next one in the list.
  310. // The subobject processing shares scope in processing so it's important that they be loaded serially
  311. // to avoid race conditions.
  312. // Promise.resolve is used as an approach to asynchronously schedule a task _before_ this frame ends to
  313. // avoid stack overflow exceptions when loading many subobjects from the cache. RequestAnimationFrame
  314. // will work but causes the load to happen after the next frame which causes the load to take significantly longer.
  315. var subobject = parseScope.subobjects[ parseScope.subobjectIndex ];
  316. Promise.resolve().then( function () {
  317. loadSubobject( subobject );
  318. } );
  319. parseScope.subobjectIndex ++;
  320. }
  321. }
  322. function finalizeObject() {
  323. if ( scope.smoothNormals && parseScope.type === 'Part' ) {
  324. smoothNormals( parseScope.triangles, parseScope.lineSegments );
  325. }
  326. var isRoot = ! parentParseScope.isFromParse;
  327. if ( scope.separateObjects && ! isPrimitiveType( parseScope.type ) || isRoot ) {
  328. const objGroup = parseScope.groupObject;
  329. if ( parseScope.triangles.length > 0 ) {
  330. objGroup.add( createObject( parseScope.triangles, 3 ) );
  331. }
  332. if ( parseScope.lineSegments.length > 0 ) {
  333. objGroup.add( createObject( parseScope.lineSegments, 2 ) );
  334. }
  335. if ( parseScope.conditionalSegments.length > 0 ) {
  336. objGroup.add( createObject( parseScope.conditionalSegments, 2 ) );
  337. }
  338. if ( parentParseScope.groupObject ) {
  339. objGroup.name = parseScope.fileName;
  340. objGroup.matrix.copy( parseScope.matrix );
  341. objGroup.matrix.decompose( objGroup.position, objGroup.quaternion, objGroup.scale );
  342. objGroup.matrixAutoUpdate = false;
  343. parentParseScope.groupObject.add( objGroup );
  344. }
  345. } else {
  346. var separateObjects = scope.separateObjects;
  347. var parentLineSegments = parentParseScope.lineSegments;
  348. var parentConditionalSegments = parentParseScope.conditionalSegments;
  349. var parentTriangles = parentParseScope.triangles;
  350. var lineSegments = parseScope.lineSegments;
  351. var conditionalSegments = parseScope.conditionalSegments;
  352. var triangles = parseScope.triangles;
  353. for ( var i = 0, l = lineSegments.length; i < l; i ++ ) {
  354. var ls = lineSegments[ i ];
  355. if ( separateObjects ) {
  356. ls.v0.applyMatrix4( parseScope.matrix );
  357. ls.v1.applyMatrix4( parseScope.matrix );
  358. }
  359. parentLineSegments.push( ls );
  360. }
  361. for ( var i = 0, l = conditionalSegments.length; i < l; i ++ ) {
  362. var os = conditionalSegments[ i ];
  363. if ( separateObjects ) {
  364. os.v0.applyMatrix4( parseScope.matrix );
  365. os.v1.applyMatrix4( parseScope.matrix );
  366. }
  367. parentConditionalSegments.push( os );
  368. }
  369. for ( var i = 0, l = triangles.length; i < l; i ++ ) {
  370. var tri = triangles[ i ];
  371. if ( separateObjects ) {
  372. tri.v0 = tri.v0.clone().applyMatrix4( parseScope.matrix );
  373. tri.v1 = tri.v1.clone().applyMatrix4( parseScope.matrix );
  374. tri.v2 = tri.v2.clone().applyMatrix4( parseScope.matrix );
  375. tempVec0.subVectors( tri.v1, tri.v0 );
  376. tempVec1.subVectors( tri.v2, tri.v1 );
  377. tri.faceNormal.crossVectors( tempVec0, tempVec1 ).normalize();
  378. }
  379. parentTriangles.push( tri );
  380. }
  381. }
  382. scope.removeScopeLevel();
  383. if ( onProcessed ) {
  384. onProcessed( parseScope.groupObject );
  385. }
  386. }
  387. function loadSubobject( subobject ) {
  388. parseScope.mainColourCode = subobject.material.userData.code;
  389. parseScope.mainEdgeColourCode = subobject.material.userData.edgeMaterial.userData.code;
  390. parseScope.currentFileName = subobject.originalFileName;
  391. // If subobject was cached previously, use the cached one
  392. var cached = scope.subobjectCache[ subobject.originalFileName.toLowerCase() ];
  393. if ( cached ) {
  394. processObject( cached, function ( subobjectGroup ) {
  395. onSubobjectLoaded( subobjectGroup, subobject );
  396. onSubobjectFinish();
  397. }, subobject );
  398. return;
  399. }
  400. // Adjust file name to locate the subobject file path in standard locations (always under directory scope.path)
  401. // Update also subobject.locationState for the next try if this load fails.
  402. var subobjectURL = subobject.fileName;
  403. var newLocationState = LDrawLoader.FILE_LOCATION_NOT_FOUND;
  404. switch ( subobject.locationState ) {
  405. case LDrawLoader.FILE_LOCATION_AS_IS:
  406. newLocationState = subobject.locationState + 1;
  407. break;
  408. case LDrawLoader.FILE_LOCATION_TRY_PARTS:
  409. subobjectURL = 'parts/' + subobjectURL;
  410. newLocationState = subobject.locationState + 1;
  411. break;
  412. case LDrawLoader.FILE_LOCATION_TRY_P:
  413. subobjectURL = 'p/' + subobjectURL;
  414. newLocationState = subobject.locationState + 1;
  415. break;
  416. case LDrawLoader.FILE_LOCATION_TRY_MODELS:
  417. subobjectURL = 'models/' + subobjectURL;
  418. newLocationState = subobject.locationState + 1;
  419. break;
  420. case LDrawLoader.FILE_LOCATION_TRY_RELATIVE:
  421. subobjectURL = url.substring( 0, url.lastIndexOf( "/" ) + 1 ) + subobjectURL;
  422. newLocationState = subobject.locationState + 1;
  423. break;
  424. case LDrawLoader.FILE_LOCATION_TRY_ABSOLUTE:
  425. if ( subobject.triedLowerCase ) {
  426. // Try absolute path
  427. newLocationState = LDrawLoader.FILE_LOCATION_NOT_FOUND;
  428. } else {
  429. // Next attempt is lower case
  430. subobject.fileName = subobject.fileName.toLowerCase();
  431. subobjectURL = subobject.fileName;
  432. subobject.triedLowerCase = true;
  433. newLocationState = LDrawLoader.FILE_LOCATION_AS_IS;
  434. }
  435. break;
  436. case LDrawLoader.FILE_LOCATION_NOT_FOUND:
  437. // All location possibilities have been tried, give up loading this object
  438. console.warn( 'LDrawLoader: Subobject "' + subobject.originalFileName + '" could not be found.' );
  439. return;
  440. }
  441. subobject.locationState = newLocationState;
  442. subobject.url = subobjectURL;
  443. // Load the subobject
  444. // Use another file loader here so we can keep track of the subobject information
  445. // and use it when processing the next model.
  446. var fileLoader = new THREE.FileLoader( scope.manager );
  447. fileLoader.setPath( scope.path );
  448. fileLoader.load( subobjectURL, function ( text ) {
  449. processObject( text, function ( subobjectGroup ) {
  450. onSubobjectLoaded( subobjectGroup, subobject );
  451. onSubobjectFinish();
  452. }, subobject );
  453. }, undefined, function ( err ) {
  454. onSubobjectError( err, subobject );
  455. }, subobject );
  456. }
  457. function onSubobjectLoaded( subobjectGroup, subobject ) {
  458. if ( subobjectGroup === null ) {
  459. // Try to reload
  460. loadSubobject( subobject );
  461. return;
  462. }
  463. scope.fileMap[ subobject.originalFileName ] = subobject.url;
  464. }
  465. function onSubobjectError( err, subobject ) {
  466. // Retry download from a different default possible location
  467. loadSubobject( subobject );
  468. }
  469. }
  470. },
  471. setPath: function ( value ) {
  472. this.path = value;
  473. return this;
  474. },
  475. setMaterials: function ( materials ) {
  476. // Clears parse scopes stack, adds new scope with material library
  477. this.parseScopesStack = [];
  478. this.newParseScopeLevel( materials );
  479. this.getCurrentParseScope().isFromParse = false;
  480. this.materials = materials;
  481. return this;
  482. },
  483. setFileMap: function ( fileMap ) {
  484. this.fileMap = fileMap;
  485. return this;
  486. },
  487. newParseScopeLevel: function ( materials ) {
  488. // Adds a new scope level, assign materials to it and returns it
  489. var matLib = {};
  490. if ( materials ) {
  491. for ( var i = 0, n = materials.length; i < n; i ++ ) {
  492. var material = materials[ i ];
  493. matLib[ material.userData.code ] = material;
  494. }
  495. }
  496. var topParseScope = this.getCurrentParseScope();
  497. var newParseScope = {
  498. lib: matLib,
  499. url: null,
  500. // Subobjects
  501. subobjects: null,
  502. numSubobjects: 0,
  503. subobjectIndex: 0,
  504. inverted: false,
  505. // Current subobject
  506. currentFileName: null,
  507. mainColourCode: topParseScope ? topParseScope.mainColourCode : '16',
  508. mainEdgeColourCode: topParseScope ? topParseScope.mainEdgeColourCode : '24',
  509. currentMatrix: new THREE.Matrix4(),
  510. matrix: new THREE.Matrix4(),
  511. // If false, it is a root material scope previous to parse
  512. isFromParse: true,
  513. triangles: null,
  514. lineSegments: null,
  515. conditionalSegments: null,
  516. };
  517. this.parseScopesStack.push( newParseScope );
  518. return newParseScope;
  519. },
  520. removeScopeLevel: function () {
  521. this.parseScopesStack.pop();
  522. return this;
  523. },
  524. addMaterial: function ( material ) {
  525. // Adds a material to the material library which is on top of the parse scopes stack. And also to the materials array
  526. var matLib = this.getCurrentParseScope().lib;
  527. if ( ! matLib[ material.userData.code ] ) {
  528. this.materials.push( material );
  529. }
  530. matLib[ material.userData.code ] = material;
  531. return this;
  532. },
  533. getMaterial: function ( colourCode ) {
  534. // Given a colour code search its material in the parse scopes stack
  535. if ( colourCode.startsWith( "0x2" ) ) {
  536. // Special 'direct' material value (RGB colour)
  537. var colour = colourCode.substring( 3 );
  538. return this.parseColourMetaDirective( new LineParser( "Direct_Color_" + colour + " CODE -1 VALUE #" + colour + " EDGE #" + colour + "" ) );
  539. }
  540. for ( var i = this.parseScopesStack.length - 1; i >= 0; i -- ) {
  541. var material = this.parseScopesStack[ i ].lib[ colourCode ];
  542. if ( material ) {
  543. return material;
  544. }
  545. }
  546. // Material was not found
  547. return null;
  548. },
  549. getParentParseScope: function () {
  550. if ( this.parseScopesStack.length > 1 ) {
  551. return this.parseScopesStack[ this.parseScopesStack.length - 2 ];
  552. }
  553. return null;
  554. },
  555. getCurrentParseScope: function () {
  556. if ( this.parseScopesStack.length > 0 ) {
  557. return this.parseScopesStack[ this.parseScopesStack.length - 1 ];
  558. }
  559. return null;
  560. },
  561. parseColourMetaDirective: function ( lineParser ) {
  562. // Parses a colour definition and returns a THREE.Material or null if error
  563. var code = null;
  564. // Triangle and line colours
  565. var colour = 0xFF00FF;
  566. var edgeColour = 0xFF00FF;
  567. // Transparency
  568. var alpha = 1;
  569. var isTransparent = false;
  570. // Self-illumination:
  571. var luminance = 0;
  572. var finishType = LDrawLoader.FINISH_TYPE_DEFAULT;
  573. var canHaveEnvMap = true;
  574. var edgeMaterial = null;
  575. var name = lineParser.getToken();
  576. if ( ! name ) {
  577. throw 'LDrawLoader: Material name was expected after "!COLOUR tag' + lineParser.getLineNumberString() + ".";
  578. }
  579. // Parse tag tokens and their parameters
  580. var token = null;
  581. while ( true ) {
  582. token = lineParser.getToken();
  583. if ( ! token ) {
  584. break;
  585. }
  586. switch ( token.toUpperCase() ) {
  587. case "CODE":
  588. code = lineParser.getToken();
  589. break;
  590. case "VALUE":
  591. colour = lineParser.getToken();
  592. if ( colour.startsWith( '0x' ) ) {
  593. colour = '#' + colour.substring( 2 );
  594. } else if ( ! colour.startsWith( '#' ) ) {
  595. throw 'LDrawLoader: Invalid colour while parsing material' + lineParser.getLineNumberString() + ".";
  596. }
  597. break;
  598. case "EDGE":
  599. edgeColour = lineParser.getToken();
  600. if ( edgeColour.startsWith( '0x' ) ) {
  601. edgeColour = '#' + edgeColour.substring( 2 );
  602. } else if ( ! edgeColour.startsWith( '#' ) ) {
  603. // Try to see if edge colour is a colour code
  604. edgeMaterial = this.getMaterial( edgeColour );
  605. if ( ! edgeMaterial ) {
  606. throw 'LDrawLoader: Invalid edge colour while parsing material' + lineParser.getLineNumberString() + ".";
  607. }
  608. // Get the edge material for this triangle material
  609. edgeMaterial = edgeMaterial.userData.edgeMaterial;
  610. }
  611. break;
  612. case 'ALPHA':
  613. alpha = parseInt( lineParser.getToken() );
  614. if ( isNaN( alpha ) ) {
  615. throw 'LDrawLoader: Invalid alpha value in material definition' + lineParser.getLineNumberString() + ".";
  616. }
  617. alpha = Math.max( 0, Math.min( 1, alpha / 255 ) );
  618. if ( alpha < 1 ) {
  619. isTransparent = true;
  620. }
  621. break;
  622. case 'LUMINANCE':
  623. luminance = parseInt( lineParser.getToken() );
  624. if ( isNaN( luminance ) ) {
  625. throw 'LDrawLoader: Invalid luminance value in material definition' + LineParser.getLineNumberString() + ".";
  626. }
  627. luminance = Math.max( 0, Math.min( 1, luminance / 255 ) );
  628. break;
  629. case 'CHROME':
  630. finishType = LDrawLoader.FINISH_TYPE_CHROME;
  631. break;
  632. case 'PEARLESCENT':
  633. finishType = LDrawLoader.FINISH_TYPE_PEARLESCENT;
  634. break;
  635. case 'RUBBER':
  636. finishType = LDrawLoader.FINISH_TYPE_RUBBER;
  637. break;
  638. case 'MATTE_METALLIC':
  639. finishType = LDrawLoader.FINISH_TYPE_MATTE_METALLIC;
  640. break;
  641. case 'METAL':
  642. finishType = LDrawLoader.FINISH_TYPE_METAL;
  643. break;
  644. case 'MATERIAL':
  645. // Not implemented
  646. lineParser.setToEnd();
  647. break;
  648. default:
  649. throw 'LDrawLoader: Unknown token "' + token + '" while parsing material' + lineParser.getLineNumberString() + ".";
  650. break;
  651. }
  652. }
  653. var material = null;
  654. switch ( finishType ) {
  655. case LDrawLoader.FINISH_TYPE_DEFAULT:
  656. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0.3, envMapIntensity: 0.3, metalness: 0 } );
  657. break;
  658. case LDrawLoader.FINISH_TYPE_PEARLESCENT:
  659. // Try to imitate pearlescency by setting the specular to the complementary of the color, and low shininess
  660. var specular = new THREE.Color( colour );
  661. var hsl = specular.getHSL( { h: 0, s: 0, l: 0 } );
  662. hsl.h = ( hsl.h + 0.5 ) % 1;
  663. hsl.l = Math.min( 1, hsl.l + ( 1 - hsl.l ) * 0.7 );
  664. specular.setHSL( hsl.h, hsl.s, hsl.l );
  665. material = new THREE.MeshPhongMaterial( { color: colour, specular: specular, shininess: 10, reflectivity: 0.3 } );
  666. break;
  667. case LDrawLoader.FINISH_TYPE_CHROME:
  668. // Mirror finish surface
  669. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0, metalness: 1 } );
  670. break;
  671. case LDrawLoader.FINISH_TYPE_RUBBER:
  672. // Rubber is best simulated with Lambert
  673. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0.9, metalness: 0 } );
  674. canHaveEnvMap = false;
  675. break;
  676. case LDrawLoader.FINISH_TYPE_MATTE_METALLIC:
  677. // Brushed metal finish
  678. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0.8, metalness: 0.4 } );
  679. break;
  680. case LDrawLoader.FINISH_TYPE_METAL:
  681. // Average metal finish
  682. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0.2, metalness: 0.85 } );
  683. break;
  684. default:
  685. // Should not happen
  686. break;
  687. }
  688. material.transparent = isTransparent;
  689. material.opacity = alpha;
  690. material.userData.canHaveEnvMap = canHaveEnvMap;
  691. if ( luminance !== 0 ) {
  692. material.emissive.set( material.color ).multiplyScalar( luminance );
  693. }
  694. if ( ! edgeMaterial ) {
  695. // This is the material used for edges
  696. edgeMaterial = new THREE.LineBasicMaterial( { color: edgeColour } );
  697. edgeMaterial.userData.code = code;
  698. edgeMaterial.name = name + " - Edge";
  699. edgeMaterial.userData.canHaveEnvMap = false;
  700. }
  701. material.userData.code = code;
  702. material.name = name;
  703. material.userData.edgeMaterial = edgeMaterial;
  704. return material;
  705. },
  706. //
  707. parse: function ( text ) {
  708. //console.time( 'LDrawLoader' );
  709. // Retrieve data from the parent parse scope
  710. var parentParseScope = this.getParentParseScope();
  711. // Main colour codes passed to this subobject (or default codes 16 and 24 if it is the root object)
  712. var mainColourCode = parentParseScope.mainColourCode;
  713. var mainEdgeColourCode = parentParseScope.mainEdgeColourCode;
  714. var url = parentParseScope.url;
  715. var currentParseScope = this.getCurrentParseScope();
  716. // Parse result variables
  717. var triangles;
  718. var lineSegments;
  719. var conditionalSegments;
  720. var subobjects = [];
  721. var category = null;
  722. var keywords = null;
  723. if ( text.indexOf( '\r\n' ) !== - 1 ) {
  724. // This is faster than String.split with regex that splits on both
  725. text = text.replace( /\r\n/g, '\n' );
  726. }
  727. var lines = text.split( '\n' );
  728. var numLines = lines.length;
  729. var lineIndex = 0;
  730. var parsingEmbeddedFiles = false;
  731. var currentEmbeddedFileName = null;
  732. var currentEmbeddedText = null;
  733. var bfcCertified = false;
  734. var bfcCCW = true;
  735. var bfcInverted = false;
  736. var bfcCull = true;
  737. var type = '';
  738. var scope = this;
  739. function parseColourCode( lineParser, forEdge ) {
  740. // Parses next colour code and returns a THREE.Material
  741. var colourCode = lineParser.getToken();
  742. if ( ! forEdge && colourCode === '16' ) {
  743. colourCode = mainColourCode;
  744. }
  745. if ( forEdge && colourCode === '24' ) {
  746. colourCode = mainEdgeColourCode;
  747. }
  748. var material = scope.getMaterial( colourCode );
  749. if ( ! material ) {
  750. throw 'LDrawLoader: Unknown colour code "' + colourCode + '" is used' + lineParser.getLineNumberString() + ' but it was not defined previously.';
  751. }
  752. return material;
  753. }
  754. function parseVector( lp ) {
  755. var v = new THREE.Vector3( parseFloat( lp.getToken() ), parseFloat( lp.getToken() ), parseFloat( lp.getToken() ) );
  756. if ( ! scope.separateObjects ) {
  757. v.applyMatrix4( currentParseScope.currentMatrix );
  758. }
  759. return v;
  760. }
  761. // Parse all line commands
  762. for ( lineIndex = 0; lineIndex < numLines; lineIndex ++ ) {
  763. var line = lines[ lineIndex ];
  764. if ( line.length === 0 ) continue;
  765. if ( parsingEmbeddedFiles ) {
  766. if ( line.startsWith( '0 FILE ' ) ) {
  767. // Save previous embedded file in the cache
  768. this.subobjectCache[ currentEmbeddedFileName.toLowerCase() ] = currentEmbeddedText;
  769. // New embedded text file
  770. currentEmbeddedFileName = line.substring( 7 );
  771. currentEmbeddedText = '';
  772. } else {
  773. currentEmbeddedText += line + '\n';
  774. }
  775. continue;
  776. }
  777. var lp = new LineParser( line, lineIndex + 1 );
  778. lp.seekNonSpace();
  779. if ( lp.isAtTheEnd() ) {
  780. // Empty line
  781. continue;
  782. }
  783. // Parse the line type
  784. var lineType = lp.getToken();
  785. switch ( lineType ) {
  786. // Line type 0: Comment or META
  787. case '0':
  788. // Parse meta directive
  789. var meta = lp.getToken();
  790. if ( meta ) {
  791. switch ( meta ) {
  792. case '!LDRAW_ORG':
  793. type = lp.getToken();
  794. if ( ! parsingEmbeddedFiles ) {
  795. currentParseScope.triangles = [];
  796. currentParseScope.lineSegments = [];
  797. currentParseScope.conditionalSegments = [];
  798. currentParseScope.type = type;
  799. var isRoot = ! parentParseScope.isFromParse;
  800. if ( isRoot || scope.separateObjects && ! isPrimitiveType( type ) ) {
  801. currentParseScope.groupObject = new THREE.Group();
  802. }
  803. // If the scale of the object is negated then the triangle winding order
  804. // needs to be flipped.
  805. var matrix = currentParseScope.matrix;
  806. if (
  807. matrix.determinant() < 0 && (
  808. scope.separateObjects && isPrimitiveType( type ) ||
  809. ! scope.separateObjects
  810. ) ) {
  811. currentParseScope.inverted = ! currentParseScope.inverted;
  812. }
  813. triangles = currentParseScope.triangles;
  814. lineSegments = currentParseScope.lineSegments;
  815. conditionalSegments = currentParseScope.conditionalSegments;
  816. }
  817. break;
  818. case '!COLOUR':
  819. var material = this.parseColourMetaDirective( lp );
  820. if ( material ) {
  821. this.addMaterial( material );
  822. } else {
  823. console.warn( 'LDrawLoader: Error parsing material' + lp.getLineNumberString() );
  824. }
  825. break;
  826. case '!CATEGORY':
  827. category = lp.getToken();
  828. break;
  829. case '!KEYWORDS':
  830. var newKeywords = lp.getRemainingString().split( ',' );
  831. if ( newKeywords.length > 0 ) {
  832. if ( ! keywords ) {
  833. keywords = [];
  834. }
  835. newKeywords.forEach( function ( keyword ) {
  836. keywords.push( keyword.trim() );
  837. } );
  838. }
  839. break;
  840. case 'FILE':
  841. if ( lineIndex > 0 ) {
  842. // Start embedded text files parsing
  843. parsingEmbeddedFiles = true;
  844. currentEmbeddedFileName = lp.getRemainingString();
  845. currentEmbeddedText = '';
  846. bfcCertified = false;
  847. bfcCCW = true;
  848. }
  849. break;
  850. case 'BFC':
  851. // Changes to the backface culling state
  852. while ( ! lp.isAtTheEnd() ) {
  853. var token = lp.getToken();
  854. switch ( token ) {
  855. case 'CERTIFY':
  856. case 'NOCERTIFY':
  857. bfcCertified = token === 'CERTIFY';
  858. bfcCCW = true;
  859. break;
  860. case 'CW':
  861. case 'CCW':
  862. bfcCCW = token === 'CCW';
  863. break;
  864. case 'INVERTNEXT':
  865. bfcInverted = true;
  866. break;
  867. case 'CLIP':
  868. case 'NOCLIP':
  869. bfcCull = token === 'CLIP';
  870. break;
  871. default:
  872. console.warn( 'THREE.LDrawLoader: BFC directive "' + token + '" is unknown.' );
  873. break;
  874. }
  875. }
  876. break;
  877. default:
  878. // Other meta directives are not implemented
  879. break;
  880. }
  881. }
  882. break;
  883. // Line type 1: Sub-object file
  884. case '1':
  885. var material = parseColourCode( lp );
  886. var posX = parseFloat( lp.getToken() );
  887. var posY = parseFloat( lp.getToken() );
  888. var posZ = parseFloat( lp.getToken() );
  889. var m0 = parseFloat( lp.getToken() );
  890. var m1 = parseFloat( lp.getToken() );
  891. var m2 = parseFloat( lp.getToken() );
  892. var m3 = parseFloat( lp.getToken() );
  893. var m4 = parseFloat( lp.getToken() );
  894. var m5 = parseFloat( lp.getToken() );
  895. var m6 = parseFloat( lp.getToken() );
  896. var m7 = parseFloat( lp.getToken() );
  897. var m8 = parseFloat( lp.getToken() );
  898. var matrix = new THREE.Matrix4().set(
  899. m0, m1, m2, posX,
  900. m3, m4, m5, posY,
  901. m6, m7, m8, posZ,
  902. 0, 0, 0, 1
  903. );
  904. var fileName = lp.getRemainingString().trim().replace( "\\", "/" );
  905. if ( scope.fileMap[ fileName ] ) {
  906. // Found the subobject path in the preloaded file path map
  907. fileName = scope.fileMap[ fileName ];
  908. } else {
  909. // Standardized subfolders
  910. if ( fileName.startsWith( 's/' ) ) {
  911. fileName = 'parts/' + fileName;
  912. } else if ( fileName.startsWith( '48/' ) ) {
  913. fileName = 'p/' + fileName;
  914. }
  915. }
  916. subobjects.push( {
  917. material: material,
  918. matrix: matrix,
  919. fileName: fileName,
  920. originalFileName: fileName,
  921. locationState: LDrawLoader.FILE_LOCATION_AS_IS,
  922. url: null,
  923. triedLowerCase: false,
  924. inverted: bfcInverted !== currentParseScope.inverted
  925. } );
  926. bfcInverted = false;
  927. break;
  928. // Line type 2: Line segment
  929. // Line type 5: Conditional Line segment
  930. case '2':
  931. case '5':
  932. var material = parseColourCode( lp, true );
  933. var arr = lineType === '2' ? lineSegments : conditionalSegments;
  934. arr.push( {
  935. material: material.userData.edgeMaterial,
  936. colourCode: material.userData.code,
  937. v0: parseVector( lp ),
  938. v1: parseVector( lp )
  939. } );
  940. break;
  941. // Line type 3: Triangle
  942. case '3':
  943. var material = parseColourCode( lp );
  944. var inverted = currentParseScope.inverted;
  945. var ccw = bfcCCW !== inverted;
  946. var doubleSided = ! bfcCertified || ! bfcCull;
  947. var v0, v1, v2, faceNormal;
  948. if ( ccw === true ) {
  949. v0 = parseVector( lp );
  950. v1 = parseVector( lp );
  951. v2 = parseVector( lp );
  952. } else {
  953. v2 = parseVector( lp );
  954. v1 = parseVector( lp );
  955. v0 = parseVector( lp );
  956. }
  957. tempVec0.subVectors( v1, v0 );
  958. tempVec1.subVectors( v2, v1 );
  959. faceNormal = new THREE.Vector3()
  960. .crossVectors( tempVec0, tempVec1 )
  961. .normalize();
  962. triangles.push( {
  963. material: material,
  964. colourCode: material.userData.code,
  965. v0: v0,
  966. v1: v1,
  967. v2: v2,
  968. faceNormal: faceNormal,
  969. n0: null,
  970. n1: null,
  971. n2: null
  972. } );
  973. if ( doubleSided === true ) {
  974. triangles.push( {
  975. material: material,
  976. colourCode: material.userData.code,
  977. v0: v0,
  978. v1: v2,
  979. v2: v1,
  980. faceNormal: faceNormal,
  981. n0: null,
  982. n1: null,
  983. n2: null
  984. } );
  985. }
  986. break;
  987. // Line type 4: Quadrilateral
  988. case '4':
  989. var material = parseColourCode( lp );
  990. var inverted = currentParseScope.inverted;
  991. var ccw = bfcCCW !== inverted;
  992. var doubleSided = ! bfcCertified || ! bfcCull;
  993. var v0, v1, v2, v3, faceNormal;
  994. if ( ccw === true ) {
  995. v0 = parseVector( lp );
  996. v1 = parseVector( lp );
  997. v2 = parseVector( lp );
  998. v3 = parseVector( lp );
  999. } else {
  1000. v3 = parseVector( lp );
  1001. v2 = parseVector( lp );
  1002. v1 = parseVector( lp );
  1003. v0 = parseVector( lp );
  1004. }
  1005. tempVec0.subVectors( v1, v0 );
  1006. tempVec1.subVectors( v2, v1 );
  1007. faceNormal = new THREE.Vector3()
  1008. .crossVectors( tempVec0, tempVec1 )
  1009. .normalize();
  1010. triangles.push( {
  1011. material: material,
  1012. colourCode: material.userData.code,
  1013. v0: v0,
  1014. v1: v1,
  1015. v2: v2,
  1016. faceNormal: faceNormal,
  1017. n0: null,
  1018. n1: null,
  1019. n2: null
  1020. } );
  1021. triangles.push( {
  1022. material: material,
  1023. colourCode: material.userData.code,
  1024. v0: v0,
  1025. v1: v2,
  1026. v2: v3,
  1027. faceNormal: faceNormal,
  1028. n0: null,
  1029. n1: null,
  1030. n2: null
  1031. } );
  1032. if ( doubleSided === true ) {
  1033. triangles.push( {
  1034. material: material,
  1035. colourCode: material.userData.code,
  1036. v0: v0,
  1037. v1: v2,
  1038. v2: v1,
  1039. faceNormal: faceNormal,
  1040. n0: null,
  1041. n1: null,
  1042. n2: null
  1043. } );
  1044. triangles.push( {
  1045. material: material,
  1046. colourCode: material.userData.code,
  1047. v0: v0,
  1048. v1: v3,
  1049. v2: v2,
  1050. faceNormal: faceNormal,
  1051. n0: null,
  1052. n1: null,
  1053. n2: null
  1054. } );
  1055. }
  1056. break;
  1057. default:
  1058. throw 'LDrawLoader: Unknown line type "' + lineType + '"' + lp.getLineNumberString() + '.';
  1059. break;
  1060. }
  1061. }
  1062. if ( parsingEmbeddedFiles ) {
  1063. this.subobjectCache[ currentEmbeddedFileName.toLowerCase() ] = currentEmbeddedText;
  1064. }
  1065. currentParseScope.category = category;
  1066. currentParseScope.keywords = keywords;
  1067. currentParseScope.subobjects = subobjects;
  1068. currentParseScope.numSubobjects = subobjects.length;
  1069. currentParseScope.subobjectIndex = 0;
  1070. }
  1071. };
  1072. return LDrawLoader;
  1073. } )();