MeshSurfaceSampler.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {
  2. Triangle
  3. } from "../../../build/three.module.js";
  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. var MeshSurfaceSampler = ( function () {
  15. var _face = new Triangle();
  16. function MeshSurfaceSampler( mesh ) {
  17. var geometry = mesh.geometry;
  18. if ( ! geometry.isBufferGeometry || geometry.attributes.position.itemSize !== 3 ) {
  19. throw new Error( 'THREE.MeshSurfaceSampler: Requires BufferGeometry triangle mesh.' );
  20. }
  21. if ( geometry.index ) {
  22. console.warn( 'THREE.MeshSurfaceSampler: Converting geometry to non-indexed BufferGeometry.' );
  23. geometry = geometry.toNonIndexed();
  24. }
  25. this.geometry = geometry;
  26. this.positionAttribute = this.geometry.getAttribute( 'position' );
  27. this.weightAttribute = null;
  28. this.distribution = null;
  29. }
  30. MeshSurfaceSampler.prototype = {
  31. constructor: MeshSurfaceSampler,
  32. setWeightAttribute: function ( name ) {
  33. this.weightAttribute = name ? this.geometry.getAttribute( name ) : null;
  34. return this;
  35. },
  36. build: function () {
  37. var positionAttribute = this.positionAttribute;
  38. var weightAttribute = this.weightAttribute;
  39. var faceWeights = new Float32Array( positionAttribute.count / 3 );
  40. // Accumulate weights for each mesh face.
  41. for ( var i = 0; i < positionAttribute.count; i += 3 ) {
  42. var faceWeight = 1;
  43. if ( weightAttribute ) {
  44. faceWeight = weightAttribute.getX( i )
  45. + weightAttribute.getX( i + 1 )
  46. + weightAttribute.getX( i + 2 );
  47. }
  48. _face.a.fromBufferAttribute( positionAttribute, i );
  49. _face.b.fromBufferAttribute( positionAttribute, i + 1 );
  50. _face.c.fromBufferAttribute( positionAttribute, i + 2 );
  51. faceWeight *= _face.getArea();
  52. faceWeights[ i / 3 ] = faceWeight;
  53. }
  54. // Store cumulative total face weights in an array, where weight index
  55. // corresponds to face index.
  56. this.distribution = new Float32Array( positionAttribute.count / 3 );
  57. var cumulativeTotal = 0;
  58. for ( var i = 0; i < faceWeights.length; i ++ ) {
  59. cumulativeTotal += faceWeights[ i ];
  60. this.distribution[ i ] = cumulativeTotal;
  61. }
  62. return this;
  63. },
  64. sample: function ( targetPosition, targetNormal ) {
  65. var cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  66. var faceIndex = this.binarySearch( Math.random() * cumulativeTotal );
  67. return this.sampleFace( faceIndex, targetPosition, targetNormal );
  68. },
  69. binarySearch: function ( x ) {
  70. var dist = this.distribution;
  71. var start = 0;
  72. var end = dist.length - 1;
  73. var index = - 1;
  74. while ( start <= end ) {
  75. var mid = Math.ceil( ( start + end ) / 2 );
  76. if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
  77. index = mid;
  78. break;
  79. } else if ( x < dist[ mid ] ) {
  80. end = mid - 1;
  81. } else {
  82. start = mid + 1;
  83. }
  84. }
  85. return index;
  86. },
  87. sampleFace: function ( faceIndex, targetPosition, targetNormal ) {
  88. var u = Math.random();
  89. var v = Math.random();
  90. if ( u + v > 1 ) {
  91. u = 1 - u;
  92. v = 1 - v;
  93. }
  94. _face.a.fromBufferAttribute( this.positionAttribute, faceIndex * 3 );
  95. _face.b.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 1 );
  96. _face.c.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 2 );
  97. targetPosition
  98. .set( 0, 0, 0 )
  99. .addScaledVector( _face.a, u )
  100. .addScaledVector( _face.b, v )
  101. .addScaledVector( _face.c, 1 - ( u + v ) );
  102. _face.getNormal( targetNormal );
  103. return this;
  104. }
  105. };
  106. return MeshSurfaceSampler;
  107. } )();
  108. export { MeshSurfaceSampler };