Browse Source

Merge pull request #20576 from gkjohnson/surface-sampler-random-function

MeshSurfaceSampler: Add a "setRandomGenerator" function
Mr.doob 4 years ago
parent
commit
ab5cb6fe79
1 changed files with 11 additions and 3 deletions
  1. 11 3
      examples/jsm/math/MeshSurfaceSampler.js

+ 11 - 3
examples/jsm/math/MeshSurfaceSampler.js

@@ -35,6 +35,7 @@ var MeshSurfaceSampler = ( function () {
 		}
 
 		this.geometry = geometry;
+		this.randomFunction = Math.random;
 
 		this.positionAttribute = this.geometry.getAttribute( 'position' );
 		this.weightAttribute = null;
@@ -104,11 +105,18 @@ var MeshSurfaceSampler = ( function () {
 
 		},
 
+		setRandomGenerator: function ( randomFunction ) {
+
+			this.randomFunction = randomFunction;
+			return this;
+
+		},
+
 		sample: function ( targetPosition, targetNormal ) {
 
 			var cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
 
-			var faceIndex = this.binarySearch( Math.random() * cumulativeTotal );
+			var faceIndex = this.binarySearch( this.randomFunction() * cumulativeTotal );
 
 			return this.sampleFace( faceIndex, targetPosition, targetNormal );
 
@@ -150,8 +158,8 @@ var MeshSurfaceSampler = ( function () {
 
 		sampleFace: function ( faceIndex, targetPosition, targetNormal ) {
 
-			var u = Math.random();
-			var v = Math.random();
+			var u = this.randomFunction();
+			var v = this.randomFunction();
 
 			if ( u + v > 1 ) {