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

Feature Request return Single MeshData struct from Mesh.AcquireReadOnlyMeshData

Discussion in 'C# Job System' started by Pr0x1d, Aug 1, 2023.

  1. Pr0x1d

    Pr0x1d

    Joined:
    Mar 29, 2014
    Posts:
    46
    I am at work working on a batching system for shadows/etc. I have to batch them myself and render them using Graphics API. Which works fine by itself, but I cannot get it all clean because I can not get single instance of MeshData but I have to get MeshDataArray for some unknown reason. I need my own NativeList as I am doing my own culling.

    Current solution is to create a secondary non native List<Mesh.MeshDataArray> and dispose of it as I would dispose of each MeshData that would get culled.

    I tried looking into copying the data from MeshDataArray[0] into a newly created MeshData struct but there does not seem to be any functions or documentation of doing that. Using Dispose on MeshDataArray disposes of every MeshData in the Array which is fine when using that array but not when I have copied one of it to the new NativeList.

    Can we please get option to get Single MeshData from Mesh.AcquireReadOnlyMeshData when return format is MeshData and not MeshDataArray?

    Thanks
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,028
    GetVertexData is probably your best option. If it gets disposed upon Apply, you can make a copy of it using CopyTo().
     
    Pr0x1d likes this.
  3. Pr0x1d

    Pr0x1d

    Joined:
    Mar 29, 2014
    Posts:
    46
    Thanks for pointing this one out but its still flawed for a system used by others.
    The problem comes with different vertex buffer streams as not every single mesh might be optimized shadowCaster, f.ex some models as there are thousands, use the original mesh (lower lod etc.), which contains additional buffers for uvs, vertex color, etc. I have optimized biggest models to just position and normals, but not everyone on the team in the future might know of optimizing every single model they wanna use as shadowCaster.

    Another problem also with GetVertices and GetNormals is that it is direct copy, not pointer to original data, which will have to be stored, using MeshData it is only a pointer to already stored copy, which would mean I would copy those data for every object, then I would have to additionaly write a way to (instance) that data by making my own structure which would always store unique MeshData and a count of subscribers so I can release when I am done, but it still has a additional cost of memory. Atleast what docs seems to point out.