Beispiel #1
0
    public AnimClipInfo Bake(List <AnimationClip> clipList, List <AnimStates> stateList)
    {
        bakeMesh = new Mesh();
        AnimClipInfo ClipDict = ScriptableObject.CreateInstance <AnimClipInfo>();

        for (int c = 0; c < clipList.Count; c++)
        {
            AnimationClip clip       = clipList[c];
            float         sampleTime = 0f;
            float         frameTime  = 0f;
            int           clipframe  = 0;

            clipframe = Mathf.ClosestPowerOfTwo((int)(clip.frameRate * clip.length));
            frameTime = clip.length / clipframe;

            //Init texture
            bakeGo.GetComponentInChildren <SkinnedMeshRenderer>().BakeMesh(bakeMesh);
            int       width   = Mathf.ClosestPowerOfTwo(bakeMesh.vertexCount);
            Texture2D animTex = new Texture2D(width, clipframe, TextureFormat.RGBAHalf, false);
            animTex.name = bakeGo.name + "_" + clip.name;

            for (int i = 0; i < clipframe; i++)
            {
                clip.SampleAnimation(bakeGo, sampleTime);
                bakeGo.GetComponentInChildren <SkinnedMeshRenderer>().BakeMesh(bakeMesh);
                //TODO set pixel
                for (int j = 0; j < bakeMesh.vertexCount; j++)
                {
                    Vector3 vertex = bakeMesh.vertices[j];
                    animTex.SetPixel(j, i, new Color(vertex.x, vertex.y, vertex.z));
                }

                //Debug.Log(bakeMesh.vertices[0].x);
                sampleTime += frameTime;
            }
            animTex.Apply();
            ClipData clipData = new ClipData()
            {
                texture = animTex,
                length  = clip.length
            };
            ClipDict.clips.Add(stateList[c], clipData);
        }

        return(ClipDict);
    }
Beispiel #2
0
    public void OnPreprocessModel()
    {
        ModelImporter mi = (ModelImporter)assetImporter;

        //修正缩放比
        // mi.globalScale = 0.01f;
        //动作类型
        mi.animationType      = ModelImporterAnimationType.Legacy;
        mi.generateAnimations = ModelImporterGenerateAnimations.GenerateAnimations;

        if (assetPath.Contains("/Models/"))
        {
            //不自动生成材质,有其他工具生成,统一生成管理
            mi.importMaterials = false;
            mi.globalScale     = 1.0f;
        }
        else if (assetPath.Contains("/Effects/zhuanshu"))
        {
            mi.globalScale = 1.0f;
        }
        else if (assetPath.Contains("/UIModel/"))
        {
        }
        else
        {
            return;
        }
        //关闭动作压缩,会导致动作占用空间增大,但是可以避免产生额外的压缩损伤
#if ANIMATION_COMPRESS
        mi.animationCompression = ModelImporterAnimationCompression.KeyframeReduction;
#else
        mi.animationCompression = ModelImporterAnimationCompression.off;
#endif

        #region modify (Author: @XB.Wu)
        if (ModelDataProcess._dicModelData.Count == 0)
        {
            ModelDataProcess.excute();
        }
        #endregion

        //如果是纯动作文件则进行裁剪
        //动作文件命名格式:modelName@anim
        if (assetPath.Contains("@"))
        {
            string[] tempAssetList     = assetPath.Split(new char[] { '@' });
            string   strAssetModelName = tempAssetList[0].Substring(tempAssetList[0].LastIndexOf('/') + 1);
            if (assetPath.Contains("/Models/") && !_listModels.Contains(strAssetModelName))
            {
                _listModels.Add(strAssetModelName);
            }

            ModelData pModelData = ModelDataProcess.getModelDataByIdx(strAssetModelName);
            if (pModelData == null)
            {
                //EditorUtility.DisplayDialog("Animation Clip", "No Clip config is found in the AnimationClipConfig.ini, and Check the config and make sure its available", "Ok");
                Debug.LogWarning(strAssetModelName + " Clip config can't be found in the AnimationClipConfig.ini, and Check the config and make sure its available");
                return;
            }

            #region modify (Author: @XB.Wu)
            //bool bInit = pModelData.InitFlag;
            //if (bInit)
            //{
            //    //EditorUtility.DisplayDialog("Animation Clip", "This animation's FBX has been imported already. Please delete the previous asset and try again.", "Ok");
            //    Debug.LogWarning("This animation's FBX has been imported already. Please delete the previous asset and try again.");
            //    return;
            //}
            #endregion

            AnimClipInfo info = null;
            ModelImporterClipAnimation[] animations = new ModelImporterClipAnimation[pModelData._listAnimClips.ToArray().Length];
            for (int i = 0; i < animations.Length; i++)
            {
                info          = pModelData._listAnimClips[i];
                animations[i] = SetClipAnimation(info.name, info.firstFrame, info.lastFrame, info.isloop);
            }

            mi.clipAnimations = animations;
            //pModelData.InitFlag = true;
        }
        else
        {
            string[] tempAssetList     = assetPath.Split(new char[] { '.', 'F', 'B', 'X' });
            int      startIdx          = assetPath.LastIndexOf('/');
            int      endIdx            = assetPath.LastIndexOf(".FBX");
            string   strAssetModelName = assetPath.Substring(startIdx + 1, endIdx - startIdx - 1);
            if (assetPath.Contains("/Models/") && !_listModels.Contains(strAssetModelName))
            {
                _listModels.Add(strAssetModelName);
            }
        }
    }
