MeshSurfaceSampler.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @author donmccurdy / https://www.donmccurdy.com/
  3. */
  4. import {
  5. Triangle
  6. } from "../../../build/three.module.js";
  7. /**
  8. * Utility class for sampling weighted random points on the surface of a mesh.
  9. *
  10. * Building the sampler is a one-time O(n) operation. Once built, any number of
  11. * random samples may be selected in O(logn) time. Memory usage is O(n).
  12. *
  13. * References:
  14. * - http://www.joesfer.com/?p=84
  15. * - https://stackoverflow.com/a/4322940/1314762
  16. */
  17. var MeshSurfaceSampler = ( function () {
  18. var _face = new Triangle();
  19. function MeshSurfaceSampler( mesh ) {
  20. var geometry = mesh.geometry;
  21. if ( ! geometry.isBufferGeometry || geometry.attributes.position.itemSize !== 3 ) {
  22. throw new Error( 'THREE.MeshSurfaceSampler: Requires BufferGeometry triangle mesh.' );
  23. }
  24. if ( geometry.index ) {
  25. console.warn( 'THREE.MeshSurfaceSampler: Converting geometry to non-indexed BufferGeometry.' );
  26. geometry = geometry.toNonIndexed();
  27. }
  28. this.geometry = geometry;
  29. this.positionAttribute = this.geometry.getAttribute( 'position' );
  30. this.weightAttribute = null;
  31. this.distribution = null;
  32. }
  33. MeshSurfaceSampler.prototype = {
  34. constructor: MeshSurfaceSampler,
  35. setWeightAttribute: function ( name ) {
  36. this.weightAttribute = name ? this.geometry.getAttribute( name ) : null;
  37. return this;
  38. },
  39. build: function () {
  40. var positionAttribute = this.positionAttribute;
  41. var weightAttribute = this.weightAttribute;
  42. var faceWeights = new Float32Array( positionAttribute.count / 3 );
  43. // Accumulate weights for each mesh face.
  44. for ( var i = 0; i < positionAttribute.count; i += 3 ) {
  45. var faceWeight = 1;
  46. if ( weightAttribute ) {
  47. faceWeight = weightAttribute.getX( i )
  48. + weightAttribute.getX( i + 1 )
  49. + weightAttribute.getX( i + 2 );
  50. }
  51. _face.a.fromBufferAttribute( positionAttribute, i );
  52. _face.b.fromBufferAttribute( positionAttribute, i + 1 );
  53. _face.c.fromBufferAttribute( positionAttribute, i + 2 );
  54. faceWeight *= _face.getArea();
  55. faceWeights[ i / 3 ] = faceWeight;
  56. }
  57. // Store cumulative total face weights in an array, where weight index
  58. // corresponds to face index.
  59. this.distribution = new Float32Array( positionAttribute.count / 3 );
  60. var cumulativeTotal = 0;
  61. for ( var i = 0; i < faceWeights.length; i ++ ) {
  62. cumulativeTotal += faceWeights[ i ];
  63. this.distribution[ i ] = cumulativeTotal;
  64. }
  65. return this;
  66. },
  67. sample: function ( targetPosition, targetNormal ) {
  68. var cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  69. var faceIndex = this.binarySearch( Math.random() * cumulativeTotal );
  70. return this.sampleFace( faceIndex, targetPosition, targetNormal );
  71. },
  72. binarySearch: function ( x ) {
  73. var dist = this.distribution;
  74. var start = 0;
  75. var end = dist.length - 1;
  76. var index = - 1;
  77. while ( start <= end ) {
  78. var mid = Math.floor( ( 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: function ( faceIndex, targetPosition, targetNormal ) {
  91. var u = Math.random();
  92. var v = Math.random();
  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. _face.getNormal( targetNormal );
  106. return this;
  107. }
  108. };
  109. return MeshSurfaceSampler;
  110. } )();
  111. export { MeshSurfaceSampler };