MeshSurfaceSampler.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>MeshSurfaceSampler - 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. <h1 translate="no">MeshSurfaceSampler</h1>
  13. <section>
  14. <header>
  15. <div class="class-description"><p>Utility class for sampling weighted random points on the surface of a mesh.</p>
  16. <p>Building the sampler is a one-time O(n) operation. Once built, any number of
  17. random samples may be selected in O(logn) time. Memory usage is O(n).</p>
  18. <p>References:</p>
  19. <ul>
  20. <li><a href="http://www.joesfer.com/?p=84" target="_blank" rel="noopener">http://www.joesfer.com/?p=84</a></li>
  21. <li><a href="https://stackoverflow.com/a/4322940/1314762" target="_blank" rel="noopener">https://stackoverflow.com/a/4322940/1314762</a></li>
  22. </ul></div>
  23. <h2>Code Example</h2>
  24. <div translate="no"><pre><code class="language-js">const sampler = new MeshSurfaceSampler( surfaceMesh )
  25. .setWeightAttribute( 'color' )
  26. .build();
  27. const mesh = new THREE.InstancedMesh( sampleGeometry, sampleMaterial, 100 );
  28. const position = new THREE.Vector3();
  29. const matrix = new THREE.Matrix4();
  30. // Sample randomly from the surface, creating an instance of the sample geometry at each sample point.
  31. for ( let i = 0; i &lt; 100; i ++ ) {
  32. sampler.sample( position );
  33. matrix.makeTranslation( position.x, position.y, position.z );
  34. mesh.setMatrixAt( i, matrix );
  35. }
  36. scene.add( mesh );
  37. </code></pre></div>
  38. </header>
  39. <article>
  40. <h2 class="subsection-title">Import</h2>
  41. <p><span translate="no">MeshSurfaceSampler</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>
  42. <pre><code class="language-js">import { MeshSurfaceSampler } from 'three/addons/math/MeshSurfaceSampler.js';</code></pre>
  43. <div class="container-overview">
  44. <h2>Constructor</h2>
  45. <h3 class="name name-method" id="MeshSurfaceSampler" translate="no">new <a href="#MeshSurfaceSampler">MeshSurfaceSampler</a><span class="signature">( mesh : <span class="param-type"><a href="Mesh.html">Mesh</a></span> )</span> </h3>
  46. <div class="method">
  47. <div class="description">
  48. <p>Constructs a mesh surface sampler.</p>
  49. </div>
  50. <table class="params">
  51. <tbody>
  52. <tr>
  53. <td class="name">
  54. <strong>mesh</strong>
  55. </td>
  56. <td class="description last">
  57. <p>Surface mesh from which to sample.</p>
  58. </td>
  59. </tr>
  60. </tbody>
  61. </table>
  62. </div>
  63. </div>
  64. <h2 class="subsection-title">Methods</h2>
  65. <h3 class="name name-method" id="build" translate="no">.<a href="#build">build</a><span class="signature">()</span><span class="type-signature"> : <a href="MeshSurfaceSampler.html">MeshSurfaceSampler</a></span> </h3>
  66. <div class="method">
  67. <div class="description">
  68. <p>Processes the input geometry and prepares to return samples. Any configuration of the
  69. geometry or sampler must occur before this method is called. Time complexity is O(n)
  70. for a surface with n faces.</p>
  71. </div>
  72. <dl class="details">
  73. <dt class="tag-returns"><strong>Returns:</strong> A reference to this sampler.</dt>
  74. </dl>
  75. </div>
  76. <h3 class="name name-method" id="sample" translate="no">.<a href="#sample">sample</a><span class="signature">( targetPosition : <span class="param-type"><a href="Vector3.html">Vector3</a></span>, targetNormal : <span class="param-type"><a href="Vector3.html">Vector3</a></span>, targetColor : <span class="param-type"><a href="Color.html">Color</a></span>, targetUV : <span class="param-type"><a href="Vector2.html">Vector2</a></span> )</span><span class="type-signature"> : <a href="MeshSurfaceSampler.html">MeshSurfaceSampler</a></span> </h3>
  77. <div class="method">
  78. <div class="description">
  79. <p>Selects a random point on the surface of the input geometry, returning the
  80. position and optionally the normal vector, color and UV Coordinate at that point.
  81. Time complexity is O(log n) for a surface with n faces.</p>
  82. </div>
  83. <table class="params">
  84. <tbody>
  85. <tr>
  86. <td class="name">
  87. <strong>targetPosition</strong>
  88. </td>
  89. <td class="description last">
  90. <p>The target object holding the sampled position.</p>
  91. </td>
  92. </tr>
  93. <tr>
  94. <td class="name">
  95. <strong>targetNormal</strong>
  96. </td>
  97. <td class="description last">
  98. <p>The target object holding the sampled normal.</p>
  99. </td>
  100. </tr>
  101. <tr>
  102. <td class="name">
  103. <strong>targetColor</strong>
  104. </td>
  105. <td class="description last">
  106. <p>The target object holding the sampled color.</p>
  107. </td>
  108. </tr>
  109. <tr>
  110. <td class="name">
  111. <strong>targetUV</strong>
  112. </td>
  113. <td class="description last">
  114. <p>The target object holding the sampled uv coordinates.</p>
  115. </td>
  116. </tr>
  117. </tbody>
  118. </table>
  119. <dl class="details">
  120. <dt class="tag-returns"><strong>Returns:</strong> A reference to this sampler.</dt>
  121. </dl>
  122. </div>
  123. <h3 class="name name-method" id="setRandomGenerator" translate="no">.<a href="#setRandomGenerator">setRandomGenerator</a><span class="signature">( randomFunction : <span class="param-type">function</span> )</span><span class="type-signature"> : <a href="MeshSurfaceSampler.html">MeshSurfaceSampler</a></span> </h3>
  124. <div class="method">
  125. <div class="description">
  126. <p>Allows to set a custom random number generator. Default is <code>Math.random()</code>.</p>
  127. </div>
  128. <table class="params">
  129. <tbody>
  130. <tr>
  131. <td class="name">
  132. <strong>randomFunction</strong>
  133. </td>
  134. <td class="description last">
  135. <p>A random number generator.</p>
  136. </td>
  137. </tr>
  138. </tbody>
  139. </table>
  140. <dl class="details">
  141. <dt class="tag-returns"><strong>Returns:</strong> A reference to this sampler.</dt>
  142. </dl>
  143. </div>
  144. <h3 class="name name-method" id="setWeightAttribute" translate="no">.<a href="#setWeightAttribute">setWeightAttribute</a><span class="signature">( name : <span class="param-type">string</span> )</span><span class="type-signature"> : <a href="MeshSurfaceSampler.html">MeshSurfaceSampler</a></span> </h3>
  145. <div class="method">
  146. <div class="description">
  147. <p>Specifies a vertex attribute to be used as a weight when sampling from the surface.
  148. Faces with higher weights are more likely to be sampled, and those with weights of
  149. zero will not be sampled at all. For vector attributes, only .x is used in sampling.</p>
  150. <p>If no weight attribute is selected, sampling is randomly distributed by area.</p>
  151. </div>
  152. <table class="params">
  153. <tbody>
  154. <tr>
  155. <td class="name">
  156. <strong>name</strong>
  157. </td>
  158. <td class="description last">
  159. <p>The attribute name.</p>
  160. </td>
  161. </tr>
  162. </tbody>
  163. </table>
  164. <dl class="details">
  165. <dt class="tag-returns"><strong>Returns:</strong> A reference to this sampler.</dt>
  166. </dl>
  167. </div>
  168. <h2 class="subsection-title">Source</h2>
  169. <p>
  170. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/math/MeshSurfaceSampler.js" translate="no" target="_blank" rel="noopener">examples/jsm/math/MeshSurfaceSampler.js</a>
  171. </p>
  172. </article>
  173. </section>
  174. <script src="../scripts/linenumber.js"></script>
  175. <script src="../scripts/page.js"></script>
  176. </body>
  177. </html>
粤ICP备19079148号