# [name]

Utility class for sampling weighted random points on the surface of a mesh.

Weighted sampling is useful for effects like heavier foliage growth in certain areas of terrain, or concentrated particle emissions from specific parts of a mesh. Vertex weights may be written programmatically, or painted by hand as vertex colors in 3D tools like Blender.

## Import

[name] is an add-on, and must be imported explicitly. See [link:#manual/introduction/Installation Installation / Addons].

`import { MeshSurfaceSampler } from 'three/addons/math/MeshSurfaceSampler.js';`

## Code Example

`// Create a sampler for a Mesh surface. const sampler = new MeshSurfaceSampler( surfaceMesh ) .setWeightAttribute( 'color' ) .build(); const mesh = new THREE.InstancedMesh( sampleGeometry, sampleMaterial, 100 ); const position = new THREE.Vector3(); const matrix = new THREE.Matrix4(); // Sample randomly from the surface, creating an instance of the sample // geometry at each sample point. for ( let i = 0; i < 100; i ++ ) { sampler.sample( position ); matrix.makeTranslation( position.x, position.y, position.z ); mesh.setMatrixAt( i, matrix ); } scene.add( mesh );`

## Examples

[example:webgl_instancing_scatter]

## Constructor

### [name]( [param:Mesh mesh] )

[page:Mesh mesh] — Surface mesh from which to sample.

Creates a new [name]. If the input geometry is indexed, a non-indexed copy is made. After construction, the sampler is not able to return samples until [page:MeshSurfaceSampler.build build] is called.

## Methods

### [method:this setWeightAttribute]( [param:String name] )

Specifies a vertex attribute to be used as a weight when sampling from the surface. Faces with higher weights are more likely to be sampled, and those with weights of zero will not be sampled at all. For vector attributes, only .x is used in sampling.

If no weight attribute is selected, sampling is randomly distributed by area.

### [method:this build]()

Processes the input geometry and prepares to return samples. Any configuration of the geometry or sampler must occur before this method is called. Time complexity is O(n) for a surface with n faces.

### [method:this sample]( [param:Vector3 targetPosition], [param:Vector3 targetNormal], [param:Color targetColor], [param:Vector2 targetUV] )

Selects a random point on the surface of the input geometry, returning the position and optionally the normal vector, color and UV Coordinate at that point. Time complexity is O(log n) for a surface with n faces.

## Source

[link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/math/MeshSurfaceSampler.js examples/jsm/math/MeshSurfaceSampler.js]