Esempio n. 1
0
        private void ReadAssets()
        {
            Logger.Info("Read assets...");

            var progressCount = assetsFileList.Sum(x => x.m_Objects.Count);
            int i             = 0;

            Progress.Reset();
            foreach (var assetsFile in assetsFileList)
            {
                foreach (var objectInfo in assetsFile.m_Objects)
                {
                    var objectReader = new ObjectReader(assetsFile.reader, assetsFile, objectInfo);
                    try
                    {
                        Object obj;
                        switch (objectReader.type)
                        {
                        case ClassIDType.Animation:
                            obj = new Animation(objectReader);
                            break;

                        case ClassIDType.AnimationClip:
                            obj = new AnimationClip(objectReader);
                            break;

                        case ClassIDType.Animator:
                            obj = new Animator(objectReader);
                            break;

                        case ClassIDType.AnimatorController:
                            obj = new AnimatorController(objectReader);
                            break;

                        case ClassIDType.AnimatorOverrideController:
                            obj = new AnimatorOverrideController(objectReader);
                            break;

                        case ClassIDType.AssetBundle:
                            obj = new AssetBundle(objectReader);
                            break;

                        case ClassIDType.AudioClip:
                            obj = new AudioClip(objectReader);
                            break;

                        case ClassIDType.Avatar:
                            obj = new Avatar(objectReader);
                            break;

                        case ClassIDType.Font:
                            obj = new Font(objectReader);
                            break;

                        case ClassIDType.GameObject:
                            obj = new GameObject(objectReader);
                            break;

                        case ClassIDType.Material:
                            obj = new Material(objectReader);
                            break;

                        case ClassIDType.Mesh:
                            obj = new Mesh(objectReader);
                            break;

                        case ClassIDType.MeshFilter:
                            obj = new MeshFilter(objectReader);
                            break;

                        case ClassIDType.MeshRenderer:
                            obj = new MeshRenderer(objectReader);
                            break;

                        case ClassIDType.MonoBehaviour:
                            obj = new MonoBehaviour(objectReader);
                            break;

                        case ClassIDType.MonoScript:
                            obj = new MonoScript(objectReader);
                            break;

                        case ClassIDType.MovieTexture:
                            obj = new MovieTexture(objectReader);
                            break;

                        case ClassIDType.PlayerSettings:
                            obj = new PlayerSettings(objectReader);
                            break;

                        case ClassIDType.RectTransform:
                            obj = new RectTransform(objectReader);
                            break;

                        case ClassIDType.Shader:
                            obj = new Shader(objectReader);
                            break;

                        case ClassIDType.SkinnedMeshRenderer:
                            obj = new SkinnedMeshRenderer(objectReader);
                            break;

                        case ClassIDType.Sprite:
                            obj = new Sprite(objectReader);
                            break;

                        case ClassIDType.SpriteAtlas:
                            obj = new SpriteAtlas(objectReader);
                            break;

                        case ClassIDType.TextAsset:
                            obj = new TextAsset(objectReader);
                            break;

                        case ClassIDType.Texture2D:
                            obj = new Texture2D(objectReader);
                            break;

                        case ClassIDType.Transform:
                            obj = new Transform(objectReader);
                            break;

                        case ClassIDType.VideoClip:
                            obj = new VideoClip(objectReader);
                            break;

                        case ClassIDType.ResourceManager:
                            obj = new ResourceManager(objectReader);
                            break;

                        default:
                            obj = new Object(objectReader);
                            break;
                        }
                        assetsFile.AddObject(obj);
                    }
                    catch (Exception e)
                    {
                        var sb = new StringBuilder();
                        sb.AppendLine("Unable to load object")
                        .AppendLine($"Assets {assetsFile.fileName}")
                        .AppendLine($"Type {objectReader.type}")
                        .AppendLine($"PathID {objectInfo.m_PathID}")
                        .Append(e);
                        Logger.Error(sb.ToString());
                    }

                    Progress.Report(++i, progressCount);
                }
            }
        }
