Skip to content

Crispigt/RealTime-Buoyancy-Unity

Repository files navigation

RealTime-Buoyancy-Unity

Real-Time Surface-Pressure Buoyancy with Adaptive Curved Clipping A Unity + C++ implementation of Hirae et al. (2025)'s closed-form hydrostatic-pressure integrator, with a custom Adaptive Curved Clipping refinement for coarse meshes on Gerstner waves.

Final project for KTH DH2323 Computer Graphics and Interaction, by Felix Stenberg.

MultipleObjectsWave.mp4

Report available here: https://github.com/Crispigt/RealTime-Buoyancy-Unity/blob/main/Report-Buoyancy.pdf


Highlights

  • Closed-form per-triangle buoyancy, exact force and torque, no quadrature error, even on coarse meshes (Hirae et al. 2025, Eqs. 5–9).
  • Wave-agnostic C++ DLL geometry-only; the wave model lives in C# so the plugin is reusable across wave systems.
  • Adaptive Curved Clipping refines per-vertex water heights along the clipping chord without geometric surface intersection. This was the project's main research contribution.
  • Burst-compiled wave sampler IJobParallelFor + Unity.Mathematics, ~6–10× speedup over plain C#.
  • Garland–Heckbert runtime mesh simplification for buoyancy meshes (UnityMeshSimplifier).
  • Bug found in the source paper. A typographical error in Hirae et al.'s printed torque equations (Eqs. 7–9) was identified, re-derived from scratch, and corrected. The derivation notes and report draft live locally under the intentionally ignored resources/ folder. Authors have been contacted.

Gallery

Cube + Bunny + dragon side-by-side

BunnyDragonCube.mp4

Cube bobbing, damping in action

cubebobbing.mp4

Angular damping coefficient sweep

DampningDifferent.mp4

Bug demos (the project's two most instructive failures)

Sub-triangle winding-order bug Paper Eq. 7–9 typo
FirstBug-wrongorder.mp4
PapperEquation.mp4

Linear vs Adaptive clipping (debug gizmos)

Linear clipping Adaptive curved clipping
Linear clipping gizmo: green = dry, blue = submerged, orange = intersecting, white = straight chord. Adaptive clipping gizmo: magenta line + yellow dots = chord refinement samples; green vertical lines = depth correction at each sample.

Mesh simplification (Garland–Heckbert, https://github.com/whinarn/unitymeshsimplifier)

30,000-triangle ground truth Roughly 7k-triangle buoyancy mesh
Full bunny mesh used as ground truth in convergence tests. Decimated bunny used for runtime buoyancy.

Test environments

Force Sweep Adaptive Clipping Comparison
Force sweep test running in Unity. Adaptive clipping comparison scene, captured late in the run after the objects had drifted from their initial spacing.
Equilibrium Tilt (Corrected) Equilibrium Tilt (Paper formula with typo)
Corrected formula settling at 26.565°. Printed formula settling at 45°.

Concept diagrams, Per-triangle force/torque

Force and torque from hydrostatic pressure acting through the center of pressure.

BuoyancyController inspector

Unity inspector showing density, angular momentum damping factor, adaptive toggle, and mesh simplification target.


More videos

All public clips live under report-assets/. Click to download/play:


Results at a glance

Metric Value
Cube force-sweep error vs analytical $F_b = \rho g V$ < 10⁻⁶ % (FP noise)
Equilibrium tilt (corrected formula) vs Igarashi & Nakamura analytical 26.565° 26.565°, residual torque 2.4×10⁻⁵ N·m
Equilibrium tilt (paper formula, with typo) 45.000°, residual torque 215.9 N·m
Linear path tilt error (~7k tris) vs 30k ground truth (rough waves) ~6° (mesh/dynamics dominated)
Adaptive N=4 tilt error vs 30k ground truth ~5.5° (Adaptive vs Linear gap < 0.5°)
Adaptive N=4 vs N=16 difference 0° (fully converged at N=4)
100-sphere stress test, Linear C++ DLL 363 FPS average
100-sphere stress test, Adaptive N=1 (managed C#) 53 FPS avg, degrades to 3 FPS at equilibrium

Plots live in report-assets/plots/ and are regenerated with python resources/plot_tests.py and python resources/plot_performance.py.

Accuracy validation Convergence study Stress test
Cube submersion force sweep, computed vs analytical, machine precision. Convergence: Linear, Adaptive N=4, Adaptive N=16 vs 30k ground truth (rough waves). 100-sphere FPS over time: Linear C++ vs Adaptive C# paths.
Equilibrium tilt, corrected formula vs paper formula
Corrected formula converges to 26.565°; paper formula sticks at 45°. Residual torque decays 6 orders of magnitude with the fix.

Building

Native plugin (C++ DLL)

The plugin uses GLM as a git submodule. Initialize submodules first:

git submodule update --init --recursive

Then configure and build with CMake (Windows / Visual Studio shown; any CMake-supported toolchain works):

cd NativePlugin
cmake -S . -B build
cmake --build build --config Release

The resulting DLL is consumed by Unity via [DllImport] in the buoyancy controllers.

Unity project

Open BaseProject/ in Unity 6.x (URP). Ensure the Read/Write property is enabled in import settings for any mesh used for buoyancy. Press Play. Test scenes contain prefabs for the cube/bunny/dragon/sphere stress scenarios used in the report.

Documentation

Blog can be found at: https://crispigt.com/dh2323


References

  • Hirae, H., Morishima, S., & Ando, R. (2025). An Analytical Integrator for Solid-Fluid Coupled Buoyancy Forces. SIGGRAPH Asia 2025 Technical Communications.
  • Hori, T. (2021). Proof that the Center of Buoyancy is Equal to the Center of Pressure by means of the Surface Integral of Hydrostatic Pressure Acting on the Inclined Ship. Nagasaki Institute of Applied Science.
  • Fábián, G. (2025). Approximate and exact buoyancy calculation for real-time floating simulation of meshes. Eurographics 2025 Short Paper.
  • Flick, J. (2024). Waves, Catlike Coding. https://catlikecoding.com/unity/tutorials/flow/waves/ (MIT)
  • Garland, M. & Heckbert, P. (1997). Surface simplification using quadric error metrics. SIGGRAPH '97.
  • Igarashi, T. & Nakamura, H. (2007). The equilibrium angles of floating cubes.
  • Hwang, W. & Salvendy, G. (2010). Number of people required for usability evaluation: the 10 ± 2 rule. CACM 53(5), 130–133.

Acknowledgments

Christopher Peters and the DH2323 course team at KTH. Jasper Flick (Catlike Coding) for the MIT-licensed Gerstner wave implementation. Hirae et al. for the integrator that this work builds on.

License

Code in this repository is released under the MIT License; see LICENSE.

Original documentation, screenshots, videos, and report media by Felix Stenberg are licensed under CC BY 4.0 unless noted otherwise.

Third-party assets and dependencies (Catlike Coding wave shader, GLM, UnityMeshSimplifier, Stanford bunny/dragon meshes) retain their respective licenses.

About

Unity + C++ implementation of Hirae et al.'s closed-form hydrostatic buoyancy integrator, extended with a novel Adaptive Curved Clipping technique for Gerstner waves.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors