DirectX 11 Particle System Sample

Explore advanced particle simulation techniques with DirectX 11.

Overview

This sample demonstrates a high-performance particle system implemented using DirectX 11. It leverages compute shaders for particle simulation and geometry shaders for efficient rendering of particles. The system supports a wide range of particle behaviors, including emitter types, forces, collisions, and material properties.

Features

  • Compute Shader-based Simulation: Efficiently update particle states on the GPU.
  • Geometry Shader Rendering: Dynamically generate particle geometry on the fly.
  • Flexible Emitters: Configurable particle sources with various emission patterns.
  • Force Fields and Interactions: Simulate complex physical behaviors.
  • Collision Detection: Basic particle-to-particle and particle-to-world collision.
  • Dynamic Parameter Control: Adjust simulation parameters in real-time.

Sample Code

Below is a snippet demonstrating the core compute shader logic for updating particle velocity based on forces:

// Particle.hlsl struct Particle { float3 position; float3 velocity; float4 color; float size; uint active; }; // Constant buffer for simulation parameters cbuffer SimParams : register(b0) { float deltaTime; float gravity; float3 globalForce; }; // UAV for particle buffer RWStructuredBuffer<Particle> particleBuffer : register(u0); [numthreads(64, 1, 1)] void CSMain(uint3 dispatchThreadID : SV_DispatchThreadID) { uint particleIndex = dispatchThreadID.x; Particle p = particleBuffer[particleIndex]; if (p.active) { // Apply forces p.velocity += (globalForce + float3(0.0f, -gravity, 0.0f)) * deltaTime; // Update position p.position += p.velocity * deltaTime; // Basic boundary checks or reset if (p.position.y < -10.0f || length(p.position) > 100.0f) { p.active = 0; // Deactivate particle } particleBuffer[particleIndex] = p; } }

How to Use

To run this sample, you will need a DirectX 11 compatible graphics card and the Windows SDK installed. The project is typically built using Visual Studio.

1. Download the sample code.

2. Open the solution file in Visual Studio.

3. Build and run the project.

Experiment with the various parameters exposed in the application's UI to observe different particle behaviors.

Download Sample Code