Esempio n. 2
0
    private static UnityEngine.AnimationClip ProcessClip(AssetStudio.AnimationClip animationClip, string savePath, string bundleName,
                                                         string materialAttributeName, CoordinateAttributeSet attributeSet, UnityEngine.SkinnedMeshRenderer[] skinnedMeshRenderers)
    {
        var uCurve = new UnityEngine.AnimationCurve();
        var vCurve = new UnityEngine.AnimationCurve();

        var clip                = animationClip.m_MuscleClip.m_Clip;
        var streamedFrames      = clip.m_StreamedClip.ReadData();
        var clipBindingConstant = animationClip.m_ClipBindingConstant;

        for (int frameIndex = 1; frameIndex < streamedFrames.Count - 1; frameIndex++)
        {
            StreamedClip.StreamedCurveKey uCoordKey = null, vCoordKey = null;
            foreach (var key in streamedFrames[frameIndex].keyList)
            {
                var binding = clipBindingConstant.FindBinding(key.index);
                if (binding.attribute == attributeSet.UAttribute)
                {
                    uCoordKey = key;
                }
                else if (binding.attribute == attributeSet.VAttribute)
                {
                    vCoordKey = key;
                }
            }

            if (uCoordKey == null || vCoordKey == null)
            {
                continue;
            }

            // Round to 0.25 steps since the input UVs might not match perfectly
            var roundedUv = new Vector2(Mathf.Round(uCoordKey.value * 4) / 4, Mathf.Round(vCoordKey.value * 4) / 4);
            var value = RtdxTextureTo2x4Mappings[roundedUv];

            uCurve.AddKey(streamedFrames[frameIndex].time, value.x);
            vCurve.AddKey(streamedFrames[frameIndex].time, value.y);
        }

        var newClip = new UnityEngine.AnimationClip {
            frameRate = animationClip.m_SampleRate
        };

        foreach (var skinnedMeshRenderer in skinnedMeshRenderers)
        {
            AnimationUtility.SetEditorCurve(newClip, new EditorCurveBinding
            {
                path         = skinnedMeshRenderer.gameObject.name,
                type         = typeof(UnityEngine.SkinnedMeshRenderer),
                propertyName = $"material.{materialAttributeName}.z"
            }, uCurve);
            AnimationUtility.SetEditorCurve(newClip, new EditorCurveBinding
            {
                path         = skinnedMeshRenderer.gameObject.name,
                type         = typeof(UnityEngine.SkinnedMeshRenderer),
                propertyName = $"material.{materialAttributeName}.w"
            }, vCurve);
        }

        // We need to use this stupid workaround instead of doing it when adding the key because
        // apparently it glitches if the curve wasn't added to an animation
        AnimationHelpers.SetAnimationTangentsToConstant(newClip);

        string animationPath = $"{savePath}/{animationClip.m_Name}.anim";

        AssetDatabase.CreateAsset(newClip, animationPath);
        AssetImporter.GetAtPath(animationPath).assetBundleName = bundleName;

        return(newClip);
    }
