StopAnimationMode() private méthode

private StopAnimationMode ( ) : void
Résultat void
Exemple #1
0
        // Main editor window
        public void OnGUI()
        {
            if (AnimationMode.InAnimationMode())
            {
                AnimationMode.StopAnimationMode();
            }

            // Wait for user to select a GameObject
            if (bakeObject == null)
            {
                EditorGUILayout.HelpBox("Please select a GameObject", MessageType.Info);
                return;
            }

            if (buffer == null)
            {
                buffer = new VertaBuffer();
            }

            EditorGUILayout.BeginVertical();
            EditorGUILayout.LabelField("Selected object: " + bakeObject.name);

            modelName = EditorGUILayout.TextField("Model Name", modelName);

            animationClip = EditorGUILayout.ObjectField(animationClip, typeof(AnimationClip), false) as AnimationClip;
            if (animationClip != null)
            {
                frameCount = GetFramesCount(animationClip);
                EditorGUILayout.LabelField("Frames to bake: " + frameCount);

                readyToBake = true;
            }


            if (GUILayout.Button("Bake mesh animations.") && readyToBake && !EditorApplication.isPlaying)
            {
                lockSelection = true;
                BakeMesh();
                lockSelection = false;
            }

            EditorGUILayout.EndVertical();
        }
Exemple #2
0
 public static void StopAnimationMode()
 {
     AnimationMode.StopAnimationMode();
 }
 public static void StopAnimationMode()
 {
     AnimationMode.StopAnimationMode(AnimationMode.DummyDriver());
 }
Exemple #4
0
        private void BakeMesh()
        {
            if (bakeObject == null)
            {
                return;
            }

            if (animationClips.Any(clip => clip == null) && !bakeOnlyMesh)
            {
                return;
            }

            // There is a bug in AnimationMode.SampleAnimationClip which crashes
            // Unity if there is no valid controller attached
            Animator animator = bakeObject.GetComponent <Animator>();

            if (animator != null && animator.runtimeAnimatorController == null && !bakeOnlyMesh)
            {
                return;
            }

            //Collect information about gameObject
            List <MeshFilter>          tmpFilters       = bakeObject.GetComponentsInChildren <MeshFilter>().ToList();
            List <SkinnedMeshRenderer> tmpMeshRenderers = new List <SkinnedMeshRenderer>();

            for (int i = 0; i < tmpFilters.Count; i++)
            {
                MeshFilter          filter       = tmpFilters[i];
                SkinnedMeshRenderer meshRenderer = filter.GetComponent <SkinnedMeshRenderer>();

                if (meshRenderer != null)
                {
                    tmpFilters.RemoveAt(i);
                    tmpMeshRenderers.Add(meshRenderer);
                    i--;
                }
            }

            filters        = tmpFilters.ToArray();
            basicRenderers = filters.Select(filter => filter.GetComponent <MeshRenderer>()).ToArray();
            List <Material> _materials = new List <Material>();

            foreach (MeshRenderer renderer in basicRenderers)
            {
                foreach (Material mat in renderer.sharedMaterials)
                {
                    if (!_materials.Contains(mat))
                    {
                        _materials.Add(mat);
                    }
                }
            }

            materials = _materials.ToArray();
            renderers = tmpMeshRenderers.ToArray();

            //Temporarily set position to zero to make matrix math easier
            Vector3 position = bakeObject.transform.position;

            bakeObject.transform.position = Vector3.zero;

            Mesh firstFrame = new Mesh();

            EditorUtility.DisplayProgressBar("Mesh Animation Baker", "Baking", 0f);

            //Now bake
            AnimationMode.StartAnimationMode();
            AnimationMode.BeginSampling();
            int totalFrame = 0;

            int BakeClip(AnimationClip animationClip)
            {
                int clipFrameCount = bakeOnlyMesh ? 1 : GetFramesCount(animationClip);

                for (int frame = 0; frame < clipFrameCount; frame++)
                {
                    EditorUtility.DisplayProgressBar("Mesh Animation Baker", "Baking mesh animations",
                                                     1f * (totalFrame + frame) / frameCount);

                    if (!bakeOnlyMesh)
                    {
                        AnimationMode.SampleAnimationClip(bakeObject, animationClip, frame / frameRate);
                    }
                    Mesh bakedMesh = CombineMeshes(bakeObject);

                    if (!bufferReady)
                    {
                        buffer.Expand(VertType.VNT, bakedMesh.vertexCount, frameCount);
                        frameStride = buffer.vertexSize * buffer.vertexCount;
                        bufferReady = true;
                    }

                    for (int j = 0; j < bakedMesh.vertexCount; j++)
                    {
                        int vertStart       = j * buffer.vertexSize;
                        int globalVertStart = frameStride * (totalFrame + frame) + vertStart;
                        buffer.data[globalVertStart]     = bakedMesh.vertices[j].x;
                        buffer.data[globalVertStart + 1] = bakedMesh.vertices[j].y;
                        buffer.data[globalVertStart + 2] = bakedMesh.vertices[j].z;

                        buffer.data[globalVertStart + 3] = bakedMesh.normals[j].x;
                        buffer.data[globalVertStart + 4] = bakedMesh.normals[j].y;
                        buffer.data[globalVertStart + 5] = bakedMesh.normals[j].z;

                        buffer.data[globalVertStart + 6] = bakedMesh.tangents[j].x;
                        buffer.data[globalVertStart + 7] = bakedMesh.tangents[j].y;
                        buffer.data[globalVertStart + 8] = bakedMesh.tangents[j].z;
                    }

                    if (totalFrame + frame == 0)
                    {
                        firstFrame = bakedMesh;
                    }
                }

                return(bakeOnlyMesh ? 1 : GetFramesCount(animationClip));
            }

            if (!bakeOnlyMesh)
            {
                foreach (AnimationClip clip in animationClips)
                {
                    totalFrame += BakeClip(clip);
                }
            }
            else
            {
                BakeClip(null);
            }

            //Return to original position
            bakeObject.transform.position = position;

            string filePath = Path.Combine(Application.dataPath, vertaPath);

            FileInfo fileInfo = new FileInfo(filePath);

            if (!Directory.Exists(fileInfo.Directory.FullName))
            {
                Directory.CreateDirectory(fileInfo.Directory.FullName);
            }

            filePath += $"/{modelName}.verta";

            if (!bakeOnlyMesh)
            {
                buffer.SaveToFile(filePath);
            }

            if (targetPathObject == null)
            {
                filePath = Path.Combine("Assets", meshDataPath);
            }
            else
            {
                filePath = AssetDatabase.GetAssetPath(targetPathObject);
            }

            fileInfo = new FileInfo(filePath);
            if (!Directory.Exists(fileInfo.Directory.FullName))
            {
                Directory.CreateDirectory(fileInfo.Directory.FullName);
            }

            if (debugMeshOutput)
            {
                AssetDatabase.CreateAsset(firstFrame, filePath + $"/{modelName}-mesh.asset");
            }

            byte[] bytes = MeshDataAssetEditor.saveMeshToMeshAsset(firstFrame);

            MeshDataAsset asset = new MeshDataAsset();

            asset.bytes     = bytes;
            asset.materials = materials.ToArray();

            AssetDatabase.CreateAsset(asset, filePath + $"/{modelName}.asset");

            EditorUtility.ClearProgressBar();

            AnimationMode.EndSampling();
            AnimationMode.StopAnimationMode();
        }
