Meta Spark Cloth Simulation Tools

The Meta Spark Cloth Simulation Tools offer a solution for simulating cloth physics through procedural real-time bone transformations. These open-source tools are designed for intermediate to advanced users who are familiar with Meta Spark and 3D modeling.

Getting Started with Cloth Simulation

To get started with cloth simulation in Meta Spark, you'll need a rigged 3D object that adheres to best practices. The Blender Addon can help you simplify the rigging process. For a complete workflow from Blender to Meta Spark, check out this workflow video.

Download assets

Tips for Effective Use

  • Preparation: Ensure your 3D object is correctly rigged with a skeleton that has at least one bone, and complies with Meta Spark's size limitations. Rectangular or square rigs work best for the cloth simulation.
  • Blender Addon: Utilize the Blender Addon to simplify the rigging and preparation process. Properly rigged objects lead to better simulation results. Learn how to use the Blender Cloth Simulation Tools Addon here.
  • Pinning: If you want to pin a specific bone in Meta Spark Studio, simply edit it's name to include the word 'PIN', for example: "Bone_0x12_2_3_PIN". This means it will no longer be affected by the simulation, and will instead be used as an anchor point for other bones.

How to Use the Cloth Simulation Tools

Using the Template

  1. Download the Template: Download the template file here.
  2. Open in Meta Spark Studio: This template contains a pre-rigged object that is ready for cloth simulation.
  3. Modify or Replace Object: You can modify the pre-rigged object or replace it with your own. To make changes to the 3D asset simply open the Block file (double click on Block in the Assets panel) and make changes to it. If you choose to do so, ensure your object is properly rigged and follows the best practices for optimal simulation results.
  4. Adjust Simulation Parameters: Adjust the bone search radius (distance in local coordinates) to get neighboring bones within the radius, and customize the simulation parameters such as gravity, elasticity, and quality to suit your project's needs.
  5. Test the Simulation: Play the project within Meta Spark Studio or push to the Facebook/ Instagram cameras to see how the cloth behaves and make necessary adjustments.

Using Blocks

  1. Download the Block Asset: Get the Block Asset files here.
  2. Import into Meta Spark Studio: Open Meta Spark Studio and import the Block asset into your project.
  3. Modify or Replace Object: You can modify the pre-rigged object or replace it with your own. If you choose to replace it, ensure your object is properly rigged and follows the best practices for optimal simulation results.
  4. Configure Block Parameters: Adjust the bone search radius (distance in local coordinates) to get neighboring bones within the radius, and adjust the block parameters to define the behavior of the cloth simulation, like: gravity, elasticity, and simulation Quality.
  5. Integrate with Your Project: Integrate the Block asset into your existing project or a new scene, ensuring it interacts correctly with other elements and is placed properly.
  6. Run and Test: Play the project to observe the cloth dynamics and fine-tune the settings as needed.

Using Scripts

  1. Download the Scripts: Obtain the math.ts and ClothEngine.ts scripts from downloads section.
  2. Import into Meta Spark Studio: Import both scripts into your new or existing Meta Spark Studio project.
  3. Import the code: Create a new file where you'll import the downloaded scripts and handle the logic of your experience. Enable all required capabilities: Scripting Writable Signal Sources, GlobalTransforms.
  4. Initialize ClothEngine: In your script, create a new ClothEngine using one of the following methods:
    • Use the ClothEngine.createFromScene method to set up the cloth simulation from a SceneObject. This method will attempt to autodiscover the skeleton structure and resolve it following simple rules.
    • Use ClothEngine.fromLooseJSON to use arrays of bone names to define the skeleton structure (only one pinned object per row/column) is supported.
    • Use ClothEngine.fromJSON to feed a strictly defined object that superseeds the automatic discovery. This is an advanced method which assumes some knowledge of the internals of the code and the simulation.

To see examples of the fromLooseJSON and fromJSON input see the output for the template object.

  1. Configure Simulation Settings:
    • Define gravity, elasticity (horizontal and vertical), and simulation quality within your script.
    • Adjust these settings to achieve the desired simulation behavior.
  2. Run the Simulation:
    • Execute your script to start the cloth simulation.
    • The simulation will continuously update the positions and rotations of the bones, creating realistic cloth movement.
  3. Testing and Optimization:
    • Test the simulation in various scenarios to ensure it meets your expectations.
    • Optimize performance by tweaking your 3D model, or the simulation settings.

CAUTION

You may directly modify the ClothEngine.ts or math.ts files to achieve custom behavior, optimizations, or extend the simulation beyond its intended scope. However, be cautious when making changes to these core scripts, doing so implies a deep understanding of programming in Meta Spark and the underlying physics.

