compute_particles

compute_particles

English | 日本語

Overview

This sample uses ComputeParticleEmitter to generate, update, and render 49,152 particles on the GPU. Application code supplies emission position, direction, speed, and lifetime; the standard class builds the WGSL and particle-buffer layout. Position, velocity, and lifetime are updated in a GPU storage buffer, and the same buffer is read for billboard rendering.

The fountain mode uses one emission request for an upward particle group. The ring mode uses 24 requests placed around a circle. Both modes use the same ComputeParticleEmitter API. HDR particle colors use additive blending.

How to Run

Open compute_particles.html through an HTTP server in a WebGPU-capable browser.

webg Features Used

Controls

Implementation Details

main.js creates ComputeParticleEmitter with the following settings.

const emitter = new ComputeParticleEmitter(screen.getGPU(), {
  capacity: 49152,
  preset: "spark",
  targetFormat: screen.getGPU().format,
  overflow: "replace-oldest",
  simulation: { gravity: [0, -0.70, 0], drag: 0.05 },
  appearance: {
    colors: [[1.0, 0.28, 0.04], [0.04, 0.46, 1.0]],
    intensity: 1.26,
    size: [0.006, 0.018]
  }
});

Fountain uses one request, while ring uses 24 requests. Each request specifies position, direction, spreadAngle, speed, and lifetime. Emission requests, camera data, and time are packed into uniforms; the Compute Shader generates initial particle state and updates motion.

targetFormat selects the swapchain format for direct Canvas rendering in this sample. For PBR scenes, WebgSceneApp.createComputeParticleEmitter() creates the HDR intermediate target and ComputeParticlePass.

The displayed estimated count is a CPU reservation estimate based on maximum lifetime. Normal rendering reads particle positions directly from the GPU, while capacity and emission diagnostics use the CPU reservation estimate.

Checkpoints