Assembling 3d model programmatically in unity

Ganga Raju Suru
Ganga Raju Suru used Ask the Experts™
on
How to assemble 3d models( individual 3d components) in unity or any game engine, programmatically, with out structural positions.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I had done similar, with a terrain engine in Unity.  I had created a noise engine to produce different numbers for heights at various positions.  Then I passed in an array of vertice positions based on that.  Attach a MeshFilter, and meshRenderer.  And then in code, you can reset the mesh vertices.

Something like

  // insert function to generate an array of Vector3 positions for your model
  var verts = GenerateMeshVertices(); 
  
  // apply the vertices
  GetComponent<MeshFilter>().mesh.vertices = verts;

Open in new window


In my code I had a 2D array of 100x100 positions, each 1 meter apart on x/y, and then a noise engine set the height.  that's pretty easy.  

I.e.
  // insert function to generate an array of Vector3 positions for your model
  Vector3[] GenerateMeshVertices()
  {
     var verts = new Vector3[100,100];
     for (int x; x < 100; x++)
     {
       for (int z; z < 100; z++)
       {
         var y = Random.Value;
         verts[x, z] = new Vector3(x,y,z);
       }
     }
     return verts;
  }

Open in new window


please note, this is pseudo code I just sketched here, so it may not compile.  But it might as well.  (hopefully I didn't forget anything :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial