// Use this for initialization void Start() { Animations.AnimationSet set = new Animations.AnimationSet(); set.animations = new Animations.RGBAnimation[5]; for (int a = 0; a < set.animations.Length; ++a) { Animations.RGBAnimation anim = new Animations.RGBAnimation(); anim.tracks = new Animations.RGBAnimationTrack[a + 1]; anim.duration = 2500; for (int i = 0; i < anim.tracks.Length; ++i) { var keyframes = new Animations.RGBKeyframe[Animations.Constants.MAX_KEYFRAMES]; for (int j = 0; j < 3; ++j) { keyframes[j].time = (byte)j; keyframes[j].red = (byte)j; keyframes[j].green = (byte)j; keyframes[j].blue = (byte)j; } var track = new Animations.RGBAnimationTrack(); track.keyframes = keyframes; track.count = 3; track.duration = (short)(2000 - 10 * i); track.startTime = (short)(123 + i); track.ledIndex = (byte)(i); anim.tracks[i] = track; } set.animations[a] = anim; var bytes = Animations.RGBAnimation.ToByteArray(anim); Debug.Log(bytes.ToString()); } }
public RGBKeyframe ToRGBKeyframe(EditDataSet editSet, DataSet.AnimationBits bits) { RGBKeyframe ret = new RGBKeyframe(); // Add the color to the palette if not already there, otherwise grab the color index var colorIndex = EditColor.toColorIndex(ref bits.palette, color); ret.setTimeAndColorIndex((ushort)(time * 1000), (ushort)colorIndex); return(ret); }
public bool Equals(RGBKeyframe other) { return(timeAndColor == other.timeAndColor); }
public AnimationSet ToAnimationSet() { AnimationSet set = new AnimationSet(); // Collect all colors used and stuff them in the palette var colors = new Dictionary <Color32, int>(); int index = 0; foreach (var anim in animations) { foreach (var animTrack in anim.tracks) { foreach (var keyframe in animTrack.keyframes) { var color = keyframe.color; if (color.a != 0) { int ignore = 0; if (!colors.TryGetValue(keyframe.color, out ignore)) { colors.Add(keyframe.color, index); index++; } } // else its a special color } } } // Add the colors to the palette set.palette = new byte[colors.Count * 3]; foreach (var ci in colors) { Color32 color = ColorMapping.RemapColor(ci.Key); int i = ci.Value; set.palette[i * 3 + 0] = color.r; set.palette[i * 3 + 1] = color.g; set.palette[i * 3 + 2] = color.b; } int currentTrackOffset = 0; int currentKeyframeOffset = 0; var anims = new List <Animation>(); var tracks = new List <AnimationTrack>(); var rgbTracks = new List <RGBTrack>(); var keyframes = new List <RGBKeyframe>(); // Add animations for (int animIndex = 0; animIndex < animations.Count; ++animIndex) { var editAnim = animations[animIndex]; var anim = new Animation(); anim.duration = (ushort)(editAnim.duration * 1000.0f); anim.tracksOffset = (ushort)currentTrackOffset; anim.trackCount = (ushort)editAnim.tracks.Sum(t => t.ledIndices.Count); anim.animationEvent = (byte)editAnim.@event; anim.specialColorType = (byte)editAnim.@specialColorType; anims.Add(anim); // Now add tracks for (int j = 0; j < editAnim.tracks.Count; ++j) { var editTrack = editAnim.tracks[j]; foreach (var led in editTrack.ledIndices) { var track = new AnimationTrack(); var rgbTrack = new RGBTrack(); rgbTrack.keyframesOffset = (ushort)currentKeyframeOffset; rgbTrack.keyFrameCount = (byte)editTrack.keyframes.Count; track.trackOffset = (ushort)rgbTracks.Count; rgbTracks.Add(rgbTrack); track.ledIndex = (byte)led; // Now add keyframes for (int k = 0; k < editTrack.keyframes.Count; ++k) { var editKeyframe = editTrack.keyframes[k]; int colorIndex = AnimationSet.SPECIAL_COLOR_INDEX; if (editKeyframe.color.a != 0) { colorIndex = colors[editKeyframe.color]; } var keyframe = new RGBKeyframe(); keyframe.setTimeAndColorIndex((ushort)(editKeyframe.time * 1000.0f), (ushort)colorIndex); keyframes.Add(keyframe); } currentKeyframeOffset += editTrack.keyframes.Count; tracks.Add(track); } } currentTrackOffset += editAnim.tracks.Sum(t => t.ledIndices.Count); } set.keyframes = keyframes.ToArray(); set.rgbTracks = rgbTracks.ToArray(); set.tracks = tracks.ToArray(); set.animations = anims.ToArray(); set.Compress(); return(set); }
public void Compress() { // First try to find identical sets of keyframes in tracks for (int t = 0; t < rgbTracks.Length; ++t) { RGBTrack trackT = rgbTracks[t]; for (int r = t + 1; r < rgbTracks.Length; ++r) { RGBTrack trackR = rgbTracks[r]; // Only try to collapse tracks that are not exactly the same if (!trackT.Equals(trackR)) { if (trackR.keyFrameCount == trackT.keyFrameCount) { // Compare actual keyframes bool kfEquals = true; for (int k = 0; k < trackR.keyFrameCount; ++k) { var kfRk = trackR.GetKeyframe(this, (ushort)k); var kfTk = trackT.GetKeyframe(this, (ushort)k); if (!kfRk.Equals(kfTk)) { kfEquals = false; break; } } if (kfEquals) { // Sweet, we can compress the keyframes // Fix up any other tracks for (int i = 0; i < rgbTracks.Length; ++i) { RGBTrack tr = rgbTracks[i]; if (tr.keyframesOffset > trackR.keyframesOffset) { tr.keyframesOffset -= trackR.keyFrameCount; rgbTracks[i] = tr; } } // Remove the duplicate keyframes var newKeyframes = new RGBKeyframe[keyframes.Length - trackR.keyFrameCount]; for (int i = 0; i < trackR.keyframesOffset; ++i) { newKeyframes[i] = keyframes[i]; } for (int i = trackR.keyframesOffset + trackR.keyFrameCount; i < keyframes.Length; ++i) { newKeyframes[i - trackR.keyFrameCount] = keyframes[i]; } keyframes = newKeyframes; // And make R point to the keyframes of T trackR.keyframesOffset = trackT.keyframesOffset; rgbTracks[r] = trackR; } } } } } // Then remove duplicate RGB tracks for (int t = 0; t < rgbTracks.Length; ++t) { ref RGBTrack trackT = ref rgbTracks[t]; for (int r = t + 1; r < rgbTracks.Length; ++r) { ref RGBTrack trackR = ref rgbTracks[r]; if (trackR.Equals(trackT)) { // Remove track R and fix anim tracks // Fix up other animation tracks for (int j = 0; j < tracks.Length; ++j) { ref AnimationTrack trj = ref tracks[j]; if (trj.trackOffset == r) { trj.trackOffset = (ushort)t; } else if (trj.trackOffset > r) { trj.trackOffset--; } } // Remove the duplicate RGBTrack var newRGBTracks = new RGBTrack[rgbTracks.Length - 1]; for (int j = 0; j < r; ++j) { newRGBTracks[j] = rgbTracks[j]; } for (int j = r + 1; j < rgbTracks.Length; ++j) { newRGBTracks[j - 1] = rgbTracks[j]; } rgbTracks = newRGBTracks; }