Editing 3D Mesh

New to Unity

I’m trying to create a simple script to control and edit an imported 3D object. Something similar to the example in the link, but less…knotty.

Example: Cinema 4D Exploding Sphere - YouTube

I’ve started attempting something using flat plains with primitive objects, attempting to create a procedural effect cloning Cube objects (Building a wall - Unity Answers). I’ve tried to break this down into steps. I try to explain this in my other post, this is just one part. I’m pretty much working from Unity script references and forum pages for support.

So I guess my question is:

Can anyone help explain how editing 3D meshes using scripts in Unity works? I’ve been reviewing the procedural examples, and related forum discussions but I don’t think I’m doing a good job of understanding this.

Reference: Unity - Scripting API: Mesh

It took me a bit of looking around to find a good tutorial, but I eventually found that Unity itself has a great tutorial with a full simple example here that got me going pretty quickly. I’ve always created meshes from script instead of editing them as it seems easier to keep track of them but a mesh is basically made up of vertices, triangles, and uvs.


First of all if you’re creating a new mesh you’ll have to add a Mesh Filter and a Mesh Renderer component to the GameObject you’re creating it on, if you’re editing a mesh these will probably already be there. I’ll show code for creating a simple quad, a square surface which will ignore z for simplicity’s sake.

function Start() {

	var mf : MeshFilter = GetComponent(MeshFilter);     //Stores the Mesh Filter component in a variable
	var mesh : Mesh = new Mesh();     //Creates a new blank Mesh for us to work with

Vertices are points in space and in Unity are Vector3’s with their x, y, and z coordinates. A mesh holds an array of all its vertices, a Vector3.

	var vertices : Vector3[] = new Vector3[4]     //Creates a new blank vertex array that can hold up to 4 vertices

	vertices[0] = new Vector3(0, 0, 0);     //Creates the first vertex at the lower left corner of the square
	vertices[1] = new Vector3(1, 0, 0);     //Creates the second vertex at the lower right corner of the square
	vertices[2] = new Vector3(1, 1, 0);     //Third vertex at upper right corner
	vertices[1] = new Vector3(0, 1, 0);     //Fourth Vertex at upper left corner

Triangles make up the surfaces of the mesh by connecting vertices and are made up of an array of vertex index’s, but in sets of three that make up the actual triangles in an int.

	var triangles : int[] = new int[6]     //Creates a new blank triangle array that can hold up to six vertices, which make up two triangles

	triangles[0] = 0     //Defines a vertex by it's index (vertices[0]) from before for the start point on the first triangle
	triangles[1] = 1     //Defines the vertex vertices[1] as the second point on the triangle
	triangles[2] = 2     //Defines the vertex vertices[2] as the final point on the triangle

	triangles[3] = 0     //We're on a new set of three, so this is a new triangle, the second one, with the start point at vertex vertices[0]
	triangles[4] = 2     //Defines the vertex vertices[2] as the second point
	triangles[5] = 3     //Defines the vertex vertices[3] as the final point

UVs will be the last thing you’ll need, and I’m not sure if you’ll really need to edit them as much when editing already created meshes. Vertices define points in space and triangles connect those points into surfaces which will give you an object. UVs are used to put textures onto the surfaces of your object. I’d look up UV mapping as it’s less intuitive than vertices or triangles.

Basically you’ll have a single texture file that actually holds multiple textures for your object, and UV is a coordinate system for that 2D texture. You have the u coordinate which is basically x and you have the v coordinate which is basically y. The maximum u or v is 1, and the minimum u or v is 0, everything in between is a decimal between 0 and 1. We’ll use only a fourth of our texture on this quad as an example.

	var uvs : Vector2[] = new Vector2[4];

	uvs[0] = new Vector2(0, 0);     //Sets the lower left corner of the texture (u = 0, v = 0) at vertex vertices[0]
	uvs[1] = new Vector2(0, 0.5);     //Sets the upper left corner of the texture at vertex vertices[1]
	uvs[2] = new Vector2(0.5, 0.5);     //Upper right corner to vertices[2]
	uvs[3] = new Vector2(0.5, 0);     //Lower right corner to vertices[3]

Now that that’s all done you can load your new arrays into the mesh and the mesh filter.

	mesh.vertices = vertices;     //Loads your new vertices into the mesh
	mesh.triangles = triangles;     //Loads your new triangles into the mesh
	mesh.uv = uv;     //Loads your new uv mapping into the mesh

	mesh.RecalculateNormals();     //Normals are used for lighting, you can define them yourself but it's much easier to let Unity do it for you

	mf.mesh = mesh;     //Loads the your new custom mesh into the mesh filter, finishing everything up

}

Mesh generation and editing can be overwhelming at first, but once you get the hang of it it actually becomes fairly easy, at least with simple meshes.

Ask any questions about any of it and I’ll be happy to help.

This is an old thread but @Cains 's project has really helped me. Seeing it has been uploaded to Mediafire for ages I think it would be a good idea to post the Cube script here. Attach it to a Cube and run it.

using UnityEngine;
using System.Collections;

public class Cube : MonoBehaviour {

	MeshFilter mf;
	Mesh mesh;
	
	public float speed;

	void Start () {
		mf = GetComponent<MeshFilter>();     //Grab the Mesh Filter
	}
	
	void Update () {
		if(Input.GetMouseButton(0)) {     //If the Left Mouse Button is down
			
				mesh = mf.mesh;     //Grab the current mesh from the Mesh Filter
				Mesh newMesh = new Mesh();     //Create a blank mesh to buil
				Vector3[] newVertices = mesh.vertices;     //Create an array of vertices for our new mesh and load in the vertices from the original mesh
				for(int i = 0;i < mesh.vertices.Length;i++) {     //Loop through every vertex
					newVertices <em>+= new Vector3(Input.GetAxis("Mouse X") * newVertices_.x * speed,0.0f,0.0f);     //Add (The mouse axis * the original vertex to maintain the sign and tell whether the vertex should go negative or positive on the x-axis * a speed value) to each vertex's x coordinate_</em>

* }*
* newMesh.vertices = newVertices; //Load in our new vertices*
* newMesh.triangles = mesh.triangles; //We don’t need to change anything other than vertices, so the rest can be the same as the original mesh*
* newMesh.uv = mesh.uv;*
* newMesh.normals = mesh.normals;*
* mf.mesh = newMesh; //Finalize the mesh by loading it into the Mesh Filter*

* }*
* }*

* void OnGUI() {*
* GUI.Label(new Rect(0,0,Screen.width,Screen.height),“Left mouse button to extrude on x-axis
Right mouse button to rotate”);*

* }*

}

/*
* Unfortunately, because the default cube mesh in Unity only uses 8 total vertices
* instead of 24 (4 separate vertices for each face) it would be very hard to extrude
* the mesh on any axis other than the X axis. This is why this is much easier when you
* create your own mesh from the start as manipulating a mesh really depends on how it
* was made. If you do create your own mesh you want to create separate vertices for
* every surface even if you can re-use a vertex from before, for reasons like this.
*/