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

unity 代码创建控制骨络动画

2017-12-25 15:35 495 查看
上一篇写了代码创建网格,现在这一篇就是骨骼动画。中间跳过了一些 UV 贴图什么的内容 有空再补上。

主要的工作内容就是

1 网络 ,点和面。

2.UV,贴图。 (代码没有 这一块 就用了个默认的材质)

3 骨骼, 骨骼权重 。

4 骨骼 的 动画曲线。

看起来都是美术的工作对不对 。 程序员就是不要搞美术。

using UnityEngine;
using System.Collections;

public class boneWeig : MonoBehaviour {
public  AnimationCurve curve;

public  AnimationClip  clip;
void Start() {
//boneWeigTest ();
cubeAnimation ();
}

void Update () {
}

GameObject cubeAnimation (){
GameObject a = new GameObject ();
a.AddComponent<Animation>();
a.AddComponent<SkinnedMeshRenderer>();
// 蒙皮网络渲染器
SkinnedMeshRenderer rend = a.GetComponent<SkinnedMeshRenderer>();
Animation anim = a.GetComponent<Animation>();
rend.material = new Material(Shader.Find("Diffuse"));
Mesh mesh = new Mesh();
//   做一个长方体的网格  段数设为5   就是
int leng = 5;
Vector3[] ves = new Vector3[leng * 4];
for (int i=0; i<leng; i++) {
ves[i*4]  = new Vector3(1,2*i,-1);
ves[i*4+1]= new Vector3(1,2*i,1);
ves[i*4+2]= new Vector3(-1,2*i,1);
ves[i*4+3]= new Vector3(-1,2*i,-1);
}
mesh.vertices = ves;
//  三角面 数组
int [] tris = new int[3 * 2 * 4 * (leng - 1)+12];
int k = 0;
// 环状面
for(int i=0;i<leng-1;i++){
for(int j=0;j<4;j++){
int base_v = i*4+j;
print (base_v+"    ...");
tris[k]=base_v;
k++;
tris[k]=base_v+4;
k++;
if(j==3){
tris[k]=base_v+1;
}else{
tris[k]=base_v+5;
}
k++;
tris[k]=base_v;
k++;
if(j==3){
tris[k]=base_v+1;
}else{
tris[k]=base_v+5;
}
k++;
if(j==3){
tris[k]=base_v-3;
}else{
tris[k]=base_v+1;
}
k++;
}

}
//  长方体上 下两个面 = 4 个三角面  = 12 长度的顶点数组
tris [k] = 0;
k++;
tris [k] = 1;
k++;
tris [k] = 2;
k++;
tris [k] = 0;
k++;
tris [k] = 2;
k++;
tris [k] = 3;
k++;
int top_base_i =(leng-1)*4;
tris [k] = top_base_i;
k++;
tris [k] = top_base_i+2;
k++;
tris [k] = top_base_i+1;
k++;
tris [k] = top_base_i;
k++;
tris [k] = top_base_i+3;
k++;
tris [k] = top_base_i+2;

mesh.triangles = tris;
mesh.RecalculateNormals ();
rend.sharedMesh = mesh;
//应该先做两个骨头  用来测试
Transform[] bones = new Transform[2];
//   绑定姿势
Matrix4x4[] bindPoses = new Matrix4x4[2];
bones[0] = new GameObject("Lower").transform;
bones[0].parent = a.transform;
bones[0].localRotation = Quaternion.identity;
bones[0].localPosition = Vector3.zero;
bindPoses[0] = bones[0].worldToLocalMatrix * a.transform.localToWorldMatrix;
bones[1] = new GameObject("Upper").transform;
bones[1].parent = a.transform;
bones[1].localRotation = Quaternion.identity;
bones[1].localPosition = new Vector3(0, 6, 0);
bindPoses[1] = bones[1].worldToLocalMatrix * a.transform.localToWorldMatrix;
mesh.bindposes = bindPoses;
BoneWeight[] weights = new BoneWeight[leng*4];
for (int i=0; i<weights.Length; i++) {
if(i<8){
//  下面 2 个环 的顶点  8个点由第一块骨头控制
weights[i].boneIndex0 = 0;
weights[i].weight0 = 1;
}else if(i<12){
//  中线环的顶点  4个点由两个骨头共同控制
weights[i].boneIndex0 = 0;
weights[i].weight0 = 0.5f;

weights[i].boneIndex1 = 1;
weights[i].weight1 = 0.5f;

}else{
//  上面 2 个环 的顶点  8个点由第二块骨头控制
weights[i].boneIndex0 = 1;
weights[i].weight0 = 1;
}
}
mesh.boneWeights = weights;

rend.bones = bones;

curve = new AnimationCurve();

//  2 个4 元数 控制  旋转 变形动画
Quaternion  roZ = Quaternion.identity;
Quaternion rotation = Quaternion.identity;
rotation.eulerAngles = new Vector3 (0, 0, 60f);

clip = new AnimationClip();
AnimationCurve curvex = new AnimationCurve();
curvex.keys = new Keyframe[] {
new Keyframe(0, roZ.x, 0, 0),
new Keyframe(4, rotation.x, 0, 0)
};

AnimationCurve curvey = new AnimationCurve();
curvey.keys = new Keyframe[] {
new Keyframe(0, roZ.y, 0, 0),
new Keyframe(4, rotation.y, 0, 0)
};
AnimationCurve curvez = new AnimationCurve();
curvez.keys = new Keyframe[] {
new Keyframe(0, roZ.z, 0, 0),
new Keyframe(4, rotation.z, 0, 0)
};
AnimationCurve curvew= new AnimationCurve();
curvew.keys = new Keyframe[] {
new Keyframe(0, roZ.w, 0, 0),
new Keyframe(4, rotation.w, 0, 0)
};
// 直线 型  的动画曲线
//AnimationCurve    curvex = AnimationCurve.Linear(0.0f, quatZero.x, 7.0f, quatRotate.x);
//  同里 可设置          localPosition.x      localScale.x

clip.SetCurve("Lower", typeof(Transform), "localRotation.x", curvex);
clip.SetCurve("Lower", typeof(Transform), "localRotation.y", curvey);
clip.SetCurve("Lower", typeof(Transform), "localRotation.z", curvez);
clip.SetCurve("Lower", typeof(Transform), "localRotation.w", curvew);

//      用曲线做 材质颜色 的动画。
//      clip.SetCurve("", typeof(Material), "_Color.g", curve);

clip.legacy = true;
anim.AddClip(clip, "test");
anim.Play("test");
return a;
}
}


运行结果:

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