CPU Skinning is a technical term that is extremely important in rendering and optimizing game graphics (technical art). A compatibility-oriented method that safely pre-calculates mesh deformation due to character bone animation on the CPU's main thread or multi-thread (Job System).
Basic concept of skinning processing and difference in role of CPU/GPU
When a character moves in a game, internal calculations are performed every frame to pull and transform the positions of the ``vertices of the mesh,'' which is the outer shell of the 3D model, according to the animation data of the ``bones.'' This process of deforming the skin mesh according to bone movement is called **Skinning**.
This skinning calculation is usually performed on the graphics processor (GPU), which is good at parallel calculations (GPU skinning), but due to specific optimization purposes, hardware constraints, or program convenience, there is a method where the calculation is performed on the **CPU** side, which is the main processor, and then the results are sent to the GPU for drawing. This is **CPU Skinning**.
Real-world analogy: A theater company where the director gives detailed handwritten instructions for everyone's poses
Let's compare this difference in processing methods to the stage of a play.
I am trying to have dozens of dancers (character mesh vertices) appear on stage and have them all perform complex dances (transforming poses).
Normal ``GPU skinning'' is a style in which each dancer makes a decision in their head, ``I will move by following this bone,'' and moves at ultra-high speed. The director (CPU) can easily just issue instructions, so it is very fast, but the director does not have a memo of who is standing where exactly at what coordinates.
On the other hand, "CPU skinning" is a strict management style in which the general director (CPU) of a theater company calls all the dancers one by one in a dressing room backstage, calculates and records the details in a notebook (memory) at hand, saying, "This is your pose, this is your position," and then sends them out on stage after fixing their poses.
This method is very time consuming as the director writes down everything in his/her notebook. But it has great strengths. Since the poses and detailed coordinates of everyone are perfectly recorded in the director's notebook, he is able to perform accurate collision calculations to determine where the dancer's hand is touching the cloak, and perform small corrections and physical simulations directly on the CPU side, such as ``grabbing the cloak and rewriting it into a different shape.''
Advantages and disadvantages of CPU Skinning
| Advantages (what you're good at) | Disadvantages (things I don't like) |
|---|---|
| Accurate vertex coordinates after transformation can be obtained with a C# script and can be used for collision detection and Raycast | Puts calculation load on the main CPU thread, causing FPS drop |
| Compatibility with older GPUs and environments with limited shader operations such as WebGL | The calculated vertex data is transferred to VRAM every frame, which puts pressure on the bus bandwidth |
| Smooth collaboration with physics engines such as clothing simulation | The larger the number of polygons, the more the calculation time increases geometrically. |
Selection criteria and modern optimization in practice (Job System)
In the past, CPU skinning was often considered to be a slow method, but in modern Unity, by combining multi-thread parallel processing technology called **`C# Job System`** and **`Burst Compiler`**, CPU skinning can now be executed at amazing speed. Therefore, it is selected in practical scenarios such as:
- Titles that require physical behavior in which clothing or ropes collide with and follow the character's body accurately.
- An effect system that accurately generates particles (blood spatter, etc.) from the surface of the character mesh after animation transformation.
- When you want to move a complex character beyond the limit on the number of bones on the GPU side, such as on old mobile devices or browser games (WebGL).
The following is a C# code example to control Unity's `SkinnedMeshRenderer`, dynamically switch between CPU skinning and GPU skinning settings, and safely bake (extract) and use the latest mesh vertex positions after deformation for physical judgment.
using UnityEngine;
[RequireComponent(typeof(SkinnedMeshRenderer))]
public class SkinningController : MonoBehaviour
{
private SkinnedMeshRenderer skinnedMeshRenderer;
private Mesh bakedMesh;
[SerializeField] private bool forceCPUSkinning = false;
void Start()
{
skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
bakedMesh = new Mesh();
if (forceCPUSkinning)
{
skinnedMeshRenderer.forceMatrixRecalculationPerFrame = true;
}
}
public void GetCurrentPhysicalMesh()
{
if (skinnedMeshRenderer != null && bakedMesh != null)
{
skinnedMeshRenderer.BakeMesh(bakedMesh);
if (bakedMesh.vertexCount > 0)
{
Vector3 currentVertexZero = bakedMesh.vertices[0];
Vector3 worldPos = transform.TransformPoint(currentVertexZero);
Debug.Log($"Vertex 0 position: {worldPos}");
}
}
}
void OnDestroy()
{
if (bakedMesh != null)
{
Destroy(bakedMesh);
}
}
}
By properly understanding the nature of CPU skinning and the combination of optimization using Unity's modern multi-threading technology, it becomes possible to develop extremely realistic and interactive games that balance graphics with advanced game physics and hit detection.