This project simulates a flag with cloth physics and allows you to experiment with cloth animation using Unity. The simulation integrates Unity's physics engine and scripts to provide realistic cloth dynamics. The environment is set up in Unity, and Python may be optionally used for advanced functionality.
https://github.com/sawang19/ClothAnimation
- Unity Editor Version:
2022.3.48f1
It is recommended to use this exact version of Unity for compatibility. You can install this version through Unity Hub.
-
Download the Project
Download and extract the project folder (e.g.,ClothAnimation.zip
). Extract the contents to your preferred location. -
Open the Project in Unity
- Open Unity Hub.
- Click
Add
and navigate to the extracted project folder. - Select the project and ensure you open it using Unity Editor version
2022.3.48f1
.
-
First-Time Project Loading
The project may take some time to load when opened for the first time as Unity compiles assets and dependencies. Please be patient during this step. -
Open the Demo Scene
Navigate to the scene in the project folder: ClothAnimation\Assets\Flag With Cloth Physics\Scene Open theDemo
scene to begin working with the cloth animation simulation.
All custom scripts for this project are located in: ClothAnimation\Assets\Flag With Cloth Physics\Script
Feel free to explore and modify these scripts to customize the behavior of the cloth physics. Key scripts include:
- Purpose: Controls the movement and animations of the character.
- Key Features:
- Movement is controlled with the
WASD
keys. - Smooth rotation of the character towards the movement direction.
- Implements acceleration and deceleration for realistic movement.
- Updates the Animator to reflect movement states (e.g., idle, walking).
- Movement is controlled with the
- Purpose: Manages the core cloth simulation using particles and springs.
- Key Features:
- Implements spring constraints between particles for structural integrity.
- Simulates cloth behavior under gravity, wind forces, and collisions.
- Supports interaction with sphere and capsule colliders for collision resolution.
- Handles self-collision detection to prevent unrealistic overlaps.
- Provides UI controls for toggling wind effects and adjusting wind strength.
- Purpose: Subdivides a mesh to increase its resolution for better simulation precision.
- Key Features:
- Adds intermediate vertices to mesh triangles.
- Refines UV mappings to maintain texture consistency after subdivision.
- Ensures smoother and more detailed cloth behavior.
- Purpose: Implements an octree for efficient spatial queries in 3D space.
- Key Features:
- Subdivides space dynamically into smaller regions (nodes).
- Optimizes self-collision detection by querying only nearby particles.
- Balances particle density and depth of the tree for performance.
- Purpose: Provides a spatial hashing system for efficient collision detection.
- Key Features:
- Divides space into uniform grid cells for storing particle positions.
- Enables fast querying of neighboring particles within a given radius.
- Uses a 3D integer coordinate system for cell identification.
- Purpose: Simulates spring constraints between particles in the cloth.
- Key Features:
- Maintains a fixed rest length between two particles connected by a spring.
- Applies forces to ensure particles remain within the spring's constraints.
- Includes damping to minimize oscillations and improve stability.
- Purpose: Represents an individual particle in the cloth simulation.
- Key Features:
- Tracks position, velocity, and acceleration of the particle.
- Uses Verlet integration for stable physics calculations.
- Applies external forces like gravity, wind, and damping.
- Supports pinning specific particles to act as fixed anchors.
- ClothSimulation.cs: Orchestrates the entire cloth simulation, using Particle.cs and Spring.cs to simulate the cloth's physical behavior.
- MeshRefinementUtility.cs: Prepares a high-resolution mesh for improved simulation.
- Octree.cs and SpatialHash.cs: Optimize collision detection by limiting the scope of checks to relevant particles.
- CharacterMovement.cs: Adds interactive character movement and animations, allowing the user to interact with the simulated cloth.
- Attach
ClothSimulation.cs
to the cloth GameObject in Unity. - Use
CharacterMovement.cs
for controlling a player character in the scene. - Refine your mesh using
MeshRefinementUtility.cs
for higher precision. - Tweak parameters like wind strength, gravity, or stiffness in the Unity Inspector to customize the simulation.
-
Set Up the Scene
Ensure theDemo
scene is open in Unity. You should see the flag set up with physics applied. -
Play the Simulation
Click thePlay
button in the Unity Editor to start the simulation. You can observe the cloth physics in action. -
Modify the Physics
Use the inspector to tweak parameters like wind strength, stiffness, or gravity to see how they affect the flag's behavior.
- Ensure you are using Unity version
2022.3.48f1
for optimal compatibility. - All scripts are customizable. Experiment with parameters to better understand how cloth physics is simulated.
- Refer to Unity's official documentation for further details on physics and scripting.
This project is distributed for educational purposes. Feel free to modify and expand upon the code for your assignments or personal projects.