A simple interface for pooling any type of object (such as a mesh, game asset, or even just a data structure of some kind).
import ObjectPool from './ObjectPool';
const geometry = new THREE.IcosahedronGeometry(1);
const pool = new ObjectPool({
// set initial size to avoid expansion
initialCapacity: 10,
create () {
// create a mesh once with a shared geometry
const material = new THREE.MeshBasicMaterial({ color: 'red' });
return new THREE.Mesh(geometry, material);
},
renew (mesh) {
// when the mesh is requested from the pool
// e.g. set initial random states
},
release (mesh) {
// when the mesh is placed back into the pool
}
});
// Get a mesh from the pool
const particle = pool.next();
// Do something with it
// Free them once you're finished
pool.release(particle);