MeshSurfaceSampler.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. var MeshSurfaceSampler = ( function () {
  16. var _face = new Triangle();
  17. var _color = new Vector3();
  18. function MeshSurfaceSampler( mesh ) {
  19. var 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. MeshSurfaceSampler.prototype = {
  35. constructor: MeshSurfaceSampler,
  36. setWeightAttribute: function ( name ) {
  37. this.weightAttribute = name ? this.geometry.getAttribute( name ) : null;
  38. return this;
  39. },
  40. build: function () {
  41. var positionAttribute = this.positionAttribute;
  42. var weightAttribute = this.weightAttribute;
  43. var faceWeights = new Float32Array( positionAttribute.count / 3 );
  44. // Accumulate weights for each mesh face.
  45. for ( var i = 0; i < positionAttribute.count; i += 3 ) {
  46. var faceWeight = 1;
  47. if ( weightAttribute ) {
  48. faceWeight = weightAttribute.getX( i )
  49. + weightAttribute.getX( i + 1 )
  50. + weightAttribute.getX( i + 2 );
  51. }
  52. _face.a.fromBufferAttribute( positionAttribute, i );
  53. _face.b.fromBufferAttribute( positionAttribute, i + 1 );
  54. _face.c.fromBufferAttribute( positionAttribute, i + 2 );
  55. faceWeight *= _face.getArea();
  56. faceWeights[ i / 3 ] = faceWeight;
  57. }
  58. // Store cumulative total face weights in an array, where weight index
  59. // corresponds to face index.
  60. this.distribution = new Float32Array( positionAttribute.count / 3 );
  61. var cumulativeTotal = 0;
  62. for ( var i = 0; i < faceWeights.length; i ++ ) {
  63. cumulativeTotal += faceWeights[ i ];
  64. this.distribution[ i ] = cumulativeTotal;
  65. }
  66. return this;
  67. },
  68. setRandomGenerator: function ( randomFunction ) {
  69. this.randomFunction = randomFunction;
  70. return this;
  71. },
  72. sample: function ( targetPosition, targetNormal, targetColor ) {
  73. var cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  74. var faceIndex = this.binarySearch( this.randomFunction() * cumulativeTotal );
  75. return this.sampleFace( faceIndex, targetPosition, targetNormal, targetColor );
  76. },
  77. binarySearch: function ( x ) {
  78. var dist = this.distribution;
  79. var start = 0;
  80. var end = dist.length - 1;
  81. var index = - 1;
  82. while ( start <= end ) {
  83. var mid = Math.ceil( ( start + end ) / 2 );
  84. if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
  85. index = mid;
  86. break;
  87. } else if ( x < dist[ mid ] ) {
  88. end = mid - 1;
  89. } else {
  90. start = mid + 1;
  91. }
  92. }
  93. return index;
  94. },
  95. sampleFace: function ( faceIndex, targetPosition, targetNormal, targetColor ) {
  96. var u = this.randomFunction();
  97. var v = this.randomFunction();
  98. if ( u + v > 1 ) {
  99. u = 1 - u;
  100. v = 1 - v;
  101. }
  102. _face.a.fromBufferAttribute( this.positionAttribute, faceIndex * 3 );
  103. _face.b.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 1 );
  104. _face.c.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 2 );
  105. targetPosition
  106. .set( 0, 0, 0 )
  107. .addScaledVector( _face.a, u )
  108. .addScaledVector( _face.b, v )
  109. .addScaledVector( _face.c, 1 - ( u + v ) );
  110. if ( targetNormal !== undefined ) {
  111. _face.getNormal( targetNormal );
  112. }
  113. if ( targetColor !== undefined && this.colorAttribute !== undefined ) {
  114. _face.a.fromBufferAttribute( this.colorAttribute, faceIndex * 3 );
  115. _face.b.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 1 );
  116. _face.c.fromBufferAttribute( this.colorAttribute, faceIndex * 3 + 2 );
  117. _color
  118. .set( 0, 0, 0 )
  119. .addScaledVector( _face.a, u )
  120. .addScaledVector( _face.b, v )
  121. .addScaledVector( _face.c, 1 - ( u + v ) );
  122. targetColor.r = _color.x;
  123. targetColor.g = _color.y;
  124. targetColor.b = _color.z;
  125. }
  126. return this;
  127. }
  128. };
  129. return MeshSurfaceSampler;
  130. } )();
  131. export { MeshSurfaceSampler };