LDrawLoader.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  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. var lines = createObject( parseScope.conditionalSegments, 2 );
  337. lines.isConditionalLine = true;
  338. objGroup.add( lines );
  339. }
  340. if ( parentParseScope.groupObject ) {
  341. objGroup.name = parseScope.fileName;
  342. objGroup.matrix.copy( parseScope.matrix );
  343. objGroup.matrix.decompose( objGroup.position, objGroup.quaternion, objGroup.scale );
  344. objGroup.matrixAutoUpdate = false;
  345. parentParseScope.groupObject.add( objGroup );
  346. }
  347. } else {
  348. var separateObjects = scope.separateObjects;
  349. var parentLineSegments = parentParseScope.lineSegments;
  350. var parentConditionalSegments = parentParseScope.conditionalSegments;
  351. var parentTriangles = parentParseScope.triangles;
  352. var lineSegments = parseScope.lineSegments;
  353. var conditionalSegments = parseScope.conditionalSegments;
  354. var triangles = parseScope.triangles;
  355. for ( var i = 0, l = lineSegments.length; i < l; i ++ ) {
  356. var ls = lineSegments[ i ];
  357. if ( separateObjects ) {
  358. ls.v0.applyMatrix4( parseScope.matrix );
  359. ls.v1.applyMatrix4( parseScope.matrix );
  360. }
  361. parentLineSegments.push( ls );
  362. }
  363. for ( var i = 0, l = conditionalSegments.length; i < l; i ++ ) {
  364. var os = conditionalSegments[ i ];
  365. if ( separateObjects ) {
  366. os.v0.applyMatrix4( parseScope.matrix );
  367. os.v1.applyMatrix4( parseScope.matrix );
  368. }
  369. parentConditionalSegments.push( os );
  370. }
  371. for ( var i = 0, l = triangles.length; i < l; i ++ ) {
  372. var tri = triangles[ i ];
  373. if ( separateObjects ) {
  374. tri.v0 = tri.v0.clone().applyMatrix4( parseScope.matrix );
  375. tri.v1 = tri.v1.clone().applyMatrix4( parseScope.matrix );
  376. tri.v2 = tri.v2.clone().applyMatrix4( parseScope.matrix );
  377. tempVec0.subVectors( tri.v1, tri.v0 );
  378. tempVec1.subVectors( tri.v2, tri.v1 );
  379. tri.faceNormal.crossVectors( tempVec0, tempVec1 ).normalize();
  380. }
  381. parentTriangles.push( tri );
  382. }
  383. }
  384. scope.removeScopeLevel();
  385. if ( onProcessed ) {
  386. onProcessed( parseScope.groupObject );
  387. }
  388. }
  389. function loadSubobject( subobject ) {
  390. parseScope.mainColourCode = subobject.material.userData.code;
  391. parseScope.mainEdgeColourCode = subobject.material.userData.edgeMaterial.userData.code;
  392. parseScope.currentFileName = subobject.originalFileName;
  393. // If subobject was cached previously, use the cached one
  394. var cached = scope.subobjectCache[ subobject.originalFileName.toLowerCase() ];
  395. if ( cached ) {
  396. processObject( cached, function ( subobjectGroup ) {
  397. onSubobjectLoaded( subobjectGroup, subobject );
  398. onSubobjectFinish();
  399. }, subobject );
  400. return;
  401. }
  402. // Adjust file name to locate the subobject file path in standard locations (always under directory scope.path)
  403. // Update also subobject.locationState for the next try if this load fails.
  404. var subobjectURL = subobject.fileName;
  405. var newLocationState = LDrawLoader.FILE_LOCATION_NOT_FOUND;
  406. switch ( subobject.locationState ) {
  407. case LDrawLoader.FILE_LOCATION_AS_IS:
  408. newLocationState = subobject.locationState + 1;
  409. break;
  410. case LDrawLoader.FILE_LOCATION_TRY_PARTS:
  411. subobjectURL = 'parts/' + subobjectURL;
  412. newLocationState = subobject.locationState + 1;
  413. break;
  414. case LDrawLoader.FILE_LOCATION_TRY_P:
  415. subobjectURL = 'p/' + subobjectURL;
  416. newLocationState = subobject.locationState + 1;
  417. break;
  418. case LDrawLoader.FILE_LOCATION_TRY_MODELS:
  419. subobjectURL = 'models/' + subobjectURL;
  420. newLocationState = subobject.locationState + 1;
  421. break;
  422. case LDrawLoader.FILE_LOCATION_TRY_RELATIVE:
  423. subobjectURL = url.substring( 0, url.lastIndexOf( "/" ) + 1 ) + subobjectURL;
  424. newLocationState = subobject.locationState + 1;
  425. break;
  426. case LDrawLoader.FILE_LOCATION_TRY_ABSOLUTE:
  427. if ( subobject.triedLowerCase ) {
  428. // Try absolute path
  429. newLocationState = LDrawLoader.FILE_LOCATION_NOT_FOUND;
  430. } else {
  431. // Next attempt is lower case
  432. subobject.fileName = subobject.fileName.toLowerCase();
  433. subobjectURL = subobject.fileName;
  434. subobject.triedLowerCase = true;
  435. newLocationState = LDrawLoader.FILE_LOCATION_AS_IS;
  436. }
  437. break;
  438. case LDrawLoader.FILE_LOCATION_NOT_FOUND:
  439. // All location possibilities have been tried, give up loading this object
  440. console.warn( 'LDrawLoader: Subobject "' + subobject.originalFileName + '" could not be found.' );
  441. return;
  442. }
  443. subobject.locationState = newLocationState;
  444. subobject.url = subobjectURL;
  445. // Load the subobject
  446. // Use another file loader here so we can keep track of the subobject information
  447. // and use it when processing the next model.
  448. var fileLoader = new THREE.FileLoader( scope.manager );
  449. fileLoader.setPath( scope.path );
  450. fileLoader.load( subobjectURL, function ( text ) {
  451. processObject( text, function ( subobjectGroup ) {
  452. onSubobjectLoaded( subobjectGroup, subobject );
  453. onSubobjectFinish();
  454. }, subobject );
  455. }, undefined, function ( err ) {
  456. onSubobjectError( err, subobject );
  457. }, subobject );
  458. }
  459. function onSubobjectLoaded( subobjectGroup, subobject ) {
  460. if ( subobjectGroup === null ) {
  461. // Try to reload
  462. loadSubobject( subobject );
  463. return;
  464. }
  465. scope.fileMap[ subobject.originalFileName ] = subobject.url;
  466. }
  467. function onSubobjectError( err, subobject ) {
  468. // Retry download from a different default possible location
  469. loadSubobject( subobject );
  470. }
  471. }
  472. },
  473. setPath: function ( value ) {
  474. this.path = value;
  475. return this;
  476. },
  477. setMaterials: function ( materials ) {
  478. // Clears parse scopes stack, adds new scope with material library
  479. this.parseScopesStack = [];
  480. this.newParseScopeLevel( materials );
  481. this.getCurrentParseScope().isFromParse = false;
  482. this.materials = materials;
  483. return this;
  484. },
  485. setFileMap: function ( fileMap ) {
  486. this.fileMap = fileMap;
  487. return this;
  488. },
  489. newParseScopeLevel: function ( materials ) {
  490. // Adds a new scope level, assign materials to it and returns it
  491. var matLib = {};
  492. if ( materials ) {
  493. for ( var i = 0, n = materials.length; i < n; i ++ ) {
  494. var material = materials[ i ];
  495. matLib[ material.userData.code ] = material;
  496. }
  497. }
  498. var topParseScope = this.getCurrentParseScope();
  499. var newParseScope = {
  500. lib: matLib,
  501. url: null,
  502. // Subobjects
  503. subobjects: null,
  504. numSubobjects: 0,
  505. subobjectIndex: 0,
  506. inverted: false,
  507. // Current subobject
  508. currentFileName: null,
  509. mainColourCode: topParseScope ? topParseScope.mainColourCode : '16',
  510. mainEdgeColourCode: topParseScope ? topParseScope.mainEdgeColourCode : '24',
  511. currentMatrix: new THREE.Matrix4(),
  512. matrix: new THREE.Matrix4(),
  513. // If false, it is a root material scope previous to parse
  514. isFromParse: true,
  515. triangles: null,
  516. lineSegments: null,
  517. conditionalSegments: null,
  518. };
  519. this.parseScopesStack.push( newParseScope );
  520. return newParseScope;
  521. },
  522. removeScopeLevel: function () {
  523. this.parseScopesStack.pop();
  524. return this;
  525. },
  526. addMaterial: function ( material ) {
  527. // Adds a material to the material library which is on top of the parse scopes stack. And also to the materials array
  528. var matLib = this.getCurrentParseScope().lib;
  529. if ( ! matLib[ material.userData.code ] ) {
  530. this.materials.push( material );
  531. }
  532. matLib[ material.userData.code ] = material;
  533. return this;
  534. },
  535. getMaterial: function ( colourCode ) {
  536. // Given a colour code search its material in the parse scopes stack
  537. if ( colourCode.startsWith( "0x2" ) ) {
  538. // Special 'direct' material value (RGB colour)
  539. var colour = colourCode.substring( 3 );
  540. return this.parseColourMetaDirective( new LineParser( "Direct_Color_" + colour + " CODE -1 VALUE #" + colour + " EDGE #" + colour + "" ) );
  541. }
  542. for ( var i = this.parseScopesStack.length - 1; i >= 0; i -- ) {
  543. var material = this.parseScopesStack[ i ].lib[ colourCode ];
  544. if ( material ) {
  545. return material;
  546. }
  547. }
  548. // Material was not found
  549. return null;
  550. },
  551. getParentParseScope: function () {
  552. if ( this.parseScopesStack.length > 1 ) {
  553. return this.parseScopesStack[ this.parseScopesStack.length - 2 ];
  554. }
  555. return null;
  556. },
  557. getCurrentParseScope: function () {
  558. if ( this.parseScopesStack.length > 0 ) {
  559. return this.parseScopesStack[ this.parseScopesStack.length - 1 ];
  560. }
  561. return null;
  562. },
  563. parseColourMetaDirective: function ( lineParser ) {
  564. // Parses a colour definition and returns a THREE.Material or null if error
  565. var code = null;
  566. // Triangle and line colours
  567. var colour = 0xFF00FF;
  568. var edgeColour = 0xFF00FF;
  569. // Transparency
  570. var alpha = 1;
  571. var isTransparent = false;
  572. // Self-illumination:
  573. var luminance = 0;
  574. var finishType = LDrawLoader.FINISH_TYPE_DEFAULT;
  575. var canHaveEnvMap = true;
  576. var edgeMaterial = null;
  577. var name = lineParser.getToken();
  578. if ( ! name ) {
  579. throw 'LDrawLoader: Material name was expected after "!COLOUR tag' + lineParser.getLineNumberString() + ".";
  580. }
  581. // Parse tag tokens and their parameters
  582. var token = null;
  583. while ( true ) {
  584. token = lineParser.getToken();
  585. if ( ! token ) {
  586. break;
  587. }
  588. switch ( token.toUpperCase() ) {
  589. case "CODE":
  590. code = lineParser.getToken();
  591. break;
  592. case "VALUE":
  593. colour = lineParser.getToken();
  594. if ( colour.startsWith( '0x' ) ) {
  595. colour = '#' + colour.substring( 2 );
  596. } else if ( ! colour.startsWith( '#' ) ) {
  597. throw 'LDrawLoader: Invalid colour while parsing material' + lineParser.getLineNumberString() + ".";
  598. }
  599. break;
  600. case "EDGE":
  601. edgeColour = lineParser.getToken();
  602. if ( edgeColour.startsWith( '0x' ) ) {
  603. edgeColour = '#' + edgeColour.substring( 2 );
  604. } else if ( ! edgeColour.startsWith( '#' ) ) {
  605. // Try to see if edge colour is a colour code
  606. edgeMaterial = this.getMaterial( edgeColour );
  607. if ( ! edgeMaterial ) {
  608. throw 'LDrawLoader: Invalid edge colour while parsing material' + lineParser.getLineNumberString() + ".";
  609. }
  610. // Get the edge material for this triangle material
  611. edgeMaterial = edgeMaterial.userData.edgeMaterial;
  612. }
  613. break;
  614. case 'ALPHA':
  615. alpha = parseInt( lineParser.getToken() );
  616. if ( isNaN( alpha ) ) {
  617. throw 'LDrawLoader: Invalid alpha value in material definition' + lineParser.getLineNumberString() + ".";
  618. }
  619. alpha = Math.max( 0, Math.min( 1, alpha / 255 ) );
  620. if ( alpha < 1 ) {
  621. isTransparent = true;
  622. }
  623. break;
  624. case 'LUMINANCE':
  625. luminance = parseInt( lineParser.getToken() );
  626. if ( isNaN( luminance ) ) {
  627. throw 'LDrawLoader: Invalid luminance value in material definition' + LineParser.getLineNumberString() + ".";
  628. }
  629. luminance = Math.max( 0, Math.min( 1, luminance / 255 ) );
  630. break;
  631. case 'CHROME':
  632. finishType = LDrawLoader.FINISH_TYPE_CHROME;
  633. break;
  634. case 'PEARLESCENT':
  635. finishType = LDrawLoader.FINISH_TYPE_PEARLESCENT;
  636. break;
  637. case 'RUBBER':
  638. finishType = LDrawLoader.FINISH_TYPE_RUBBER;
  639. break;
  640. case 'MATTE_METALLIC':
  641. finishType = LDrawLoader.FINISH_TYPE_MATTE_METALLIC;
  642. break;
  643. case 'METAL':
  644. finishType = LDrawLoader.FINISH_TYPE_METAL;
  645. break;
  646. case 'MATERIAL':
  647. // Not implemented
  648. lineParser.setToEnd();
  649. break;
  650. default:
  651. throw 'LDrawLoader: Unknown token "' + token + '" while parsing material' + lineParser.getLineNumberString() + ".";
  652. break;
  653. }
  654. }
  655. var material = null;
  656. switch ( finishType ) {
  657. case LDrawLoader.FINISH_TYPE_DEFAULT:
  658. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0.3, envMapIntensity: 0.3, metalness: 0 } );
  659. break;
  660. case LDrawLoader.FINISH_TYPE_PEARLESCENT:
  661. // Try to imitate pearlescency by setting the specular to the complementary of the color, and low shininess
  662. var specular = new THREE.Color( colour );
  663. var hsl = specular.getHSL( { h: 0, s: 0, l: 0 } );
  664. hsl.h = ( hsl.h + 0.5 ) % 1;
  665. hsl.l = Math.min( 1, hsl.l + ( 1 - hsl.l ) * 0.7 );
  666. specular.setHSL( hsl.h, hsl.s, hsl.l );
  667. material = new THREE.MeshPhongMaterial( { color: colour, specular: specular, shininess: 10, reflectivity: 0.3 } );
  668. break;
  669. case LDrawLoader.FINISH_TYPE_CHROME:
  670. // Mirror finish surface
  671. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0, metalness: 1 } );
  672. break;
  673. case LDrawLoader.FINISH_TYPE_RUBBER:
  674. // Rubber is best simulated with Lambert
  675. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0.9, metalness: 0 } );
  676. canHaveEnvMap = false;
  677. break;
  678. case LDrawLoader.FINISH_TYPE_MATTE_METALLIC:
  679. // Brushed metal finish
  680. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0.8, metalness: 0.4 } );
  681. break;
  682. case LDrawLoader.FINISH_TYPE_METAL:
  683. // Average metal finish
  684. material = new THREE.MeshStandardMaterial( { color: colour, roughness: 0.2, metalness: 0.85 } );
  685. break;
  686. default:
  687. // Should not happen
  688. break;
  689. }
  690. material.transparent = isTransparent;
  691. material.opacity = alpha;
  692. material.userData.canHaveEnvMap = canHaveEnvMap;
  693. if ( luminance !== 0 ) {
  694. material.emissive.set( material.color ).multiplyScalar( luminance );
  695. }
  696. if ( ! edgeMaterial ) {
  697. // This is the material used for edges
  698. edgeMaterial = new THREE.LineBasicMaterial( { color: edgeColour } );
  699. edgeMaterial.userData.code = code;
  700. edgeMaterial.name = name + " - Edge";
  701. edgeMaterial.userData.canHaveEnvMap = false;
  702. }
  703. material.userData.code = code;
  704. material.name = name;
  705. material.userData.edgeMaterial = edgeMaterial;
  706. return material;
  707. },
  708. //
  709. parse: function ( text ) {
  710. //console.time( 'LDrawLoader' );
  711. // Retrieve data from the parent parse scope
  712. var parentParseScope = this.getParentParseScope();
  713. // Main colour codes passed to this subobject (or default codes 16 and 24 if it is the root object)
  714. var mainColourCode = parentParseScope.mainColourCode;
  715. var mainEdgeColourCode = parentParseScope.mainEdgeColourCode;
  716. var url = parentParseScope.url;
  717. var currentParseScope = this.getCurrentParseScope();
  718. // Parse result variables
  719. var triangles;
  720. var lineSegments;
  721. var conditionalSegments;
  722. var subobjects = [];
  723. var category = null;
  724. var keywords = null;
  725. if ( text.indexOf( '\r\n' ) !== - 1 ) {
  726. // This is faster than String.split with regex that splits on both
  727. text = text.replace( /\r\n/g, '\n' );
  728. }
  729. var lines = text.split( '\n' );
  730. var numLines = lines.length;
  731. var lineIndex = 0;
  732. var parsingEmbeddedFiles = false;
  733. var currentEmbeddedFileName = null;
  734. var currentEmbeddedText = null;
  735. var bfcCertified = false;
  736. var bfcCCW = true;
  737. var bfcInverted = false;
  738. var bfcCull = true;
  739. var type = '';
  740. var scope = this;
  741. function parseColourCode( lineParser, forEdge ) {
  742. // Parses next colour code and returns a THREE.Material
  743. var colourCode = lineParser.getToken();
  744. if ( ! forEdge && colourCode === '16' ) {
  745. colourCode = mainColourCode;
  746. }
  747. if ( forEdge && colourCode === '24' ) {
  748. colourCode = mainEdgeColourCode;
  749. }
  750. var material = scope.getMaterial( colourCode );
  751. if ( ! material ) {
  752. throw 'LDrawLoader: Unknown colour code "' + colourCode + '" is used' + lineParser.getLineNumberString() + ' but it was not defined previously.';
  753. }
  754. return material;
  755. }
  756. function parseVector( lp ) {
  757. var v = new THREE.Vector3( parseFloat( lp.getToken() ), parseFloat( lp.getToken() ), parseFloat( lp.getToken() ) );
  758. if ( ! scope.separateObjects ) {
  759. v.applyMatrix4( currentParseScope.currentMatrix );
  760. }
  761. return v;
  762. }
  763. // Parse all line commands
  764. for ( lineIndex = 0; lineIndex < numLines; lineIndex ++ ) {
  765. var line = lines[ lineIndex ];
  766. if ( line.length === 0 ) continue;
  767. if ( parsingEmbeddedFiles ) {
  768. if ( line.startsWith( '0 FILE ' ) ) {
  769. // Save previous embedded file in the cache
  770. this.subobjectCache[ currentEmbeddedFileName.toLowerCase() ] = currentEmbeddedText;
  771. // New embedded text file
  772. currentEmbeddedFileName = line.substring( 7 );
  773. currentEmbeddedText = '';
  774. } else {
  775. currentEmbeddedText += line + '\n';
  776. }
  777. continue;
  778. }
  779. var lp = new LineParser( line, lineIndex + 1 );
  780. lp.seekNonSpace();
  781. if ( lp.isAtTheEnd() ) {
  782. // Empty line
  783. continue;
  784. }
  785. // Parse the line type
  786. var lineType = lp.getToken();
  787. switch ( lineType ) {
  788. // Line type 0: Comment or META
  789. case '0':
  790. // Parse meta directive
  791. var meta = lp.getToken();
  792. if ( meta ) {
  793. switch ( meta ) {
  794. case '!LDRAW_ORG':
  795. type = lp.getToken();
  796. if ( ! parsingEmbeddedFiles ) {
  797. currentParseScope.triangles = [];
  798. currentParseScope.lineSegments = [];
  799. currentParseScope.conditionalSegments = [];
  800. currentParseScope.type = type;
  801. var isRoot = ! parentParseScope.isFromParse;
  802. if ( isRoot || scope.separateObjects && ! isPrimitiveType( type ) ) {
  803. currentParseScope.groupObject = new THREE.Group();
  804. }
  805. // If the scale of the object is negated then the triangle winding order
  806. // needs to be flipped.
  807. var matrix = currentParseScope.matrix;
  808. if (
  809. matrix.determinant() < 0 && (
  810. scope.separateObjects && isPrimitiveType( type ) ||
  811. ! scope.separateObjects
  812. ) ) {
  813. currentParseScope.inverted = ! currentParseScope.inverted;
  814. }
  815. triangles = currentParseScope.triangles;
  816. lineSegments = currentParseScope.lineSegments;
  817. conditionalSegments = currentParseScope.conditionalSegments;
  818. }
  819. break;
  820. case '!COLOUR':
  821. var material = this.parseColourMetaDirective( lp );
  822. if ( material ) {
  823. this.addMaterial( material );
  824. } else {
  825. console.warn( 'LDrawLoader: Error parsing material' + lp.getLineNumberString() );
  826. }
  827. break;
  828. case '!CATEGORY':
  829. category = lp.getToken();
  830. break;
  831. case '!KEYWORDS':
  832. var newKeywords = lp.getRemainingString().split( ',' );
  833. if ( newKeywords.length > 0 ) {
  834. if ( ! keywords ) {
  835. keywords = [];
  836. }
  837. newKeywords.forEach( function ( keyword ) {
  838. keywords.push( keyword.trim() );
  839. } );
  840. }
  841. break;
  842. case 'FILE':
  843. if ( lineIndex > 0 ) {
  844. // Start embedded text files parsing
  845. parsingEmbeddedFiles = true;
  846. currentEmbeddedFileName = lp.getRemainingString();
  847. currentEmbeddedText = '';
  848. bfcCertified = false;
  849. bfcCCW = true;
  850. }
  851. break;
  852. case 'BFC':
  853. // Changes to the backface culling state
  854. while ( ! lp.isAtTheEnd() ) {
  855. var token = lp.getToken();
  856. switch ( token ) {
  857. case 'CERTIFY':
  858. case 'NOCERTIFY':
  859. bfcCertified = token === 'CERTIFY';
  860. bfcCCW = true;
  861. break;
  862. case 'CW':
  863. case 'CCW':
  864. bfcCCW = token === 'CCW';
  865. break;
  866. case 'INVERTNEXT':
  867. bfcInverted = true;
  868. break;
  869. case 'CLIP':
  870. case 'NOCLIP':
  871. bfcCull = token === 'CLIP';
  872. break;
  873. default:
  874. console.warn( 'THREE.LDrawLoader: BFC directive "' + token + '" is unknown.' );
  875. break;
  876. }
  877. }
  878. break;
  879. default:
  880. // Other meta directives are not implemented
  881. break;
  882. }
  883. }
  884. break;
  885. // Line type 1: Sub-object file
  886. case '1':
  887. var material = parseColourCode( lp );
  888. var posX = parseFloat( lp.getToken() );
  889. var posY = parseFloat( lp.getToken() );
  890. var posZ = parseFloat( lp.getToken() );
  891. var m0 = parseFloat( lp.getToken() );
  892. var m1 = parseFloat( lp.getToken() );
  893. var m2 = parseFloat( lp.getToken() );
  894. var m3 = parseFloat( lp.getToken() );
  895. var m4 = parseFloat( lp.getToken() );
  896. var m5 = parseFloat( lp.getToken() );
  897. var m6 = parseFloat( lp.getToken() );
  898. var m7 = parseFloat( lp.getToken() );
  899. var m8 = parseFloat( lp.getToken() );
  900. var matrix = new THREE.Matrix4().set(
  901. m0, m1, m2, posX,
  902. m3, m4, m5, posY,
  903. m6, m7, m8, posZ,
  904. 0, 0, 0, 1
  905. );
  906. var fileName = lp.getRemainingString().trim().replace( "\\", "/" );
  907. if ( scope.fileMap[ fileName ] ) {
  908. // Found the subobject path in the preloaded file path map
  909. fileName = scope.fileMap[ fileName ];
  910. } else {
  911. // Standardized subfolders
  912. if ( fileName.startsWith( 's/' ) ) {
  913. fileName = 'parts/' + fileName;
  914. } else if ( fileName.startsWith( '48/' ) ) {
  915. fileName = 'p/' + fileName;
  916. }
  917. }
  918. subobjects.push( {
  919. material: material,
  920. matrix: matrix,
  921. fileName: fileName,
  922. originalFileName: fileName,
  923. locationState: LDrawLoader.FILE_LOCATION_AS_IS,
  924. url: null,
  925. triedLowerCase: false,
  926. inverted: bfcInverted !== currentParseScope.inverted
  927. } );
  928. bfcInverted = false;
  929. break;
  930. // Line type 2: Line segment
  931. // Line type 5: Conditional Line segment
  932. case '2':
  933. case '5':
  934. var material = parseColourCode( lp, true );
  935. var arr = lineType === '2' ? lineSegments : conditionalSegments;
  936. arr.push( {
  937. material: material.userData.edgeMaterial,
  938. colourCode: material.userData.code,
  939. v0: parseVector( lp ),
  940. v1: parseVector( lp )
  941. } );
  942. break;
  943. // Line type 3: Triangle
  944. case '3':
  945. var material = parseColourCode( lp );
  946. var inverted = currentParseScope.inverted;
  947. var ccw = bfcCCW !== inverted;
  948. var doubleSided = ! bfcCertified || ! bfcCull;
  949. var v0, v1, v2, faceNormal;
  950. if ( ccw === true ) {
  951. v0 = parseVector( lp );
  952. v1 = parseVector( lp );
  953. v2 = parseVector( lp );
  954. } else {
  955. v2 = parseVector( lp );
  956. v1 = parseVector( lp );
  957. v0 = parseVector( lp );
  958. }
  959. tempVec0.subVectors( v1, v0 );
  960. tempVec1.subVectors( v2, v1 );
  961. faceNormal = new THREE.Vector3()
  962. .crossVectors( tempVec0, tempVec1 )
  963. .normalize();
  964. triangles.push( {
  965. material: material,
  966. colourCode: material.userData.code,
  967. v0: v0,
  968. v1: v1,
  969. v2: v2,
  970. faceNormal: faceNormal,
  971. n0: null,
  972. n1: null,
  973. n2: null
  974. } );
  975. if ( doubleSided === true ) {
  976. triangles.push( {
  977. material: material,
  978. colourCode: material.userData.code,
  979. v0: v0,
  980. v1: v2,
  981. v2: v1,
  982. faceNormal: faceNormal,
  983. n0: null,
  984. n1: null,
  985. n2: null
  986. } );
  987. }
  988. break;
  989. // Line type 4: Quadrilateral
  990. case '4':
  991. var material = parseColourCode( lp );
  992. var inverted = currentParseScope.inverted;
  993. var ccw = bfcCCW !== inverted;
  994. var doubleSided = ! bfcCertified || ! bfcCull;
  995. var v0, v1, v2, v3, faceNormal;
  996. if ( ccw === true ) {
  997. v0 = parseVector( lp );
  998. v1 = parseVector( lp );
  999. v2 = parseVector( lp );
  1000. v3 = parseVector( lp );
  1001. } else {
  1002. v3 = parseVector( lp );
  1003. v2 = parseVector( lp );
  1004. v1 = parseVector( lp );
  1005. v0 = parseVector( lp );
  1006. }
  1007. tempVec0.subVectors( v1, v0 );
  1008. tempVec1.subVectors( v2, v1 );
  1009. faceNormal = new THREE.Vector3()
  1010. .crossVectors( tempVec0, tempVec1 )
  1011. .normalize();
  1012. triangles.push( {
  1013. material: material,
  1014. colourCode: material.userData.code,
  1015. v0: v0,
  1016. v1: v1,
  1017. v2: v2,
  1018. faceNormal: faceNormal,
  1019. n0: null,
  1020. n1: null,
  1021. n2: null
  1022. } );
  1023. triangles.push( {
  1024. material: material,
  1025. colourCode: material.userData.code,
  1026. v0: v0,
  1027. v1: v2,
  1028. v2: v3,
  1029. faceNormal: faceNormal,
  1030. n0: null,
  1031. n1: null,
  1032. n2: null
  1033. } );
  1034. if ( doubleSided === true ) {
  1035. triangles.push( {
  1036. material: material,
  1037. colourCode: material.userData.code,
  1038. v0: v0,
  1039. v1: v2,
  1040. v2: v1,
  1041. faceNormal: faceNormal,
  1042. n0: null,
  1043. n1: null,
  1044. n2: null
  1045. } );
  1046. triangles.push( {
  1047. material: material,
  1048. colourCode: material.userData.code,
  1049. v0: v0,
  1050. v1: v3,
  1051. v2: v2,
  1052. faceNormal: faceNormal,
  1053. n0: null,
  1054. n1: null,
  1055. n2: null
  1056. } );
  1057. }
  1058. break;
  1059. default:
  1060. throw 'LDrawLoader: Unknown line type "' + lineType + '"' + lp.getLineNumberString() + '.';
  1061. break;
  1062. }
  1063. }
  1064. if ( parsingEmbeddedFiles ) {
  1065. this.subobjectCache[ currentEmbeddedFileName.toLowerCase() ] = currentEmbeddedText;
  1066. }
  1067. currentParseScope.category = category;
  1068. currentParseScope.keywords = keywords;
  1069. currentParseScope.subobjects = subobjects;
  1070. currentParseScope.numSubobjects = subobjects.length;
  1071. currentParseScope.subobjectIndex = 0;
  1072. }
  1073. };
  1074. return LDrawLoader;
  1075. } )();