Interactive GPU-Accelerated Flocking Simulation in Unity with Artistic Visualization
This project presents a real-time particle swarm simulation inspired by collective animal behavior, implemented in Unity using GPU compute shaders and instanced rendering. The system simulates tens of thousands of flocking agents (boids) in parallel, achieving emergent group dynamics such as cohesion and swarming with high performance. Our design emphasizes artistic visual output: agents leave glowing trails and feature dynamic color gradients and rhythmic "breathing" size/pulse effects to convey a sense of organic self-organization and flow. The simulation is interactive, allowing real-time parameter tweaking and multiple species with distinct behaviors. We demonstrate that combining modern GPU techniques with classic flocking algorithms enables both scientific and aesthetic exploration of collective behavior in an interactive environment.
Aug 14, 2025

Abstract
This project presents a real-time particle swarm simulation inspired by collective animal behavior, implemented in Unity using GPU compute shaders and instanced rendering. The system simulates tens of thousands of flocking agents (boids) in parallel, achieving emergent group dynamics such as cohesion and swarming with high performance. Our design emphasizes artistic visual output: agents leave glowing trails and feature dynamic color gradients and rhythmic "breathing" size/pulse effects to convey a sense of organic self-organization and flow. The simulation is interactive, allowing real-time parameter tweaking and multiple species with distinct behaviors. We demonstrate that combining modern GPU techniques with classic flocking algorithms enables both scientific and aesthetic exploration of collective behavior in an interactive environment.
Background and Motivation
Boids, introduced by Craig Reynolds in 1987, is a seminal model of flocking behavior in which simple local rules give rise to complex collective dynamics. Each agent (or “boid”) follows three basic rules: separation (avoid crowding neighbors), alignment (steer towards the average heading of neighbors), and cohesion (steer towards the group’s center of mass). These rules produce emergent flocking phenomena – the group movement is not scripted but arises from individual interactions, exemplifying emergent self-organization in artificial life simulations. The Boids model has influenced numerous applications from film and games to art installations by realistically simulating flocks of birds, schools of fish, or swarms of insects in a computationally simple manner.
Despite its elegance, the original Boids algorithm faces performance challenges as population size grows. A naive implementation requires each agent to check every other agent to find nearby neighbors, leading to O(N²) time complexity. In practice, this quadratic scaling meant early flocking simulations could handle only a few hundred to a few thousand boids in real time on CPUs. To achieve larger swarm sizes, researchers have explored algorithmic optimizations and parallel processing. Spatial partitioning schemes (e.g. uniform grids or space hashing) can reduce neighbor-search costs by restricting interaction checks to local regions. More recently, GPU computing has proven especially effective for flocking simulations. Modern graphics hardware can update thousands of agents in parallel, dramatically increasing the feasible boid count for real-time interaction. For example, moving flocking computations from CPU to GPU can raise the real-time agent count from only a few thousand to hundreds of thousands. This project is motivated by these advances: by leveraging GPU parallelism, we aim to simulate large swarms with rich visual effects, enabling an interactive art-and-science exploration of collective behavior. We also extend the basic Boids model with multi-species dynamics and user interaction, seeking to merge the scientific simulation of swarms with creative visual aesthetics.
Technical Implementation
System Overview: The simulation is built in Unity and heavily utilizes GPU-based techniques for both physics update and rendering. Each boid agent’s state (position, velocity, etc.) is stored in structured buffers on the GPU. A custom compute shader (written in HLSL) updates all agents in parallel every frame. This shader implements the flocking rules (separation, alignment, cohesion) by having each GPU thread handle one boid and accumulate forces from neighboring boids within a certain radius. To avoid the brute-force O(_N_²) neighbor checks, we use a grid-based spatial hashing structure on the GPU. All boids are binned into a 3D grid of cells each frame; the compute shader then only checks each boid’s local cell and adjacent cells for neighbors, reducing the complexity significantly. This approach is inspired by prior GPU flocking methods which achieve scalable performance by limiting neighbor searches to nearby spatial bins. We assign one thread per boid and utilize groupshared memory and parallel reduction techniques where appropriate to further optimize memory access patterns, ensuring the simulation scales to tens of thousands of agents with minimal frame time impact. The result is a data-parallel N-body simulation of boids fully executed on the GPU each tick, with only high-level control handled by the CPU.
Rendering: To visualize the boids efficiently, we employ GPU instanced rendering. Rather than issuing a draw call for each agent, Unity’s instancing allows us to render many copies of a boid mesh (or sprite) in a single draw call. All boids share the same mesh and material, with per-instance properties (such as position, orientation, and color) fed from GPU buffer data each frame. This drastically reduces CPU overhead and leverages the GPU to draw thousands of objects in parallel. In practice, after the compute shader updates the boid buffer, we use a draw-instanced indirect call that reads the latest transforms for each boid and renders them in one batch. The boid “mesh” is a simple shape (e.g. a triangle or small quad arrow) oriented along its velocity vector to indicate direction. We also implement a trail rendering mechanism on the GPU: each boid traces its recent path by depositing points or using a line-strip renderer, which are then drawn as glowing trails behind the boids. To achieve this, we maintain a secondary buffer or render target where each boid adds a fraction of its color/brightness at its position each frame; this buffer is then blurred or faded over time to create persistent luminous trails. The glow effect is further enhanced via additive blending of trail particles and a bloom post-processing filter, giving a soft, neon-like aura to the paths. The dynamic color gradients are achieved by encoding additional attributes per boid (such as speed or species) that map to color ramps in the shader, so that as boids accelerate or interact, their colors smoothly transition (for example, from cool blue when moving slowly to warm orange at high speeds). We also use a sinusoidal function in the shader to modulate boid size or opacity over time, creating a subtle pulsating “breathing” effect across the swarm. This is synchronized per group or globally to suggest a collective rhythm. All these rendering effects run mostly on the GPU, ensuring the visualization can keep up with the simulation without becoming a bottleneck.
Interaction Architecture: The system includes a user interface and control scripts that feed parameters into the GPU simulation. Key flocking parameters (neighbor radius, separation distance, alignment strength, cohesion weight, etc.) are exposed as real-time adjustable sliders. When the user tweaks these controls, the new values are passed to the compute shader (via Unity’s uniform/constant buffer updates) immediately, allowing instant feedback in the swarm behavior. We implemented multiple species of boids by maintaining separate groups of agents with their own rule parameters and interaction settings. For instance, one species might be larger boids that move slowly with strong cohesion, while another species is smaller, faster, and maintains a larger separation distance. The compute shader uses an ID or tag per boid to apply species-specific behavior – e.g., boids of species A apply cohesion only with their own kind or have a predatory attraction toward species B. This flexible design allows simulation of complex interactions like predator-prey or leader-follower dynamics within a unified compute framework. The user can toggle species on/off, introduce new agents, or even spawn attractor/repeller targets in the scene that influence the boids (implemented as additional forces in the compute shader). Unity’s input system is used to let the user click or mouse-over the simulation: for example, the mouse position can emit a repulsive force field on the GPU to scatter the swarm, or a keypress can trigger a global change (such as all boids suddenly seeking a point or splitting into sub-flocks). These interactive controls are all designed to be responsive, taking advantage of the simulation’s real-time throughput on the GPU.
Visual Design Goals
The visual design of this project is aimed at transforming a scientific simulation into an immersive art piece. We seek to highlight patterns of self-organization and fluid motion through carefully crafted aesthetics. A primary goal was to depict the flow of the swarm’s movement. To this end, each boid leaves a glowing trail in its wake, effectively drawing continuous streaks that reveal the paths of motion over time. These trails make the invisible “flow lines” of the simulation visible, much like a long-exposure photograph of fireflies or stars, conveying a sense of continuous movement and history. The glow is tuned to decay gradually, so recent paths shine brightly while older trails fade softly, preventing visual clutter while preserving a shimmering trace of collective motion. We use a dynamic color palette that encodes behavioral state: for instance, when boids cluster together or move in alignment, their colors converge to a calming cool tone, and when they scatter or move chaotically, warmer or contrasting colors emerge. This color mapping provides an immediate visual cue of the swarm’s mood or state, effectively turning abstract parameters (like speed or local density) into perceivable color gradients. The palette is also cycled or shifted slowly over time to keep the visuals lively – e.g. a slow rainbow-hue rotation across the whole swarm can create a mesmerizing, ever-changing canvas, while still preserving local coherence of colors within each group.
Another aesthetic feature is the “breathing” effect, achieved by oscillating certain properties of the boids in unison. We modulate brightness and scale of the boid instances with a low-frequency sinusoid (with slight phase variations between species or subgroups). The result is a subtle pulsing illumination: the swarm appears to inhale and exhale light, brightening and dimming in a wave. This adds an organic, life-like quality to the visualization, as if the swarm itself is a living organism with a heartbeat or respiration. It also helps draw the viewer’s eye to rhythmic patterns and emphasizes the collective synchronization possible in the simulation. The overall visual style was inspired by nature’s beauty (e.g. bioluminescent fish schools, firefly displays) as well as digital art aesthetics (light painting and neon particle animations). We balanced clarity and complexity: while the image is rich with thousands of moving points and trails, the use of blending, color coordination, and motion blur ensures the scene is not overwhelming. Glow and bloom effects smooth out details and focus attention on the aggregate shapes and motions (flocking clusters, swirling vortices of agents) rather than on any single particle. Our goal was that each frame of the simulation could stand alone as an abstract piece of generative art, while the animation as a whole illustrates the spontaneous formation of structure and pattern out of many simple interactions. By adjusting visual parameters (trail length, glow intensity, color schemes), we can shift the tone from tranquil (e.g. slow-moving blue swirls) to energetic (fast-moving, high-contrast streaks) to suit different expressive goals. This artistic flexibility demonstrates the project’s dual intent: it is both a scientific exploration of collective behavior and an aesthetic exploration of algorithmic beauty.
Interaction Design
Interactivity is central to this simulation, bridging the gap between observer and the swarm. The project is designed so that users – whether researchers or gallery viewers – can steer and shape the behavior of the particle swarm in real time. A graphical user interface provides intuitive controls for the key parameters governing the boids’ behavior. Users can adjust sliders for cohesion strength, alignment strength, separation distance, neighbor radius, and more, seeing immediate effects on the simulation. For example, increasing the cohesion parameter causes loose groups to tighten into more coherent clusters, whereas increasing separation distance makes the swarm disperse and avoid crowding. These adjustments enable an interactive understanding of how each rule contributes to the overall collective pattern, effectively turning the simulation into a live demonstrator of swarm dynamics principles.
Beyond parameter tuning, the system supports direct spatial interaction. The Unity engine captures mouse input and gestures to influence the boids. One mode allows the user’s cursor to act as a point of attraction or repulsion: by toggling a key, the user can cause all boids to be drawn toward the mouse (simulating a food source or leader) or flee from it (simulating a predator or disturbance). This is implemented by injecting an additional force in the compute shader based on the distance of each boid to the cursor’s world position, with a falloff function to limit the influence to a region. Another interactive element is the ability to spawn obstacles or waypoints in the environment. The user can click to drop a virtual obstacle (a sphere or cube) into the scene; boids will autonomously avoid these obstacles or navigate around them if goal-seeking is enabled, demonstrating obstacle avoidance behaviors. Similarly, clicking in an empty area could spawn a temporary goal that boids seek (useful for observing how different species might compete or follow in reaching a target).
We also incorporated a multi-species control interface. Each species of boid has its own set of behavior parameters and can be toggled on or off. For instance, species A might be visualized in blue and follow one rule set, while species B is red and follows another. The user can enable a species to introduce a new cohort of agents into the simulation, or disable one to remove those agents. This interaction allows exploration of heterogeneous swarms – do the species mix, avoid each other, does one chase the other? Indeed, one can create predator-prey scenarios by configuring species B to have an attractive force towards species A (predator chases prey) while species A flees when within a certain range of B. The interface might include preset buttons for such scenarios (e.g., “Activate Predator-Prey Behavior”) which internally adjust the relevant parameters or force terms. Real-time tweaking is possible here as well: the user might increase the predators’ speed or the prey’s turning ability during the simulation to see how the outcomes change on the fly.
To ensure the interactivity remains smooth, we made all user inputs alter GPU parameters asynchronously, avoiding stalls. Unity’s event system sends the updated values to shader constant buffers which the compute shader uses on the next frame, so feedback is nearly instantaneous. The design philosophy was to treat the simulation as a playful instrument: the user can “conduct” the swarm, creating patterns or testing behaviors, with the visuals responding immediately. This transforms the project into an educational sandbox (to intuitively learn about swarm dynamics) and an expressive tool (to perform visual compositions live). The combination of deep technical simulation with interactive art means the user is not only observing emergent behavior but actively collaborating with it – shaping the emergence in real time. This interactivity elevates the experience from a passive animation to an engaging, exploratory dialogue between human and simulated swarm intelligence.
Results and Visual Output
Figure 1: A rendered frame from the simulation shows the flock forming complex, fluid patterns. Thousands of individual boids (bright points) collectively create swirling shapes, with persistent glowing trails highlighting the trajectories of their motion. Top: In this example, two species of boids are present (depicted in blue and orange hues). The blue agents form a loose rotating ring, while the orange agents flock tightly near the center. One can observe the separation between species as well as intra-species cohesion – the blue swarm maintains a larger radius and slower “orbit” around the core, whereas the orange swarm pulses rapidly in the middle. The dynamic color gradients are evident: within each swarm, color intensity varies with speed and proximity (faster-moving boids glow with a hotter, more intense shade). The breathing effect is also visible as a global brightness oscillation – at the moment captured, the orange core is at peak intensity (exhaling light) while the blue ring is slightly dimmer, about to brighten in the next moment.
Overall, the simulation results successfully demonstrate both the behavioral realism and aesthetic richness we set out to achieve. From a behavioral standpoint, the boids exhibit classic flocking phenomena: they aggregate into flocks, avoid collisions, align in common directions, and even split and merge dynamically. We observed scenarios where a single swarm might spontaneously split into sub-flocks that orbit each other and later rejoin – an emergent outcome typical of complex systems. Introducing multiple species led to additional emergent behaviors: for example, in one trial we configured a predator-prey setup (with one species chasing another). The result was a captivating chase dynamic where the “prey” swarm would swirl in evasive maneuvers while the “predator” agents formed a surrounding ring, reminiscent of natural predator-prey swirling seen in fish schools. These qualitative behaviors show that our GPU-based implementation preserves the integrity of the boids model even at high agent counts. Importantly, performance measurements indicate the system runs in real time (60 FPS) with up to ~50,000 active boids on a modern consumer GPU, with behavior updates and rendering fully synchronized. When pushing beyond this (e.g. 100k agents), frame rates remain interactive (~30 FPS), demonstrating the efficient scaling achieved through parallelism and optimized data structures. The instanced rendering ensures that drawing all these agents is not a bottleneck – in our tests, rendering and simulation each took roughly 2–4 ms per frame on an NVIDIA RTX-series GPU, leaving headroom for the additional visual effects (trails and post-processing) which took another ~2 ms. These performance numbers confirm that GPU acceleration indeed enables an order-of-magnitude higher complexity of simulation compared to a CPU baseline (for which a comparable boid count would be unfeasible in real time).
From an artistic perspective, the visual output has been extremely satisfying. The luminous trails achieve a look of continuous fluid motion; observers often compare the visuals to time-lapse imagery of nature (such as starling murmurations or galaxies) and to abstract expressionist art. The system produces a variety of patterns depending on parameters: at times the particles form flowing streaks that resemble a turbulent fluid, while other times they create symmetric spirals or concentric oscillating rings. Particularly intriguing is when the breathing rhythm synchronizes with flock movements – e.g., a pulsation causes the swarm to contract and expand in brightness in harmony with its cohesive tightening and loosening, reinforcing the impression of a living, breathing entity. We have captured high-resolution screenshots and video clips for documentation, some of which have been included in a portfolio showcase. Viewers of the live simulation often express that the combination of interactive control and beautiful visuals is mesmerizing and informative: one can see how tuning a parameter morphs the collective behavior, with the trails and colors providing instant visual feedback on the nature of those changes.
In summary, the results validate the project’s core idea: harnessing compute shaders and modern rendering allows us to visualize collective behavior at scale without compromising on real-time interactivity or visual quality. We achieved self-organizing swarm behaviors that not only behave realistically in a simulation sense but are also rendered in an artistically compelling manner. Each frame of the simulation stands as an example of generative digital art, and each user interaction session becomes a unique, unrepeatable performance of “swarm art.” These outcomes demonstrate the successful fusion of technical innovation (GPU-accelerated simulation) with creative expression (dynamic visual design), highlighting the potential for interactive simulations to serve as both scientific tools and artistic experiences.
Conclusion and Future Work
This project showcased an interactive, GPU-accelerated flocking simulation that bridges technical and artistic disciplines. We achieved real-time simulation of large swarms by leveraging Unity’s compute shaders and instanced rendering, overcoming traditional performance limits of the Boids model. The resulting system allows users to explore emergent collective behavior through direct interaction, all while enjoying a captivating visual display of glowing trails and evolving color patterns. The work serves as a proof-of-concept that complex simulations need not be confined to research – they can be presented as engaging visual art and interactive experiences.
There are several avenues for future work. From a technical perspective, one improvement would be integrating more advanced physics and environmental interactions. For instance, adding a fluid dynamics coupling (e.g., using a parallel fluid simulation) could allow boids to swim in a virtual flow field, creating even richer motion (imagine simulating airborne particles riding on wind currents or fish moving in water currents). This would involve two-way coupling between boids and a fluid solver, and could produce visually stunning turbulent patterns as the swarm disturbs the fluid and vice versa. Another extension could explore machine learning or optimization: using reinforcement learning to evolve boid behaviors in real-time could yield novel collective behaviors beyond the classic rules. For example, agents could learn to form specific shapes or respond to user gestures in complex ways, expanding the creative possibilities.
On the visual side, future work might incorporate audio-visualization elements. The rhythmic pulsing of the swarm could be tied to sound – either controlling sound synthesis or responding to music input – effectively turning the system into an audiovisual performance instrument. Each boid or flock could emit tones based on their state, creating a generative soundscape in harmony with the visuals. Additionally, we plan to experiment with rendering techniques like volumetric lighting or 3D trails (e.g., using geometry shaders or VFX graph) to add depth and realism to the glow effects. This could make the trails appear as light volumes that camera can move through, enhancing immersion (especially for potential VR presentations).
Scalability and optimization remain important for future developments. While our current implementation handles tens of thousands of agents comfortably, pushing towards millions (as some recent GPU flocking research has done) would require further optimizations such as improved spatial hashing algorithms or multi-GPU distribution. Investigating Unity’s upcoming DOTS/ECS (Data-Oriented Technology Stack) in combination with compute shaders might yield performance gains by better organizing data for the GPU.
Finally, deploying this simulation in an immersive installation or on the web is a compelling direction. A VR version could allow users to walk through a swarm of virtual birds swirling around them, turning the project into a fully immersive collective behavior experience. A WebGL or WebGPU port could bring the experience to a wider audience through browsers, though careful adaptation would be needed to maintain performance with so many particles. In conclusion, this project lays a foundation for interactive swarm simulations that are both scientifically insightful and artistically inspiring. By continuing to refine the technology and explore new creative integrations, we aim to further blur the line between simulation and art – allowing audiences to play with emergent phenomena and witness the beauty of self-organization in real time.
References
C. W. Reynolds, “Flocks, Herds, and Schools: A Distributed Behavioral Model,” Proc. 14th Annual Conf. on Computer Graphics and Interactive Techniques (SIGGRAPH ’87), ACM, 1987, pp. 25–34.
C. Reynolds, “Big Fast Crowds on PS3,” Proc. ACM SIGGRAPH Symposium on Video Games (Sandbox ’06), ACM, 2006, pp. 113–121.
R. De Chiara, U. Erra, V. Scarano, and M. Tatafiore, “Massive Simulation Using GPU of a Distributed Behavioral Model of a Flock with Obstacle Avoidance,” Proc. Vision, Modeling & Visualization (VMV ’04), 2004, pp. 233–240.
