How to manipulate the Shape of a 3D object at runtime

I am attempting to create a simple Clay modeling app for Kindergartners. What I would like is to allow the player to Dent and Stretch a piece of Clay by touching any area of the clay (ex: touching an area of the clay should show a visible dent in the clay and stretch the clay as a result). How would I go about doing this? I am new to Unity and am unable to find information for this kind of behavior. Would appreciate if someone could point me in the right direction or suggest some methods that I could take a look at.

The geometry of a GameObject is held within it’s Mesh; it contains useful data such as triangles and vertex arrays which can be manipulated in order to alter the GameObject’s shape. It’s pretty easy to stuff up a mesh if you don’t know what you’re doing with it, so here’s a brief explanation of how Unity handles it’s vertices contained within a Mesh.

Firstly, Unity draws GameObject’s exclusively using triangles. A cube, for instance, would be made up of six sides, with each side consisting of two triangles put together to make a square. One way of storing vertex data for a shape like this would be to keep it in a single array. To Draw from this array, Unity could iterate through it three vertices at a time, rendering out each triplet of vertices in the order that they appear. The problem with this approach comes into play when you consider most Meshes use each vertex more than once, meaning that this would require a lot of duplicate vertices.

The other, more clever way of storing vertex data is to have two arrays; one that stores vertex data and another that stores the triangles. This triangles array stores integers that represent the index of the intended vertex inside the vertex array. It is this approach that Unity uses.

So to circle back to the original question; how to manipulate the shape of a 3d object at runtime, you need to set mesh.vertices and mesh.triangles to alter the shape of a GameObject. Keep in mind that both these properties return a copy of the original data. To alter them, you need to store a copy of the original data as a variable, make any changes you wish to the copy and then set the property to the copy using the = operator.

Here’s a link to Unity’s script reference of the Mesh class. You’ll also need to look over uv’s because they’re required to draw a Mesh properly. Hopefully this gives you something to work with in terms of learning how to manipulate a GameObject’s Mesh. Good luck with your Clay Modelling App!