Particlize

Constraint

Base class for creating particle constraints and forces

Constructor

new Constraint(name: string, glsl?: string, uniforms?: Record<string, any>)

Parameters

ParameterTypeDescriptionDefault
namestringUnique name for the constraint-
glslstringGLSL shader code for the constraint""
uniformsRecord<string, any>Uniform values for the shader{}

Properties

PropertyTypeDescription
namestringUnique identifier for the constraint
activebooleanWhether the constraint is active
glslstringGLSL shader code that implements the constraint
uniformsRecord<string, any>Uniform values passed to the shader
paramsRecord<string, { value: number | number[], hardcode?: boolean }> | nullParameter definitions for the constraint

Methods

build(): void

Processes the constraint's parameters and builds the final GLSL code.

Example Usage

import { Constraint, PropertyManager } from 'particlize';

// Create a basic gravity constraint
const gravity = new Constraint(
  'gravity',
  `
    vec3 gravity = vec3(0.0, -0.1, 0.0);
    velocity.xyz += gravity * u_deltaTime;
  `,
  {
    u_deltaTime: 0.016
  }
);

// Create property manager and add constraint
const propertyManager = new PropertyManager({ /* ... */ });
propertyManager.addProperty('velocity', { size: 3 });
propertyManager.addConstraint('velocity', gravity);

// Create a custom parameterized constraint
class SinewaveConstraint extends Constraint {
  constructor(amplitude: number, frequency: number) {
    super('sinewave');
    
    this.params = {
      amplitude: { value: amplitude, hardcode: true },
      frequency: { value: frequency, hardcode: false }
    };
    
    this.glsl = `
      float amplitude@ = #AMPLITUDE;
      float frequency@ = #FREQUENCY;
      
      position.y += sin(position.x * frequency@ + u_time) * amplitude@;
    `;
  }
}

// Use custom constraint
const sinewave = new SinewaveConstraint(0.5, 2.0);
propertyManager.addConstraint('position', sinewave);