private void ReadStreamedData(ImportedKeyframedAnimation iAnim, AnimationClipBindingConstant m_ClipBindingConstant, float time, StreamedClip.StreamedCurveKey curveKey)
        {
            var binding = m_ClipBindingConstant.FindBinding(curveKey.index);

            GetLive2dPath(binding, out var target, out var boneName);
            if (!string.IsNullOrEmpty(boneName))
            {
                var track = iAnim.FindTrack(boneName);
                track.Target = target;
                track.Curve.Add(new ImportedKeyframe <float>(time, curveKey.value, curveKey.inSlope, curveKey.outSlope, curveKey.coeff));
            }
        }
Ejemplo n.º 2
0
        private void ReadStreamedData(ImportedKeyframedAnimation iAnim, AnimationClipBindingConstant m_ClipBindingConstant, float time, StreamedClip.StreamedCurveKey curveKey)
        {
            var binding = m_ClipBindingConstant.FindBinding(curveKey.index);

            if (binding.path == 0)
            {
                return;
            }

            GetLive2dPath(binding.path, out var target, out var boneName);
            var track = iAnim.FindTrack(boneName);

            track.Target = target;
            track.Curve.Add(new ImportedKeyframe <float>(time, curveKey.value, curveKey.coeff));
        }
Ejemplo n.º 3
0
        private void ReadStreamedData(ImportedKeyframedAnimation iAnim, AnimationClipBindingConstant m_ClipBindingConstant, float time, StreamedClip.StreamedCurveKey curveKey)
        {
            var binding = m_ClipBindingConstant.FindBinding(curveKey.index);

            if (binding.path == 0) // Model binding, path is empty
            {
                var target = "Model";
                var id     = string.Empty;
                if (binding.attribute == 2353026298) // Opacity -> Opacity
                {
                    id = "Opacity";
                }
                else if (binding.attribute == 66473442) // EyeOpening -> EyeBlink
                {
                    id = "EyeBlink";
                }
                else if (binding.attribute == 4109387685) // MouthOpening -> LipSync
                {
                    id = "LipSync";
                }
                var track = iAnim.FindTrack(id);
                track.Target = target;
                track.Curve.Add(new ImportedKeyframe <float>(time, curveKey.value, curveKey.inSlope, curveKey.outSlope));
            }
            else
            {
                GetLive2dPath(binding.path, out var target, out var boneName);
                var track = iAnim.FindTrack(boneName);
                track.Target = target;
                track.Curve.Add(new ImportedKeyframe <float>(time, curveKey.value, curveKey.inSlope, curveKey.outSlope));
            }
        }
Ejemplo n.º 4
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);
    }