MeshSurfaceSampler.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 );
  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 ) + weightAttribute.getX( i + 1 ) + weightAttribute.getX( i + 2 );
  44. }
  45. _face.a.fromBufferAttribute( positionAttribute, i );
  46. _face.b.fromBufferAttribute( positionAttribute, i + 1 );
  47. _face.c.fromBufferAttribute( positionAttribute, i + 2 );
  48. faceWeight *= _face.getArea();
  49. faceWeights[ i / 3 ] = faceWeight;
  50. }
  51. // Store cumulative total face weights in an array, where weight index
  52. // corresponds to face index.
  53. this.distribution = new Float32Array( positionAttribute.count / 3 );
  54. let cumulativeTotal = 0;
  55. for ( let i = 0; i < faceWeights.length; i ++ ) {
  56. cumulativeTotal += faceWeights[ i ];
  57. this.distribution[ i ] = cumulativeTotal;
  58. }
  59. return this;
  60. }
  61. setRandomGenerator( randomFunction ) {
  62. this.randomFunction = randomFunction;
  63. return this;
  64. }
  65. sample( targetPosition, targetNormal, targetColor ) {
  66. const cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  67. const faceIndex = this.binarySearch( this.randomFunction() * cumulativeTotal );
  68. return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor );
  69. }
  70. binarySearch( x ) {
  71. const dist = this.distribution;
  72. let start = 0;
  73. let end = dist.length - 1;
  74. let index = - 1;
  75. while ( start <= end ) {
  76. const mid = Math.ceil( ( start + end ) / 2 );
  77. if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
  78. index = mid;
  79. break;
  80. } else if ( x < dist[ mid ] ) {
  81. end = mid - 1;
  82. } else {
  83. start = mid + 1;
  84. }
  85. }
  86. return index;
  87. }
  88. sampleFace( faceIndex, targetPosition, targetNormal, targetColor ) {
  89. let u = this.randomFunction();
  90. let v = this.randomFunction();
  91. if ( u + v > 1 ) {
  92. u = 1 - u;
  93. v = 1 - v;
  94. }
  95. _face.a.fromBufferAttribute( this.positionAttribute, faceIndex * 3 );
  96. _face.b.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 1 );
  97. _face.c.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 2 );
  98. targetPosition.set( 0, 0, 0 ).addScaledVector( _face.a, u ).addScaledVector( _face.b, v ).addScaledVector( _face.c, 1 - ( u + v ) );
  99. if ( targetNormal !== undefined ) {
  100. _face.getNormal( targetNormal );
  101. }
  102. if ( targetColor !== undefined && this.colorAttribute !== undefined ) {
  103. _face.a.fromBufferAttribute( this.colorAttribute, faceIndex * 3 );
  104. _face.b.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 1 );
  105. _face.c.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 2 );
  106. _color.set( 0, 0, 0 ).addScaledVector( _face.a, u ).addScaledVector( _face.b, v ).addScaledVector( _face.c, 1 - ( u + v ) );
  107. targetColor.r = _color.x;
  108. targetColor.g = _color.y;
  109. targetColor.b = _color.z;
  110. }
  111. return this;
  112. }
  113. }
  114. THREE.MeshSurfaceSampler = MeshSurfaceSampler;
  115. } )();