Beispiel #3
0
    protected override void OnInitialize()
    {
        m_speed           = 1.0;
        m_animationSpeed  = 1.0f;
        m_time            = 0;
        m_normalizedTime  = 0;
        m_currentFrame    = 0;
        m_totalFrame      = 1;
        m_loopCount       = 0;
        m_acceptKeys      = 0;
        m_acceptInput     = true;
        m_waitToResetKey  = false;
        m_waitToDefault   = false;
        m_waitToLayDown   = false;
        m_waitToLoop      = false;
        m_ended           = false;
        m_preventNextTurn = false;
        m_rimLight        = true;
        m_holdRimCount    = 0;

        name             = info.state;
        stateNameHash    = info.hash;
        m_ID             = info.ID;
        m_stateLevel     = info.level;
        m_stateDamageMul = info.damageMul;
        m_skill          = -1;
        m_level          = -1;
        m_skillDamageMul = 0;
        m_loopCount      = 0;
        m_length         = 1;
        m_floatLength    = 0.001f;
        frameCount       = 1;
        loop             = false;

        m_acceptKeys = 0;

        isProtect = m_ID == STATE_PROTECT;
        isIdle    = m_ID == STATE_IDLE || m_ID == STATE_STAND;
        isRun     = m_ID == STATE_RUN;
        isDie     = m_ID == STATE_DIE;
        isVictory = m_ID == STATE_VICTORY;
        isLaydown = m_ID == STATE_LAYDOWN;

        isExecution = info.execution;
        isUltimate  = info.ultimate;

        off     = parent is StateMachineInfoOff;
        passive = parent is StateMachineInfoPassive;

        ignoreElementTrigger = info.ignoreElementTrigger;

        m_showOffWeapon = info.showOffweapon || off;

        noDefaultTrans = info.noDefaultTrans;

        lastExitTime = -info.coolDown;

        m_bindInfo = off ? stateMachine.info.offWeaponBinds : passive ? stateMachine.info.passiveWeaponBinds : stateMachine.info.defaultWeaponBinds;

        AnimClipInfo c = null;

        if (stateMachine.creature.gender == 0)
        {
            var an = info.animation.Replace("_nan_", "_nv_");
            c = ConfigManager.Find <AnimClipInfo>(a => a.name == an);
        }

        if (!c)
        {
            c = ConfigManager.Find <AnimClipInfo>(a => a.name == info.animation);
        }
        if (c)
        {
            frameCount    = c.frameCount;
            loop          = c.loop;
            m_length      = frameCount * 33;
            m_floatLength = m_length * 0.001f;
        }

        ResetMotion();

#if UNITY_EDITOR
        EventManager.AddEventListener("EditorReloadConfig", OnEditorReloadConfig);
#endif
    }
