Mesh 网格
Inherits from Object
A class that allows creating or modifying meshes from scripts.
一个允许通过脚本来创建和修改meshes的类.
Meshes contain vertices and multiple triangle arrays. See the Procedural example project for examples of using the mesh interface.
网格(meshes)包括顶点和多个三角形数组。参考Procedural example project中的例子来使用网格的界面。
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.
每个顶点可以有一条法线,两个纹理坐标,及颜色和切线。虽然这些是可选的,但是也可以去掉。所有的顶点信息是被储存在单独的同等规格的数组中,所以如果你的网格(mesh)有10个顶点,你同样应该有大小为10的数组来存储法线和其它属性。
There are probably 3 things you might want to use the modifyable mesh interface for:
大概有3件事情是你想要使用可修改的表格.
1. Building a mesh from scratch: should always be done in the following order: 1) assign vertices 2) assign triangles
新建立一个网格 :应该总是按照这个顺序来做:
1)为顶点数组赋值
2)为三角形数组赋值
示例:
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;
}
}
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;
}
2. Modifying vertex attributes every frame: 1) get vertices, 2) modify them, 3) assign them back to the mesh.
每帧修改定点属性
1)获取顶点数组
2)修改它们
3)把它们放回网格
示例:
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;
}
}
3. Continously changing the mesh triangles and vertices: 1) call Clear to start fresh, 2) assign vertices and other attributes, 3) assign triangle indices.
连续的改变网格的三角形数组值和顶点值
1)使用Clean刷新
2)赋予顶点值和其他属性
3)赋予索引值
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.
调用Clean函数在赋予新的顶点值和三角形索引值之前是非常重要的,Unity总是检查三角形的索引值,判断它们是否超出边界。调用Clear函数后,给顶点赋值,再给三角形数组赋值,以确保没有超出数组的边界。
示例:
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;
}
}
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;
}
Variables变量
-
Returns a copy of the vertex positions or assigns a new vertex positions array.
返回一个顶点位置的数组值,或者为一组新的顶点位置数组赋值。 -
The normals of the mesh.
网格的法线 -
The tangents of the mesh.
网格的切线 -
The base texture coordinates of the mesh.
网格的基础纹理坐标 -
The second texture coordinate set of the mesh, if present.
如果存在,这是为网格设定的第二个纹理坐标。 -
The bounding volume of the mesh.
网格的包围体。 -
Returns the vertex colors of the mesh.
返回网格的顶点颜色。 -
An array containing all triangles in the mesh.
在网格里,一个包含所有三角形的数组。 -
Returns the number of vertices in the mesh (Read Only).
返回网格中顶点的数量(只读的) -
The number of submeshes. Every material has a separate triangle list.
子网格的数量。每种材质都有一个独立的网格列表。
-
The bone weights of each vertex
每个顶点的骨骼权重。 -
The bind poses. The bind pose at each index refers to the bone with the same index.
绑定的姿势。每个索引绑定的姿势使用具有相同索引的骨骼。
Constructors构造器
-
Creates an empty mesh
创建一个空的网格。
Functions函数
-
Clears all vertex data and all triangle indices.
清空所有顶点数据和所有三角形索引。 -
Recalculate the bounding volume of the mesh from the vertices.
重新计算从网格包围体的顶点。 -
Recalculates the normals of the mesh from the triangles and vertices.
重新计算网格的法线 -
Optimizes the mesh for display.
显示优化的网格。 -
Returns the triangle list for the submesh.
返回子网格的三角形列表。 -
Sets the triangle list for the submesh
为子网格设定三角形列表 -
Combines several meshes into this mesh.
组合多个网格到同一个网格
Inherited members继承成员
Inherited Variables继承变量
-
The name of the object. //物体的名字
-
Should the object be hidden, saved with the scene or modifiable by the user?
物体是否被隐藏、保存在场景中或被用户修改?
Inherited Functions继承函数
-
Returns the instance id of the object.
返回物体的实例ID -
Returns the name of the game object.
返回游戏物体的名称。
Inherited Class Functions继承类函数
-
Does the object exist?
物体是否存在? -
Clones the object original and returns the clone.
克隆原始物体,并返回克隆的物体 -
-
Removes a gameobject, component or asset.
删除一个游戏物体、组件或资源 -
Destroys the object obj immediately. It is strongly recommended to use Destroy instead.
立即销毁物体obj,强烈建议使用Destroy代替。 -
Returns a list of all active loaded objects of Type type.
返回Type类型的所有激活的加载的物体列表 -
Returns the first active loaded object of Type type.
返回Type类型第一个激活的加载的物体。 -
Compares if two objects refer to the same
比较如果两个物体相同 -
Compares if two objects refer to a different object
比较如果两个物体不同 -
Makes the object target not be destroyed automatically when loading a new scene.
加载新场景的时候使目标物体不被自动销毁。