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

Mesh vs Shared Mesh in procedural generation

Discussion in 'General Graphics' started by VictorKs, Nov 15, 2018.

  1. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    I'm doing continuous procedural mesh generation and I want each object to have its own mesh displaced every frame should I use mesh or shared mesh? I don't want to create a new mesh every frame only to edit it but every object needs a unique mesh instance.
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Use mesh and not sharedMesh.

    The SharedMesh property refers to your actual mesh asset, so changes to the mesh will affect all objects that use that mesh reference, and (I think) changes will apply to the mesh asset in your asset folder with running in-editor.

    The Mesh property will apply to the "mesh instance" created for this specific object. The first time you access the "mesh" on an object, a new copy of the mesh will be created for you to modify. All additional times you access the "mesh" on the same object, you will get the same mesh that was created for you the first time you tried to access the "mesh" on this object. Since each object has it's own instance, edits will only apply to the one object.

    More detailed info, if you want:
    https://docs.unity3d.com/ScriptReference/MeshFilter-mesh.html
     
    pacmaybz, ShantiB95, DonPuno and 6 others like this.
  3. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    Thanks for the clarification I have already read the documentation but it wasn't clear to me wether calling meshFilter.mesh would generate a new instance every frame. I thought that I should call mesh the first time to create the unique instance and then sharedMesh to edit it.
     
    DonPuno likes this.