Particlize
Samplers

MeshSurfaceSampler

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

MeshSurfaceSampler

The MeshSurfaceSampler class provides efficient random sampling of points on the surface of a mesh.

Constructor

new MeshSurfaceSampler(mesh: THREE.Mesh)

Parameters

ParameterTypeDescription
meshTHREE.MeshThe surface mesh from which to sample points

Properties

PropertyTypeDescription
geometryTHREE.BufferGeometryThe mesh geometry being sampled
weightAttributeTHREE.BufferAttribute | nullAttribute used for weighted sampling
distributionFloat32Array | nullCumulative distribution for sampling

Methods

MethodDescription
setWeightAttribute(name: string | null)Sets vertex attribute to use as weight for sampling
setRandomGenerator(randomFunction: () => number)Sets custom random number generator
build()Processes geometry and prepares sampler for sampling
sample(targetPosition: Vector3, targetNormal?: Vector3, targetColor?: Color, targetUV?: Vector2)Samples a random point on the surface

Example Usage

import { MeshSurfaceSampler } from 'particlize';
import * as THREE from 'three';

// Create mesh
const geometry = new THREE.SphereGeometry(1, 32, 32);
const mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial());

// Create and build sampler
const sampler = new MeshSurfaceSampler(mesh)
  .setWeightAttribute('color')
  .build();

// Sample points
const position = new THREE.Vector3();
const normal = new THREE.Vector3();

for (let i = 0; i < 1000; i++) {
  sampler.sample(position, normal);
  // Use sampled position and normal
}