Unity实现一个morpher/blendShape

时间:2021-02-12 20:19:07
using UnityEngine;
using System.Collections; [RequireComponent (typeof (MeshFilter))]
public class BlendShape : MonoBehaviour
{
public Mesh[] meshs;
#if UNITY_EDITOR
public float[] weights;
#else
float[] weights;
#endif bool isChange = false;
void Start ()
{
weights = new float[meshs.Length];
System.Array.Clear (weights,,weights.Length);
GetComponent<MeshFilter> ().mesh = meshs [];
} public void SetWeight(int argIndex,float argWeight)
{
weights [argIndex] = (argWeight > ? : argWeight );
isChange = true;
} void Update()
{
UpdateMesh ();
} void UpdateMesh()
{
#if UNITY_EDITOR
#else
if (!isChange) return;
#endif Mesh mesh = GetComponent<MeshFilter>().mesh; Vector3[] vertices = new Vector3[meshs[].vertices.Length];
System.Array.Copy (meshs [].vertices, vertices, vertices.Length); for (int w=;w<weights.Length;w++)
{
if (weights[w] <= ) continue; Vector3[] verticesT = meshs[w].vertices; int i = ;
while (i < vertices.Length)
{
vertices[i] = vertices[i] + (verticesT[i] - meshs[].vertices[i]) * weights[w];
i++;
}
}
mesh.vertices = vertices;
isChange = false;
}
}