用户工具

站点工具


侧边栏

script:unityengine:classes:mesh:mesh

Mesh 网格

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.
网格的顶点颜色。
script/unityengine/classes/mesh/mesh.txt · 最后更改: 2016/08/02 05:31 (外部编辑)