Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to create Procedural Meshes at Runtime?

Discussion in 'Graphics for ECS' started by morphex, May 13, 2023.

  1. morphex


    Dec 18, 2012
    I am quite confused on how the new render system and dots handles procedural mesh generation, since the RenderMeshArray is per entity chunk,
    and every single mesh will be unique and runtime generated based on some paremeters and external inputs. Those parameters are dynamic at runtime, and will be updated quite often.

    Are there any docs, samples, examples of generating Many(tm) meshes procedurally and runtime? Do I really need to mantain game objects , or use Graphics.DrawMesh manually?
  2. DreamingImLatios


    Jun 3, 2017
    You can register meshes directly with EntitiesGraphicsSystem and assign the index that gives you to MaterialMeshInfo. The resulting MaterialMeshInfo will ignore the RenderMeshArray and use this custom mesh instead, and then you can animate that mesh.

    However, since you seem to be doing this a lot and frequently, having a bunch of meshes to individually modify could be very expensive. Depending on your requirements, maybe this will interest you? Animation and Rendering/Dynamic
    Egad_McDad and JussiKnuuttila like this.
  3. wannaplaymore


    Jan 4, 2019
    Do you have an updated simple example? I checked out your project on github and tried to use your framework. I was very hopeful it would work for me. I was trying to use your Dynamic Meshes example, but it didn't work with the latest version of your framework (7.4) and Entities Graphics 1.0.11. Ideally I would like to see an example outside your framework that shows how to register meshes directly with the EntitiesGraphicsSystem. I have attempted to copy the mesh and materials from the Shared RenderMeshArray into a new RenderMeshArray, but saw the same massive performance hit when changing the vertices of the mesh.
    Last edited: Jun 23, 2023
  4. DreamingImLatios


    Jun 3, 2017
    I'll update the guide to not use obsolete baker APIs. But the example code is working for me right now. I'm willing to investigate the issue you encountered if you provide more info.
    I wrote my framework partly to bypass performance problems like this. You will need to find an answer from someone else.
  5. shalm_


    Apr 20, 2023
    You can spawn entities with an empty mesh and than in a system register a new mesh and collider.
    For me works something like this:
    Code (CSharp):
    2.                     var newMesh = new Mesh();
    3.                     newMesh.vertices = vert.ToArray();
    4.                     newMesh.triangles = tris.ToArray();
    5.                     newMesh.colors = colors.ToArray();
    6.                     newMesh.normals = normals.ToArray();
    7.                     rb.Value.Center = new float3(7f, 7f, 7f);//ref RenderBounds rb
    8.                     rb.Value.Extents = new float3(7f, 7f, 7f);
    9.                     newMesh.RecalculateBounds();
    10.                     var newId = hybridRenderer.RegisterMesh(newMesh);
    11.                     mmi.MeshID = newId;//ref MaterialMeshInfo mmi
    14.                     //Physics collider
    15.                     Collider = Unity.Physics.MeshCollider.Create(vertCol, trisCol);
    16.                     ecb.SetComponent(e, new PhysicsCollider { Value = Collider });