Esempio n. 3
0
        private void ConvertAnimations()
        {
            foreach (var assetPreloadData in animationClipHashSet)
            {
                var animationClip = new AnimationClip(assetPreloadData);
                var iAnim         = new ImportedKeyframedAnimation();
                AnimationList.Add(iAnim);
                iAnim.Name      = animationClip.m_Name;
                iAnim.TrackList = new List <ImportedAnimationKeyframedTrack>();
                if (animationClip.m_Legacy)
                {
                    foreach (var m_RotationCurve in animationClip.m_RotationCurves)
                    {
                        var path     = m_RotationCurve.path;
                        var boneName = path.Substring(path.LastIndexOf('/') + 1);
                        var track    = iAnim.FindTrack(boneName);
                        if (track == null)
                        {
                            track      = new ImportedAnimationKeyframedTrack();
                            track.Name = boneName;
                            iAnim.TrackList.Add(track);
                        }
                        foreach (var m_Curve in m_RotationCurve.curve.m_Curve)
                        {
                            var value = Fbx.QuaternionToEuler(new Quaternion(m_Curve.value.X, -m_Curve.value.Y, -m_Curve.value.Z, m_Curve.value.W));
                            track.Rotations.Add(new ImportedKeyframe <Vector3>(m_Curve.time, value));
                        }
                    }
                    foreach (var m_PositionCurve in animationClip.m_PositionCurves)
                    {
                        var path     = m_PositionCurve.path;
                        var boneName = path.Substring(path.LastIndexOf('/') + 1);
                        var track    = iAnim.FindTrack(boneName);
                        if (track == null)
                        {
                            track      = new ImportedAnimationKeyframedTrack();
                            track.Name = boneName;
                            iAnim.TrackList.Add(track);
                        }
                        foreach (var m_Curve in m_PositionCurve.curve.m_Curve)
                        {
                            track.Translations.Add(new ImportedKeyframe <Vector3>(m_Curve.time, new Vector3(-m_Curve.value.X, m_Curve.value.Y, m_Curve.value.Z)));
                        }
                    }
                    foreach (var m_ScaleCurve in animationClip.m_ScaleCurves)
                    {
                        var path     = m_ScaleCurve.path;
                        var boneName = path.Substring(path.LastIndexOf('/') + 1);
                        var track    = iAnim.FindTrack(boneName);
                        if (track == null)
                        {
                            track      = new ImportedAnimationKeyframedTrack();
                            track.Name = boneName;
                            iAnim.TrackList.Add(track);
                        }
                        foreach (var m_Curve in m_ScaleCurve.curve.m_Curve)
                        {
                            track.Scalings.Add(new ImportedKeyframe <Vector3>(m_Curve.time, new Vector3(m_Curve.value.X, m_Curve.value.Y, m_Curve.value.Z)));
                        }
                    }
                }
                else
                {
                    var m_Clip                = animationClip.m_MuscleClip.m_Clip;
                    var streamedFrames        = m_Clip.m_StreamedClip.ReadData();
                    var m_ClipBindingConstant = animationClip.m_ClipBindingConstant;
                    for (int frameIndex = 1; frameIndex < streamedFrames.Count - 1; frameIndex++)
                    {
                        var frame          = streamedFrames[frameIndex];
                        var streamedValues = frame.keyList.Select(x => x.value).ToArray();
                        for (int curveIndex = 0; curveIndex < frame.keyList.Count;)
                        {
                            ReadCurveData(iAnim, m_ClipBindingConstant, frame.keyList[curveIndex].index, frame.time, streamedValues, 0, ref curveIndex);
                        }
                    }
                    var m_DenseClip = m_Clip.m_DenseClip;
                    var streamCount = m_Clip.m_StreamedClip.curveCount;
                    for (int frameIndex = 0; frameIndex < m_DenseClip.m_FrameCount; frameIndex++)
                    {
                        var time        = frameIndex / m_DenseClip.m_SampleRate;
                        var frameOffset = frameIndex * m_DenseClip.m_CurveCount;
                        for (int curveIndex = 0; curveIndex < m_DenseClip.m_CurveCount;)
                        {
                            var index = streamCount + curveIndex;
                            ReadCurveData(iAnim, m_ClipBindingConstant, (int)index, time, m_DenseClip.m_SampleArray, (int)frameOffset, ref curveIndex);
                        }
                    }
                    var m_ConstantClip = m_Clip.m_ConstantClip;
                    var denseCount     = m_Clip.m_DenseClip.m_CurveCount;
                    var time2          = 0.0f;
                    for (int i = 0; i < 2; i++)
                    {
                        for (int curveIndex = 0; curveIndex < m_ConstantClip.data.Length;)
                        {
                            var index = streamCount + denseCount + curveIndex;
                            ReadCurveData(iAnim, m_ClipBindingConstant, (int)index, time2, m_ConstantClip.data, 0, ref curveIndex);
                        }
                        time2 = animationClip.m_MuscleClip.m_StopTime;
                    }
                }

                if ((bool)Properties.Settings.Default["FixRotation"])
                {
                    foreach (var track in iAnim.TrackList)
                    {
                        var prevKey = new Vector3();
                        foreach (var rotation in track.Rotations)
                        {
                            var value = rotation.value;
                            ReplaceOutOfBound(ref prevKey, ref value);
                            prevKey        = value;
                            rotation.value = value;
                        }
                    }
                }
            }
        }