Search Unity

Skinned Mesh Renderer 'BakeMesh' not working correctly.

Discussion in 'Editor & General Support' started by Tesrym, Aug 1, 2018.

  1. Tesrym

    Tesrym

    Joined:
    Dec 2, 2012
    Posts:
    64
    I want to create a collider for a character.
    So I use the bake mesh feature that the skinned mesh renderer has.

    https://imgur.com/FQ9O6vi.gifv

    Working 100% so far.


    https://imgur.com/b5bf5i9.gifv

    Still good.


    https://imgur.com/Q4bjFjF.gifv
    Silly forum restrictions does not allow me to post necessary visual aids.

    And this is where I get confused.

    https://docs.unity3d.com/ScriptReference/SkinnedMeshRenderer.BakeMesh.html
    I thought a snapshot meant that it would bake a mesh in its current animated state.
    But it only bakes it as if it was permanently in the idle animation from the animators blend tree.


    Current configuration of the human:


    Using 2018.2.0f2


    Is this a bug, or is bakemesh intended for something else?
     

    Attached Files:

    • Mesh.gif
      Mesh.gif
      File size:
      237.8 KB
      Views:
      1,037
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    What does your script look like?
    Also generating a mesh collider for your character like this is going to be quite heavy on performance.
     
  3. Tesrym

    Tesrym

    Joined:
    Dec 2, 2012
    Posts:
    64

    public SkinnedMeshRenderer skinnedMeshRenderer;
    public Mesh bakedMesh;
    public Vector3 colliderRotation = new Vector3(-90, 0, 0); //To combat blender.

    private MeshCollider meshCollider;
    private GameObject colliderContainer;

    private void Start() {
    bakedMesh = new Mesh();
    colliderContainer = new GameObject("Collider Container");
    colliderContainer.transform.SetParent(transform);
    colliderContainer.transform.localRotation = Quaternion.Euler(colliderRotation);
    meshCollider = colliderContainer.AddComponent<MeshCollider>();
    meshCollider.sharedMesh = bakedMesh;
    }

    private void FixedUpdate() {
    skinnedMeshRenderer.BakeMesh(bakedMesh);
    }


    And the Animator is driven by a state machine behavior. Could this be a problem? I will test with a "normal" script later when I get the time.
    edit: I did try a non-SMB script, but it changed nothing.

    Dont worry about performance, I plan to use it in smart places later.
     
    Last edited: Aug 1, 2018
    Firlefanz73 likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    I tested your script and it works for me with Animator with state machines.
    Try looking at the Mesh data instead of the collider. Double click it in the inspector so you can preview the mesh data.
    If its still not working then file a bug report so we can take a look
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    Ok I see the problem. Its the mesh collider, not the bake mesh. If the mesh changes you need to reassign it to the collider. Internally it copies the data into its own mesh physics structure, It does not detect changes to the mesh.

    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         skinnedMeshRenderer.BakeMesh(bakedMesh);
    4.         meshCollider.sharedMesh = bakedMesh;
    5.     }

    https://docs.unity3d.com/ScriptReference/MeshCollider-sharedMesh.html
     
    BrainSlugs83, Firlefanz73 and Tesrym like this.
  6. Tesrym

    Tesrym

    Joined:
    Dec 2, 2012
    Posts:
    64
    That worked. I would never have thought of doing that. Thank you.
     
    BrainSlugs83 and karl_jones like this.
  7. VResearch

    VResearch

    Joined:
    Jan 6, 2021
    Posts:
    21
    I tried your solution and it worked great in the editor. However in build it throws "[Physics.PhysX] TriangleMesh::loadFromDesc: desc.isValid() failed!" and does nothing.
     
  8. Course-Interactive

    Course-Interactive

    Joined:
    Nov 30, 2015
    Posts:
    6
    You need to enable the "Read/Write" property of the mesh!
    Thank you for pointing out, that it does not work in the build.