- Home /

# Cutting simple plane with a line (~2D)

Hey there!

I want to create a tool that can cut simple meshes (no depth in any of the meshes) in half by dragging a line with the mouse over a mesh.

I was thinking of raycasing on GetKeyUp, from mouseDown position to mouseUp position and vica versa to find the intersection points (5 and 6). And then rebuild the mesh using the new vertices into two meshes using the "normal" of the line to determine which mesh they belong to.

Im only looking for ideas here, some push in the right direction. Im not sure if this is even close to a good method and I'm a bit stuck.

Thanks for any help!

You could also use plane clipping shaders. There is a few threads on them (aka cross-section shaders) on the forum, like here: http://forum.unity3d.com/threads/simple-cross-section-shader.34508/. (This is on cross-section, you don't need co care about the backfaces in your case.) You would need to apply plane clipping shader to your plane material. Then you have to determine the cutting plane for which you need a plane point (which can be any of the clicked points) and normal which is:

`Vector3 clippingPlaneNormal = Vector3.Cross((ClickedPoint1-ClickedPoint2),yourPlaneNormal);`

Then you pass these parameters to the clipping shader.

**Answer** by Bunny83
·
Jan 12, 2013 at 01:04 PM

Well the easiest way is to use the Plane class and it's Raycast function to get the two points on the plane of your mesh. Since you said all vartices lie in one plane you can take the first 3 points of the mesh to build the plane. Keep in mind that mesh vertices are in localspace and you have to use TransformPoint to get them into worldspace.

Of course it's also possible to transform the rays into local space but that's more mind challenging.

Once you have the two click points on your plane, you can use one of the 3d math functions in the wiki to calculate intersections with the border lines.

Keep in mind that a mesh is build up of triangles, so in your example above you have more intersection points ;) two for each triangle.

So i would do it like this:

Get the cutting line with the above mentioned way.

iterate trough all triangles and test each edge for an intersection

if you have two intersections you can split the triangle into 1 triangle and one quad (so 3 triangles)

For edge-cases when a really small triangle would be created you could calculate the area of the triangle you're going to create and drop those which are too small.

I've only made some snapping tool and transformed some vertices before. But I want to boost my mesh programming skills, so will read up on a lot, thanks.

**Answer** by Fattie
·
Jan 12, 2013 at 01:32 PM

For cutting a square...

the weave is:

125

526

564

463

If you have never built mesh before, begin here ! -->

http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Splitting a Mesh into Submeshes 2 Answers

Null Reference Exception Assigning Vertices to Mesh 0 Answers

Voxel mesh generation not working. 1 Answer

Best Way to Link Vertices into Mesh 0 Answers

Grouping vertices? 1 Answer