MeshSurfaceSampler.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @author donmccurdy / https://www.donmccurdy.com/
  3. */
  4. /**
  5. * Utility class for sampling weighted random points on the surface of a mesh.
  6. *
  7. * Building the sampler is a one-time O(n) operation. Once built, any number of
  8. * random samples may be selected in O(logn) time. Memory usage is O(n).
  9. *
  10. * References:
  11. * - http://www.joesfer.com/?p=84
  12. * - https://stackoverflow.com/a/4322940/1314762
  13. */
  14. THREE.MeshSurfaceSampler = ( function () {
  15. var _face = new THREE.Triangle();
  16. function MeshSurfaceSampler ( 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.positionAttribute = this.geometry.getAttribute( 'position' );
  26. this.weightAttribute = null;
  27. this.distribution = null;
  28. };
  29. MeshSurfaceSampler.prototype = {
  30. constructor: MeshSurfaceSampler,
  31. setWeightAttribute: function ( name ) {
  32. this.weightAttribute = name ? this.geometry.getAttribute( name ) : null;
  33. return this;
  34. },
  35. build: function () {
  36. var positionAttribute = this.positionAttribute;
  37. var weightAttribute = this.weightAttribute;
  38. var faceWeights = new Float32Array( positionAttribute.count / 3 );
  39. // Accumulate weights for each mesh face.
  40. for ( var i = 0; i < positionAttribute.count; i += 3 ) {
  41. var 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. var cumulativeTotal = 0;
  57. for ( var i = 0; i < faceWeights.length; i ++ ) {
  58. cumulativeTotal += faceWeights[ i ];
  59. this.distribution[ i ] = cumulativeTotal;
  60. }
  61. return this;
  62. },
  63. sample: function ( targetPosition, targetNormal ) {
  64. var cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  65. var faceIndex = this.binarySearch( Math.random() * cumulativeTotal );
  66. return this.sampleFace( faceIndex, targetPosition, targetNormal );
  67. },
  68. binarySearch: function ( x ) {
  69. var dist = this.distribution;
  70. var start = 0;
  71. var end = dist.length - 1;
  72. var index = -1;
  73. while ( start <= end ) {
  74. var mid = Math.floor( ( start + end ) / 2 );
  75. if ( 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: function ( faceIndex, targetPosition, targetNormal ) {
  87. var u = Math.random();
  88. var v = Math.random();
  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
  97. .set( 0, 0, 0 )
  98. .addScaledVector( _face.a, u )
  99. .addScaledVector( _face.b, v )
  100. .addScaledVector( _face.c, 1 - ( u + v ) );
  101. _face.getNormal( targetNormal );
  102. return this;
  103. }
  104. };
  105. return MeshSurfaceSampler;
  106. } )();