- Home /
how to subdivide a mesh in unity in code
so i have a box built in unity by code
VectorPosition = new List<Vector3>();
VectorPosition.Add(new Vector3(pos.x+Size, pos.y-Size, pos.z-Size) * 0.5f);
VectorPosition.Add(new Vector3(pos.x+Size, pos.y+Size, pos.z-Size)* 0.5f);
VectorPosition.Add(new Vector3(pos.x-Size, pos.y-Size, pos.z-Size)* 0.5f);
VectorPosition.Add(new Vector3(pos.x-Size, pos.y+Size, pos.z-Size)* 0.5f);
VectorPosition.Add(new Vector3(pos.x+Size, pos.y-Size, pos.z+Size) * 0.5f);
VectorPosition.Add(new Vector3(pos.x+Size, pos.y+Size, pos.z+Size)* 0.5f);
VectorPosition.Add(new Vector3(pos.x-Size, pos.y-Size, pos.z+Size)* 0.5f);
VectorPosition.Add(new Vector3(pos.x-Size, pos.y+Size, pos.z+Size)* 0.5f);
verts = new Vector3[VectorPosition.Count];
for(int i = 0; i < verts.Length; i++) {
verts[i] = VectorPosition[i];
}
tris = new int[36] {0, 1, 5, 5, 4, 0,
0, 4, 6, 6, 2, 0,
4, 5, 7, 7, 6, 4,
3, 7, 5, 5, 1, 3,
6, 7, 3, 3, 2, 6,
0, 2, 3, 3, 1, 0};
but how would i go about getting the "tris" set up right when i subdivide because obviously i cant do that for say the 10th subdivision i assume there is some algorithm of some sort
any ideas or links?
Answer by Owen-Reynolds · Oct 14, 2013 at 09:12 PM
Best might be to look at non-Unity sources. Subdividing is subdividing.
A real Modeling program (and you can look how Blender code does it, but tons of work) maintains a list of quad faces, another of edges ... . Subdivide is a pile of loops and lists: work through all of quads, breaking each edge in two and replacing the quad with 4 new quads. Interpolate the UVs. Then go through all the broken edges that have adjacent tris, and fix those by breaking the tri with one extra edge (but for just a cube, you won't have any.) The final result would be copied into the Unity Mesh class.
A tri-subdivide (where you just make a new center-point for each tri and break 1 tri into 3) would be simpler. Could probably do that directly from the Unity Mesh class Tri list. But probably not what you want.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Triangles overlapping in mesh 0 Answers
Shooting problem the bullets fly in diffrent direction then player looks? 2 Answers
How to make cubes between 2 point and stick them together? 0 Answers