MeshSurfaceSampler.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.colorAttribute = this.geometry.getAttribute( 'color' );
  30. this.uvAttribute = this.geometry.getAttribute( 'uv' );
  31. this.weightAttribute = null;
  32. this.distribution = null;
  33. }
  34. setWeightAttribute( name ) {
  35. this.weightAttribute = name ? this.geometry.getAttribute( name ) : null;
  36. return this;
  37. }
  38. build() {
  39. const positionAttribute = this.positionAttribute;
  40. const weightAttribute = this.weightAttribute;
  41. const faceWeights = new Float32Array( positionAttribute.count / 3 );
  42. // Accumulate weights for each mesh face.
  43. for ( let i = 0; i < positionAttribute.count; i += 3 ) {
  44. let faceWeight = 1;
  45. if ( weightAttribute ) {
  46. faceWeight = weightAttribute.getX( i )
  47. + weightAttribute.getX( i + 1 )
  48. + weightAttribute.getX( i + 2 );
  49. }
  50. _face.a.fromBufferAttribute( positionAttribute, i );
  51. _face.b.fromBufferAttribute( positionAttribute, i + 1 );
  52. _face.c.fromBufferAttribute( positionAttribute, i + 2 );
  53. faceWeight *= _face.getArea();
  54. faceWeights[ i / 3 ] = faceWeight;
  55. }
  56. // Store cumulative total face weights in an array, where weight index
  57. // corresponds to face index.
  58. this.distribution = new Float32Array( positionAttribute.count / 3 );
  59. let cumulativeTotal = 0;
  60. for ( let i = 0; i < faceWeights.length; i ++ ) {
  61. cumulativeTotal += faceWeights[ i ];
  62. this.distribution[ i ] = cumulativeTotal;
  63. }
  64. return this;
  65. }
  66. setRandomGenerator( randomFunction ) {
  67. this.randomFunction = randomFunction;
  68. return this;
  69. }
  70. sample( targetPosition, targetNormal, targetColor, targetUV ) {
  71. const faceIndex = this.sampleFaceIndex();
  72. return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV );
  73. }
  74. sampleFaceIndex() {
  75. const cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  76. return this.binarySearch( this.randomFunction() * cumulativeTotal );
  77. }
  78. binarySearch( x ) {
  79. const dist = this.distribution;
  80. let start = 0;
  81. let end = dist.length - 1;
  82. let index = - 1;
  83. while ( start <= end ) {
  84. const mid = Math.ceil( ( start + end ) / 2 );
  85. if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
  86. index = mid;
  87. break;
  88. } else if ( x < dist[ mid ] ) {
  89. end = mid - 1;
  90. } else {
  91. start = mid + 1;
  92. }
  93. }
  94. return index;
  95. }
  96. sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV ) {
  97. let u = this.randomFunction();
  98. let v = this.randomFunction();
  99. if ( u + v > 1 ) {
  100. u = 1 - u;
  101. v = 1 - v;
  102. }
  103. _face.a.fromBufferAttribute( this.positionAttribute, faceIndex * 3 );
  104. _face.b.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 1 );
  105. _face.c.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 2 );
  106. targetPosition
  107. .set( 0, 0, 0 )
  108. .addScaledVector( _face.a, u )
  109. .addScaledVector( _face.b, v )
  110. .addScaledVector( _face.c, 1 - ( u + v ) );
  111. if ( targetNormal !== undefined ) {
  112. _face.getNormal( targetNormal );
  113. }
  114. if ( targetColor !== undefined && this.colorAttribute !== undefined ) {
  115. _face.a.fromBufferAttribute( this.colorAttribute, faceIndex * 3 );
  116. _face.b.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 1 );
  117. _face.c.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 2 );
  118. _color
  119. .set( 0, 0, 0 )
  120. .addScaledVector( _face.a, u )
  121. .addScaledVector( _face.b, v )
  122. .addScaledVector( _face.c, 1 - ( u + v ) );
  123. targetColor.r = _color.x;
  124. targetColor.g = _color.y;
  125. targetColor.b = _color.z;
  126. }
  127. if ( targetUV !== undefined && this.uvAttribute !== undefined ) {
  128. _uva.fromBufferAttribute( this.uvAttribute, faceIndex * 3 );
  129. _uvb.fromBufferAttribute( this.uvAttribute, faceIndex * 3 + 1 );
  130. _uvc.fromBufferAttribute( this.uvAttribute, faceIndex * 3 + 2 );
  131. targetUV.set( 0, 0 ).addScaledVector( _uva, u ).addScaledVector( _uvb, v ).addScaledVector( _uvc, 1 - ( u + v ) );
  132. }
  133. return this;
  134. }
  135. }
  136. export { MeshSurfaceSampler };