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

Basic Mesh subdivision

Discussion in 'Scripting' started by Wulirocks, Oct 23, 2021.

  1. Wulirocks

    Wulirocks

    Joined:
    Mar 18, 2013
    Posts:
    63
    Hi there,
    Is there a way in Unity to dynamically subdivide a mesh? I am taking about simple subdivision of a quad into let's say 8 quad or 16 triangle?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,015
    Wulirocks and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,104
    I've written a subdivision routine some years ago over here. Though the code in the current form only works with 16 bit index buffers as those were the only meshes we had for several years. It could be adjusted to work with 32 bit index buffers, though it requires a few changes. Specifically the dictionary to hold the new vertices currently uses an uint(32bit) to store a pair of indices(16 bit). To make it work with 32 bit indices you would need to use an ulong (64 bit) and the bit operations need to be adjusted as well.

    I had another more advanced version on the wiki that could also do a "9 subdivide" (so every triangle is turned into 9 triangles). Unfortunately the wiki seems to be down and doesn't look like it's coming back. Though thanks to archive.org, there's a backup.

    Note that at the time Unity did not even support any other topologies, only triangles. So if you want to support other topologies as well it gets exponentially more difficult ^^.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,015
    SPIFFY! Two questions:

    1. does it share newly-added edge verts from original tri to tri (eg, a quad would only get 1 additional vert) ? I started to stare at the code and went "meh, I'll just ask."

    2. May I muck about with it and stick it in my MakeGeo project with credit to you?
     
    Wulirocks likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,104
    1.
    Well the original vertices stay the same and it only inserts vertices inside each unique edge. So each edge of a triangle is split in half.
    SubdividedTriangle.PNG
    SubdividedTriangle9.png

    Here's an example how it would behave with shared edges:

    SubDivSharedEdges.PNG
    So the edges 1-5 and 5-6 are not shared so each will get their own mid point (g and h). However if the edge was shared before the subdivide, it should be shared after the subdivision.

    2.
    Sure, feel free to include it :) Maybe if I find some time I put an updated version on my github.
     
  6. Wulirocks

    Wulirocks

    Joined:
    Mar 18, 2013
    Posts:
    63
    Hi guys, thanks for the very helpful replies. I was able to find some code (before your answers) and did some quick testing. And for artistic reason, I won't be using mesh subdivision. The idea was to break a tree (a quad that displays a texture of a tree )... and when the tree breaks/explode, its mesh would be subdivided and all the triangle would be scattered. It simply does not look good on a tree... this would works for glass, metal etc... or non organic things.. but believe me.. a tree that explodes in triangle pieces looks bad.

    But hey, I hope this post will be useful for future use or any other user that will surf the web!
     
  7. Wulirocks

    Wulirocks

    Joined:
    Mar 18, 2013
    Posts:
    63
    Totally appreciate man!
     
    Kurt-Dekker likes this.
  8. CrimOudin

    CrimOudin

    Joined:
    Apr 13, 2016
    Posts:
    3

    Thanks for this. I am using this to create a basic polygon mesh and then I subdivide it so I can use it to generate random looking terrain.