Particlize

FBO

Frame Buffer Object class for GPU-based particle computations

Constructor

new FBO(params: FBOParams)

Parameters

ParameterTypeDescriptionDefault
namestringUnique name for the FBO-
widthnumberWidth of the render target-
heightnumberHeight of the render target-
rendererTHREE.WebGLRendererWebGL renderer instance-
cameraTHREE.OrthographicCameraOrthographic camera for rendering-
propertiesProperty[]Array of properties managed by this FBO[]
channelsnumberNumber of color channels (1-4)4

Properties

PropertyTypeDescription
uuidstringUnique identifier for the FBO
namestringName of the FBO
textureNamestringTexture uniform name (automatically generated)
widthnumberRender target width
heightnumberRender target height
channelsnumberNumber of color channels (1-4)
rendererTHREE.WebGLRendererWebGL renderer
cameraTHREE.OrthographicCameraCamera for rendering
sceneTHREE.SceneScene for FBO rendering
materialTHREE.ShaderMaterialShader material for computations
readTHREE.WebGLRenderTargetRead render target
writeTHREE.WebGLRenderTargetWrite render target
propertiesProperty[]Properties managed by this FBO
dependenciesSet<FBO>FBOs that this FBO depends on
constraintsMap<string, Constraint>Constraints applied to this FBO
needsUpdatebooleanWhether the FBO needs updating

Methods

addConstraint(constraint: Constraint): void

Adds a constraint to the FBO.

removeConstraint(name: string): boolean

Removes a constraint by name.

hasConstraint(name: string): boolean

Checks if a constraint exists.

addProperty(property: Property): void

Adds a property to be managed by this FBO.

removeProperty(property: Property): void

Removes a property from the FBO.

inject(data: Float32Array, offset?: number): void

Injects data into the FBO at the specified offset.

update(): void

Updates the FBO by rendering with current constraints and uniforms.

swap(): void

Swaps the read and write render targets.

setUniforms(uniforms: Record<string, any>): void

Sets uniforms on the FBO's shader material.

rebuildShader(): void

Rebuilds the FBO's shader with current constraints.

addDependency(fbo: FBO): void

Adds a dependency on another FBO.

removeDependency(fbo: FBO): void

Removes a dependency.

dispose(): void

Disposes of all FBO resources.

Example Usage

import { FBO, Property, RadialForce } from 'particlize';
import * as THREE from 'three';

// Setup
const renderer = new THREE.WebGLRenderer();
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 1000);

// Create position FBO
const positionFBO = new FBO({
  name: 'position',
  width: 512,
  height: 512,
  renderer,
  camera,
  channels: 4
});

// Add constraints
const radialForce = new RadialForce({
  center: [0, 0, 0],
  strength: 0.1
});
positionFBO.addConstraint(radialForce);

// Inject initial data
const particleCount = 1000;
const initialPositions = new Float32Array(particleCount * 4);
// ... fill initial positions ...

positionFBO.inject(initialPositions);

// Animation loop
function animate() {
  positionFBO.setUniforms({
    u_time: performance.now() / 1000,
    u_deltaTime: 0.016
  });
  
  positionFBO.update();
  requestAnimationFrame(animate);
}
animate();