LDrawLoader.js 39 KB

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