MeshSurfaceSampler.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ( function () {
  2. /**
  3. * Utility class for sampling weighted random points on the surface of a mesh.
  4. *
  5. * Building the sampler is a one-time O(n) operation. Once built, any number of
  6. * random samples may be selected in O(logn) time. Memory usage is O(n).
  7. *
  8. * References:
  9. * - http://www.joesfer.com/?p=84
  10. * - https://stackoverflow.com/a/4322940/1314762
  11. */
  12. const _face = new THREE.Triangle();
  13. const _color = new THREE.Vector3();
  14. class MeshSurfaceSampler {
  15. constructor( mesh ) {
  16. let geometry = mesh.geometry;
  17. if ( ! geometry.isBufferGeometry || geometry.attributes.position.itemSize !== 3 ) {
  18. throw new Error( 'THREE.MeshSurfaceSampler: Requires BufferGeometry triangle mesh.' );
  19. }
  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 ); // Accumulate weights for each mesh face.
  39. for ( let i = 0; i < positionAttribute.count; i += 3 ) {
  40. let faceWeight = 1;
  41. if ( weightAttribute ) {
  42. faceWeight = weightAttribute.getX( i ) + weightAttribute.getX( i + 1 ) + weightAttribute.getX( i + 2 );
  43. }
  44. _face.a.fromBufferAttribute( positionAttribute, i );
  45. _face.b.fromBufferAttribute( positionAttribute, i + 1 );
  46. _face.c.fromBufferAttribute( positionAttribute, i + 2 );
  47. faceWeight *= _face.getArea();
  48. faceWeights[ i / 3 ] = faceWeight;
  49. } // Store cumulative total face weights in an array, where weight index
  50. // corresponds to face index.
  51. this.distribution = new Float32Array( positionAttribute.count / 3 );
  52. let cumulativeTotal = 0;
  53. for ( let i = 0; i < faceWeights.length; i ++ ) {
  54. cumulativeTotal += faceWeights[ i ];
  55. this.distribution[ i ] = cumulativeTotal;
  56. }
  57. return this;
  58. }
  59. setRandomGenerator( randomFunction ) {
  60. this.randomFunction = randomFunction;
  61. return this;
  62. }
  63. sample( targetPosition, targetNormal, targetColor ) {
  64. const cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  65. const faceIndex = this.binarySearch( this.randomFunction() * cumulativeTotal );
  66. return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor );
  67. }
  68. binarySearch( x ) {
  69. const dist = this.distribution;
  70. let start = 0;
  71. let end = dist.length - 1;
  72. let index = - 1;
  73. while ( start <= end ) {
  74. const mid = Math.ceil( ( start + end ) / 2 );
  75. if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
  76. index = mid;
  77. break;
  78. } else if ( x < dist[ mid ] ) {
  79. end = mid - 1;
  80. } else {
  81. start = mid + 1;
  82. }
  83. }
  84. return index;
  85. }
  86. sampleFace( faceIndex, targetPosition, targetNormal, targetColor ) {
  87. let u = this.randomFunction();
  88. let v = this.randomFunction();
  89. if ( u + v > 1 ) {
  90. u = 1 - u;
  91. v = 1 - v;
  92. }
  93. _face.a.fromBufferAttribute( this.positionAttribute, faceIndex * 3 );
  94. _face.b.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 1 );
  95. _face.c.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 2 );
  96. targetPosition.set( 0, 0, 0 ).addScaledVector( _face.a, u ).addScaledVector( _face.b, v ).addScaledVector( _face.c, 1 - ( u + v ) );
  97. if ( targetNormal !== undefined ) {
  98. _face.getNormal( targetNormal );
  99. }
  100. if ( targetColor !== undefined && this.colorAttribute !== undefined ) {
  101. _face.a.fromBufferAttribute( this.colorAttribute, faceIndex * 3 );
  102. _face.b.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 1 );
  103. _face.c.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 2 );
  104. _color.set( 0, 0, 0 ).addScaledVector( _face.a, u ).addScaledVector( _face.b, v ).addScaledVector( _face.c, 1 - ( u + v ) );
  105. targetColor.r = _color.x;
  106. targetColor.g = _color.y;
  107. targetColor.b = _color.z;
  108. }
  109. return this;
  110. }
  111. }
  112. THREE.MeshSurfaceSampler = MeshSurfaceSampler;
  113. } )();