您的位置:首页 > 移动开发 > Unity3D

Unity实现一个morpher/blendShape

2015-12-03 10:34 543 查看
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,0,weights.Length);
GetComponent<MeshFilter> ().mesh = meshs [0];
}

public void SetWeight(int argIndex,float argWeight)
{
weights [argIndex] = (argWeight > 1 ? 1: 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[0].vertices.Length];
System.Array.Copy (meshs [0].vertices, vertices, vertices.Length);

for (int w=1;w<weights.Length;w++)
{
if (weights[w] <= 0) continue;

Vector3[] verticesT = meshs[w].vertices;

int i = 0;
while (i < vertices.Length)
{
vertices[i] = vertices[i] + (verticesT[i] -  meshs[0].vertices[i]) * weights[w];
i++;
}
}
mesh.vertices = vertices;
isChange = false;
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: