public List<Vector3> vert = new List<Vector3>();
private void Update()
{
doSomethingWithVert();
GOmeshFilter.mesh.SetVertices(vert); // 1
GOmeshFilter.mesh.RecalculateBounds(); // 2
GOmeshFilter.mesh.RecalculateNormals(); // 3
}
Above is the code for dynamically manipulating a mesh.
The number of Vertices stay the same, I only deform some of them.
Line 1 copies the whole variable. 2 and 3 are probably costly.
My Question is: Character Animation, like Idle-Animation, walking animation, etc. does it work the same way (permanent copy of the recomputed Vertices)? Or are the vertices only transferred once an than manipulated/interpreted in a different way by the graphics card and how would this technique be called.
By “character animation” I assume you’re referring to meshes that use a skinned mesh renderer.
Skinned mesh renderers are deformed by animated bones on demand, either on the CPU or the GPU depending on the platform and project settings. You can modify the vertices of a skinned mesh the same way you can a regular mesh renderer, though the mesh is accessed via the SkinnedMeshRenderer component’s .sharedMesh rather than a separate MeshFilter component. The only gotchas being the skinning will still be applied, and that the orientation and scale of the original mesh needs to be respected when modifying the vertices otherwise the mesh will look very weird in use. The scale and orientation will be exactly what was exported from the original application, and won’t be easy to calculate at runtime, so you’ll want values you can configure manually. You also will need to manually make a copy of the mesh before modifying it as otherwise you’ll be directly modifying the asset itself affecting all other skinned meshes that use that mesh, and even the asset on disk if running from the editor. meshFilter.mesh automatically returns a copy of its .sharedMesh when first accessed to avoid this.
Skinning (applying the bone animation) happens just before rendering outside the control of c#.
When using GPU skinning the mesh is only uploaded once when first needed, and then the skinning is done via special shaders that output a deformed mesh to the GPU directly. This means the CPU doesn’t need to update the mesh every frame, only the bone locations. When this happens the CPU does not ever “know” the location of the vertices post deformation by skinning.
Though if you modify the mesh it can. For platforms that don’t support GPU skinning or projects that have the option disabled, the mesh is deformed and uploaded to the GPU in that deformed state. Technically the CPU has access to the vertices post deformation, but the output of the on demand skinning is not directly exposed to c#.
You can get access to the post-deformed vertices in c# using skinnedMeshRenderer.BakeMesh(outputMesh);, but you cannot assign these back to the skinned mesh renderer and have it work. The idea would be to use the output mesh with a MeshFilter & MeshRenderer combo or with a Graphics.DrawMesh() call. Though it could be used to find out the position of vertices after deformation as the vertex order will remain unchanged. This is using the same code that the on demand CPU skinning uses, but if you call this function I believe Unity will still deform the mesh again for use in rendering regardless of if the mesh changes between the time you call it and rendering, if it’s using CPU skinning. And won’t itself upload that baked mesh to the GPU if GPU skinning is enabled unless you use it elsewhere.