Basic Mesh subdivision

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?

There maybe something in the ProBuilder API but you’re probably better off winding your own, since procedural geometry creation in Unity is pretty straightforward.

You’re welcome to sniff around my MakeGeo package for random samples of procgen:

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

https://github.com/kurtdekker/makegeo

2 Likes

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 Likes

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?

1 Like

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.
7597201--942493--SubdividedTriangle.PNG
7597201--942496--SubdividedTriangle9.png

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

7597201--942499--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.

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

4 Likes

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!

Totally appreciate man!

1 Like

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.