1、Mesh.Triangles 三角形
网格中,一个包含所有三角形的数组。
顶点可以通过简单的索引同一顶点来共享。如果网格包含多个子网格(材质),三角形列表将包含所有子网格的所有三角形。当你赋值三角形数组,subMeshCount设置为1。如果你想要有多个子网格,使用subMeshCount and SetTriangles。
建议先赋值顶点数组之后再赋值三角形数组,为了避免越界的错误。
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Start() {
gameObject.AddComponent("MeshFilter");
gameObject.AddComponent("MeshRenderer");
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.vertices = new Vector3[] {new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0)};
mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
mesh.triangles = new int[] {0, 1, 2};
}
}
colors[i] = Color.Lerp(Color.red, Color.green, vertices[i].y);
2、Mesh。boneWeight 权重
每个顶点的骨骼权重。
每一个顶点都可以最多收到4个不同骨骼的影响。所有这4个骨骼的权值应该的和为1.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Start() {
gameObject.AddComponent<Animation>();
gameObject.AddComponent<SkinnedMeshRenderer>();
SkinnedMeshRenderer renderer = GetComponent<SkinnedMeshRenderer>();
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] {new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(-1, 5, 0), new Vector3(1, 5, 0)};
mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1)};
mesh.triangles = new int[] {0, 1, 2, 1, 3, 2};
mesh.RecalculateNormals();
renderer.material = new Material(Shader.Find(" Diffuse"));
BoneWeight[] weights = new BoneWeight[4];
weights[0].boneIndex0 = 0;
weights[0].weight0 = 1;
weights[1].boneIndex0 = 0;
weights[1].weight0 = 1;
weights[2].boneIndex0 = 1;
weights[2].weight0 = 1;
weights[3].boneIndex0 = 1;
weights[3].weight0 = 1;
mesh.boneWeights = weights;
Transform[] bones = new Transform[2];
Matrix4x4[] bindPoses = new Matrix4x4[2];
bones[0] = new GameObject("Lower").transform;
bones[0].parent = transform;
bones[0].localRotation = Quaternion.identity;
bones[0].localPosition = Vector3.zero;
bindPoses[0] = bones[0].worldToLocalMatrix * transform.localToWorldMatrix;
bones[1] = new GameObject("Upper").transform;
bones[1].parent = transform;
bones[1].localRotation = Quaternion.identity;
bones[1].localPosition = new Vector3(0, 5, 0);
bindPoses[1] = bones[1].worldToLocalMatrix * transform.localToWorldMatrix;
mesh.bindposes = bindPoses;
renderer.bones = bones;
renderer.sharedMesh = mesh;
AnimationCurve curve = new AnimationCurve();
curve.keys = new Keyframe[] {new Keyframe(0, 0, 0, 0), new Keyframe(1, 3, 0, 0), new Keyframe(2, 0.0F, 0, 0)};
AnimationClip clip = new AnimationClip();
clip.SetCurve("Lower", typeof(Transform), "m_LocalPosition.z", curve);
animation.AddClip(clip, "test");
animation.Play("test");
}
}
3、
Mesh.Optimize 网格优化
public class example : MonoBehaviour {
void Start() {
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Optimize();
}
}