This video details a comprehensive journey into building a realistic and performant underwater fish simulation system in Unity, challenging the perceived limitations of AAA game titles. Starting from a critique of existing game environments, the creator develops a robust solution encompassing realistic fish swimming animations implemented efficiently via vertex shaders, sophisticated movement control based on acceleration and angular input, and autonomous single-fish behaviors like target following and obstacle avoidance. The core of the multi-fish simulation is built upon the Boids algorithm, incorporating separation, alignment, and cohesion. Crucially, the video explores extensive performance optimizations, progressing from CPU-based spatial partitioning to GPU-accelerated compute shaders and indirect instanced rendering, culminating in a highly optimized system capable of simulating tens of thousands of fish at high frame rates. The project concludes with a demonstration of a vibrant underwater scene teeming with diverse species and the successful recreation of complex schooling patterns like a circular vortex.
Building a Realistic and Performant Underwater Fish Simulation in Unity
0:00 Initial Disappointment: The video begins by highlighting the underwhelming underwater environments and scarce fish populations in popular AAA games like GTA V and Assassin's Creed Valhalla, contrasting them with the vibrant underwater world of indie title "Abzu."
0:42 Project Goal: The creator sets out to prove that realistic, lively, and performant fish simulations are achievable in games by building one from scratch in Unity.
1:09 Environment Setup: A basic underwater environment is quickly constructed using 3D models, terrain, grass, corals, rocks, a water surface, and post-processing effects, establishing an initial performance benchmark of 250 FPS (3.5-4 milliseconds per frame).
2:01 Realistic Swimming Motion: To avoid performance issues with skeletal animations for large numbers of fish, the creator implements fish swimming motion directly in a vertex shader. This involves combining several simple motions:
3:42 Yaw Rotation: Rotation around the fish's y-axis.
4:25 Roll Rotation along the Spine: Offset based on vertex distance from the pivot, adding an "organic feel."
4:55 Second Yaw Rotation: Another y-rotation offset similarly to the roll, creating a fluid, flag-like movement when combined with others.
5:29 Controlling Swim Intensity: The swimming motion's intensity is dynamically controlled. Initially based on velocity, it's refined to be guided by the fish's acceleration (6:30) for more realistic responsiveness.
6:46 Implementing Steering and Angular Movement: Steering is added, with angular acceleration also influencing the movement acceleration and swimming motion intensity (7:39), resulting in convincing turns.
8:29 Autonomous Single Fish Movement: A system for autonomous fish movement is developed, allowing fish to follow targets and avoid obstacles by accelerating and turning away from them.
10:11 Multi-Fish Schooling (Boids Algorithm): The simulation expands to multiple fish using the Boids algorithm, based on three core behaviors:
10:54 Separation: Avoiding collisions with other fish.
11:04 Alignment: Matching the movement direction of nearby fish.
11:11 Cohesion: Moving towards the center of the school.
Customizable weights are added for different "fish personalities" (e.g., introvert fish with higher separation).
13:27 Improved Initial Spawning: Fish are spawned uniformly within a sphere to ensure immediate cohesive schooling.
14:08 Multi-Species Simulation: Support for different fish species (Golden Trevally, Clownfish, Yellowtail Snapper) is added, with cohesion and alignment being species-specific, and separation accounting for larger fish avoiding smaller ones.
14:42 Performance Bottleneck Identification: Increasing the fish count to 600 significantly drops performance to 25 FPS (40ms/frame) due to O(NΒ²) calculations as each fish checks every other fish.
15:49 Optimization 1: Spatial Partitioning (CPU): The simulation area is divided into cells, so each fish only checks its neighbors within its cell and adjacent cells. This improves performance to 50 FPS (20ms/frame), a 50% speedup, but is still insufficient for large numbers.
18:16 Optimization 2: GPU Transfer (Compute Shaders): The entire simulation logic is transferred to the GPU using compute shaders for parallel execution. This boosts performance for 600 fish to 90 FPS (11ms/frame), but struggles with 3,000 fish due to the bottleneck of fetching results back to the CPU.
19:38 Optimization 3: Indirect Instanced Rendering: The GPU is instructed to render fish directly from the GPU-side data without transferring it back to the CPU. After fixing a small bug (21:06), this dramatically improves performance, allowing 3,000 fish to run at around 200 FPS (5ms/frame), and even 6,000 fish without performance impact.
22:46 Optimization 4: Spatial Partitioning (GPU): The spatial partitioning logic is implemented on the GPU, allowing 30,000 fish to run at 120 FPS (8ms/frame), a 60% speedup over the previous GPU-only approach.
24:08 Final Scene and Performance: Sharks are added as obstacles. The final scene, with tens of thousands of fish and complex behaviors, runs at an impressive 200 FPS (5ms/frame), proving that high-quality, dense underwater scenes are indeed achievable in games.
24:54 Bonus Experiment: Circular Schooling: The creator successfully recreates the mesmerizing circular fish formation seen in Abzu by using a central target and a matching spherical obstacle, initiating fish on the sphere's surface for immediate formation.
26:42 Conclusion: The video concludes by demonstrating that realistic and joyful underwater scenes full of life can be achieved with thoughtful optimization, suggesting that AAA studios might be "slacking."
Error: value error