Exemplo n.º 1
0
        // Start is called before the first frame update
        void Start()
        {
            animator = GetComponent <Animator>();
            texAnim  = GetComponent <TextureAnimation>();

            var stateInfo = animator.GetCurrentAnimatorStateInfo(layerIndex);
            var curIndex  = texAnim.GetClipIndex(stateInfo.shortNameHash);

            texAnim.Play(curIndex);
        }
        public int BakeAllClips(GameObject go, List <AnimClipData> clips, Dictionary <int, AnimClipData> states)
        {
            var skin = go.GetComponentInChildren <SkinnedMeshRenderer>();

            var dir = $"{Application.dataPath}/{DEFAULT_TEX_DIR}/";

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            var manifest = ScriptableObject.CreateInstance <AnimTextureManifest>();

            Vector3 tempPos   = skin.transform.position;
            Vector3 tempEuler = go.transform.eulerAngles;
            Vector3 tempScale = skin.transform.lossyScale;

            go.transform.localPosition    = Vector3.zero;
            go.transform.localEulerAngles = Vector3.zero;
            go.transform.localScale       = Vector3.one;

            skin.transform.parent = go.transform;

            skin.transform.localPosition    = Vector3.zero;
            skin.transform.localEulerAngles = Vector3.zero;
            skin.transform.localScale       = Vector3.one;

            var yList = GenerateAtlas(skin, clips, out manifest.posTex, out manifest.normalTex, out manifest.tangentTex);
            var count = BakeClip(go, skin, clips, manifest, yList, states);

            manifest.posTex.Apply();
            manifest.normalTex.Apply();

            //output atlas
            AssetDatabase.CreateAsset(manifest.posTex, $"Assets/{DEFAULT_TEX_DIR}/{go.name}_PosTexture.asset");
            AssetDatabase.CreateAsset(manifest.normalTex, $"Assets/{DEFAULT_TEX_DIR}/{go.name}_NormalTexture.asset");
            AssetDatabase.CreateAsset(manifest.tangentTex, $"Assets/{DEFAULT_TEX_DIR}/{go.name}_TangentTexture.asset");
            //output infos
            AssetDatabase.CreateAsset(manifest, $"Assets/{DEFAULT_TEX_DIR}/{go.name}_{typeof(AnimTextureManifest).Name}.asset");

            AssetDatabase.Refresh();

            GameObject newObj = Instantiate(go);

            newObj.name = "GpuAnim_" + go.name;
            var        skinObj = newObj.GetComponentInChildren <SkinnedMeshRenderer>();
            GameObject gpuMesh = new GameObject(skinObj.name + "_Gpu");

            gpuMesh.transform.parent = newObj.transform;
            DestroyImmediate(skinObj.gameObject);
            MeshFilter filter = gpuMesh.AddComponent <MeshFilter>();

            filter.mesh = skin.sharedMesh;
            gpuMesh.AddComponent <MeshRenderer>();
            TextureAnimation aniTex = gpuMesh.AddComponent <TextureAnimation>();

            aniTex.manifest = manifest;
            newObj.transform.localRotation = generatedPrefab.transform.localRotation;
            newObj.transform.localScale    = generatedPrefab.transform.localScale;

            //GameObject newObj = new GameObject("GpuAnim_" + go.name);
            //MeshFilter filter = newObj.AddComponent<MeshFilter>();
            //filter.mesh = skin.sharedMesh;
            //newObj.AddComponent<MeshRenderer>();
            //TextureAnimation aniTex = newObj.AddComponent<TextureAnimation>();
            //aniTex.manifest = manifest;
            //Animator newAnimator = newObj.AddComponent<Animator>();
            //Animator oldAnimator = go.GetComponent<Animator>();
            //newAnimator.runtimeAnimatorController = oldAnimator.runtimeAnimatorController;
            //newAnimator.updateMode = oldAnimator.updateMode;
            //newAnimator.cullingMode = oldAnimator.cullingMode;
            //newObj.transform.position = tempPos;
            //newObj.transform.eulerAngles = tempEuler;
            //newObj.transform.localScale = tempScale;

            return(count);
        }