Exemple #5
0
        // Main editor window
        public void OnGUI()
        {
            if (AnimationMode.InAnimationMode())
            {
                AnimationMode.StopAnimationMode();
            }

            int needSize = 110;

            // Wait for user to select a GameObject
            if (bakeObject == null)
            {
                EditorGUILayout.HelpBox("Please select a GameObject", MessageType.Info);
                return;
            }

            if (buffer == null)
            {
                buffer = new VertaBuffer();
            }

            EditorGUILayout.BeginVertical();
            EditorGUILayout.LabelField("Selected object: " + bakeObject.name);

            modelName = EditorGUILayout.TextField("Model Name", modelName);

            targetPathObject = EditorGUILayout.ObjectField("Output folder", targetPathObject, typeof(Object), true);

            bakeOnlyMesh = EditorGUILayout.Toggle("No animations", bakeOnlyMesh);

            if (!bakeOnlyMesh)
            {
                needSize += 20;
            }

            debugMeshOutput = EditorGUILayout.Toggle("Output debug mesh", debugMeshOutput);


            if (!bakeOnlyMesh)
            {
                int newLen = EditorGUILayout.IntField("Number of clips:", animationClips.Length);
                newLen = Math.Min(newLen, 5);
                if (newLen != animationClips.Length)
                {
                    Array.Resize(ref animationClips, newLen);
                }

                EditorGUILayout.BeginVertical();
                for (int i = 0; i < animationClips.Length; i++)
                {
                    animationClips[i] = EditorGUILayout.ObjectField(animationClips[i], typeof(AnimationClip), false) as AnimationClip;
                    needSize         += 19;
                }
                EditorGUILayout.EndVertical();

                if (animationClips != null && animationClips.Length > 0)
                {
                    int tmpFrameCount = 0;
                    foreach (AnimationClip clip in animationClips)
                    {
                        tmpFrameCount += GetFramesCount(clip);
                    }

                    frameCount = tmpFrameCount;
                    EditorGUILayout.LabelField("Frames to bake: " + frameCount);
                    needSize += 10;
                }
            }
            else
            {
                frameCount = 1;
            }

            minSize = new Vector2(300, needSize);
            maxSize = new Vector2(300, needSize);

            bool clipsReady = animationClips != null && animationClips.Length > 0;

            if (clipsReady)
            {
                clipsReady = animationClips.All(clip => clip != null);
            }

            readyToBake = (clipsReady || bakeOnlyMesh) && !EditorApplication.isPlaying &&
                          !modelName.Equals("");

            if (GUILayout.Button("Bake mesh animations.") && readyToBake)
            {
                lockSelection = true;
                BakeMesh();
                lockSelection = false;
            }

            EditorGUILayout.EndVertical();
        }