Beispiel #4
0
    public static ModelImporterClipAnimation[] SpliteClips(string path, ModelImporterClipAnimation refClip)
    {
        string takeName = "Take 001";

        if (refClip != null)
        {
            takeName = refClip.takeName;
        }

        List <AnimClipInfo> clipInfo = new List <AnimClipInfo>();
        XmlDocument         xml      = null;

        if (File.Exists(path))
        {
            xml = new XmlDocument();
            xml.Load(path);
        }

        if (xml == null)
        {
            Debug.LogError("xml文件不存在");
            return(null);
        }

        XmlNodeList nodeList = xml.SelectSingleNode("character").ChildNodes;

        foreach (XmlElement xe in nodeList)
        {
            int enabled = 0;
            int start   = -1;
            int end     = -1;
            int loop    = 0;
            int.TryParse(xe.GetAttribute("enable"), out enabled);
            int.TryParse(xe.GetAttribute("start_frame"), out start);
            int.TryParse(xe.GetAttribute("end_frame"), out end);
            int.TryParse(xe.GetAttribute("loop"), out loop);

            if (enabled != 0 && start >= 0 && end > 0)
            {
                AnimClipInfo clip = new AnimClipInfo();
                clip.name       = xe.GetAttribute("name");
                clip.name       = clip.name.ToLower();
                clip.name       = clip.name.Trim();
                clip.loop       = (loop == 1);
                clip.startFrame = start;
                clip.endFrame   = end;
                clipInfo.Add(clip);
            }
        }

        ModelImporterClipAnimation[] newClips = new ModelImporterClipAnimation[clipInfo.Count];
        for (int i = 0; i < clipInfo.Count; ++i)
        {
            newClips[i]          = new ModelImporterClipAnimation();
            newClips[i].takeName = takeName;
            newClips[i].name     = clipInfo[i].name;
            newClips[i].loopTime = clipInfo[i].loop;
            newClips [i].keepOriginalPositionY   = true;
            newClips [i].keepOriginalPositionXZ  = true;
            newClips [i].keepOriginalOrientation = true;
            newClips[i].firstFrame = clipInfo[i].startFrame;
            newClips[i].lastFrame  = clipInfo[i].endFrame;
        }
        return(newClips);
    }
Beispiel #5
0
    private void OnGUI()
    {
        _serializedObject.Update();

        GUILayout.Space(10);

        GUIStyle labelStyle = new GUIStyle();

        labelStyle.fontSize         = 15;
        labelStyle.fontStyle        = FontStyle.BoldAndItalic;
        labelStyle.normal.textColor = Color.gray;
        labelStyle.alignment        = TextAnchor.UpperCenter;
        GUILayout.Label("AnimationClip to bake", labelStyle);
        GUILayout.Space(10);

        EditorGUI.BeginChangeCheck();

        currentGameobject = (GameObject)EditorGUILayout.ObjectField("Current Bake Object", currentGameobject, typeof(GameObject), true);

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PropertyField(_assetListProperty, true);
        EditorGUILayout.PropertyField(_assetStateProperty, true);
        EditorGUILayout.EndHorizontal();

        if (EditorGUI.EndChangeCheck())
        {
            _serializedObject.ApplyModifiedProperties();

            if (baker == null)
            {
                baker = new ZBaker();
            }

            IList <AnimationClip> clips = baker.TryGetDataFromGameObject(currentGameobject);
            foreach (var clip in clips)
            {
                if (!_assetList.Contains(clip))
                {
                    _assetList.Add(clip);
                }
            }

            //sync 2 list
            while (_assetState.Count < _assetList.Count)
            {
                _assetState.Add(AnimStates.idle);
            }
            while (_assetState.Count > _assetList.Count)
            {
                _assetState.RemoveAt(_assetState.Count - 1);
            }

            myWindow.ShowNotification(new GUIContent("Data Inited"));
        }

        GUILayout.Space(10);
        GUILayout.Label("Path to Save", labelStyle);
        EditorGUILayout.LabelField(string.Format("Save at :  {0}", Path.Combine(path, subPath)));
        EditorGUILayout.BeginHorizontal();
        path    = EditorGUILayout.TextField(path);
        subPath = EditorGUILayout.TextField(subPath);
        EditorGUILayout.EndHorizontal();

        GUILayout.Space(10);
        if (GUILayout.Button("Bake"))
        {
            if (_assetList.Count == 0)
            {
                EditorUtility.DisplayDialog("Warning", "List is Empty", "OK");
                return;
            }


            AnimClipInfo ClipDict = baker.Bake(_assetList, _assetState);
            if (!System.IO.Directory.Exists(Path.Combine("Assets/", path)))
            {
                System.IO.Directory.CreateDirectory(Path.Combine("Assets/", path));
            }

            foreach (var data in ClipDict.clips)
            {
                AssetDatabase.CreateAsset(data.Value.texture, Path.Combine("Assets/", Path.Combine(path, data.Key.ToString() + ".asset")));
            }

            AssetDatabase.CreateAsset(ClipDict, Path.Combine("Assets/", Path.Combine(path, subPath + ".asset")));


            myWindow.ShowNotification(new GUIContent("Bake Complete"));
        }
    }