返回介绍

Mesh 网格

发布于 2019-12-18 15:38:01 字数 13214 浏览 1152 评论 0 收藏 0

class in UnityEngine / Inherits from: Object

Description 描述

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.新建一个网格,应该按照这个顺序来做:
1)为顶点数组赋值
2)为三角形数组赋值

JavaScript:

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;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : 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;
    }
}

2. Modifying vertex attributes every frame: 1) get vertices, 2) modify them, 3) assign them back to the mesh.

2. 每帧修改顶点属性:
1)获取顶点数组
2)修改它们
3)赋回网格

示例:

JavaScript:

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;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : 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.

3. 连续的改变网格的三角形数组值和顶点值
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函数后,给顶点赋值,再给三角形数组赋值,以确保没有超出数组的边界。

示例:

JavaScript:

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;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : 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;
    }
}

Variables 变量

bindposesThe bind poses. The bind pose at each index refers to the bone with the same index.
绑定姿势。每个索引绑定姿势引用的是具有相同索引的骨骼。
blendShapeCountReturns BlendShape count on this mesh.
返回该网格上的BlendShape数。
boneWeightsThe bone weights of each vertex.
每个顶点的骨骼权重。
boundsThe bounding volume of the mesh.
网格的边界体。
colorsVertex colors of the mesh.
网格的顶点颜色。
colors32Vertex colors of the mesh.
网格的顶点颜色。
isReadableReturns state of the Read/Write Enabled checkbox when model was imported.
返回模型导入设置的“Read/Write Enabled”复选框状态。
normalsThe normals of the mesh.
网格的法线。
subMeshCountThe number of submeshes. Every material has a separate triangle list.
子网格数,每材质有单独的三角形列表。
tangentsThe tangents of the mesh.
网格的切线。
trianglesAn array containing all triangles in the mesh.
网格包含的所有三角形的数组。
uvThe base texture coordinates of the mesh.
网格的基本纹理坐标。
uv2The second texture coordinate set of the mesh, if present.
网格的第二套纹理坐标,如果存在的话。
uv3The third texture coordinate set of the mesh, if present.
网格的第三套纹理坐标,如果存在的话。
uv4The fourth texture coordinate set of the mesh, if present.
网格的第四套纹理坐标,如果存在的话。
vertexCountReturns the number of vertices in the mesh (Read Only).
返回网格的顶点数量(只读)。
verticesReturns a copy of the vertex positions or assigns a new vertex positions array.
返回顶点位置的拷贝或指定新顶点位置的数组。

Constructors 构造器

MeshCreates an empty mesh.
创建一个空网格。

Public Functions 共有函数

AddBlendShapeFrameAdds a new blend shape frame.
添加新的混合图形帧。
ClearClears all vertex data and all triangle indices.
清空所有顶点数据和三角形索引,你应该在重建三角形数组前使用这个函数。
ClearBlendShapesClears all blend shapes from Mesh.
从网格清除所有混合图形。
CombineMeshesCombines several meshes into this mesh.
合并几个网格到该网格。
GetBlendShapeFrameCountReturns the frame count for a blend shape.
返回混合图形的帧数。
GetBlendShapeFrameVerticesRetreives deltaVertices, deltaNormals and deltaTangents of a blend shape frame.
获取混合图形帧的deltaVertices、deltaNormals和deltaTangents。
GetBlendShapeFrameWeightReturns the weight of a blend shape frame.
返回混合图形帧的权重。
GetBlendShapeIndexReturns index of BlendShape by given name.
通过给定名称返回混合图形的索引。
GetBlendShapeNameReturns name of BlendShape by given index.
通过给定索引返回混合图形的名称。
GetIndicesReturns the index buffer for the submesh.
返回子网格的索引。
GetTopologyGets the topology of a submesh.
获取子网格的拓扑。
GetTrianglesReturns the triangle list for the submesh.
返回子网格的三角形列表。
GetUVsGet the UVs for a given chanel.
获取给定chanel索引的UV。
MarkDynamicOptimize mesh for frequent updates.
优化频繁更新的网格。
OptimizeOptimizes the mesh for display.
优化用于显示的网格。
RecalculateBoundsRecalculate the bounding volume of the mesh from the vertices.
从顶点重新计算从网格包围体。
RecalculateNormalsRecalculates the normals of the mesh from the triangles and vertices.
重新计算网格的法线。
SetColorsVertex colors of the mesh.
网格的顶点颜色。
SetIndicesSets the index buffer for the submesh.
设置网格的索引。
SetNormalsSet the normals of the mesh.
设置网格的法线。
SetTangentsSet the tangents of the mesh.
设置网格的切线。
SetTrianglesSets the triangle list for the submesh.
设置子网格的三角形列表。
SetUVsSet the UVs for a given chanel.
用给定通道设置UV。
SetVerticesAssigns a new vertex positions array.
指定新的顶点位置数组。
UploadMeshDataUpload previously done mesh modifications to the graphics API.
上传之前完成的网格修改到显卡API。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文