Exemple #6
0
        private void BakeMesh()
        {
            if (bakeObject == null)
            {
                return;
            }

            if (animationClip == null)
            {
                return;
            }

            // There is a bug in AnimationMode.SampleAnimationClip which crashes
            // Unity if there is no valid controller attached
            Animator animator = bakeObject.GetComponent <Animator>();

            if (animator != null && animator.runtimeAnimatorController == null)
            {
                return;
            }

            //Collect information about gameObject
            List <MeshFilter>          tmpFilters       = bakeObject.GetComponentsInChildren <MeshFilter>().ToList();
            List <SkinnedMeshRenderer> tmpMeshRenderers = new List <SkinnedMeshRenderer>();

            for (int i = 0; i < tmpFilters.Count; i++)
            {
                MeshFilter          filter       = tmpFilters[i];
                SkinnedMeshRenderer meshRenderer = filter.GetComponent <SkinnedMeshRenderer>();

                if (meshRenderer != null)
                {
                    tmpFilters.RemoveAt(i);
                    tmpMeshRenderers.Add(meshRenderer);
                    i--;
                }
            }

            filters   = tmpFilters.ToArray();
            renderers = tmpMeshRenderers.ToArray();

            Mesh firstFrame = new Mesh();

            EditorUtility.DisplayProgressBar("Mesh Animation Baker", "Baking", 0f);

            //Now bake
            AnimationMode.StartAnimationMode();
            AnimationMode.BeginSampling();

            for (int frame = 0; frame < frameCount; frame++)
            {
                EditorUtility.DisplayProgressBar("Mesh Animation Baker", "Baking mesh animations", 1f * frame / frameCount);

                AnimationMode.SampleAnimationClip(bakeObject, animationClip, frame / frameRate);
                Mesh bakedMesh = CombineMeshes(bakeObject);

                if (!bufferReady)
                {
                    buffer.Expand(VertType.VNT, bakedMesh.vertexCount, frameCount);
                    frameStride = buffer.vertexSize * buffer.vertexCount;
                    bufferReady = true;
                }

                for (int i = 0; i < bakedMesh.vertexCount; i++)
                {
                    int vertStart       = i * buffer.vertexSize;
                    int globalVertStart = frameStride * frame + vertStart;
                    buffer.data[globalVertStart]     = bakedMesh.vertices[i].x;
                    buffer.data[globalVertStart + 1] = bakedMesh.vertices[i].y;
                    buffer.data[globalVertStart + 2] = bakedMesh.vertices[i].z;

                    buffer.data[globalVertStart + 3] = bakedMesh.normals[i].x;
                    buffer.data[globalVertStart + 4] = bakedMesh.normals[i].y;
                    buffer.data[globalVertStart + 5] = bakedMesh.normals[i].z;

                    buffer.data[globalVertStart + 6] = bakedMesh.tangents[i].x;
                    buffer.data[globalVertStart + 7] = bakedMesh.tangents[i].y;
                    buffer.data[globalVertStart + 8] = bakedMesh.tangents[i].z;
                }

                if (frame == 0)
                {
                    firstFrame = bakedMesh;
                }
            }

            string filePath = Path.Combine(Application.dataPath, vertaPath);

            FileInfo fileInfo = new FileInfo(filePath);

            if (!Directory.Exists(fileInfo.Directory.FullName))
            {
                Directory.CreateDirectory(fileInfo.Directory.FullName);
            }

            filePath += $"/{modelName}.verta";

            buffer.SaveToFile(filePath);

            filePath = Path.Combine("Assets", meshDataPath);

            fileInfo = new FileInfo(filePath);
            if (!Directory.Exists(fileInfo.Directory.FullName))
            {
                Directory.CreateDirectory(fileInfo.Directory.FullName);
            }

            filePath += $"/{modelName}.asset";

            byte[] bytes = MeshDataAssetEditor.saveMeshToMeshAsset(firstFrame);

            MeshDataAsset asset = new MeshDataAsset();

            asset.bytes = bytes;

            AssetDatabase.CreateAsset(asset, filePath);

            EditorUtility.ClearProgressBar();

            AnimationMode.EndSampling();
            AnimationMode.StopAnimationMode();
        }