zoukankan      html  css  js  c++  java
  • Unity实现一个morpher/blendShape

    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
  • 相关阅读:
    EOF输入
    2019春总结作业
    2019春第二次课程设计报告
    2019春第一次课程设计实验报告
    2019第一次作业的项目模块结构介绍
    2019春第十二周作业
    2019春第十一周作业
    2019春第十周作业
    2019春第九周作业
    2019春第八周作业
  • 原文地址:https://www.cnblogs.com/sitt/p/5015335.html
Copyright © 2011-2022 走看看