0
0

MeshSurfaceSampler.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {
  2. Triangle,
  3. Vector3
  4. } from '../../../build/three.module.js';
  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.isBufferGeometry || geometry.attributes.position.itemSize !== 3 ) {
  21. throw new Error( 'THREE.MeshSurfaceSampler: Requires BufferGeometry triangle mesh.' );
  22. }
  23. if ( geometry.index ) {
  24. console.warn( 'THREE.MeshSurfaceSampler: Converting geometry to non-indexed BufferGeometry.' );
  25. geometry = geometry.toNonIndexed();
  26. }
  27. this.geometry = geometry;
  28. this.randomFunction = Math.random;
  29. this.positionAttribute = this.geometry.getAttribute( 'position' );
  30. this.colorAttribute = this.geometry.getAttribute( 'color' );
  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 ) {
  71. const cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  72. const faceIndex = this.binarySearch( this.randomFunction() * cumulativeTotal );
  73. return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor );
  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 };