MeshSurfaceSampler.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import {
  2. Triangle,
  3. Vector2,
  4. Vector3
  5. } from 'three';
  6. /**
  7. * Utility class for sampling weighted random points on the surface of a mesh.
  8. *
  9. * Building the sampler is a one-time O(n) operation. Once built, any number of
  10. * random samples may be selected in O(logn) time. Memory usage is O(n).
  11. *
  12. * References:
  13. * - http://www.joesfer.com/?p=84
  14. * - https://stackoverflow.com/a/4322940/1314762
  15. */
  16. const _face = new Triangle();
  17. const _color = new Vector3();
  18. const _uva = new Vector2(), _uvb = new Vector2(), _uvc = new Vector2();
  19. class MeshSurfaceSampler {
  20. constructor( mesh ) {
  21. let geometry = mesh.geometry;
  22. if ( geometry.index ) {
  23. console.warn( 'THREE.MeshSurfaceSampler: Converting geometry to non-indexed BufferGeometry.' );
  24. geometry = geometry.toNonIndexed();
  25. }
  26. this.geometry = geometry;
  27. this.randomFunction = Math.random;
  28. this.positionAttribute = this.geometry.getAttribute( 'position' );
  29. this.normalAttribute = this.geometry.getAttribute( 'normal' );
  30. this.colorAttribute = this.geometry.getAttribute( 'color' );
  31. this.uvAttribute = this.geometry.getAttribute( 'uv' );
  32. this.weightAttribute = null;
  33. this.distribution = null;
  34. }
  35. setWeightAttribute( name ) {
  36. this.weightAttribute = name ? this.geometry.getAttribute( name ) : null;
  37. return this;
  38. }
  39. build() {
  40. const positionAttribute = this.positionAttribute;
  41. const weightAttribute = this.weightAttribute;
  42. const faceWeights = new Float32Array( positionAttribute.count / 3 );
  43. // Accumulate weights for each mesh face.
  44. for ( let i = 0; i < positionAttribute.count; i += 3 ) {
  45. let faceWeight = 1;
  46. if ( weightAttribute ) {
  47. faceWeight = weightAttribute.getX( i )
  48. + weightAttribute.getX( i + 1 )
  49. + weightAttribute.getX( i + 2 );
  50. }
  51. _face.a.fromBufferAttribute( positionAttribute, i );
  52. _face.b.fromBufferAttribute( positionAttribute, i + 1 );
  53. _face.c.fromBufferAttribute( positionAttribute, i + 2 );
  54. faceWeight *= _face.getArea();
  55. faceWeights[ i / 3 ] = faceWeight;
  56. }
  57. // Store cumulative total face weights in an array, where weight index
  58. // corresponds to face index.
  59. this.distribution = new Float32Array( positionAttribute.count / 3 );
  60. let cumulativeTotal = 0;
  61. for ( let i = 0; i < faceWeights.length; i ++ ) {
  62. cumulativeTotal += faceWeights[ i ];
  63. this.distribution[ i ] = cumulativeTotal;
  64. }
  65. return this;
  66. }
  67. setRandomGenerator( randomFunction ) {
  68. this.randomFunction = randomFunction;
  69. return this;
  70. }
  71. sample( targetPosition, targetNormal, targetColor, targetUV ) {
  72. const faceIndex = this.sampleFaceIndex();
  73. return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV );
  74. }
  75. sampleFaceIndex() {
  76. const cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  77. return this.binarySearch( this.randomFunction() * cumulativeTotal );
  78. }
  79. binarySearch( x ) {
  80. const dist = this.distribution;
  81. let start = 0;
  82. let end = dist.length - 1;
  83. let index = - 1;
  84. while ( start <= end ) {
  85. const mid = Math.ceil( ( start + end ) / 2 );
  86. if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
  87. index = mid;
  88. break;
  89. } else if ( x < dist[ mid ] ) {
  90. end = mid - 1;
  91. } else {
  92. start = mid + 1;
  93. }
  94. }
  95. return index;
  96. }
  97. sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV ) {
  98. let u = this.randomFunction();
  99. let v = this.randomFunction();
  100. if ( u + v > 1 ) {
  101. u = 1 - u;
  102. v = 1 - v;
  103. }
  104. _face.a.fromBufferAttribute( this.positionAttribute, faceIndex * 3 );
  105. _face.b.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 1 );
  106. _face.c.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 2 );
  107. targetPosition
  108. .set( 0, 0, 0 )
  109. .addScaledVector( _face.a, u )
  110. .addScaledVector( _face.b, v )
  111. .addScaledVector( _face.c, 1 - ( u + v ) );
  112. if ( targetNormal !== undefined ) {
  113. if ( this.normalAttribute !== undefined ) {
  114. _face.a.fromBufferAttribute( this.normalAttribute, faceIndex * 3 );
  115. _face.b.fromBufferAttribute( this.normalAttribute, faceIndex * 3 + 1 );
  116. _face.c.fromBufferAttribute( this.normalAttribute, faceIndex * 3 + 2 );
  117. targetNormal.set( 0, 0, 0 ).addScaledVector( _face.a, u ).addScaledVector( _face.b, v ).addScaledVector( _face.c, 1 - ( u + v ) ).normalize();
  118. } else {
  119. _face.getNormal( targetNormal );
  120. }
  121. }
  122. if ( targetColor !== undefined && this.colorAttribute !== undefined ) {
  123. _face.a.fromBufferAttribute( this.colorAttribute, faceIndex * 3 );
  124. _face.b.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 1 );
  125. _face.c.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 2 );
  126. _color
  127. .set( 0, 0, 0 )
  128. .addScaledVector( _face.a, u )
  129. .addScaledVector( _face.b, v )
  130. .addScaledVector( _face.c, 1 - ( u + v ) );
  131. targetColor.r = _color.x;
  132. targetColor.g = _color.y;
  133. targetColor.b = _color.z;
  134. }
  135. if ( targetUV !== undefined && this.uvAttribute !== undefined ) {
  136. _uva.fromBufferAttribute( this.uvAttribute, faceIndex * 3 );
  137. _uvb.fromBufferAttribute( this.uvAttribute, faceIndex * 3 + 1 );
  138. _uvc.fromBufferAttribute( this.uvAttribute, faceIndex * 3 + 2 );
  139. targetUV.set( 0, 0 ).addScaledVector( _uva, u ).addScaledVector( _uvb, v ).addScaledVector( _uvc, 1 - ( u + v ) );
  140. }
  141. return this;
  142. }
  143. }
  144. export { MeshSurfaceSampler };