Exemplo n.º 1
0
    public static SkeletonAnimationData[] exportAnimation(SkinnedMeshRenderer renderer, GameObject gameObject)
    {
        var root  = SkeletonGetRootBone(renderer.rootBone, gameObject);
        var bones = renderer.bones;

        // renderer.sharedMesh.


        Dictionary <string, CurveData> boneObj = new Dictionary <string, CurveData>();

        foramtCurveData(boneObj, root, "");

        Animator           aniamtor   = gameObject.GetComponentInChildren <Animator>();
        AnimatorController controller = aniamtor.runtimeAnimatorController as AnimatorController;

        SkeletonAnimationData[] animDatas = null;
        if (controller)
        {
            AnimationClip[] clips = controller.animationClips;
            animDatas = new SkeletonAnimationData[clips.Length];

            for (int i = 0; i < clips.Length; i++)
            {
                animDatas[i] = clipToAnimationData(clips[i], boneObj);
            }
        }

        return(animDatas);
    }
Exemplo n.º 2
0
    public static SkeletonAnimationData clipToAnimationData(AnimationClip clip, Dictionary <string, CurveData> boneObj)
    {
        var frameCount = (int)Math.Floor(clip.length * clip.frameRate) + 1;
        var curveBinds = AnimationUtility.GetCurveBindings(clip);

        foreach (var item in curveBinds)
        {
            if (item.type == typeof(Transform))
            {
                var path = item.path;
                var i    = path.LastIndexOf("/");
                if (i != -1)
                {
                    path = path.Substring(i + 1);
                }

                CurveData data = boneObj[path];

                var curve = AnimationUtility.GetEditorCurve(clip, item);

                switch (item.propertyName)
                {
                case "m_LocalPosition.x":
                    data.pos_curve[0] = curve;
                    break;

                case "m_LocalPosition.y":
                    data.pos_curve[1] = curve;
                    break;

                case "m_LocalPosition.z":
                    data.pos_curve[2] = curve;
                    break;

                case "m_LocalRotation.x":
                    data.qua_curve[0] = curve;
                    break;

                case "m_LocalRotation.y":
                    data.qua_curve[1] = curve;
                    break;

                case "m_LocalRotation.z":
                    data.qua_curve[2] = curve;
                    break;

                case "m_LocalRotation.w":
                    data.qua_curve[3] = curve;
                    break;

                case "m_LocalScale.x":
                    data.sca_curve[0] = curve;
                    break;

                case "m_LocalScale.y":
                    data.sca_curve[1] = curve;
                    break;

                case "m_LocalScale.z":
                    data.sca_curve[2] = curve;
                    break;
                }
            }
        }



        SkeletonAnimationData animData = new SkeletonAnimationData();

        animData.name       = Export.getAnimationName(clip);
        animData.duration   = clip.length;
        animData.totalFrame = frameCount;
        animData.eDuration  = 1 / clip.frameRate;

        animData.frames = new Dictionary <string, byte[]>();


        foreach (var item in boneObj)
        {
            var   curve = item.Value;
            var   name  = curve.bone.name;
            Bytes bytes = new Bytes();
            for (int i = 0; i < frameCount; i++)
            {
                float     now    = i / clip.frameRate;
                Matrix4x4 matrix = curve.toMatrix(now);
                bytes.write(Matrix4x4ToBytes(matrix));
            }
            animData.frames.Add(name, bytes.ToArray());
            bytes.Dispose();
        }

        return(animData);
    }
Exemplo n.º 3
0
    public PrefabElement exportMesh(GameObject gameObject)
    {
        Mesh         mesh         = null;
        Material     material     = null;
        MeshData     meshData     = null;
        SkeletonData skeletonData = null;

        SkeletonAnimationData[] animDatas = null;
        GameObject skinObject             = null;

        PrefabElement element = null;
        string        path    = null;



        var render = gameObject.GetComponentInChildren <MeshRenderer>();

        if (render)
        {
            //EditorGUILayout.ObjectField(render.gameObject, typeof(MeshRenderer), false);
            mesh       = render.GetComponent <MeshFilter>().mesh;
            material   = render.material;
            skinObject = render.gameObject;

            path = AssetDatabase.GetAssetPath(mesh);
            if (exportedObj.ContainsKey(path))
            {
                return(exportedObj[path]);
            }
        }
        else
        {
            var skinRender = gameObject.GetComponentInChildren <SkinnedMeshRenderer>();
            if (skinRender)
            {
                //EditorGUILayout.ObjectField(skinRender.gameObject, typeof(SkinnedMeshRenderer), false);
                mesh       = skinRender.sharedMesh;
                material   = skinRender.sharedMaterial;
                skinObject = skinRender.gameObject;

                path = AssetDatabase.GetAssetPath(mesh);

                if (exportedObj.ContainsKey(path))
                {
                    return(exportedObj[path]);
                }

                skeletonData = Export.exportSkeleton(skinRender, gameObject);

                var animator = gameObject.GetComponentInChildren <Animator>();
                var avatar   = animator.avatar;
                var bones    = skinRender.bones;
                var rootBone = skinRender.rootBone;

                animDatas = Export.exportAnimation(skinRender, gameObject);



                // var controller = animator.runtimeAnimatorController as AnimatorController;
                // var layer = controller.layers[0] as AnimatorControllerLayer;
                // var clips=controller.animationClips;
            }
        }

        if (mesh)
        {
            Debug.Log("parse " + gameObject.name);


            meshData = Export.exportMeshData(mesh, skinObject);

            ByteArray byteArray = null;

            SkeletonMeshData data = new SkeletonMeshData();
            data.mesh         = meshData;
            data.skeletonData = skeletonData;

            string root = "E:/project/h5games/data/";


            string refName  = path.Replace("Assets/", "");
            int    dotIndex = refName.IndexOf(".");
            refName = refName.Substring(0, dotIndex) + "/";

            string outpath = root + refName;


            if (null != animDatas)
            {
                data.anims = new string[animDatas.Length];
                for (int i = 0; i < animDatas.Length; i++)
                {
                    SkeletonAnimationData animData = animDatas[i];
                    byteArray = new ByteArray();
                    byteArray.WriteObject(animData);
                    byteArray.Compress();
                    AmfHelper.saveByteArray(byteArray, root + animData.name);
                    data.anims[i] = animData.name;
                }
            }

            if (null != material)
            {
                data.material = Export.ExportMaterial(material);
                string diffpath = data.material.diffTex.url;
                File.Copy(diffpath, outpath + "diff.png", true);
            }


            byteArray = new ByteArray();
            byteArray.WriteObject(data);
            byteArray.Compress();
            AmfHelper.saveByteArray(byteArray, outpath + "mesh.km");

            //element
            element          = new PrefabElement();
            element.name     = path;
            element.refName  = refName;
            element.meshData = data;
            exportedObj.Add(path, element);
        }


        // return Path.GetFileName(path).Replace(Path.GetExtension(path),"");

        return(element);
    }