# Cloth Physics + Morph Targets

*Why use morph targets on cloth assets?*

The traditional way to handle something like "shape-changing cloth" on a skinned mesh is to use bone manipulations via translating or scaling bones. This is a straightforward method since it requires just a few nodes, and if your character creator already uses this method to change character proportions, this guide may be unnecessary.&#x20;

However, in many cases it's desirable to use **morph targets** to customize character shapes, rather than relying on bone changes alone. This is because morph targets allow for precise adjustment on a per-vertex basis, which can be tricky and cumbersome to accomplish with bone manipulations.

This method allows you to use cloth simulation and enables the use of morph targets on the simulating cloth. It's important to understand that morph targets are handled by the GPU, and collisions are handled by the CPU. Therefore, in order to maintain accurate collisions when a morph target is applied, a combination of morph targets **and** bone manipulations must be used. This is explained on the [Collisions](https://dilly.gitbook.io/dilly/cloth-physics-+-morph-targets/collisions) page.

*Solution*

Using [deformer graph](https://dilly.gitbook.io/dilly/deformer-graph) we can enable morph targets on simulating cloth assets. The idea is to read the morph delta data from a skinned mesh with morph targets, and apply the offset to a simulating cloth asset. Naturally this will require two components: A component to read from (the morphed mesh), and a component to write to (the simulating mesh).&#x20;

This guide uses cloth assets from the experimental [Panel Cloth Editor](https://dev.epicgames.com/community/learning/tutorials/pv7x/unreal-engine-panel-cloth-editor) plugin. It will also work with the 'legacy' [cloth paint tool](https://dev.epicgames.com/documentation/en-us/unreal-engine/clothing-tool-in-unreal-engine). I prefer the new cloth assets as they allow for tighter control over simulation params as well as the option to add kinematic colliders for more accurate collisions.

After you enable the chaos cloth asset and editor plugins, the first thing you need is a skinned mesh with morph targets:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2Fb8C6aexphLzmBJZpAS1F%2FCapture.PNG?alt=media&#x26;token=e2fbe9d4-1404-4ed5-a95d-d8a7cfa2180b" alt=""><figcaption></figcaption></figure>

Next you'll need to create a Cloth Asset for use with the panel cloth plugin. If you're not familiar with the new panel cloth workflow, read the Epic page.

Add a SkeletalMeshImport node and select your mesh:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2Fm0953bAw0r2ycIdpZLe5%2FCapture.PNG?alt=media&#x26;token=2294dd0f-6455-4508-8c8c-0b0e611dccdc" alt=""><figcaption></figcaption></figure>

Then add the following nodes and paint the simulation area:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2FQqHmOg1MhQL688RIVWkL%2FCapture.PNG?alt=media&#x26;token=cc27c0e0-f110-49d9-9b60-1ad2ed3e792e" alt=""><figcaption></figcaption></figure>

After that you can add whichever simulation config nodes you want to use. Refer to Epic documentation or the default cloth asset as an example on how these can be configured. Ensure your data flow graph contains a "Set Physics Asset" node so we get collisions from the character's physics asset (create and set one up with colliders if you don't have one already).

Once you have the cloth simulating in the preview window you can proceed.

Now that we have a skinned mesh with morphs and a simulating cloth asset, we can apply the deformer graph. For this to work we need a skeletal mesh component and cloth asset component added to the character blueprint:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2F1HnDPItisHnAx7Lu3QWE%2FCapture.PNG?alt=media&#x26;token=7381f1a2-a4e0-4db3-80f1-b534f3783606" alt=""><figcaption></figcaption></figure>

Ensure the SkeletalMesh component has its leader pose set to Mesh, which is typically done in the construction script, or by adding it dynamically at runtime when an item is equipped (I prefer this method, in C++).&#x20;

We don't want the skeletal mesh component visible, since it's only there to read morph data from. However if you disable its visibility, it won't render in the scene, and therefore the deformer graph can't read from it. The best solution is to apply a masked material with an opacity value of 0:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2FDLcdxNP7fnam9m8UUDpz%2FCapture.PNG?alt=media&#x26;token=501f6e0d-8470-4475-a482-9fde461a8cd1" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2FvbfwVdt6dXoSmpL2Svfw%2FCapture.PNG?alt=media&#x26;token=55fe9b5b-0a6f-4f1f-8aa8-699d5442d55b" alt=""><figcaption><p>If your clothing item has multiple material slots, apply the transparent masked material to all of them</p></figcaption></figure>

Next you need to assign the deformer graph to the cloth component. You can download my ClothMorphSimple graph here:

{% embed url="<https://mega.nz/file/tgxBGarT#d0J-U3g4cnhBhU4eCNmiHqS6BEwV5F6RP702myJ6UDY>" %}
Note the graph is for UE 5.5+, versions prior to that will need to download 5.5 and backport the graph by creating a new one in the previous version and copying nodes.
{% endembed %}

Once you import the graph, apply it here:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2FqeB880nQxKe27KPDHamW%2FCapture.PNG?alt=media&#x26;token=0adec7e3-24d0-4cf1-a688-3981203609e1" alt=""><figcaption></figcaption></figure>

Open the graph and take note of the **component binding tag:**

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2F86ImbU9Pat5JbkvSZyC3%2FCapture.PNG?alt=media&#x26;token=d3291a7a-41e2-4f2b-a1c9-aa6657915648" alt=""><figcaption></figcaption></figure>

This tag is how the deformer graph knows which component to read from. Naturally, we need to assign this tag to our skeletal mesh component, since that's what we're reading the morph data from:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2FKuW3zo9K2M7yzAWZv794%2FCapture.PNG?alt=media&#x26;token=bffe48a2-0267-4f26-9c8b-8dcf5e2b6630" alt=""><figcaption></figcaption></figure>

Next, in the deformer graph, there is a node "ColorInfluence." It reads a vertex color channel from the skeletal mesh, and that's how it knows where to apply the cloth/morph blend.

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2FYUPHHvb5VNrVFpNGdKUS%2FCapture.PNG?alt=media&#x26;token=5bb74ebc-7a45-437e-9c74-7873106c6c72" alt=""><figcaption><p>The kernel reads from the red vertex color channel, specified by .r. Other acceptable values are .g, .b, and .w for alpha channel</p></figcaption></figure>

The graph is reading from the red vertex color channel. Therefore we need to apply red paint to the skeletal mesh's vertex color attributes.&#x20;

Add a new vertex color attribute on your mesh and paint it red. The gradient should roughly match the cloth weight you painted in either the cloth asset editor or the legacy cloth paint tool:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2Fw1fPEKx7hlA1F4CBjyYB%2FCapture.PNG?alt=media&#x26;token=8a05859d-27c8-4c13-9770-bc01e63ae4e5" alt=""><figcaption><p>Here the vertex color is added to the red channel in Blender using the <a href="https://github.com/andyp123/blender_vertex_color_master">Vertex Color Master</a> addon linear gradient tool. You can also add vertex paint using the UE editor's mesh paint tools, however as of 5.5, no in-editor gradient tool is available for vertex painting. I strongly recommend your vertex paint consist of a smooth gradient, otherwise the end result may have noticeably hard edges.</p></figcaption></figure>

After you've added the vertex paint, re-import your mesh. Confirm the paint was imported correctly by enabling Show > Advanced > Vertex Colors in the skeletal mesh asset viewer:

<figure><img src="https://3558733374-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPratlX5Y75g7mK9upu8G%2Fuploads%2FSDYVBrlhTNZ5OaaKbdA1%2FUntitled.png?alt=media&#x26;token=6b7b62c0-7ed1-425f-9984-a373c3552613" alt=""><figcaption></figcaption></figure>

Now that the vertex color has been added, you're done! Simply add the mesh to the skeletal mesh component in BP\_Character, ensure your cloth asset is selected on the chaos cloth component, and you're good to go. Apply a morph and watch the simulating cloth update to match.
