Пример #1
0
	static void ParseRotateTimeline (Skeleton skeleton, RotateTimeline timeline, AnimationClip clip) {
		var boneData = skeleton.Data.Bones[timeline.BoneIndex];
		var bone = skeleton.Bones[timeline.BoneIndex];

		AnimationCurve curve = new AnimationCurve();

		float endTime = timeline.Frames[(timeline.FrameCount * 2) - 2];

		float currentTime = timeline.Frames[0];

		List<Keyframe> keys = new List<Keyframe>();

		float rotation = timeline.Frames[1] + boneData.Rotation;

		keys.Add(new Keyframe(timeline.Frames[0], rotation, 0, 0));

		int listIndex = 1;
		int frameIndex = 1;
		int f = 2;
		float[] frames = timeline.Frames;
		skeleton.SetToSetupPose();
		float lastTime = 0;
		float angle = rotation;
		while (currentTime < endTime) {
			int pIndex = listIndex - 1;
			float curveType = timeline.GetCurveType(frameIndex - 1);

			if (curveType == 0) {
				//linear
				Keyframe pk = keys[pIndex];

				float time = frames[f];

				rotation = frames[f + 1] + boneData.Rotation;
				angle += Mathf.DeltaAngle(angle, rotation);
				float r = angle;

				float rOut = (r - pk.value) / (time - pk.time);

				pk.outTangent = rOut;

				keys.Add(new Keyframe(time, r, rOut, 0));

				keys[pIndex] = pk;

				currentTime = time;

				timeline.Apply(skeleton, lastTime, currentTime, null, 1);

				lastTime = time;
				listIndex++;
			} else if (curveType == 1) {
				//stepped

				Keyframe pk = keys[pIndex];

				float time = frames[f];

				rotation = frames[f + 1] + boneData.Rotation;
				angle += Mathf.DeltaAngle(angle, rotation);
				float r = angle;

				float rOut = float.PositiveInfinity;

				pk.outTangent = rOut;

				keys.Add(new Keyframe(time, r, rOut, 0));

				keys[pIndex] = pk;

				currentTime = time;

				timeline.Apply(skeleton, lastTime, currentTime, null, 1);

				lastTime = time;
				listIndex++;
			} else if (curveType == 2) {
				//bezier
				Keyframe pk = keys[pIndex];

				float time = frames[f];

				timeline.Apply(skeleton, lastTime, currentTime, null, 1);
				skeleton.UpdateWorldTransform();

				rotation = frames[f + 1] + boneData.Rotation;
				angle += Mathf.DeltaAngle(angle, rotation);
				float r = angle;

				int steps = Mathf.FloorToInt((time - pk.time) / bakeIncrement);

				for (int i = 1; i <= steps; i++) {
					currentTime += bakeIncrement;
					if (i == steps)
						currentTime = time;

					timeline.Apply(skeleton, lastTime, currentTime, null, 1);
					skeleton.UpdateWorldTransform();
					pk = keys[listIndex - 1];

					rotation = bone.Rotation;
					angle += Mathf.DeltaAngle(angle, rotation);
					r = angle;

					float rOut = (r - pk.value) / (currentTime - pk.time);

					pk.outTangent = rOut;

					keys.Add(new Keyframe(currentTime, r, rOut, 0));

					keys[listIndex - 1] = pk;

					listIndex++;
					lastTime = currentTime;
				}
			}

			frameIndex++;
			f += 2;
		}

		curve = EnsureCurveKeyCount(new AnimationCurve(keys.ToArray()));

		string path = GetPath(boneData);
		string propertyName = "localEulerAnglesBaked";

		EditorCurveBinding xBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".x");
		AnimationUtility.SetEditorCurve(clip, xBind, new AnimationCurve());
		EditorCurveBinding yBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".y");
		AnimationUtility.SetEditorCurve(clip, yBind, new AnimationCurve());
		EditorCurveBinding zBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".z");
		AnimationUtility.SetEditorCurve(clip, zBind, curve);

	}