public void Init(SkinnedMeshRenderer skinnedMesh, MorphAnimationData data, MorphAnimator animator)
        {
            _skinnedMeshRenderer = skinnedMesh;
            _morphAnimationData  = data;
            _morphAnimator       = animator;

            _currentClip       = null;
            _currentKeyframe   = null;
            _currentBone       = null;
            _currentBoneParent = null;
            _keepingDistance   = 0;

            _isRecord                   = false;
            _isPreview                  = false;
            _previewIndex               = 0;
            _previewLocation            = 0;
            _isRenameClip               = false;
            _newNameClip                = "";
            _keyframeView               = Vector2.zero;
            _clipSizeWidth              = 150;
            _clipSizeHeight             = 30;
            _moveCenter                 = Vector2.zero;
            _keyframePropertyViewWidth  = 0;
            _keyframePropertyViewHeight = 0;
            _keyframePropertyView       = Vector2.zero;
            _boneNameFiltrate           = "";
            _clipPropertyViewWidth      = 0;
            _clipPropertyViewHeight     = 0;

            ReviewClips();
        }
 private void OnEnable()
 {
     _meshFilter          = target as MeshFilter;
     _meshRenderer        = _meshFilter.transform.GetComponent <MeshRenderer>();
     _skinnedMeshRenderer = _meshFilter.transform.GetComponent <SkinnedMeshRenderer>();
     _meshCollider        = _meshFilter.transform.GetComponent <MeshCollider>();
     _morphAnimationData  = _meshFilter.transform.GetComponent <MorphAnimationData>();
 }
 public MorphAnimationKeyframe(MorphAnimationData data)
 {
     Positions     = data.BindposesPosition.ToArray();
     Rotations     = data.BindposesRotation.ToArray();
     Scales        = data.BindposesScale.ToArray();
     EventCallBack = new MorphCallBack();
     Time          = 1f;
 }
예제 #4
0
 public static MorphVertex GetVertexByIndex(this MorphAnimationData morph, int index)
 {
     for (int i = 0; i < morph.Vertexs.Count; i++)
     {
         if (morph.Vertexs[i].VertexIndexs.Contains(index))
         {
             return(morph.Vertexs[i]);
         }
     }
     return(null);
 }
