How To Slice Meshes In Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so i made this badass lightsaber in unity and i wanted to figure out how to use it to slide up for this i had to figure out how unity deals with mesh data according to their documentation most shapes are made up of triangles other topologies are supported but i figured i'd just deal with the common case triangles are drawn by supplying the mesh object with vertex data and then referencing these vertices in groups of three the mesh object also keeps track of normal and uv data for each vertex which helps with calculating lighting and texture coordinate mapping now to slice this triangle i needed a way to tell which vertex was on which side of the slice after digging around a bit i came across planes which unity defines as an infinitely large surface basically it's a giant quad pointing in a direction that i defined with a method telling me if a vertex is on the same side that it's pointing at or not to draw my plane i kept track of the tip and the base of the blade on trigger enter and the tip of the bed on trigger exit computing the cross product of these three points gave me a point perpendicular which i used for the direction of the plane i needed to transform these points to local space as well now for every triangle in the mesh i had to make some decisions the goal was to create two new meshes one mesh for everything on the positive side of the plane and one for everything on the negative side of the plane scenario one all vertices for the current triangle on the same side of the plane i checked which side that was and added all the vertices uv and normal data accordingly scenario 2 vertex 1 and 2 are on the same side an intersection occurred by casting array from vertex 2 to vertex 3 i was able to determine the point at which the ray intersects the plane giving me intersection point 1. then casting array from vertex 3 to vertex 1 gave me my second intersection point during the rare casting process i also interpolated the uv information on side one i created two triangles first from vertex 1 to vertex 2 to intersection 1 then from vertex 1 to intersection 1 and into section 2. on side 2 i added a triangle from intersection point 1 to vertex 3 into section point 2. it was important to keep the winding direction the same so that the triangles were facing the right way i applied similar logic when vertex 1 and 3 were on the same side and when vertex 1 was alone i also made sure to keep track of any intersection points that were added because if the object is solid we can use these to create some triangles and make it look solid what's that i feel empty inside using the points of intersection along the plane i was able to determine a center point that i could use to fill the object with the triangles i feel not empty inside so just add the lightsaber then add the object i want to slice position it correctly and add the script to make it sliceable hit play and watch the magic happen [Music] hit play and watch the magic happen call me luke you can find all the code in the github repo that will be linked in the description of this video but as a quick overview in the code that controls the lightsaber is where i have the ontrigger intend exit methods which help us draw a plane i have a slicer class with a static slice method inside of it that we can call whenever we want to slice an object by passing it a plane and a game object along with some other details about the object we want to slice these details come from a class called sliceable which every sliceable game object needs to have in order to be sliced finally the slices metadata class is the class with all the complex logic in it which takes the information we just spoke about in and splits out two game objects uh one on the positive and one on the negative side of the plane there are some minor issues i've noticed with the second cut specifically which i may or may not fix i think i've learned what i set out to learn and i hope some of you learned something from watching this too i'll catch you on the next one
Info
Channel: Tvtig
Views: 84,823
Rating: undefined out of 5
Keywords: Unity, Cutting Mesh, Mesh Cutting, Mesh Slicing, SLice Mesh In Unity, Unity Slice Mesh
Id: BVCNDUcnE1o
Channel Id: undefined
Length: 4min 43sec (283 seconds)
Published: Sun Aug 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.