LDrawLoader.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>LDrawLoader - Three.js Docs</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <script src="../scripts/highlight.min.js"></script>
  8. <link type="text/css" rel="stylesheet" href="../styles/highlight-three.css">
  9. <link type="text/css" rel="stylesheet" href="../styles/page.css">
  10. </head>
  11. <body>
  12. <p class="inheritance" translate="no"><a href="Loader.html">Loader</a> → </p>
  13. <h1 translate="no">LDrawLoader</h1>
  14. <section>
  15. <header>
  16. <div class="class-description"><p>A loader for the LDraw format.</p>
  17. <p>[LDraw](https://ldraw.org/} (LEGO Draw) is an <a href="https://ldraw.org/article/218.html" target="_blank" rel="noopener">open format specification</a>
  18. for describing LEGO and other construction set 3D models.</p>
  19. <p>An LDraw asset (a text file usually with extension .ldr, .dat or .txt) can describe just a single construction
  20. piece, or an entire model. In the case of a model the LDraw file can reference other LDraw files, which are
  21. loaded from a library path set with <code>setPartsLibraryPath</code>. You usually download the LDraw official parts library,
  22. extract to a folder and point setPartsLibraryPath to it.</p>
  23. <p>Library parts will be loaded by trial and error in subfolders 'parts', 'p' and 'models'. These file accesses
  24. are not optimal for web environment, so a script tool has been made to pack an LDraw file with all its dependencies
  25. into a single file, which loads much faster. See section 'Packing LDraw models'. The LDrawLoader example loads
  26. several packed files. The official parts library is not included due to its large size.</p>
  27. <p><code>LDrawLoader</code> supports the following extensions:</p>
  28. <ul>
  29. <li>!COLOUR: Color and surface finish declarations.</li>
  30. <li>BFC: Back Face Culling specification.</li>
  31. <li>!CATEGORY: Model/part category declarations.</li>
  32. <li>!KEYWORDS: Model/part keywords declarations.</li>
  33. </ul></div>
  34. <h2>Code Example</h2>
  35. <div translate="no"><pre><code class="language-js">const loader = new LDrawLoader();
  36. loader.setConditionalLineMaterial( LDrawConditionalLineMaterial ); // the type of line material depends on the used renderer
  37. const object = await loader.loadAsync( 'models/ldraw/officialLibrary/models/car.ldr_Packed.mpd' );
  38. scene.add( object );
  39. </code></pre></div>
  40. </header>
  41. <article>
  42. <h2 class="subsection-title">Import</h2>
  43. <p><span translate="no">LDrawLoader</span> is an addon, and must be imported explicitly, see <a href="https://threejs.org/manual/#en/installation" target="_blank" rel="noopener">Installation#Addons</a>.</p>
  44. <pre><code class="language-js">import { LDrawLoader } from 'three/addons/loaders/LDrawLoader.js';</code></pre>
  45. <div class="container-overview">
  46. <h2>Constructor</h2>
  47. <h3 class="name name-method" id="LDrawLoader" translate="no">new <a href="#LDrawLoader">LDrawLoader</a><span class="signature">( manager : <span class="param-type"><a href="LoadingManager.html">LoadingManager</a></span> )</span> </h3>
  48. <div class="method">
  49. <div class="description">
  50. <p>Constructs a new LDraw loader.</p>
  51. </div>
  52. <table class="params">
  53. <tbody>
  54. <tr>
  55. <td class="name">
  56. <strong>manager</strong>
  57. </td>
  58. <td class="description last">
  59. <p>The loading manager.</p>
  60. </td>
  61. </tr>
  62. </tbody>
  63. </table>
  64. </div>
  65. </div>
  66. <h2 class="subsection-title">Methods</h2>
  67. <h3 class="name name-method" id="addDefaultMaterials" translate="no">.<a href="#addDefaultMaterials">addDefaultMaterials</a><span class="signature">()</span><span class="type-signature"> : <a href="LDrawLoader.html">LDrawLoader</a></span> </h3>
  68. <div class="method">
  69. <div class="description">
  70. <p>Initializes the loader with default materials.</p>
  71. </div>
  72. <dl class="details">
  73. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loader.</dt>
  74. </dl>
  75. </div>
  76. <h3 class="name name-method" id="addMaterial" translate="no">.<a href="#addMaterial">addMaterial</a><span class="signature">( material : <span class="param-type"><a href="Material.html">Material</a></span> )</span><span class="type-signature"> : <a href="LDrawLoader.html">LDrawLoader</a></span> </h3>
  77. <div class="method">
  78. <div class="description">
  79. <p>Adds a single material to the loader's material library.</p>
  80. </div>
  81. <table class="params">
  82. <tbody>
  83. <tr>
  84. <td class="name">
  85. <strong>material</strong>
  86. </td>
  87. <td class="description last">
  88. <p>The material to add.</p>
  89. </td>
  90. </tr>
  91. </tbody>
  92. </table>
  93. <dl class="details">
  94. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loader.</dt>
  95. </dl>
  96. </div>
  97. <h3 class="name name-method" id="addMaterials" translate="no">.<a href="#addMaterials">addMaterials</a><span class="signature">( materials : <span class="param-type">Array.&lt;<a href="Material.html">Material</a>></span> )</span><span class="type-signature"> : <a href="LDrawLoader.html">LDrawLoader</a></span> </h3>
  98. <div class="method">
  99. <div class="description">
  100. <p>Adds a list of materials to the loader's material library.</p>
  101. </div>
  102. <table class="params">
  103. <tbody>
  104. <tr>
  105. <td class="name">
  106. <strong>materials</strong>
  107. </td>
  108. <td class="description last">
  109. <p>The materials to add.</p>
  110. </td>
  111. </tr>
  112. </tbody>
  113. </table>
  114. <dl class="details">
  115. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loader.</dt>
  116. </dl>
  117. </div>
  118. <h3 class="name name-method" id="clearMaterials" translate="no">.<a href="#clearMaterials">clearMaterials</a><span class="signature">()</span><span class="type-signature"> : <a href="LDrawLoader.html">LDrawLoader</a></span> </h3>
  119. <div class="method">
  120. <div class="description">
  121. <p>Clears the loader's material library.</p>
  122. </div>
  123. <dl class="details">
  124. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loader.</dt>
  125. </dl>
  126. </div>
  127. <h3 class="name name-method" id="getMainEdgeMaterial" translate="no">.<a href="#getMainEdgeMaterial">getMainEdgeMaterial</a><span class="signature">()</span><span class="type-signature"> : <a href="Material.html">Material</a></span> </h3>
  128. <div class="method">
  129. <div class="description">
  130. <p>Returns the material for the edges main LDraw color.</p>
  131. </div>
  132. <dl class="details">
  133. <dt class="tag-returns"><strong>Returns:</strong> The material. Returns <code>null</code> if no material has been found.</dt>
  134. </dl>
  135. </div>
  136. <h3 class="name name-method" id="getMainMaterial" translate="no">.<a href="#getMainMaterial">getMainMaterial</a><span class="signature">()</span><span class="type-signature"> : <a href="Material.html">Material</a></span> </h3>
  137. <div class="method">
  138. <div class="description">
  139. <p>Returns the Material for the main LDraw color.</p>
  140. <p>For an already loaded LDraw asset, returns the Material associated with the main color code.
  141. This method can be useful to modify the main material of a model or part that exposes it.</p>
  142. <p>The main color code is the standard way to color an LDraw part. It is '16' for triangles and
  143. '24' for edges. Usually a complete model will not expose the main color (that is, no part
  144. uses the code '16' at the top level, because they are assigned other specific colors) An LDraw
  145. part file on the other hand will expose the code '16' to be colored, and can have additional
  146. fixed colors.</p>
  147. </div>
  148. <dl class="details">
  149. <dt class="tag-returns"><strong>Returns:</strong> The material. Returns <code>null</code> if no material has been found.</dt>
  150. </dl>
  151. </div>
  152. <h3 class="name name-method" id="getMaterial" translate="no">.<a href="#getMaterial">getMaterial</a><span class="signature">( colorCode : <span class="param-type">string</span> )</span><span class="type-signature"> : <a href="Material.html">Material</a></span> </h3>
  153. <div class="method">
  154. <div class="description">
  155. <p>Returns a material for the given color code.</p>
  156. </div>
  157. <table class="params">
  158. <tbody>
  159. <tr>
  160. <td class="name">
  161. <strong>colorCode</strong>
  162. </td>
  163. <td class="description last">
  164. <p>The color code.</p>
  165. </td>
  166. </tr>
  167. </tbody>
  168. </table>
  169. <dl class="details">
  170. <dt class="tag-returns"><strong>Returns:</strong> The material. Returns <code>null</code> if no material has been found.</dt>
  171. </dl>
  172. </div>
  173. <h3 class="name name-method" id="load" translate="no">.<a href="#load">load</a><span class="signature">( url : <span class="param-type">string</span>, onLoad : <span class="param-type">function</span>, onProgress : <span class="param-type"><a href="global.html#onProgressCallback">onProgressCallback</a></span>, onError : <span class="param-type"><a href="global.html#onErrorCallback">onErrorCallback</a></span> )</span> </h3>
  174. <div class="method">
  175. <div class="description">
  176. <p>Starts loading from the given URL and passes the loaded LDraw asset
  177. to the <code>onLoad()</code> callback.</p>
  178. </div>
  179. <table class="params">
  180. <tbody>
  181. <tr>
  182. <td class="name">
  183. <strong>url</strong>
  184. </td>
  185. <td class="description last">
  186. <p>The path/URL of the file to be loaded. This can also be a data URI.</p>
  187. </td>
  188. </tr>
  189. <tr>
  190. <td class="name">
  191. <strong>onLoad</strong>
  192. </td>
  193. <td class="description last">
  194. <p>Executed when the loading process has been finished.</p>
  195. </td>
  196. </tr>
  197. <tr>
  198. <td class="name">
  199. <strong>onProgress</strong>
  200. </td>
  201. <td class="description last">
  202. <p>Executed while the loading is in progress.</p>
  203. </td>
  204. </tr>
  205. <tr>
  206. <td class="name">
  207. <strong>onError</strong>
  208. </td>
  209. <td class="description last">
  210. <p>Executed when errors occur.</p>
  211. </td>
  212. </tr>
  213. </tbody>
  214. </table>
  215. <dl class="details">
  216. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Loader.html#load">Loader#load</a></dt>
  217. </dl>
  218. </div>
  219. <h3 class="name name-method" id="parse" translate="no">.<a href="#parse">parse</a><span class="signature">( text : <span class="param-type">string</span>, onLoad : <span class="param-type">function</span>, onError : <span class="param-type"><a href="global.html#onErrorCallback">onErrorCallback</a></span> )</span> </h3>
  220. <div class="method">
  221. <div class="description">
  222. <p>Parses the given LDraw data and returns the resulting group.</p>
  223. </div>
  224. <table class="params">
  225. <tbody>
  226. <tr>
  227. <td class="name">
  228. <strong>text</strong>
  229. </td>
  230. <td class="description last">
  231. <p>The raw VRML data as a string.</p>
  232. </td>
  233. </tr>
  234. <tr>
  235. <td class="name">
  236. <strong>onLoad</strong>
  237. </td>
  238. <td class="description last">
  239. <p>Executed when the loading/parsing process has been finished.</p>
  240. </td>
  241. </tr>
  242. <tr>
  243. <td class="name">
  244. <strong>onError</strong>
  245. </td>
  246. <td class="description last">
  247. <p>Executed when errors occur.</p>
  248. </td>
  249. </tr>
  250. </tbody>
  251. </table>
  252. <dl class="details">
  253. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Loader.html#parse">Loader#parse</a></dt>
  254. </dl>
  255. </div>
  256. <h3 class="name name-method" id="preloadMaterials" translate="no">.<a href="#preloadMaterials">preloadMaterials</a><span class="signature">( url : <span class="param-type">string</span> )</span><span class="type-signature"> : Promise</span> <span class="type-signature">(async) </span></h3>
  257. <div class="method">
  258. <div class="description">
  259. <p>This async method preloads materials from a single LDraw file. In the official
  260. parts library there is a special file which is loaded always the first (LDConfig.ldr)
  261. and contains all the standard color codes. This method is intended to be used with
  262. not packed files, for example in an editor where materials are preloaded and parts
  263. are loaded on demand.</p>
  264. </div>
  265. <table class="params">
  266. <tbody>
  267. <tr>
  268. <td class="name">
  269. <strong>url</strong>
  270. </td>
  271. <td class="description last">
  272. <p>Path of the LDraw materials asset.</p>
  273. </td>
  274. </tr>
  275. </tbody>
  276. </table>
  277. <dl class="details">
  278. <dt class="tag-returns"><strong>Returns:</strong> A Promise that resolves when the preload has finished.</dt>
  279. </dl>
  280. </div>
  281. <h3 class="name name-method" id="setConditionalLineMaterial" translate="no">.<a href="#setConditionalLineMaterial">setConditionalLineMaterial</a><span class="signature">( type : <span class="param-type">LDrawConditionalLineMaterial.constructor | LDrawConditionalLineNodeMaterial.constructor</span> )</span><span class="type-signature"> : <a href="LDrawLoader.html">LDrawLoader</a></span> </h3>
  282. <div class="method">
  283. <div class="description">
  284. <p>Sets the conditional line material type which depends on the used renderer.
  285. Use <a href="LDrawConditionalLineMaterial.html">LDrawConditionalLineMaterial</a> when using <code>WebGLRenderer</code> and
  286. LDrawConditionalLineNodeMaterial when using <code>WebGPURenderer</code>.</p>
  287. </div>
  288. <table class="params">
  289. <tbody>
  290. <tr>
  291. <td class="name">
  292. <strong>type</strong>
  293. </td>
  294. <td class="description last">
  295. <p>The conditional line material type.</p>
  296. </td>
  297. </tr>
  298. </tbody>
  299. </table>
  300. <dl class="details">
  301. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loader.</dt>
  302. </dl>
  303. </div>
  304. <h3 class="name name-method" id="setFileMap" translate="no">.<a href="#setFileMap">setFileMap</a><span class="signature">( fileMap : <span class="param-type">Object.&lt;string, string></span> )</span><span class="type-signature"> : <a href="LDrawLoader.html">LDrawLoader</a></span> </h3>
  305. <div class="method">
  306. <div class="description">
  307. <p>Sets a map which maps referenced library filenames to new filenames.
  308. If a fileMap is not specified (the default), library parts will be accessed by trial and
  309. error in subfolders 'parts', 'p' and 'models'.</p>
  310. </div>
  311. <table class="params">
  312. <tbody>
  313. <tr>
  314. <td class="name">
  315. <strong>fileMap</strong>
  316. </td>
  317. <td class="description last">
  318. <p>The file map to set.</p>
  319. </td>
  320. </tr>
  321. </tbody>
  322. </table>
  323. <dl class="details">
  324. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loader.</dt>
  325. </dl>
  326. </div>
  327. <h3 class="name name-method" id="setMaterials" translate="no">.<a href="#setMaterials">setMaterials</a><span class="signature">( materials : <span class="param-type">Array.&lt;<a href="Material.html">Material</a>></span> )</span><span class="type-signature"> : <a href="LDrawLoader.html">LDrawLoader</a></span> </h3>
  328. <div class="method">
  329. <div class="description">
  330. <p>Sets the loader's material library. This method clears existing
  331. material definitions.</p>
  332. </div>
  333. <table class="params">
  334. <tbody>
  335. <tr>
  336. <td class="name">
  337. <strong>materials</strong>
  338. </td>
  339. <td class="description last">
  340. <p>The materials to set.</p>
  341. </td>
  342. </tr>
  343. </tbody>
  344. </table>
  345. <dl class="details">
  346. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loader.</dt>
  347. </dl>
  348. </div>
  349. <h3 class="name name-method" id="setPartsLibraryPath" translate="no">.<a href="#setPartsLibraryPath">setPartsLibraryPath</a><span class="signature">( path : <span class="param-type">string</span> )</span><span class="type-signature"> : <a href="LDrawLoader.html">LDrawLoader</a></span> </h3>
  350. <div class="method">
  351. <div class="description">
  352. <p>This method must be called prior to <code>load()</code> unless the model to load does not reference
  353. library parts (usually it will be a model with all its parts packed in a single file).</p>
  354. </div>
  355. <table class="params">
  356. <tbody>
  357. <tr>
  358. <td class="name">
  359. <strong>path</strong>
  360. </td>
  361. <td class="description last">
  362. <p>Path to library parts files to load referenced parts from.
  363. This is different from Loader.setPath, which indicates the path to load the main asset from.</p>
  364. </td>
  365. </tr>
  366. </tbody>
  367. </table>
  368. <dl class="details">
  369. <dt class="tag-returns"><strong>Returns:</strong> A reference to this loader.</dt>
  370. </dl>
  371. </div>
  372. <h2 class="subsection-title">Source</h2>
  373. <p>
  374. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/LDrawLoader.js" translate="no" target="_blank" rel="noopener">examples/jsm/loaders/LDrawLoader.js</a>
  375. </p>
  376. </article>
  377. </section>
  378. <script src="../scripts/linenumber.js"></script>
  379. <script src="../scripts/page.js"></script>
  380. </body>
  381. </html>
粤ICP备19079148号