예제 #5
0
        public static MorphVertex GetVertexByClick(this MorphAnimationData morph, MorphTriangle triangle, Vector3 clickPoint)
        {
            float distance1 = Vector3.Distance(morph.Vertexs[triangle.Vertex1].Vertex, clickPoint);
            float distance2 = Vector3.Distance(morph.Vertexs[triangle.Vertex2].Vertex, clickPoint);
            float distance3 = Vector3.Distance(morph.Vertexs[triangle.Vertex3].Vertex, clickPoint);

            if (distance1 < distance2 && distance1 < distance3)
            {
                return(morph.Vertexs[triangle.Vertex1]);
            }
            if (distance2 < distance1 && distance2 < distance3)
            {
                return(morph.Vertexs[triangle.Vertex2]);
            }
            if (distance3 < distance1 && distance3 < distance2)
            {
                return(morph.Vertexs[triangle.Vertex3]);
            }
            return(morph.Vertexs[triangle.Vertex1]);
        }
        private void GenerateMorphController()
        {
            if (!_meshFilter.sharedMesh)
            {
                MorphDebug.LogError("MeshFilter组件丢失了Mesh数据!", _meshFilter.gameObject);
                return;
            }
            if (!_meshRenderer)
            {
                MorphDebug.LogError("GameObject丢失了组件MeshRenderer!", _meshFilter.gameObject);
                return;
            }

            string path = EditorUtility.SaveFilePanel("Save Morph Mesh", Application.dataPath, _meshFilter.sharedMesh.name + "(Morph)", "asset");

            if (path.Length != 0)
            {
                Collider[] cols = _meshFilter.GetComponents <Collider>();
                for (int i = 0; i < cols.Length; i++)
                {
                    cols[i].enabled = false;
                }

                string subPath = path.Substring(0, path.IndexOf("Asset"));
                path = path.Replace(subPath, "");
                Mesh mesh = Instantiate(_meshFilter.sharedMesh);
                AssetDatabase.CreateAsset(mesh, path);
                AssetDatabase.SaveAssets();

                Mesh meshAsset = AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)) as Mesh;

                //生成蒙皮网格组件,并创建根骨骼
                if (!_skinnedMeshRenderer)
                {
                    _skinnedMeshRenderer = _meshFilter.transform.gameObject.AddComponent <SkinnedMeshRenderer>();
                }
                _skinnedMeshRenderer.sharedMesh      = meshAsset;
                _skinnedMeshRenderer.rootBone        = _meshFilter.transform;
                _skinnedMeshRenderer.sharedMaterials = _meshRenderer.sharedMaterials;
                _skinnedMeshRenderer.enabled         = true;

                GameObject boneRoot = new GameObject("BoneRoot");
                boneRoot.hideFlags = HideFlags.HideInHierarchy;
                MorphBone mb = boneRoot.AddComponent <MorphBone>();
                mb.hideFlags = HideFlags.HideInInspector;
                Transform[] bones     = new Transform[1];
                Matrix4x4[] bindposes = new Matrix4x4[1];
                bones[0] = boneRoot.transform;
                bones[0].SetParent(_skinnedMeshRenderer.rootBone);
                bones[0].localPosition     = Vector3.zero;
                bones[0].localRotation     = Quaternion.identity;
                bindposes[0]               = bones[0].worldToLocalMatrix * _skinnedMeshRenderer.transform.localToWorldMatrix;
                _skinnedMeshRenderer.bones = bones;
                _skinnedMeshRenderer.sharedMesh.bindposes = bindposes;

                //生成网格碰撞器
                if (!_meshCollider)
                {
                    _meshCollider = _meshFilter.transform.gameObject.AddComponent <MeshCollider>();
                }
                _meshCollider.sharedMesh = meshAsset;
                _meshCollider.enabled    = true;

                //生成变形动画数据组件
                if (!_morphAnimationData)
                {
                    _morphAnimationData = _meshFilter.transform.gameObject.AddComponent <MorphAnimationData>();
                }
                _morphAnimationData.Identity  = true;
                _morphAnimationData.hideFlags = HideFlags.HideInInspector;
                _morphAnimationData.Vertexs.Clear();
                _morphAnimationData.Triangles.Clear();
                //处理顶点
                List <int> repetitionVertices = new List <int>();
                for (int i = 0; i < meshAsset.vertices.Length; i++)
                {
                    EditorUtility.DisplayProgressBar("Please wait", "Dispose vertices(" + i + "/" + meshAsset.vertices.Length + ")......", 1.0f / meshAsset.vertices.Length * i);

                    if (repetitionVertices.Contains(i))
                    {
                        continue;
                    }

                    List <int> verticesGroup = new List <int>();
                    verticesGroup.Add(i);

                    for (int j = i + 1; j < meshAsset.vertices.Length; j++)
                    {
                        if (meshAsset.vertices[i] == meshAsset.vertices[j])
                        {
                            verticesGroup.Add(j);
                            repetitionVertices.Add(j);
                        }
                    }
                    _morphAnimationData.Vertexs.Add(new MorphVertex(_meshFilter.transform.localToWorldMatrix.MultiplyPoint3x4(meshAsset.vertices[i]), verticesGroup));
                }
                //处理三角面
                List <int> allTriangles = new List <int>(meshAsset.triangles);
                for (int i = 0; (i + 2) < allTriangles.Count; i += 3)
                {
                    EditorUtility.DisplayProgressBar("Please wait", "Dispose triangles(" + i + "/" + allTriangles.Count + ")......", 1.0f / allTriangles.Count * i);

                    int           mv1 = _morphAnimationData.GetVertexIndexByIndex(allTriangles[i]);
                    int           mv2 = _morphAnimationData.GetVertexIndexByIndex(allTriangles[i + 1]);
                    int           mv3 = _morphAnimationData.GetVertexIndexByIndex(allTriangles[i + 2]);
                    MorphTriangle mt  = new MorphTriangle(mv1, mv2, mv3);
                    _morphAnimationData.Triangles.Add(mt);
                }
                _morphAnimationData.BindposesPosition.Add(boneRoot.transform.localPosition);
                _morphAnimationData.BindposesRotation.Add(boneRoot.transform.localRotation);
                _morphAnimationData.BindposesScale.Add(boneRoot.transform.localScale);

                EditorUtility.ClearProgressBar();

                DestroyImmediate(_meshFilter);
                DestroyImmediate(_meshRenderer);

                _skinnedMeshRenderer.transform.parent = null;
            }
        }