Best Practices

  • Rig Structure: Objects with a regular, grid-like structure of bones yield the best simulation results. Avoid using irregular, cyclic, or amorphous structures as they may not work as expected.

  • Positioning: The shape of your object's rig will be evaluated once (in world space) at the start of the simulation. Ensure your object is the correct way up when this happens.

  • Rotations: It is assumed that the two inmediate parent's of the object's bones will not rotate locally throughout the simulation lifecycle and will remain in a neutral (identity) rotation state. This means that in most cases you may rotate a null object or block containing the simulation object, but not the object itself. Doing so may lead to the incorrect calculation of bone rotations. It's recommeneded to use the Block asset and manipulate the Block asset instead of the target object.

NOTE

The rigging of your 3D object is crucial. Ensure that the object has a well-constructed skeleton with appropriately placed bones. This skeleton serves as the foundation for the cloth simulation.

Performance Optimization

  • Bone Count: The number of bones in your rig directly affects the performance of the simulation. More bones provide higher accuracy but require more computational power. For better performance on older devices, consider using a lower resolution rig.
  • Simulation Quality: Adjust the simulation quality settings to balance between performance and visual fidelity. Higher quality settings improve realism but can reduce performance on less powerful devices.
    • LOW: The simulation runs every other frame. This reduces the resource consumtion but decreases the smoothness. Only vertical resolution is performed.
    • MEDIUM: Runs the simulation every frame, but only vertical resolution is performed.
    • HIGH: Runs every frame and computes both vertical and horizontal constraints are calculated.

Detailed Theory and Context

Fundamental Assumptions

The important thing to remember when simulating real life situations, is that everything will be an approximation. We start by simulating and constructing the most basic building blocks and then work with them, but it will always be approximations and assumptions all the way down. In this project, the fundamental assumption is that "a piece of cloth can be approximated by a grid of bones, which all preserve their original spacing at all points throughout the simulation".

Cloth Simulation. Ilustrated as an animated GIF showing how distances between points on a grid are preserved

Theory put into Practice

The project introduces several classes to manage constraints on objects:

  • Link: This class keeps two objects a fixed distance apart.
  • Chain: A series of linked objects that simulate a flexible strand.
  • PinnableChain: A chain with some objects "pinned" to fixed positions, acting as anchor points of the chain.

By applying these constraints to the bones of a rigged 3D object, the physics of cloth can be simulated effectively.

NOTE

Strand Simulation: The simulation updates bones in vertical and horizontal strands individually. When all strands are simulated together, it creates the appearance of cohesive cloth movement.

WARNING

Bone Resolution: Cloth objects are simulated with a finite number of bones, balancing accuracy and performance. Increasing the number of bones can improve accuracy but may impact performance.

Understanding the Simulation Lifecycle

An object in the simulation undergoes several steps:

  1. Skeleton Resolution: Bones are grouped into vertical and horizontal strands. This step forms the foundation of the simulation by identifying the key components that will move/ stay pinned.
  2. Pinning: The top bone in each vertical strand is pinned and remains static, serving as an anchor for other bones. These pinned bones provide stable reference points for the rest of the simulation.
  3. Solving: The positions of non-pinned bones are calculated, considering gravity and distance constraints. This step ensures that the bones move in a realistic manner, taking into account physical forces.
  4. Rotation Solving: Rotations are adjusted to ensure correct shading and lighting. Proper rotation solving ensures that the cloth appears natural under different lighting conditions.
  5. Repetition: These steps are repeated 30 times per second (once per frame) for a smooth simulation. This high frequency of updates provides a fluid and realistic simulation experience.

Takeaways

By understanding and applying these principles, you can create cloth simulations in Meta Spark Studio. These tools open up new possibilities for developers looking to enhance their AR projects with lifelike cloth dynamics.

The Meta Spark Cloth Simulation Tools empower developers to create stunningly realistic cloth effects, enhancing the immersive experience of AR applications. Whether for fashion, gaming, or interactive media, this tool offers the precision and flexibility needed to bring cloth simulations to life.

Here are some ideas for applying cloth simulation in your projects:

  • Virtual Try-Ons: Create realistic cloth simulations for virtual fashion try-ons in augmented reality.
  • Interactive Storytelling: Enhance interactive AR experiences by adding dynamic cloth elements that respond to user interactions.
  • Game Development: Use the cloth simulation for realistic character clothing in AR games and applications.

Learn More About Meta Spark

Meta Spark is a powerful platform for creating augmented reality experiences. To fully leverage the cloth simulation tools within Meta Spark, consider exploring these resources:


Image of Tomas

Written, developed, and hosted with ❤️ by Tomas Pietravallo. 2024. Tomas is a Computer Science student with a passion for simulations and teaching. More about Tomas