STLLoader.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>STLLoader - 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">STLLoader</h1>
  14. <section>
  15. <header>
  16. <div class="class-description"><p>A loader for the STL format, as created by Solidworks and other CAD programs.</p>
  17. <p>Supports both binary and ASCII encoded files. The loader returns a non-indexed buffer geometry.</p>
  18. <p>Limitations:</p>
  19. <ul>
  20. <li>Binary decoding supports &quot;Magics&quot; color format (http://en.wikipedia.org/wiki/STL_(file_format)#Color_in_binary_STL).</li>
  21. <li>There is perhaps some question as to how valid it is to always assume little-endian-ness.</li>
  22. <li>ASCII decoding assumes file is UTF-8.</li>
  23. </ul>
  24. <p>For binary STLs geometry might contain colors for vertices. To use it:</p>
  25. <pre><code class="language-js">// use the same code to load STL as above
  26. if ( geometry.hasColors ) {
  27. material = new THREE.MeshPhongMaterial( { opacity: geometry.alpha, vertexColors: true } );
  28. }
  29. const mesh = new THREE.Mesh( geometry, material );
  30. </code></pre>
  31. <p>For ASCII STLs containing multiple solids, each solid is assigned to a different group.
  32. Groups can be used to assign a different color by defining an array of materials with the same length of
  33. geometry.groups and passing it to the Mesh constructor:</p>
  34. <pre><code class="language-js">const materials = [];
  35. const nGeometryGroups = geometry.groups.length;
  36. for ( let i = 0; i &lt; nGeometryGroups; i ++ ) {
  37. const material = new THREE.MeshPhongMaterial( { color: colorMap[ i ], wireframe: false } );
  38. materials.push( material );
  39. }
  40. const mesh = new THREE.Mesh(geometry, materials);
  41. </code></pre></div>
  42. <h2>Code Example</h2>
  43. <div translate="no"><pre><code class="language-js">const loader = new STLLoader();
  44. const geometry = await loader.loadAsync( './models/stl/slotted_disk.stl' )
  45. scene.add( new THREE.Mesh( geometry ) );
  46. </code></pre></div>
  47. </header>
  48. <article>
  49. <h2 class="subsection-title">Import</h2>
  50. <p><span translate="no">STLLoader</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>
  51. <pre><code class="language-js">import { STLLoader } from 'three/addons/loaders/STLLoader.js';</code></pre>
  52. <div class="container-overview">
  53. <h2>Constructor</h2>
  54. <h3 class="name name-method" id="STLLoader" translate="no">new <a href="#STLLoader">STLLoader</a><span class="signature">( manager : <span class="param-type"><a href="LoadingManager.html">LoadingManager</a></span> )</span> </h3>
  55. <div class="method">
  56. <div class="description">
  57. <p>Constructs a new STL loader.</p>
  58. </div>
  59. <table class="params">
  60. <tbody>
  61. <tr>
  62. <td class="name">
  63. <strong>manager</strong>
  64. </td>
  65. <td class="description last">
  66. <p>The loading manager.</p>
  67. </td>
  68. </tr>
  69. </tbody>
  70. </table>
  71. </div>
  72. </div>
  73. <h2 class="subsection-title">Methods</h2>
  74. <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>
  75. <div class="method">
  76. <div class="description">
  77. <p>Starts loading from the given URL and passes the loaded STL asset
  78. to the <code>onLoad()</code> callback.</p>
  79. </div>
  80. <table class="params">
  81. <tbody>
  82. <tr>
  83. <td class="name">
  84. <strong>url</strong>
  85. </td>
  86. <td class="description last">
  87. <p>The path/URL of the file to be loaded. This can also be a data URI.</p>
  88. </td>
  89. </tr>
  90. <tr>
  91. <td class="name">
  92. <strong>onLoad</strong>
  93. </td>
  94. <td class="description last">
  95. <p>Executed when the loading process has been finished.</p>
  96. </td>
  97. </tr>
  98. <tr>
  99. <td class="name">
  100. <strong>onProgress</strong>
  101. </td>
  102. <td class="description last">
  103. <p>Executed while the loading is in progress.</p>
  104. </td>
  105. </tr>
  106. <tr>
  107. <td class="name">
  108. <strong>onError</strong>
  109. </td>
  110. <td class="description last">
  111. <p>Executed when errors occur.</p>
  112. </td>
  113. </tr>
  114. </tbody>
  115. </table>
  116. <dl class="details">
  117. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Loader.html#load">Loader#load</a></dt>
  118. </dl>
  119. </div>
  120. <h3 class="name name-method" id="parse" translate="no">.<a href="#parse">parse</a><span class="signature">( data : <span class="param-type">ArrayBuffer</span> )</span><span class="type-signature"> : <a href="BufferGeometry.html">BufferGeometry</a></span> </h3>
  121. <div class="method">
  122. <div class="description">
  123. <p>Parses the given STL data and returns the resulting geometry.</p>
  124. </div>
  125. <table class="params">
  126. <tbody>
  127. <tr>
  128. <td class="name">
  129. <strong>data</strong>
  130. </td>
  131. <td class="description last">
  132. <p>The raw STL data as an array buffer.</p>
  133. </td>
  134. </tr>
  135. </tbody>
  136. </table>
  137. <dl class="details">
  138. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Loader.html#parse">Loader#parse</a></dt>
  139. </dl>
  140. <dl class="details">
  141. <dt class="tag-returns"><strong>Returns:</strong> The parsed geometry.</dt>
  142. </dl>
  143. </div>
  144. <h2 class="subsection-title">Source</h2>
  145. <p>
  146. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/STLLoader.js" translate="no" target="_blank" rel="noopener">examples/jsm/loaders/STLLoader.js</a>
  147. </p>
  148. </article>
  149. </section>
  150. <script src="../scripts/linenumber.js"></script>
  151. <script src="../scripts/page.js"></script>
  152. </body>
  153. </html>
粤ICP备19079148号