examples/lib/Three.debug.js
function ( geometry, n ) {

		var face, i,
			faces = geometry.faces,
			vertices = geometry.vertices,
			il = faces.length,
			totalArea = 0,
			cumulativeAreas = [],
			vA, vB, vC, vD;

		// precompute face areas

		for ( i = 0; i < il; i ++ ) {

			face = faces[ i ];

			if ( face instanceof THREE.Face3 ) {

				vA = vertices[ face.a ];
				vB = vertices[ face.b ];
				vC = vertices[ face.c ];

				face._area = THREE.GeometryUtils.triangleArea( vA, vB, vC );

			} else if ( face instanceof THREE.Face4 ) {

				vA = vertices[ face.a ];
				vB = vertices[ face.b ];
				vC = vertices[ face.c ];
				vD = vertices[ face.d ];

				face._area1 = THREE.GeometryUtils.triangleArea( vA, vB, vD );
				face._area2 = THREE.GeometryUtils.triangleArea( vB, vC, vD );

				face._area = face._area1 + face._area2;

			}

			totalArea += face._area;

			cumulativeAreas[ i ] = totalArea;

		}

		// binary search cumulative areas array

		function binarySearchIndices( value ) {

			function binarySearch( start, end ) {

				// return closest larger index
				// if exact number is not found

				if ( end < start )
					return start;

				var mid = start + Math.floor( ( end - start ) / 2 );

				if ( cumulativeAreas[ mid ] > value ) {

					return binarySearch( start, mid - 1 );

				} else if ( cumulativeAreas[ mid ] < value ) {

					return binarySearch( mid + 1, end );

				} else {

					return mid;

				}

			}

			var result = binarySearch( 0, cumulativeAreas.length - 1 )
			return result;

		}

		// pick random face weighted by face area

		var r, index,
			result = [];

		var stats = {};

		for ( i = 0; i < n; i ++ ) {

			r = THREE.GeometryUtils.random() * totalArea;

			index = binarySearchIndices( r );

			result[ i ] = THREE.GeometryUtils.randomPointInFace( faces[ index ], geometry, true );

			if ( ! stats[ index ] ) {

				stats[ index ] = 1;

			} else {

				stats[ index ] += 1;

			}

		}

		return result;

	}
function ( geometry, n ) {

		var face, i,
			faces = geometry.faces,
			vertices = geometry.vertices,
			il = faces.length,
			totalArea = 0,
			cumulativeAreas = [],
			vA, vB, vC, vD;

		// precompute face areas

		for ( i = 0; i < il; i ++ ) {

			face = faces[ i ];

			if ( face instanceof THREE.Face3 ) {

				vA = vertices[ face.a ];
				vB = vertices[ face.b ];
				vC = vertices[ face.c ];

				face._area = THREE.GeometryUtils.triangleArea( vA, vB, vC );

			} else if ( face instanceof THREE.Face4 ) {

				vA = vertices[ face.a ];
				vB = vertices[ face.b ];
				vC = vertices[ face.c ];
				vD = vertices[ face.d ];

				face._area1 = THREE.GeometryUtils.triangleArea( vA, vB, vD );
				face._area2 = THREE.GeometryUtils.triangleArea( vB, vC, vD );

				face._area = face._area1 + face._area2;

			}

			totalArea += face._area;

			cumulativeAreas[ i ] = totalArea;

		}

		// binary search cumulative areas array

		function binarySearchIndices( value ) {

			function binarySearch( start, end ) {

				// return closest larger index
				// if exact number is not found

				if ( end < start )
					return start;

				var mid = start + Math.floor( ( end - start ) / 2 );

				if ( cumulativeAreas[ mid ] > value ) {

					return binarySearch( start, mid - 1 );

				} else if ( cumulativeAreas[ mid ] < value ) {

					return binarySearch( mid + 1, end );

				} else {

					return mid;

				}

			}

			var result = binarySearch( 0, cumulativeAreas.length - 1 )
			return result;

		}

		// pick random face weighted by face area

		var r, index,
			result = [];

		var stats = {};

		for ( i = 0; i < n; i ++ ) {

			r = THREE.GeometryUtils.random() * totalArea;

			index = binarySearchIndices( r );

			result[ i ] = THREE.GeometryUtils.randomPointInFace( faces[ index ], geometry, true );

			if ( ! stats[ index ] ) {

				stats[ index ] = 1;

			} else {

				stats[ index ] += 1;

			}

		}

		return result;

	}
function ( geometry , n ) { var face , i , faces = geometry . faces , vertices = geometry . vertices , il = faces . length , totalArea = 0 , cumulativeAreas = [ ] , vA , vB , vC , vD ; // precompute face areas for ( i = 0 ; i < il ; i ++ ) { face = faces [ i ] ; if ( face instanceof THREE . Face3 ) { vA = vertices [ face . a ] ; vB = vertices [ face . b ] ; vC = vertices [ face . c ] ; face . _area = THREE . GeometryUtils . triangleArea ( vA , vB , vC ) ; } else if ( face instanceof THREE . Face4 ) { vA = vertices [ face . a ] ; vB = vertices [ face . b ] ; vC = vertices [ face . c ] ; vD = vertices [ face . d ] ; face . _area1 = THREE . GeometryUtils . triangleArea ( vA , vB , vD ) ; face . _area2 = THREE . GeometryUtils . triangleArea ( vB , vC , vD ) ; face . _area = face . _area1 + face . _area2 ; } totalArea += face . _area ; cumulativeAreas [ i ] = totalArea ; } // binary search cumulative areas array function binarySearchIndices ( value ) { function binarySearch ( start , end ) { // return closest larger index // if exact number is not found if ( end < start ) return start ; var mid = start + Math . floor ( ( end - start ) / 2 ) ; if ( cumulativeAreas [ mid ] > value ) { return binarySearch ( start , mid - 1 ) ; } else if ( cumulativeAreas [ mid ] < value ) { return binarySearch ( mid + 1 , end ) ; } else { return mid ; } } var result = binarySearch ( 0 , cumulativeAreas . length - 1 ) return result ; } // pick random face weighted by face area var r , index , result = [ ] ; var stats = { } ; for ( i = 0 ; i < n ; i ++ ) { r = THREE . GeometryUtils . random ( ) * totalArea ; index = binarySearchIndices ( r ) ; result [ i ] = THREE . GeometryUtils . randomPointInFace ( faces [ index ] , geometry , true ) ; if ( ! stats [ index ] ) { stats [ index ] = 1 ; } else { stats [ index ] += 1 ; } } return result ; }
Get uniformly distributed random points in mesh - create array with cumulative sums of face areas - pick random number from 0 to total area - find corresponding place in area array by binary search - get random point in face
Get uniformly distributed random points in mesh - create array with cumulative sums of face areas - pick random number from 0 to total area - find corresponding place in area array by binary search - get random point in face