Simulating Hair and Fabrics with Unity Cloth on Mobile





Many designers sooner or later face the challenge of achieving realistic tissue or hair behavior. There are a number of technologies for this, but not all of them are suitable for mobile platforms. Usually mobile developers bake animations because it is the least expensive. We're going to talk about real-time simulation - the Unity Cloth module .







Unity Cloth is a tissue physics simulation based system that works in conjunction with SkinnedMeshRenderer. It allows you to control a wide range of parameters, such as tensile or torsional stiffness, damping (the amount that is responsible for how quickly the fabric will come to equilibrium), wind simulation, interaction of internal collisions (self-collision), friction, gravity, influence, etc. interaction of colliders, etc.



One of the teams from the Krasnodar studio Plarium studied the applicability of Unity Cloth for mobile devices.



Part 1. Tissue simulation



We started with stress tests to assess the acceptable limits for using the module settings on mobile devices. The first sample to be examined was a high-poly multi-layer mesh - a cape with ~ 10k vertices, ~ 7k triangles. 19 capsule colliders were added to the object in the shape of the character's body parts. The rest of the settings are default.



The graph below shows the latency of the metrics being examined in milliseconds depending on the number of object instances. The tested device is Xiaomi Mi5 (4 cores, 2.15 GHz, 3 GB of RAM).







Xiaomi Mi5 is not the weakest device, but serious performance losses are already visible on it. On less powerful devices (Lenovo A536, Quad-core, 1.3 GHz, 1 GB RAM), they become disastrous. Next, we will evaluate the results that the weakest device demonstrates.







One update of tissue physics alone accounts for 775 ms out of 800. Let's try to exclude the influence of colliders.







Collider collision calculations greatly reduce performance; with 25 instances, a maximum of 475 colliders were present on the scene.



The next measurement is relative to a low-poly single-layer model (153 vertices, 256 triangles) with fewer instances (up to 9) and fewer colliders (up to 8).











Let's see what impact collider calculations have. Let's build the corresponding dependencies using the example of one instance of an object on different devices (in addition to the devices already involved, we take Asus ZenFone 5, Dual-core, 2 GHz, 1 GB of RAM).







The resulting functions are well approximated by a polynomial function of degree 2.



It can be seen that the situation with low poly objects is much better. On the most ancient devices, one instance of an object takes about 1 ms on average (versus ~ 3 ms for a high poly model, all other things being equal), which is pretty good.



Next, let's see how additional parameters affect performance .



In the tests, a single-layer mesh was used with a slightly greater detail than in the previous dimension (~ 450 vertices, ~ 800 triangles). 1 capsule collider was added to the Unity Cloth component, 1 instance of the object was examined, additional parameters of internal collisions and wind effects were varied .











Additional wind simulation, as well as internal collision calculations, degrade performance.



This is especially noticeable on more complex models. Let us analyze the effect of additional parameters of stiffness (tension, torsion) and attenuation on meshes with ~ 10,000 vertices and ~ 500 with 1 capsule collider.







Below are the results obtained on the weakest device (Lenovo A536) with varying parameters in each of the measurements.



1. Stiffness: Stretching and twisting - off.

Low-poly: 1.67ms High-poly: 145.32 ms.



2. Stiffness: Elongation - incl. / Curl - off.

Low-poly: 1.79ms High-poly: 160.44ms.



3. Stiffness: Stretch - off. / Twisting - incl.

Low-poly: 1.90ms. High-poly: 172.19 ms.



4. Stiffness: Stretching and twisting - incl.

Low-poly: 1.92ms High-poly: 194.50 ms.



5. Stiffness: Elongation - incl. / Curl - off. Wind influence - incl.

Low-poly: 1.69 ms. High-poly: 165.73ms.



6. Stiffness: Elongation - incl. / Curl - off. Attenuation incl.

Low-poly: 1.69 ms. High-poly: 172.75ms



7. Stiffness: Elongation - incl. / Curl - off. Wind influence - incl. Attenuation - incl.

Low-poly: 1.76ms. High-poly: 173.10ms



8. Stiffness: Elongation - incl. / Curl - off. Internal collisions - incl.

Low-poly: 2.01ms High-poly: Unity Crash



The parameters of simulation of wind, fade, compression, twisting and internal interaction add an additional load, significantly degrading performance on weak devices when using high-poly meshes. Meshes with fewer vertices have less latency.



  • Wind simulation adds ~ 9% of the total metric latency.
  • Fading simulation adds ~ 12% of the total metric delay.
  • The shrink / twist simulation adds ~ 13-15% of the total metric latency.
  • ~ 12–16% ( . : ).


2.





Model from cgtrader.com



Let's talk a little about the capabilities of the Unity Cloth component in working with hair. At first glance, this should be no more difficult than with single-layer fabrics - first fix the desired vertices, and then distribute the degree of simulation influence to the remaining vertices. But there is a problem - kinks of volumetric objects such as curls. To see more clearly the reason for their occurrence, take the face-centered cubic crystal lattice of some metal, for example sodium chloride, as an illustration.







The kinks are explained by the fact that the nearby peaks go beyond the distant ones (shown by the arrow). Kinks and twists could not be avoided even using calculations of interactions of internal collisions. We conclude that Unity Cloth is not suitable for solid bodies.







Simulation settings:



  • Params 1: 3 colliders, stiffness on for tension and twisting, wind and internal collisions off, damping off;
  • Params 2: 3 colliders, tensile stiffness enabled, wind and internal collisions disabled, attenuation disabled;
  • Params 3: 1 collider, tensile stiffness enabled, wind and internal collisions disabled, attenuation disabled.


The performance indicators of the two weak devices are comparable to each other, Xiaomi gives the best results.



On busy scenes, even relatively small delays become significant, and in mobile development these numbers multiply, so you have to fight for every tenth of a millisecond.



Summary of the Unity Cloth Component



  • It copes well with simple single-layer objects (flags, capes, bedspreads and tablecloths).
  • .
  • .
  • . , : Graphics Settings .
  • - Unity ( Bug Story).




The simulation of simple single-layer fabrics created with the Unity Cloth module is quite realistic. However, it should be used with caution: interacting with a large number of colliders, as well as excessive computation of wind simulation, damping, compression, twisting, internal interaction, lead to performance degradation. In general, Unity Cloth is quite resource-intensive for use on mobile devices, especially when simulating fabrics and hair on high-poly meshes.



If you do intend to use Unity Cloth on mobile platforms, then pay attention to the following recommendations:

  • Fewer vertices mean better performance. Use meshes with the minimum number of vertices you can handle.
  • , .
  • , , , , .
  • , , .


Bug Story
, - . Unity Support.



1. Unity 2018.4.4f1 Cloth Bounds SkinnedMeshRenderer, , .







8 9 . Xiaomi Mi 5.







2018.4.8f1.



2. 2018.4.8 Constraints , - /. , :

  • Constraints;
  • -, ;
  • Play Mode ;
  • Cloth.






2018.4.14f.



All Articles