MeshSurfaceSampler.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  69. const faceIndex = this.binarySearch( this.randomFunction() * cumulativeTotal );
  70. return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor );
  71. }
  72. binarySearch( x ) {
  73. const dist = this.distribution;
  74. let start = 0;
  75. let end = dist.length - 1;
  76. let index = - 1;
  77. while ( start <= end ) {
  78. const mid = Math.ceil( ( start + end ) / 2 );
  79. if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
  80. index = mid;
  81. break;
  82. } else if ( x < dist[ mid ] ) {
  83. end = mid - 1;
  84. } else {
  85. start = mid + 1;
  86. }
  87. }
  88. return index;
  89. }
  90. sampleFace( faceIndex, targetPosition, targetNormal, targetColor ) {
  91. let u = this.randomFunction();
  92. let v = this.randomFunction();
  93. if ( u + v > 1 ) {
  94. u = 1 - u;
  95. v = 1 - v;
  96. }
  97. _face.a.fromBufferAttribute( this.positionAttribute, faceIndex * 3 );
  98. _face.b.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 1 );
  99. _face.c.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 2 );
  100. targetPosition
  101. .set( 0, 0, 0 )
  102. .addScaledVector( _face.a, u )
  103. .addScaledVector( _face.b, v )
  104. .addScaledVector( _face.c, 1 - ( u + v ) );
  105. if ( targetNormal !== undefined ) {
  106. _face.getNormal( targetNormal );
  107. }
  108. if ( targetColor !== undefined && this.colorAttribute !== undefined ) {
  109. _face.a.fromBufferAttribute( this.colorAttribute, faceIndex * 3 );
  110. _face.b.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 1 );
  111. _face.c.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 2 );
  112. _color
  113. .set( 0, 0, 0 )
  114. .addScaledVector( _face.a, u )
  115. .addScaledVector( _face.b, v )
  116. .addScaledVector( _face.c, 1 - ( u + v ) );
  117. targetColor.r = _color.x;
  118. targetColor.g = _color.y;
  119. targetColor.b = _color.z;
  120. }
  121. return this;
  122. }
  123. }
  124. export { MeshSurfaceSampler };