|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEditor; |
| 5 | + |
| 6 | +namespace nwTools |
| 7 | +{ |
| 8 | + |
| 9 | +public class BundleKeyframe |
| 10 | +{ |
| 11 | + public Vector3 pos = new Vector3(0, 0, 0); |
| 12 | + public Vector3 scale = new Vector3(1, 1, 1); |
| 13 | + public Quaternion rot = Quaternion.identity; |
| 14 | + public float time = 0.0f; |
| 15 | +} |
| 16 | + |
| 17 | +public class BundleAnimationClip |
| 18 | +{ |
| 19 | + public string name; |
| 20 | + public Dictionary<string, List<BundleKeyframe>> keyframes = new Dictionary<string, List<BundleKeyframe>>(); |
| 21 | +} |
| 22 | + |
| 23 | +public class BundleAnimation : BundleComponent |
| 24 | +{ |
| 25 | + override public void Preprocess() |
| 26 | + { |
| 27 | + // m_LocalScale.x - z |
| 28 | + // m_LocalPosition.x - z |
| 29 | + // m_LocalRotation.x - w |
| 30 | + |
| 31 | + // for now copy the keys, we could evaluate and maybe should |
| 32 | + // as Unity keys have in/out tangents |
| 33 | + unityAnim = unityComponent as AnimationHelper; |
| 34 | + |
| 35 | + // no way to get clips like this, going to need a custom component |
| 36 | + // for now, support one animation |
| 37 | + //for (int i = 0; i < unityAnim.GetClipCount(); i++) |
| 38 | + //{ |
| 39 | + // var clip = unityAnim.clips[i]; |
| 40 | + //} |
| 41 | + |
| 42 | + for ( int i=0; i<unityAnim.animationClips.Length; i++ ) |
| 43 | + { |
| 44 | + AddClip( unityAnim.animationClips[i] ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + void AddClip( AnimationClip clip ) |
| 49 | + { |
| 50 | + BundleAnimationClip aclip = new BundleAnimationClip(); |
| 51 | + aclip.name = clip.name; |
| 52 | + clips.Add( aclip ); |
| 53 | + |
| 54 | + AnimationClipCurveData[] curveData = AnimationUtility.GetAllCurves(clip, true); |
| 55 | + Dictionary<string, List<AnimationClipCurveData>> animdata = new Dictionary<string, List<AnimationClipCurveData>>(); |
| 56 | + for ( int i=0; i<curveData.Length; i++ ) |
| 57 | + { |
| 58 | + AnimationClipCurveData cd = curveData[i]; |
| 59 | + List<AnimationClipCurveData> nodedata; |
| 60 | + if ( !animdata.TryGetValue(cd.path, out nodedata) ) |
| 61 | + nodedata = animdata[cd.path] = new List<AnimationClipCurveData>(); |
| 62 | + nodedata.Add( cd ); |
| 63 | + } |
| 64 | + |
| 65 | + foreach ( KeyValuePair<string, List<AnimationClipCurveData>> entry in animdata ) |
| 66 | + { |
| 67 | + var boneName = entry.Key; |
| 68 | + var keyframes = aclip.keyframes[boneName] = new List<BundleKeyframe>(); |
| 69 | + |
| 70 | + float maxTime = 0; |
| 71 | + foreach ( AnimationClipCurveData cd in entry.Value ) |
| 72 | + { |
| 73 | + var curve = cd.curve; |
| 74 | + if ( curve.keys.Length==0 ) |
| 75 | + continue; |
| 76 | + if ( curve.keys[curve.keys.Length-1].time>maxTime ) |
| 77 | + maxTime = curve.keys[curve.keys.Length-1].time; |
| 78 | + } |
| 79 | + |
| 80 | + Vector3 pos = new Vector3(0, 0, 0); |
| 81 | + Vector3 scale = new Vector3(1, 1, 1); |
| 82 | + Quaternion rot = Quaternion.identity; |
| 83 | + |
| 84 | + Vector3 lastpos = new Vector3(0, 0, 0); |
| 85 | + Vector3 lastscale = new Vector3(1, 1, 1); |
| 86 | + Quaternion lastrot = Quaternion.identity; |
| 87 | + |
| 88 | + for ( float time=0.0f; time<=maxTime; ) |
| 89 | + { |
| 90 | + foreach ( AnimationClipCurveData cd in entry.Value ) |
| 91 | + { |
| 92 | + var curve = cd.curve; |
| 93 | + float value = curve.Evaluate(time); |
| 94 | + if ( cd.propertyName=="m_LocalScale.x" ) |
| 95 | + scale.x = value; |
| 96 | + else if ( cd.propertyName=="m_LocalScale.y" ) |
| 97 | + scale.y = value; |
| 98 | + else if ( cd.propertyName=="m_LocalScale.z" ) |
| 99 | + scale.z = value; |
| 100 | + else if ( cd.propertyName=="m_LocalPosition.x" ) |
| 101 | + pos.x = value; |
| 102 | + else if ( cd.propertyName=="m_LocalPosition.y" ) |
| 103 | + pos.y = value; |
| 104 | + else if ( cd.propertyName=="m_LocalPosition.z" ) |
| 105 | + pos.z = value; |
| 106 | + else if ( cd.propertyName=="m_LocalRotation.x" ) |
| 107 | + rot.x = value; |
| 108 | + else if ( cd.propertyName=="m_LocalRotation.y" ) |
| 109 | + rot.y = value; |
| 110 | + else if ( cd.propertyName=="m_LocalRotation.z" ) |
| 111 | + rot.z = value; |
| 112 | + else if ( cd.propertyName=="m_LocalRotation.w" ) |
| 113 | + rot.w = value; |
| 114 | + else |
| 115 | + Debug.Log("Unknown Animtation: " + cd.propertyName); |
| 116 | + } |
| 117 | + |
| 118 | + bool needFrame = false; |
| 119 | + float t = Math.Abs(lastpos.x - pos.x); |
| 120 | + if ( t<0.01f ) needFrame = true; |
| 121 | + t = Math.Abs(lastpos.y - pos.y); |
| 122 | + if ( t<0.01f ) needFrame = true; |
| 123 | + t = Math.Abs(lastpos.z - pos.z); |
| 124 | + if ( t<0.01f ) needFrame = true; |
| 125 | + t = Math.Abs(lastscale.x - scale.x); |
| 126 | + if ( t<0.01f ) needFrame = true; |
| 127 | + t = Math.Abs(lastscale.y - scale.y); |
| 128 | + if ( t<0.01f ) needFrame = true; |
| 129 | + t = Math.Abs(lastscale.z - scale.z); |
| 130 | + if ( t<0.01f ) needFrame = true; |
| 131 | + t = Math.Abs(lastrot.x - rot.x); |
| 132 | + if ( t<0.01f ) needFrame = true; |
| 133 | + t = Math.Abs(lastrot.y - rot.y); |
| 134 | + if ( t<0.01f ) needFrame = true; |
| 135 | + t = Math.Abs(lastrot.z - rot.z); |
| 136 | + if ( t<0.01f ) needFrame = true; |
| 137 | + t = Math.Abs(lastrot.w - rot.w); |
| 138 | + if ( t<0.01f ) needFrame = true; |
| 139 | + |
| 140 | + if ( needFrame ) |
| 141 | + { |
| 142 | + lastpos = pos; |
| 143 | + lastrot = rot; |
| 144 | + lastscale = scale; |
| 145 | + |
| 146 | + BundleKeyframe keyframe = new BundleKeyframe(); |
| 147 | + keyframe.pos = pos; |
| 148 | + keyframe.rot = rot; |
| 149 | + keyframe.scale = scale; |
| 150 | + keyframe.time = time; |
| 151 | + keyframes.Add( keyframe ); |
| 152 | + } |
| 153 | + time += 0.025f; // FIXME? |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + new public static void Reset() |
| 159 | + { |
| 160 | + } |
| 161 | + |
| 162 | + public override SceneComponent GetObjectData() |
| 163 | + { |
| 164 | + var sceneData = new SceneAnimation(); |
| 165 | + sceneData.type = "Animation"; |
| 166 | + sceneData.clips = new SceneAnimationClip[clips.Count]; |
| 167 | + for ( int i=0; i<clips.Count; i++ ) |
| 168 | + { |
| 169 | + BundleAnimationClip clip = clips[i]; |
| 170 | + var jclip = sceneData.clips[i] = new SceneAnimationClip(); |
| 171 | + jclip.name = clip.name; |
| 172 | + |
| 173 | + int count = 0; |
| 174 | + SceneAnimationNode[] jnodes = jclip.nodes = new SceneAnimationNode[clip.keyframes.Count]; |
| 175 | + foreach ( KeyValuePair<string, List<BundleKeyframe>> entry in clip.keyframes ) |
| 176 | + { |
| 177 | + SceneAnimationNode node = new SceneAnimationNode(); |
| 178 | + node.name = entry.Key; |
| 179 | + node.keyframes = new SceneKeyframe[entry.Value.Count]; |
| 180 | + |
| 181 | + int kcount = 0; |
| 182 | + foreach ( var key in entry.Value ) |
| 183 | + { |
| 184 | + var jkeyframe = new SceneKeyframe(); |
| 185 | + jkeyframe.pos = key.pos; |
| 186 | + jkeyframe.scale = key.scale; |
| 187 | + jkeyframe.rot = key.rot; |
| 188 | + jkeyframe.time = key.time; |
| 189 | + node.keyframes[kcount++] = jkeyframe; |
| 190 | + } |
| 191 | + jnodes[count++] = node; |
| 192 | + } |
| 193 | + } |
| 194 | + return sceneData; |
| 195 | + } |
| 196 | + |
| 197 | + AnimationHelper unityAnim; |
| 198 | + List<BundleAnimationClip> clips = new List<BundleAnimationClip>(); |
| 199 | +} |
| 200 | + |
| 201 | +} |
0 commit comments