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

Make custom mesh if for loop

Discussion in 'Scripting' started by GivoMakes, Jan 30, 2020.

  1. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    ***Sorry it should be "in a for loop"***

    Hi!

    This is something that has been really bugging me, and I thought he would be a good place to ask.

    So I have a custom mesh, and I would like to have it in a for loop. basically taking it so it would make the mesh, then make another mesh without removing the previous one. I don't feel like copying the code, but here is my best pseudo-code (Not actually copy-paste code!!):
    Code (CSharp):
    1. Vector3[] verts;
    2. int[] tris;
    3. Mesh mesh;
    4.  
    5. //  normal custom mesh code here
    6.  
    7.  
    8. for (int i = 0; i < 50; i++) {
    9.    verts = new Vector3[
    10.       new Vector3(-i, 0, -i),
    11.       new Vector3(i, 0, -i),
    12.       new Vector3(i, 0, i)
    13.    ];
    14.    tris = new int[
    15.       0, 1, 2
    16.    ];
    17.  
    18.    mesh.vertices = verts;
    19.    mesh.triangles = tris;
    20. }
    is there anyway to do something like instantiate, or is it not possible?

    thanks for any help, givo
     
    Last edited: Jan 30, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
  3. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    Thanks, but that doesn't really help as this isn't procedural generation. Im wondering if I can make several custom meshes with only one empty gameobject with a mesh filter and mesh renderer. All done with a for loop.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You can make meshes with any loop you want.

    You can make an array of meshes and sequentially assign them to the MeshFilter on your GameObject, and if it has a MeshRenderer, then it will show that newly assigned mesh.

    To do it:

    During initialization:
    - make an array of meshes
    - iterate and create each mesh and assign it into the slot

    During running:
    - choose what the next mesh should be
    - assign that mesh to the MeshFilter of the GameObject
     
  5. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    Thanks, but do you have any examples of that? It seems kinda complicated
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
  7. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    It's not procedural generation. I'm pulling verts and tris from a json but I simplified it to just use the loop iteration variable to make it easier.
     
    Last edited: Jan 31, 2020
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Regardless of where you get your source data, you'd still create the mesh in the exact same way. If you're pulling all the data that you need from a file, it should be possible to adapt that example to suit your needs.
     
  9. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    I already have code to make the mesh. I wanted to use a for loop to make every mesh in the file. @Kurt-Dekker understands what I'm trying to do. Use a for loop to make x amount of custom meshes by assigning each new one to a new game object.
     
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Did you want them in separate GameObjects or all submeshes of the same GameObject?
    Never mind, you said seperate objects. So, do these GameObjects already exist or do you want to create them as you go?
     
  11. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    Create as I go. They are in an array so it makes it easier
     
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    You have an array of Meshes already? Then you've already done the difficult part. Putting them in to GameObjects will be easy.

    So let's say your array is called meshArray. You just need to do the following
    Code (csharp):
    1.  
    2. GameObject myCurrentGameObject;
    3. MeshFilter myCurrentMeshFilter;
    4. for (int i=0;i<meshArray.Length;i++)
    5. {
    6.      myCurrentGameObject = new GameObject("Mesh Object "+i);
    7.      myCurrentMeshFilter = myCurrentGameObject.AddComponent<MeshFilter>();
    8.      myCurrentMeshFilter.mesh = meshArray[i];
    9. }
    10.  
    That's just the base-line, you'll probably want to add some sort of Material too, and other components.
     
  13. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    Sorry, I have an array with components made up of an array of vertices and an array of triangles
     
  14. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Right, that's why I posted that link about procedural mesh creation a few posts back. It shows how to create a mesh object from triangles and vertices.

    Edit: by the way, if we just have an array of triangles and array of vertices, than how do get multiple meshes out of that? How do you know which triangle goes to which mesh? Are you planning to read-in the meshes-one-at-a-time and reuse the same array each time?
     
    Last edited: Jan 31, 2020
  15. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    Basically, yes. I know how to make a mesh. Each object in the array has verts and tris, and they go together. I have a for loop that takes the number of meshes in the array, and for each one makes the mesh. The problem Im having is that each time i make a new mesh the old one gets overwritten, so I would like each mesh to be its own game object
     
  16. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Ok, so I had already posted some code to create the game object in this post:
    https://forum.unity.com/threads/make-custom-mesh-if-for-loop.818646/#post-5429598
    So All you need to do is basically take the code inside of the loop and put that in your own loop instead. Then assign the MeshFilter.mesh to the mesh that you create in each iteration.
     
  17. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    OK, thanks! I'll try that! I was unsure if it was goanna work
     
  18. GivoMakes

    GivoMakes

    Joined:
    Jan 21, 2020
    Posts:
    11
    Thanks! It works! I just had to add a mesh renderer and all good! final code:
    Code (CSharp):
    1. var stuff = JSON.Parse(filedata.text);
    2.  
    3.         GameObject myCurrentGameObject;
    4.         MeshFilter myCurrentMeshFilter;
    5.         MeshRenderer myCurrentMeshRenderer;
    6.         for (int e=0; e<stuff["chunkData"].Count; e++)
    7.         {
    8.             vertsl = stuff["chunkData"][e]["verts"].Count;
    9.             verts = new Vector3[vertsl];
    10.        
    11.             for (var i = 0; i < stuff["chunkData"][e]["verts"].Count; i++) {
    12.                 verts[i] = new Vector3(stuff["chunkData"][e]["verts"][i]["X"], stuff["chunkData"][e]["verts"][i]["Y"], stuff["chunkData"][e]["verts"][i]["Z"]);
    13.             }
    14.        
    15.             trisl = stuff["chunkData"][e]["tris"].Count;
    16.             tris = new int[trisl];
    17.        
    18.             for (var r = 0; r < stuff["chunkData"][e]["tris"].Count; r++) {
    19.                 tris[r] = stuff["chunkData"][e]["tris"][r];
    20.             }
    21.  
    22.             myCurrentGameObject = new GameObject("Mesh Object " + e);
    23.             myCurrentMeshFilter = myCurrentGameObject.AddComponent<MeshFilter>();
    24.             myCurrentMeshRenderer = myCurrentGameObject.AddComponent<MeshRenderer>();
    25.             myCurrentMeshFilter.mesh.vertices = verts;
    26.             myCurrentMeshFilter.mesh.triangles = tris;
    27.             int scale = stuff["scale"];
    28.             myCurrentGameObject.transform.position = new Vector3(stuff["chunkData"][e]["id"]["X"]*stuff["chunkSize"]["X"]/2, stuff["chunkData"][e]["id"]["Y"]*stuff["chunkSize"]["Y"]/2, stuff["chunkData"][e]["id"]["Z"]*stuff["chunkSize"]["Z"]/2);
    29.         }
     
  19. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Oops. I forgot about the mesh renderer. :)