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

[Solved] Combining dynamically created meshes in Editor scene view

Discussion in 'Scripting' started by garrrth, Jan 28, 2021.

  1. garrrth

    garrrth

    Joined:
    May 8, 2018
    Posts:
    2
    I've been having a good old dig around for an answer to this and feel like I've been close a few times, but without a good solution. Would really appreciate any help you can muster on this.

    I'm generating meshes from a floorplan as an extruded form of a polygon, sometimes with holes in. All this is fine and had that working (rudimentary, but working). But I've come across a new use case recently where a floorplan may be split into multiple parts. Think two disconnected squares.

    So my script is adjusted for this and is creating multiple meshes as CombineInstances, then the sharedMesh is Combined from these multiple meshes.

    Problem is, after running mesh.CombineMeshes, I'm just getting (0,0,0) vertexes. The correct number of vertices. Triangles also seem to be set correctly, but zero for every vertex. VertexCount is only 451, so I'm in no danger of exceeding that.

    Here's the code that combines the meshes.

    Code (CSharp):
    1.         public Mesh Extrude ()
    2.         {
    3.             CombineInstance[] combiners = new CombineInstance[geometry.outlines.Length];
    4.  
    5.             for ( int i = 0; i < combiners.Length; i++ )
    6.             {
    7.                 int vertCount = geometry.VertexCount ( i );
    8.                 Shape shape = geometry.outlines[i];
    9.                 Shape[] holes = geometry.holes[i];
    10.  
    11.                 Mesh partMesh = new Mesh ();
    12.                 List<Vector3> vertices = new List<Vector3> ( vertCount * 3 );
    13.                 List<int> triangles = new List<int> ();
    14.  
    15.                 CreateEdgeVertices ( vertices, shape, holes);
    16.                 CreateEdgeTriangles ( triangles, shape, holes, vertCount);
    17.                 CreateFaceVertsAndTris (vertices, triangles, shape, holes, vertCount);
    18.  
    19.                 partMesh.vertices = vertices.ToArray ();
    20.                 partMesh.triangles = triangles.ToArray ();
    21.                 partMesh.RecalculateBounds ();
    22.                 partMesh.Optimize ();
    23.                 partMesh.RecalculateNormals ();
    24.  
    25.                 combiners[i].mesh = partMesh;
    26.             }
    27.  
    28.             mesh.CombineMeshes ( combiners );
    29.  
    30.             return mesh;
    31.         }
    Definitely missing something here, just not sure what it is.

    Thanks in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Just looking at the sheets of code above it's hard to reason about how this is even intended to work.

    I recommend stripping your dataset down to two simple objects, each with 3 vertices and a single triangle, then debug what's going on.

    The problem might even be in one of the three Create*() functions, which you don't show above.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,531
    Well, the issue seems quite simple and clear. You haven't initialized your combine instances properly. Specifically you only assigned your different meshes to that struct. However the CombineInstance struct has several fields and two of them are mandatory, specifically the mesh and it'S transformation matrix. Since you haven't initialized them at all, all the matrices will be filled with "0" so all vertices will of course result in (0,0,0). If you don't need any special transformation for each mesh, you have to assign at least Matrix4x4.identity to the transform field of each CombineInstance.
     
    Eco-Editor likes this.
  4. garrrth

    garrrth

    Joined:
    May 8, 2018
    Posts:
    2
    That's the one! Using the Matrix4x4.identity has worked. Thanks @Bunny83.

    Admittedly, my triangles are rendered all over the place, but that's a separate issue I think. I'm at least getting the Meshes rendered.
     
    Eco-Editor likes this.