File size: 13,919 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | using System;
using System.Globalization;
using System.Text.RegularExpressions;
#if TIMELINE_FRAMEACCURATE
using UnityEngine.Playables;
#endif
namespace UnityEngine.Timeline
{
/// <summary>
/// The standard frame rates supported when locking Timeline playback to frames.
/// The frame rate is expressed in frames per second (fps).
/// </summary>
public enum StandardFrameRates
{
/// <summary>
/// Represents a frame rate of 24 fps. This is the common frame rate for film.
/// </summary>
Fps24,
/// <summary>
/// Represents a drop frame rate of 23.97 fps. This is the common frame rate for NTSC film broadcast.
/// </summary>
Fps23_97,
/// <summary>
/// Represents a frame rate of 25 fps. This is commonly used for non-interlaced PAL television broadcast.
/// </summary>
Fps25,
/// <summary>
/// Represents a frame rate of 30 fps. This is commonly used for HD footage.
/// </summary>
Fps30,
/// <summary>
/// Represents a drop frame rate of 29.97 fps. This is commonly used for NTSC television broadcast.
/// </summary>
Fps29_97,
/// <summary>
/// Represents a frame rate of 50 fps. This is commonly used for interlaced PAL television broadcast.
/// </summary>
Fps50,
/// <summary>
/// Represents a frame rate of 60 fps. This is commonly used for games.
/// </summary>
Fps60,
/// <summary>
/// Represents a drop frame rate of 59.94 fps. This is commonly used for interlaced NTSC television broadcast.
/// </summary>
Fps59_94
}
// Sequence specific utilities for time manipulation
static class TimeUtility
{
// chosen because it will cause no rounding errors between time/frames for frames values up to at least 10 million
public static readonly double kTimeEpsilon = 1e-14;
public static readonly double kFrameRateEpsilon = 1e-6;
public static readonly double k_MaxTimelineDurationInSeconds = 9e6; //104 days of running time
public static readonly double kFrameRateRounding = 1e-2;
static void ValidateFrameRate(double frameRate)
{
if (frameRate <= kTimeEpsilon)
throw new ArgumentException("frame rate cannot be 0 or negative");
}
public static int ToFrames(double time, double frameRate)
{
ValidateFrameRate(frameRate);
time = Math.Min(Math.Max(time, -k_MaxTimelineDurationInSeconds), k_MaxTimelineDurationInSeconds);
// this matches OnFrameBoundary
double tolerance = GetEpsilon(time, frameRate);
if (time < 0)
{
return (int)Math.Ceiling(time * frameRate - tolerance);
}
return (int)Math.Floor(time * frameRate + tolerance);
}
public static double ToExactFrames(double time, double frameRate)
{
ValidateFrameRate(frameRate);
return time * frameRate;
}
public static double FromFrames(int frames, double frameRate)
{
ValidateFrameRate(frameRate);
return (frames / frameRate);
}
public static double FromFrames(double frames, double frameRate)
{
ValidateFrameRate(frameRate);
return frames / frameRate;
}
public static bool OnFrameBoundary(double time, double frameRate)
{
return OnFrameBoundary(time, frameRate, GetEpsilon(time, frameRate));
}
public static double GetEpsilon(double time, double frameRate)
{
return Math.Max(Math.Abs(time), 1) * frameRate * kTimeEpsilon;
}
public static bool OnFrameBoundary(double time, double frameRate, double epsilon)
{
ValidateFrameRate(frameRate);
double exact = ToExactFrames(time, frameRate);
double rounded = Math.Round(exact);
return Math.Abs(exact - rounded) < epsilon;
}
public static double RoundToFrame(double time, double frameRate)
{
ValidateFrameRate(frameRate);
var frameBefore = (int)Math.Floor(time * frameRate) / frameRate;
var frameAfter = (int)Math.Ceiling(time * frameRate) / frameRate;
return Math.Abs(time - frameBefore) < Math.Abs(time - frameAfter) ? frameBefore : frameAfter;
}
public static string TimeAsFrames(double timeValue, double frameRate, string format = "F2")
{
if (OnFrameBoundary(timeValue, frameRate)) // make integral values when on time borders
return ToFrames(timeValue, frameRate).ToString();
return ToExactFrames(timeValue, frameRate).ToString(format);
}
public static string TimeAsTimeCode(double timeValue, double frameRate, string format = "F2")
{
ValidateFrameRate(frameRate);
int intTime = (int)Math.Abs(timeValue);
int hours = intTime / 3600;
int minutes = (intTime % 3600) / 60;
int seconds = intTime % 60;
string result;
string sign = timeValue < 0 ? "-" : string.Empty;
if (hours > 0)
result = hours + ":" + minutes.ToString("D2") + ":" + seconds.ToString("D2");
else if (minutes > 0)
result = minutes + ":" + seconds.ToString("D2");
else
result = seconds.ToString();
int frameDigits = (int)Math.Floor(Math.Log10(frameRate) + 1);
// Add partial digits on the frame if needed.
// we are testing the original value (not the truncated), because the truncation can cause rounding errors leading
// to invalid strings for items on frame boundaries
string frames = (ToFrames(timeValue, frameRate) - ToFrames(intTime, frameRate)).ToString().PadLeft(frameDigits, '0');
if (!OnFrameBoundary(timeValue, frameRate))
{
string decimals = ToExactFrames(timeValue, frameRate).ToString(format);
int decPlace = decimals.IndexOf('.');
if (decPlace >= 0)
frames += " [" + decimals.Substring(decPlace) + "]";
}
return sign + result + ":" + frames;
}
// Given a time code string, return the time in seconds
// 1.5 -> 1.5 seconds
// 1:1.5 -> 1 minute, 1.5 seconds
// 1:1[.5] -> 1 second, 1.5 frames
// 2:3:4 -> 2 minutes, 3 seconds, 4 frames
// 1[.6] -> 1.6 frames
public static double ParseTimeCode(string timeCode, double frameRate, double defaultValue)
{
timeCode = RemoveChar(timeCode, c => char.IsWhiteSpace(c));
string[] sections = timeCode.Split(':');
if (sections.Length == 0 || sections.Length > 4)
return defaultValue;
int hours = 0;
int minutes = 0;
double seconds = 0;
double frames = 0;
try
{
// depending on the format of the last numbers
// seconds format
string lastSection = sections[sections.Length - 1];
if (Regex.Match(lastSection, @"^\d+\.\d+$").Success)
{
seconds = double.Parse(lastSection);
if (sections.Length > 3) return defaultValue;
if (sections.Length > 1) minutes = int.Parse(sections[sections.Length - 2]);
if (sections.Length > 2) hours = int.Parse(sections[sections.Length - 3]);
}
// frame formats
else
{
if (Regex.Match(lastSection, @"^\d+\[\.\d+\]$").Success)
{
string stripped = RemoveChar(lastSection, c => c == '[' || c == ']');
frames = double.Parse(stripped);
}
else if (Regex.Match(lastSection, @"^\d*$").Success)
{
frames = int.Parse(lastSection);
}
else
{
return defaultValue;
}
if (sections.Length > 1) seconds = int.Parse(sections[sections.Length - 2]);
if (sections.Length > 2) minutes = int.Parse(sections[sections.Length - 3]);
if (sections.Length > 3) hours = int.Parse(sections[sections.Length - 4]);
}
}
catch (FormatException)
{
return defaultValue;
}
return frames / frameRate + seconds + minutes * 60 + hours * 3600;
}
public static double ParseTimeSeconds(string timeCode, double frameRate, double defaultValue)
{
timeCode = RemoveChar(timeCode, c => char.IsWhiteSpace(c));
string[] sections = timeCode.Split(':');
if (sections.Length == 0 || sections.Length > 4)
return defaultValue;
int hours = 0;
int minutes = 0;
double seconds = 0;
try
{
// depending on the format of the last numbers
// seconds format
string lastSection = sections[sections.Length - 1];
{
if (!double.TryParse(lastSection, NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
if (Regex.Match(lastSection, @"^\d+\.\d+$").Success)
seconds = double.Parse(lastSection);
else
return defaultValue;
if (!double.TryParse(lastSection, NumberStyles.Float, CultureInfo.InvariantCulture, out seconds))
return defaultValue;
if (sections.Length > 3) return defaultValue;
if (sections.Length > 1) minutes = int.Parse(sections[sections.Length - 2]);
if (sections.Length > 2) hours = int.Parse(sections[sections.Length - 3]);
}
}
catch (FormatException)
{
return defaultValue;
}
return seconds + minutes * 60 + hours * 3600;
}
// fixes rounding errors from using single precision for length
public static double GetAnimationClipLength(AnimationClip clip)
{
if (clip == null || clip.empty)
return 0;
double length = clip.length;
if (clip.frameRate > 0)
{
double frames = Mathf.Round(clip.length * clip.frameRate);
length = frames / clip.frameRate;
}
return length;
}
static string RemoveChar(string str, Func<char, bool> charToRemoveFunc)
{
var len = str.Length;
var src = str.ToCharArray();
var dstIdx = 0;
for (var i = 0; i < len; i++)
{
if (!charToRemoveFunc(src[i]))
src[dstIdx++] = src[i];
}
return new string(src, 0, dstIdx);
}
public static FrameRate GetClosestFrameRate(double frameRate)
{
ValidateFrameRate(frameRate);
var actualFrameRate = FrameRate.DoubleToFrameRate(frameRate);
return Math.Abs(frameRate - actualFrameRate.rate) < kFrameRateRounding ? actualFrameRate : new FrameRate();
}
public static FrameRate ToFrameRate(StandardFrameRates enumValue)
{
switch (enumValue)
{
case StandardFrameRates.Fps23_97:
return FrameRate.k_23_976Fps;
case StandardFrameRates.Fps24:
return FrameRate.k_24Fps;
case StandardFrameRates.Fps25:
return FrameRate.k_25Fps;
case StandardFrameRates.Fps29_97:
return FrameRate.k_29_97Fps;
case StandardFrameRates.Fps30:
return FrameRate.k_30Fps;
case StandardFrameRates.Fps50:
return FrameRate.k_50Fps;
case StandardFrameRates.Fps59_94:
return FrameRate.k_59_94Fps;
case StandardFrameRates.Fps60:
return FrameRate.k_60Fps;
default:
return new FrameRate();
}
;
}
internal static bool ToStandardFrameRate(FrameRate rate, out StandardFrameRates standard)
{
if (rate == FrameRate.k_23_976Fps)
standard = StandardFrameRates.Fps23_97;
else if (rate == FrameRate.k_24Fps)
standard = StandardFrameRates.Fps24;
else if (rate == FrameRate.k_25Fps)
standard = StandardFrameRates.Fps25;
else if (rate == FrameRate.k_30Fps)
standard = StandardFrameRates.Fps30;
else if (rate == FrameRate.k_29_97Fps)
standard = StandardFrameRates.Fps29_97;
else if (rate == FrameRate.k_50Fps)
standard = StandardFrameRates.Fps50;
else if (rate == FrameRate.k_59_94Fps)
standard = StandardFrameRates.Fps59_94;
else if (rate == FrameRate.k_60Fps)
standard = StandardFrameRates.Fps60;
else
{
standard = (StandardFrameRates)Enum.GetValues(typeof(StandardFrameRates)).Length;
return false;
}
return true;
}
}
}
|