From 637b99945202390c8f9ea43a79f8e2753e550d0a Mon Sep 17 00:00:00 2001 From: NaoyaKohda Date: Sun, 17 Nov 2024 05:21:08 +0900 Subject: [PATCH] =?UTF-8?q?KeyFrameReducer=20=E3=81=AB=E5=88=86=E3=81=91?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/DmxRecorder/TimelineConverter.cs | 32 +------------- Assets/ArtNet/Editor/KeyFrameReducer.cs | 43 +++++++++++++++++++ Assets/ArtNet/Editor/KeyFrameReducer.cs.meta | 3 ++ 3 files changed, 47 insertions(+), 31 deletions(-) create mode 100644 Assets/ArtNet/Editor/KeyFrameReducer.cs create mode 100644 Assets/ArtNet/Editor/KeyFrameReducer.cs.meta diff --git a/Assets/ArtNet/Editor/DmxRecorder/TimelineConverter.cs b/Assets/ArtNet/Editor/DmxRecorder/TimelineConverter.cs index 1f7c0a6..a74904f 100644 --- a/Assets/ArtNet/Editor/DmxRecorder/TimelineConverter.cs +++ b/Assets/ArtNet/Editor/DmxRecorder/TimelineConverter.cs @@ -182,40 +182,10 @@ public void ThinOutUnchangedFrames() for (var i = 0; i < ChannelDmxFrameData.Length; i++) { var dmxFrameData = ChannelDmxFrameData[i]; - if (dmxFrameData.Count == 0) continue; - - var latest = dmxFrameData[0]; - var newDmxFrameData = new List { dmxFrameData[0] }; - - for (var j = 1; j < dmxFrameData.Count - 1; j++) - { - var current = dmxFrameData[j]; - var next = dmxFrameData[j + 1]; - if (IsOmittedFrame(latest, current, next)) continue; - - latest = current; - newDmxFrameData.Add(dmxFrameData[j]); - } - - newDmxFrameData.Add(dmxFrameData[^1]); - ChannelDmxFrameData[i] = newDmxFrameData; + ChannelDmxFrameData[i] = KeyFrameReducer.Reduce(dmxFrameData); } } - private static bool IsOmittedFrame( - KeyFrameData prev, - KeyFrameData current, - KeyFrameData next, - float tolerance = 0.01f) - { - var prevDiff = current.Value - prev.Value; - var nextDiff = next.Value - current.Value; - var prevDiffTime = current.Time - prev.Time; - var nextDiffTime = next.Time - current.Time; - - return Math.Abs(prevDiff / prevDiffTime - nextDiff / nextDiffTime) <= tolerance; - } - public IEnumerable ToUniverseData() { var universeData = new List(); diff --git a/Assets/ArtNet/Editor/KeyFrameReducer.cs b/Assets/ArtNet/Editor/KeyFrameReducer.cs new file mode 100644 index 0000000..f3612ed --- /dev/null +++ b/Assets/ArtNet/Editor/KeyFrameReducer.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; + +namespace ArtNet.Editor +{ + public static class KeyFrameReducer + { + public static List Reduce(List keyFrameData) + { + if (keyFrameData.Count <= 2) return keyFrameData; + + var newDmxFrameData = new List { keyFrameData[0] }; + var latest = keyFrameData[0]; + + for (var i = 1; i < keyFrameData.Count - 1; i++) + { + var current = keyFrameData[i]; + var next = keyFrameData[i + 1]; + if (IsOmittedFrame(latest, current, next)) continue; + + newDmxFrameData.Add(current); + latest = current; + } + + newDmxFrameData.Add(keyFrameData[^1]); + return newDmxFrameData; + } + + private static bool IsOmittedFrame( + KeyFrameData prev, + KeyFrameData current, + KeyFrameData next, + float tolerance = 0.01f) + { + var prevDiffValue = current.Value - prev.Value; + var prevDiffTime = current.Time - prev.Time; + var nextDiffValue = next.Value - current.Value; + var nextDiffTime = next.Time - current.Time; + + return Math.Abs(prevDiffValue / prevDiffTime - nextDiffValue / nextDiffTime) <= tolerance; + } + } +} diff --git a/Assets/ArtNet/Editor/KeyFrameReducer.cs.meta b/Assets/ArtNet/Editor/KeyFrameReducer.cs.meta new file mode 100644 index 0000000..b2313b6 --- /dev/null +++ b/Assets/ArtNet/Editor/KeyFrameReducer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6671a62d1c81439e8f2ddac42bc853b9 +timeCreated: 1731694026 \ No newline at end of file