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. Dismiss Notice

How to combine meshes with different materials?

Discussion in 'Scripting' started by Epic-Username, Nov 24, 2016.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I'm making a small minecraft clone for a new project, but I've encountered a problem, I am using this code to combine meshes in a chunk:
    Code (CSharp):
    1. public void CombineMeshes(){
    2.         MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
    3.         CombineInstance[] combine = new CombineInstance[meshFilters.Length];
    4.         int i = 0;
    5.         while (i < meshFilters.Length) {
    6.             combine[i].mesh = meshFilters[i].sharedMesh;
    7.             combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
    8.             meshFilters[i].gameObject.active = false;
    9.             i++;
    10.         }
    11.         transform.GetComponent<MeshFilter>().mesh = new Mesh();
    12.         transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine,false);
    13.         transform.GetComponent<MeshCollider> ().sharedMesh = transform.GetComponent<MeshFilter> ().mesh;
    14.         transform.gameObject.active = true;
    15.     }
    but each block in the chunk(which are the children) have different materials and when i combine the mesh it uses the same material for the entire chunk. How can i combine meshes while using different materials for the different parts of the chunk?

    "Why i want to combine the meshes":
    I want to combine meshes so it doesn't have to update all the blocks renderers and colliders separately which will cause major lag, instead it will only update 1 mesh/chunk.
     
  2. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    If I remind correclty you cannot combine two meshes that have different materials... actually a mesh with two material when sent to the gpu becomes two meshes. You'll have to combine materials in some way.

    I don't know if it would help but...
    https://www.assetstore.unity3d.com/en/#!/content/5017
     
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I guess my question has changed to how to combine materials then.
     
  4. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Using atlas textures if you're using a single shader for all your materials.

    EDIT: I will expand on what I just said because it's a little scarce. You'll have to use the same material sharing the same texture atlas on all your meshes. It's the UVs of your meshes that will determine which part of that texture atlas to use. You can change the UVs of your meshes by script... I'm not sure it is the right way to go for your use case though... just answering to your question in the limited context of combining mesh.
     
    Last edited: Nov 24, 2016
  5. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I think i'm going about this the wrong way because i just though that if i want water in the game it has to be a trigger but i'm setting the whole chunk as a solid collider, so what would you guys do about this?
     
    Last edited: Nov 24, 2016
  6. FreeFly90

    FreeFly90

    Joined:
    May 28, 2016
    Posts:
    177
    Why don't you use multiple materials applied on different submeshes?
     
  7. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    What do you mean by that? There's only 1 mesh for each chunk.
     
  8. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    What he means is that its not quite true that 1 mesh = 1 material. Meshes can be divided into submeshes, which can each have their own materials. Essentially its the same as having several seperate meshes, just combined into 1 for convenience. The combinemesh feature as far as i'm aware doesn't support submeshes/multiple materials, so it just combines everything into 1 submesh. It is technically possible to combine them so that each mesh you started with is a different submesh and can keep its own seperate material slot in the combined mesh but you'll either have to write the function to do that yourself or look for an existing solution in the asset store.
     
  9. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    So i might as well just have every block as a separate object then instead of chunks if each mesh can only have 1 material?
     
  10. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    No, if you have every block as a seperate gameobject then your going to get abysmally bad performance. Draw large sections of blocks in chunks and use texture atlasing to make them have the textures you need to look different. You don't need a different material on every block type, just a different texture.
     
  11. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    What is texture atlasing and how do i use it?
     
    Last edited: Nov 26, 2016
  12. sushil9394l

    sushil9394l

    Joined:
    Aug 2, 2020
    Posts:
    6
    Yeah Why No Reply To Use Texture Atlasing??
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    Please don't reply to five-year-old threads with random questions about why nobody replied.

    For one, spamming old threads is actually against forum rules.

    For two, "texture atlasing" is extremely well documented in the general sense, and given the context of the comment it replied to (combining meshes and minimizing drawcall batches for better performance), it's pretty obvious that nobody here is going to retype the internet worth of knowledge around texture atlasing.

    If you actually have an issue, please start your own new post. It's FREE!

    When you do, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/