MeshSurfaceSampler.js 4.2 KB

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