- Home /
Cutting a Mesh Into Smaller Pieces
Hi everyone, is it possible to cut a mesh into smaller pieces in Unity? I want to make a mesh cut up script but I'm not even sure if it's possible.
What I am trying to accomplish is cutting a sphere in half, to make a closed dome shape, if that's possible in any way ;3
Answer by ExpiredIndexCard · May 05, 2013 at 04:50 AM
Yes it is possible. Pretty much anything you can imagine in programming you can create. Let your imagination run wild!!!!!! POWER OF PROGRAMMING ;)
to the Power of Program$$anonymous$$g =]
@DangerousBeans : what do you know about meshes? Have you made a mesh? Have you manipulated a mesh? What is your understanding of vertices, uvs, triangles, normals and tangents? What information have you come across in your searching before asking your question?
Here is the story of the mesh in Unity from the very beginning : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$esh.html
This is a sincere and serious comment =]
Yes I have made meshes outside of Unity in Blender. When I went searching for cutting meshes I barely found anything and they weren't that helpful.
Ok just need to establish a base line. But still, you need to expand on your question further. How are you wanting to split this mesh? By finding and separating groups of triangles that are touching each other? By the player using a laser to cut the mesh in their own design?
I want a large cube to fall apart into small random chunks as time passes.I'm not sure how I would seperate one single cube into smaller pieces and do it randomly though.
How is this the most up-voted answer? It gives no concrete information or guidance.
Answer by Loius · May 05, 2013 at 05:51 AM
Yes.
There are some assets on the Asset Store that cut meshes. A couple of them only duplicate the mesh and flatten the vertices, which may not work out for you if it's a complex mesh. There are one or two that slice objects and create new faces, and there's exactly one that I know of that can cut an animated mesh, although it seems to be slightly limited in the angle of its cuts. I'm working on one that can slice an animated mesh but it's bonkers hard to do it from scratch.
Here's one: http://forum.unity3d.com/threads/149008-Turbo-Slicer Here's the animated mesh cutter: http://u3d.as/content/noble-muffins-llc-/limb-hacker/497
I would prefer to make my own script.Could you show me how to get started? I want a large square to fall apart into smaller random chunks over time. So first thing I ask myself would be how do I seperate one large mesh into smaller meshes?From there I'm stumped.
Sure!
Start with a cube or sphere, they're simple and 'predictable' shapes.
var basev : Vector3[] = mesh.vertices;
var baseu : Vector2[] = mesh.uv;
var baset : int[] = mesh.triangles;
var side : boolean[] = new boolean[ basev.length ];
var pos : CutData = new CutData(mesh); // custom class that just copies the mesh data like above
var neg : CutData = new CutData(mesh);
Copy out the vertices, uv, and triangles of your original mesh into arrays for you to use (for speed and safety).
You're going to end up with two pieces after a cut, so arbitrarily decide that one side will be 'pos' and the other 'neg'. Assign each vertex to a side - I cut with a plane, so I used:
for ( vix = basev.length-1; vix>=0; vix-- ) {
basev[vix] = meshTransform.TransformPoint(basev[vix]);
side[vix] = plane.GetSide((basev[vix]));
}
Now, slicing a mesh means you have to cut triangles in half. Each triangle will be cut into a "left" side and a "right" side (again, arbitrary names for convenience). Every triangle is cut in exactly the same way - a trapezoid (two triangles) on the 'left' and a single triangle on the 'right'. This requires two new vertices for both sides of the mesh (pos and neg).
But which triangles need to be split? Any triangle that is not completely on one side of the cut:
for ( trix = baset.length-3; trix>=0; trix-=3 ) {
lonePoint = -1;
// Detect a split triangle - whole triangles are all on one side
if ( side[baset[trix+1]] != side[baset[trix+2]] || side[ baset[trix+1] ] != side[ baset[trix] ] || side[ baset[trix+2] ] != side[ baset[trix] ] ) {
if ( side[baset[trix]]==side[baset[trix+1]] ) lonePoint = 2;
else if ( side[baset[trix+1]]==side[baset[trix+2]] ) lonePoint = 0;
else lonePoint = 1;
}
/////////////////////////////////////////
if ( lonePoint >= 0 ) { // Triangle was split by cut
To keep triangle winding working properly, you have to record things in the right order. This is where I"m struggling at the moment so, grain of salt and all that.
Find the right point along the triangle's edge to split it. Add or re-use an added vertex at that point. Record the edge defined by the two "new-edge-vertices" this triangle is using - you'll need it to add in the missing faces later. After you've split all the triangles you need to, you have to rebuild the 'edge loop' you created, and use that/those loops to build the revealed interior faces (Triangulator on the Unity wiki seems very appropriate for this, though you'll have to flatten the edge loop to 2D)
At the end you'll need to clear all the positive-side vertices from Neg and vice versa, then reassign the Pos mesh to the original object and create a new object to assign Neg to.
Never$$anonymous$$d, I figured out that cut$$anonymous$$esh was a meshfilter.
I'm making a C# version of the bits you have given me. I need help with the variables. The console is giving me this error
a field initializer cannot reference the nonstatic field, method or property. Should I make them static or put them in a function?
public $$anonymous$$esh mesh = GetComponent<$$anonymous$$eshFilter>().mesh;
public Vector3[] basev = mesh.vertices;
public Vector2[] baseh = mesh.uv;
public bool[] side = new bool[basev.Length];
In the function that does the cutting.
cut$$anonymous$$esh (and original$$anonymous$$esh) are $$anonymous$$eshes.
Answer by stasclick · Jan 19, 2018 at 06:37 AM
Here is a simple asset that slice it very fast: Mesh Slicer
watch video: youtube