スクリプトからメッシュの作成や変更をできるようにするクラスです。
Meshes contain vertex data (Positions, Normals, TexCoords etc.) and "face" data,
with faces most often being triangles.
Conceptually, all vertex data is stored in separate arrays of the same size. For example, if you have
a mesh of 100 Vertices, and want to have a position, normal and two texture coordinates
for each vertex, then the mesh should have vertices, normals, uv and uv2
arrays, each being 100 in size. Data for i-th vertex is at index "i" in each array.
For every vertex there can be a vertex position, normal, tangent, color and up to 8 texture coordinates.
Texture coordinates most often are 2D data (Vector2), but it is possible to make them
Vector3 or Vector4 if needed. This is most often used for holding arbitrary data in mesh
vertices, for special effects used in shaders. For skinned meshes, the vertex data can also
contain boneWeights.
The mesh face data, i.e. the triangles it is made of, is simply three vertex indices for each triangle.
For example, if the mesh has 10 triangles, then the triangles array should be 30 numbers,
with each number indicating which vertex to use. The first three elements in the triangles array are
the indices for the vertices that make up that triange; the second three elements make up
another triangle and so on.
Note that while triangle meshes are the most common use case, Unity also supports other
mesh topology types, for example Line or Point meshes. For line meshes, each line
is composed of two vertex indices and so on. See SetIndices and MeshTopology.
Simple vs Advanced Mesh API
The Mesh class has two sets of methods for assigning data to a Mesh from script. The "simple" set of methods provide a basis for setting the indices, triangle, normals, tangents, etc. These methods include validation checks, for example to ensure that you are not passing in data that would include out-of-bounds indices. They represent the standard way to assign Mesh data from script in Unity.
The "simple" methods are: SetColors, SetIndices, SetNormals, SetTangents, SetTriangles, SetUVs, SetVertices, SetBoneWeights
There is also an "advanced" set of methods, which allow you to directly write to the mesh data with control over whether any checks or validation should be performed. These methods are intended for advanced use cases which require maximum performance. They are faster, but allow you to skip the checks on the data you supply. If you use these methods you must make sure that you are not supplying invalid data, because Unity will not check for you.
The "advanced" methods are: SetVertexBufferParams, SetVertexBufferData, SetIndexBufferParams, SetIndexBufferData, SetSubMesh, and you can use the MeshUpdateFlags to control which checks or validation are performed or omitted.
Manipulating meshes from a script
There are three common tasks that might want to use the Mesh API for:
1. Building a mesh from scratch:
should always be done in the following order:
a) Assign vertices
b) Assign triangles.
using UnityEngine;
public class Example : MonoBehaviour { Vector3[] newVertices; Vector2[] newUV; int[] newTriangles;
void Start() { Mesh mesh = new Mesh(); GetComponent<MeshFilter>().mesh = mesh; mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles; } }
2. Modifying vertex attributes every frame:
a) Get vertices
b) Modify them
c) Assign them back to the mesh.
using UnityEngine;
public class Example : MonoBehaviour { void Update() { Mesh mesh = GetComponent<MeshFilter>().mesh; Vector3[] vertices = mesh.vertices; Vector3[] normals = mesh.normals;
for (var i = 0; i < vertices.Length; i++) { vertices[i] += normals[i] * Mathf.Sin(Time.time); }
mesh.vertices = vertices; } }
3. Continously changing the mesh triangles and vertices:
a) Call Clear to start fresh
b) Assign vertices and other attributes
c) 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.
using UnityEngine;
public class ExampleClass : MonoBehaviour { Vector3[] newVertices; Vector2[] newUV; int[] newTriangles;
void Start() { Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
// Do some calculations... mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles; } }
bindposes | バインドポーズ。各インデックスのバインドポーズは同じインデックスのボーンを参照します。 |
blendShapeCount | メッシュの BlendShape 数を返します |
boneWeights | The bone weights for each vertex in the Mesh, up to a maximum of 4. |
bounds | The bounding volume of the Mesh. |
colors | メッシュの頂点の色配列 |
colors32 | メッシュの頂点の色配列 |
indexFormat | Format of the mesh index buffer data. |
isReadable | Returns true if the Mesh is read/write enabled, or false if it is not. |
normals | メッシュの法線 |
subMeshCount | The number of sub-meshes inside the Mesh object. |
tangents | メッシュの接線 |
triangles | メッシュ内のすべての三角形を含む配列 |
uv | Mash のベースとなるテクスチャの座標 |
uv2 | 2 つめのテクスチャの座標 |
uv3 | 存在する場合、メッシュの 3 番目のテクスチャ座標設定 |
uv4 | 存在する場合、メッシュの 4 番目のテクスチャ座標設定 |
uv5 | The fifth texture coordinate set of the mesh, if present. |
uv6 | The sixth texture coordinate set of the mesh, if present. |
uv7 | The seventh texture coordinate set of the mesh, if present. |
uv8 | The eighth texture coordinate set of the mesh, if present. |
vertexAttributeCount | Returns the number of vertex attributes that the mesh has. (Read Only) |
vertexBufferCount | Gets the number of vertex buffers present in the Mesh. (Read Only) |
vertexCount | メッシュの頂点の数 (読み取り専用) を返します。 |
vertices | 頂点の位置や、新しい頂点の位置の配列 |
Mesh | 空のメッシュを作成します |
AddBlendShapeFrame | 新しいブレンドシェイプのフレームを追加します |
Clear | すべての頂点データと三角形のインデックスを削除します |
ClearBlendShapes | メッシュからすべてのブレンドシェイプをクリアします |
CombineMeshes | メッシュに複数のメッシュを組み合わせます |
GetAllBoneWeights | Gets the bone weights for the Mesh. |
GetBaseVertex | Gets the base vertex index of the given sub-mesh. |
GetBindposes | Gets the bind poses of the Mesh. |
GetBlendShapeFrameCount | ブレンドシェイプのフレーム数を返します |
GetBlendShapeFrameVertices | ブレンドシェイプフレームの deltaTangents、deltaTangents、deltaTangents を取得します |
GetBlendShapeFrameWeight | ブレンドシェイプフレームの重みを返します |
GetBlendShapeIndex | 指定インデックスの BlendShape のインデックスを返します。 |
GetBlendShapeName | 指定インデックスの BlendShape 名を返します |
GetBonesPerVertex | The number of non-zero bone weights for each vertex. |
GetBoneWeights | Gets the bone weights for the Mesh. |
GetColors | Gets the vertex colors of the Mesh. |
GetIndexCount | Gets the index count of the given sub-mesh. |
GetIndexStart | Gets the starting index location within the Mesh's index buffer, for the given sub-mesh. |
GetIndices | Fetches the index list for the specified sub-mesh. |
GetNativeIndexBufferPtr | インデックスバッファへのネイティブ (規定のグラフィックス API) ポインターを探します。 |
GetNativeVertexBufferPtr | 頂点バッファを指すネイティブ (基底にあるグラフィックス API) ポインターを捜します。 |
GetNormals | Gets the vertex normals of the Mesh. |
GetSubMesh | Get information about a sub-mesh of the Mesh. |
GetTangents | Gets the tangents of the Mesh. |
GetTopology | Gets the topology of a sub-mesh. |
GetTriangles | Fetches the triangle list for the specified sub-mesh on this object. |
GetUVDistributionMetric | The UV distribution metric can be used to calculate the desired mipmap level based on the position of the camera. |
GetUVs | Gets the UVs of the Mesh. |
GetVertexAttribute | Returns information about a vertex attribute based on its index. |
GetVertexAttributeDimension | Get dimension of a specific vertex data attribute on this Mesh. |
GetVertexAttributeFormat | Get format of a specific vertex data attribute on this Mesh. |
GetVertexAttributes | Get information about vertex attributes of a Mesh. |
GetVertices | Gets the vertex positions of the Mesh. |
HasVertexAttribute | Checks if a specific vertex data attribute exists on this Mesh. |
MarkDynamic | 頻繁な更新を行うためにメッシュを最適化します |
MarkModified | Notify Renderer components of mesh geometry change. |
Optimize | Optimizes the Mesh data to improve rendering performance. |
OptimizeIndexBuffers | Optimizes the geometry of the Mesh to improve rendering performance. |
OptimizeReorderVertexBuffer | Optimizes the vertices of the Mesh to improve rendering performance. |
RecalculateBounds | 頂点からメッシュのバウンディングボリュームを再計算します |
RecalculateNormals | 三角形と頂点からメッシュの法線を再計算します |
RecalculateTangents | Recalculates the tangents of the Mesh from the normals and texture coordinates. |
SetBoneWeights | Sets the bone weights for the Mesh. |
SetColors | Set the per-vertex colors of the Mesh. |
SetIndexBufferData | Sets the data of the index buffer of the Mesh. |
SetIndexBufferParams | Sets the index buffer size and format. |
SetIndices | Sets the index buffer for the sub-mesh. |
SetNormals | メッシュの法線を設定します。 |
SetSubMesh | Sets the information about a sub-mesh of the Mesh. |
SetTangents | メッシュの接線を設定します。 |
SetTriangles | Sets the triangle list for the sub-mesh. |
SetUVs | Sets the UVs of the Mesh. |
SetVertexBufferData | Sets the data of the vertex buffer of the Mesh. |
SetVertexBufferParams | Sets the vertex buffer size and layout. |
SetVertices | 新しい頂点位置の配列を割り当てます |
UploadMeshData | 以前行ったメッシュの変更をグラフィック API へアップロードします。 |
hideFlags | Should the object be hidden, saved with the Scene or modifiable by the user? |
name | オブジェクト名 |
GetInstanceID | オブジェクトのインスタンス ID を返します |
ToString | Returns the name of the object. |
Destroy | Removes a GameObject, component or asset. |
DestroyImmediate | Destroys the object obj immediately. You are strongly recommended to use Destroy instead. |
DontDestroyOnLoad | Do not destroy the target Object when loading a new Scene. |
FindObjectOfType | タイプ type から最初に見つけたアクティブのオブジェクトを返します |
FindObjectsOfType | タイプから見つけたすべてのアクティブのオブジェクト配列を返します |
Instantiate | original のオブジェクトをクローンします |
bool | オブジェクトが存在するかどうか |
operator != | 二つのオブジェクトが異なるオブジェクトを参照しているか比較します |
operator == | 2つのオブジェクト参照が同じオブジェクトを参照しているか比較します。 |