Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

BakeMesh() with multiple materials

Discussion in 'Scripting' started by zedz, Nov 29, 2021.

  1. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    135
    I have the following code, which I use after a person has died, which helps performance by turning the skinned meshes into static ones, it works perfect for 90% of the charcters except for the cases where a skinned mesh has more than one material
    eg
    Mesh = human
    Material[0] = body_material
    Material[1] = hair_material

    In that case only the part of the mesh is saved that has the first (body_material) the mesh with the hair_material is not drawn

    I assume this is a bug? Or do I need to do something else?

    Cheers zed


    Code (CSharp):
    1. public void CreateDeadStaticMeshes( SkinnedMeshRenderer[] skinRenderers )
    2.     {
    3.         deadMeshRenderers = new Mesh[ skinRenderers.Length ];
    4.         for ( int i=0; i<skinRenderers.Length; i++ )
    5.         {
    6.             deadMeshRenderers[i] = new Mesh();
    7.             skinRenderers[i].BakeMesh( deadMeshRenderers[i] );
    8.         }
    9.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    21,394
    If you look on the MeshRenderer where this ends up, you are probably giving it a 2-material mesh but only one material. You would need to supply it with an array of two materials.

    ALSO, this line is kinda weirdly misleading:

    Your array of Meshes is called "deadMeshRenderers."

    Meshes are NOT MeshRenderers. Those are two separate completely-unrelated things. Make sure your code reflects reality or it will impair your ability to reason about what is happening.

    MeshFilters hold Meshes

    MeshRenderers reach to MeshFilters to get Meshes

    etc.
     
    zedz likes this.
  3. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    135
    Sorry its been a very long day, I thought that was the only relevant code but I now see I wrote another longer function that calls this and thats where the problem was, yes I needed to supply all materials there

    Fair enough, you're right, I should use a better term, I'll change that

    Thanks a lot mate :)
     
    Kurt-Dekker likes this.
unityunity