Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error when resizing the number of quads in a procedural tile mesh

Discussion in 'General Graphics' started by plangworthy, Aug 15, 2019.

  1. plangworthy

    plangworthy

    Joined:
    Jan 3, 2017
    Posts:
    13
    I am having a problem with procedural Mesh code. The code is very large so I'm going to try explaining some things rather than pasting all of my code. Also let me preface by saying that the code works fine the first time, just not when being run more than once.

    I have a TileMesh class that assembles quad data for a Mesh using a threaded Job. The Job creates output NativeArrays for the new vertices, triangles, uvs, colors, and normals. This process is designed to run over and over again. The Job will keep being run every time there are new quads that are needed.

    The TileMesh class has Lists for vertices, triangles, etc. When the job is done, I use:

        verticeList.AddRange(jobOutput.vertices)

    ...and so on (for triangles, etc).
    I then use:

        mesh.SetVertices(verticeList)

    ...and so on to actually apply the changes on to the mesh.

    The code works flawlessly for the first batch, even for very large batches. However, subsequent batches do not appear correctly at all. Looking at both the jobOutput data and the Mesh data, it is identical whether I make 2 quads in the same batch or 1 quad each in 2 batches. The output is NOT the same though. When using multiple batches, the first quad appears normally, and with every subsequent quad that is added, no new quads appear and the original quad gets progressive lighter until it becomes white (using Standard shader).

    What is happening here?

    Is it actually possible to add additional vertices and triangles to a Mesh? I suspect the answer is no based on my results, but the documentation for Mesh/vertices states "Note that if you resize the vertex array then all other vertex attributes (normals, colors, tangents, UVs) are automatically resized too." So this leads me to believe that I should be able to resize the Mesh with more vertices later. Maybe I can't resize it using SetVertices though?
     
  2. plangworthy

    plangworthy

    Joined:
    Jan 3, 2017
    Posts:
    13
    Fixed. The problem was that the Job was not applying any base offset to the vertices referenced in the triangle array. Looked like everything was the same but the triangles were always re-referring to vertex 0.. etc when put together triangle values. Sorry about the noise.