Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved The order of execution between C# Mesh deform and Skinned Mesh deformations

Discussion in 'Scripting' started by DimitriX89, May 14, 2022.

  1. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    I wonder if scripted actions on mesh such as setting vertex positions are executed before skin deformation or after it? Also, if the scripted deformation is executed before skinning, is there a way to make it execute after, as a post-processing effect? I've made useful "squash and stretch" animation modifier in Blender, but havent tried porting the code to Unity yet. The main advantage of this thing is that it is executed in world space and after skinning. If it is not possible in Unity, then better not to bother.
    https://gyazo.com/70b4b05c091f658dfd8553d84fbe934e
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    DimitriX89 likes this.
  3. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    I dont get where SkinnedMeshRenderer component belongs on this graphic :confused: To Scene Rendering, since it is Renderer?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    You're right, it's not called out.

    But since there is LateUpdate and then OnPreCull, it has to be somewhere between there.

    I reason this because LateUpdate() could change meshes

    OnPreCull must have all the meshes as they will ultimately be to cull.
     
    DimitriX89 likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    This tutorial on making your own custom player loop in Unity shows a method for seeing all the sub systems in each of the player loop stages.

    In PostLateUpdate I can see 'UpdateAllSkinnedMeshes' which might be what you're looking for.

    And if it's not at the right stage, you can always add in your own custom player loop.
     
    DimitriX89 and Kurt-Dekker like this.
  6. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Seems to be it. Only practical testing will tell for sure. Thanks
    Ok, I'll check PreCull as well
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,915
    The question is what do you actually expect to happen before or after skinning? For example when GPU skinning is on, the skinned vertices are not even present on the CPU side. Even with CPU skinning, Unity does not provide any direct access to the skinned mesh. The actual "mesh" that is referenced by the SkinnedMeshRenderer is never modified. The skinning only takes place in a copy of the data on the native C++ side. Unity provides the BakeMesh method for quite some time now which allows you to read-out the skinned version of the mesh at any time. However that's just a copy of the mesh and not the data Unity actually uses.

    There is no way to directly get access to the skinned data that the renderer uses. So if you really want to modify the mesh data after skinning, your only option at the moment is to use BakeMesh manually to create a skinned mesh on the fly, modify that mesh and use that mesh with a normal MeshRenderer. Even when caching and reusing the temp mesh and using a cached List and GetVertices to read out the vertices, I'm pretty sure performance wouldn't be that great. It may work but is probably significatly slower, especially since you are now forced to do CPU based skinning.
     
    DimitriX89 likes this.
  8. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Oh, Bake Mesh workflow sounds complicated. Is performing deformations in vertex shader an alternative (to do something post skinning)?
     
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,915
    Yes, sure. However depending on what kind of "deformations" you have in mind it can get complicated as well ^^. If it's really just about uniform stretching along an axis and a uniform offset, that's quite trivial. You can even use _Time in the shader to do your "animation". You may use a multiplier to turn off your procedural animation in the shader which you can set from the outside.