Description
A class that allows creating or modifying meshes from scripts.
Meshes contain vertices and multiple triangle arrays.
See the
Procedural example project
for examples of using the mesh interface.
The triangle arrays are simply indices into the vertex arrays; three indices for each triangle.
For every vertex there can be a normal, two texture coordinates, color and tangent.
These are optional though and can be removed at will. All vertex information is stored in separate
arrays of the same size, so if your mesh has 10 vertices, you would also have 10-size arrays
for normals and other attributes.
There are probably 3 things you might want to use the modifyable mesh interface for:
1. Building a mesh from scratch:
should always be done in the following order: 1) assign
vertices 2) assign
triangles.
var newVertices : Vector3[];
var newUV : Vector2[];
var newTriangles : int[];function Start () {
var mesh : Mesh = new Mesh ();
GetComponent(MeshFilter).mesh = mesh;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
void Start() {
Mesh mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
}
import UnityEngine
import System.Collections
public class Example(MonoBehaviour):
public newVertices as (Vector3)
public newUV as (Vector2)
public newTriangles as (int)
def Start() as void:
mesh as Mesh = Mesh()
GetComponent[of MeshFilter]().mesh = mesh
mesh.vertices = newVertices
mesh.uv = newUV
mesh.triangles = newTriangles
2. Modifying vertex attributes every frame:
1) get vertices, 2) modify them, 3) assign them back to the mesh.
function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = mesh.vertices;
var normals : Vector3[] = mesh.normals; for (var i = 0; i < vertices.Length; i++)
vertices[i] += normals[i] * Mathf.Sin(Time.time); mesh.vertices = vertices;
}
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void Update() {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
int i = 0;
while (i < vertices.Length) {
vertices[i] += normals[i] * Mathf.Sin(Time.time);
i++;
}
mesh.vertices = vertices;
}
}
import UnityEngine
import System.Collections
public class Example(MonoBehaviour):
def Update() as void:
mesh as Mesh = GetComponent[of MeshFilter]().mesh
vertices as (Vector3) = mesh.vertices
normals as (Vector3) = mesh.normals
i as int = 0
while i < vertices.Length:
vertices[i] += (normals[i] * Mathf.Sin(Time.time))
i++
mesh.vertices = vertices
3. Continously changing the mesh triangles and vertices:
1) call
Clear to start fresh, 2) assign vertices and other attributes, 3) assign triangle indices.
It is important to call Clear before assigning new vertices or triangles.
Unity always checks the supplied triangle indices whether they don't reference out of bounds vertices.
Calling Clear then assigning vertices then triangles makes sure you never have out of bounds data.
var newVertices : Vector3[];
var newUV : Vector2[];
var newTriangles : int[];function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh; mesh.Clear();
// Do some calculations...
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
void Update() {
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
}
import UnityEngine
import System.Collections
public class Example(MonoBehaviour):
public newVertices as (Vector3)
public newUV as (Vector2)
public newTriangles as (int)
def Update() as void:
mesh as Mesh = GetComponent[of MeshFilter]().mesh
mesh.Clear()
mesh.vertices = newVertices
mesh.uv = newUV
mesh.triangles = newTriangles
bindposes |
The bind poses. The bind pose at each index refers to the bone with the same index. |
boneWeights |
The bone weights of each vertex. |
bounds |
The bounding volume of the mesh. |
colors |
Vertex colors of the mesh. |
colors32 |
Vertex colors of the mesh. |
isReadable |
Returns state of the Read/Write Enabled checkbox when model was imported. |
normals |
The normals of the mesh. |
subMeshCount |
The number of submeshes. Every material has a separate triangle list. |
tangents |
The tangents of the mesh. |
triangles |
An array containing all triangles in the mesh. |
uv |
The base texture coordinates of the mesh. |
uv2 |
The second texture coordinate set of the mesh, if present. |
vertexCount |
Returns the number of vertices in the mesh (Read Only). |
vertices |
Returns a copy of the vertex positions or assigns a new vertex positions array. |