LDrawLoader.js 39 KB

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