天天看點

Mesh頂點動畫,旋轉

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DingDianDongHua : MonoBehaviour {

    float twist;
    float inputSensitiv=1.5f;


    Mesh twistMesh;

    private Vector3[] baseVertex;
    private Vector3[] baseNormals;

    

	// Use this for initialization
	void Start () {
       

    }
	
    public void GetVertexAndNormals()
    {
        twistMesh = GetComponent<MeshFilter>().mesh;
        baseVertex = twistMesh.vertices;
        baseNormals = twistMesh.normals;
    }


    public void ChangeMesh()
    {
        
        GetVertexAndNormals();
        Vector3[] vectors = new Vector3[baseVertex.Length];
        Vector3[] normals = new Vector3[baseNormals.Length];
        for(int i=0;i< baseVertex.Length; i++)
        {
            vectors[i] = DoTwist(baseVertex[i], ChangT(baseVertex[i]));
            normals[i] = DoTwist(baseNormals[i],ChangT(baseVertex[i])); 
        }

        twistMesh.vertices = vectors;
        twistMesh.normals = normals;

        //每次改變後,送給GPU渲染
        twistMesh.RecalculateBounds();
        twistMesh.RecalculateNormals();

    }

    //彈簧扭曲的變化的公式
    private Vector3 DoTwist(Vector3 pos,float t)
    {
        float st = Mathf.Sin(t);
        float ct = Mathf.Cos(t);
        Vector3 result = Vector3.zero;
        result.x = pos.x * ct - pos.z * st;
        result.y = pos.y;
        result.z = pos.x * st + pos.z * ct;
        return result;


    }
    //改變的數值
    public float ChangT(Vector3 pos)
    {
        twist = Input.GetAxis("Horizontal") * inputSensitiv * Time.deltaTime;
        return pos.y * twist;
        
    }

	// Update is called once per frame
	void Update () {
        ChangeMesh();

    }
}
           

 morph動畫

//動畫定點融合,可以做表情動畫




Vector3 Function(Vector3 x,Vector3 y,float a){

Vector3 result=a*x+(1-a)y;

return result;

}
           
U3D