Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

MeshCombineUtility Questions

Discussion in 'Scripting' started by terransage, Nov 28, 2006.

  1. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    I'm in the dark on how to use this script and what its material/texture side-effects might be.

    I have a test object that's made up of separate but grouped smaller objects, each sharing different parts of the same texture (each separately UV-mapped, that is) and each having separate lightmaps (that's a workaround for importing from C4D to Unity). I want to combine the meshes in Unity, but I'm not sure I've done it right. My questions are:

    1. Where do I attach the script? I attached it to the parent, but got the "must derive from monoscript" error message. I wasn't sure how to correct that, but I worked around it by attaching an empty MonoBehavior component to the parent and placing the script there. Then--nothing different seemed to happen. There were no error messages either, so I'm not sure one way or the other if I've done it right.

    2. Did I solve the "must derive from monobehavior" problem correctly?

    3. How do I tell if I've actually combined the meshes? (By the children disappearing because they become one with the parent? By the purple wire mesh covering both objects? No, that happens anyway when you click the parent. )

    4. If the meshes are truly combined, do they then have to share only one "version" of the material, causing me to lose the separate UV maps?

    5. Can I successfully use a different material on each object that is combined?

    6. Will I not be able to use the separate lightmaps, and--if I'm still able to use the separate lightmaps--will they still cost the engine just as much as if I never combined the meshes and shared the base texture?

    Thank you for your help!

    :)
     
  2. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    dunno on the mono prob. you want to combine meshes that share the same material though. not sure if can use multiple uvs (i'd guess yes) but you definitely want to use the same image so you can have a single shared material. multiple materials are like multiple objects (so no gain). can you combine your lightmaps in pshop?
     
  3. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    Yes, but I can also create the full lightmap in C4D. The problem is that the lightmap is distorted by the different UV maps when I import them into Unity on a single object. This seems to be a problem that other C4D users are having, and one of the workarounds is to divide the model into separate objects, each with its own single UV map of a shared texture, and each with its own lightmap. It's kind of tedious and costly, but it works.

    I'm afraid that the combining of meshes will recreate the original problem of lightmap distortion or will wipe out the separate UV's or both!
     
  4. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    i don't use c4d. so i'm thinking pretty generally. can you distort your lightmap in pshop to fit your uvs as a single image?

    as far as mulitple uvs, i'm betting you'll be ok. i've done trees with multiple uvs but it was a bit different than what you're doing. "try it" is the best advice i can come up with. :?
     
  5. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    I have the two objects that I want to combine grouped together under a "null object" parent, and I have the script attached to the parent. Is that where the script is supposed to go? Hm, I just thought that maybe I should make one of the two objects the child of the other instead and attach the script to the new parent, getting rid of the "null object" altogether. I'm still not sure what I'm doing....

    Pete said:
    I'm not sure. I'd have to distort the lightmap so that it redistorts correctly on the separate UV maps. Like you said, I have to experiment with that.
     
  6. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    I re-grouped and re-parented the objects and reattached the script, but still nothing happened. No error messages either.
     
  7. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    >>grouped together under a "null object" parent, and I have the script attached to the parent

    that's how i've done it. might work the other way but i understood it literally as combine children.
     
  8. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    Looking at the script, I see this:
    Code (csharp):
    1. Mesh mesh = new Mesh();
    2.         mesh.name = "Combined Mesh";
    3.         mesh.vertices = vertices;
    4.         mesh.normals = normals;
    5.         mesh.uv = uv;
    6.         mesh.uv1 = uv1;
    7.         mesh.tangents = tangents;
    Am I supposed to attach the script to one object and name the second object (to be combined with the first) in the parentheses after "new Mesh" and then give this new combined object a name in "Combined Mesh?"

    Or do I attach the script to both objects and combine them by giving them both the same mesh.name?

    I'll try both ways and see if they work.

    I tried to find instances of this script being used in the tutorials, but all I found was this script sitting unused all by itself in an odd file.
     
  9. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    sounds like maybe you're starting to confuse yourself! ;) the way i understood it was pretty simple. just make an empty then make all the meshes you want to combine childern of that empty. attach the script to the empty. that's all there is to it.

    it doesn't give you any feedback. it just does it on game load (or play in the editor). if you don't have a ton of polys you might not notice any performance boost. it's still doing it though. and again the materials thing... if it's not shared materials you don't gain anything.

    not like i haven't been wrong before though.
     
  10. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    Yes, I think I was starting to confuse myself! I'm not used to things being easy, I guess. It's funny, but after I re-parented and screwed around with the script, etc., I did exactly what you just said and created an empty, made the objects its children, and attached the original script to the empty. Thanks again, Pete, for your help and your replies! :)
     
  11. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Isn't the actual problem that you're trying to use MeshCombineUtility, which is not an actual script, just a file with helper functions?

    The way to use it: import standard assets (or just get CombineChildren and MeshCombineUtility files), then add CombineChildren script to a parent. CombineChildren uses MeshCombineUtility file, but you should not do anything with MeshCombineUtility yourself.

    CombineChildren will combine objects that use the same material. The objects themselves can be different (and UVs mapped into different portions of textures etc.), but the materials used have to be the same to get them combined.
     
  12. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    Thank you, Aras, I had the feeling that I was approaching this from the wrong direction. I will try that.
     
  13. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    Yes, that definitely did the trick, but I'm still having the same problem with the lightmap being distorted by the different UV maps, although it's the same material. It looks fine in C4D otherwise. If I import the model into Unity with the lightmap on it, the different UV maps are wiped out. If I import it without the lightmap, then apply the lightmap after import, the lightmap is distorted by the UV maps. I thought maybe scaling the lightmap might help, but parts of the lightmap are in correct proportion to parts of the object, and some parts are not. (Okay, I've probably gone on long enough about this problem! I'll just stick to my original workaround and shut up....) 8)

    Here's a workaround using two materials, two objects, two lightmaps. Costly, so I'll have to go easy on lightmap usage in other parts of the game.
     

    Attached Files:

  14. SARWAN

    SARWAN

    Joined:
    Oct 23, 2012
    Posts:
    11
    On this CombineChildren Script. Did anyone change hashtable into Dictioanry And ArrayList into List<>???