image.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VertexAnimalMatrix : MonoBehaviour {
float tmpFloat;
Mesh myMesh;
float timer;
Vector3[] myList;
void Start () {
myMesh = transform.GetComponent<MeshFilter>().mesh;
//for(int i=0;i< myMesh.vertices.Length; i++)
//{
// myList[i] = myMesh.vertices[i];
//}
}
void Update () {
myList = myMesh.vertices;
tmpFloat = Input.GetAxis("Horizontal");
for (int i = 0; i < myMesh.vertices.Length; i++)
{
//这里旋转的层数不多是因为顶点少
myList[i]= DoTwistX(tmpFloat* myList[i].x*0.1f, myList[i]);
}
myMesh.vertices = myList;
//myMesh.SetVertices(DoTwistX(tmpFloat, gameObject.transform.position))
}
/// <summary>
/// 根据矩阵,围绕X轴旋转
/// </summary>
/// <param name="angle">要旋转的角度</param>
/// <param name="pos">obj的位置</param>
/// <returns></returns>
public Vector3 DoTwistX(float angle,Vector3 pos)
{
float cos = Mathf.Cos(angle);
float sin = Mathf.Sin(angle);
Vector3 result = pos;
result.y = pos.y * cos + pos.z * sin;
result.z = pos.y * (-sin) + pos.z * cos;
return result;
}
/// <summary>
/// 根据矩阵,围绕Y轴旋转
/// </summary>
/// <param name="angle">要旋转的角度</param>
/// <param name="pos">要旋转的obj的位置</param>
/// <returns></returns>
public Vector3 DoTwistY(float angle,Vector3 pos)
{
float cos = Mathf.Cos(angle);
float sin = Mathf.Sin(angle);
Vector3 result = pos;
result.x = pos.x * cos + pos.y * (-sin);
result.z = pos.x * sin + pos.y * cos;
return result;
}
/// <summary>
/// 根据矩阵,围绕Z轴旋转
/// </summary>
/// <param name="angle">要旋转的角度</param>
/// <param name="pos">要旋转的obj的位置</param>
/// <returns></returns>
public Vector3 DoTwistZ(float angle,Vector3 pos)
{
float cos = Mathf.Cos(angle);
float sin = Mathf.Sin(angle);
Vector3 result = pos;
result.x = pos.x * cos + pos.y * sin;
result.y = pos.x * (-sin) + pos.y * cos;
return result;
}
}
网友评论