Added image sequence recording

This commit is contained in:
katboi01
2025-08-02 09:19:38 +02:00
parent b3d179c0c7
commit 4eb00e9ee9
4 changed files with 2909 additions and 469 deletions
+2825 -463
View File
File diff suppressed because it is too large Load Diff
+68 -4
View File
@@ -4,7 +4,6 @@ using System.IO;
using uGIF; using uGIF;
using UnityEngine; using UnityEngine;
using UnityEngine.Rendering.PostProcessing; using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UIElements;
using Image = uGIF.Image; using Image = uGIF.Image;
public class Screenshot : MonoBehaviour public class Screenshot : MonoBehaviour
@@ -53,7 +52,6 @@ public class Screenshot : MonoBehaviour
StartCoroutine(RecordGif(ScreenshotSettings.GifWidth, ScreenshotSettings.GifHeight, ScreenshotSettings.GifTransparent, 10)); //(int)UmaViewerUI.Instance.GifQuality.value)); StartCoroutine(RecordGif(ScreenshotSettings.GifWidth, ScreenshotSettings.GifHeight, ScreenshotSettings.GifTransparent, 10)); //(int)UmaViewerUI.Instance.GifQuality.value));
} }
private IEnumerator RecordGif(int width, int height, bool transparent, int quality) private IEnumerator RecordGif(int width, int height, bool transparent, int quality)
{ {
var camera = GetActiveCamera(); var camera = GetActiveCamera();
@@ -67,9 +65,11 @@ public class Screenshot : MonoBehaviour
int frame = 0; int frame = 0;
var animeClip = uma.OverrideController["clip_2"]; var animeClip = uma.OverrideController["clip_2"];
var clipFrameCount = Mathf.RoundToInt(animeClip.length * animeClip.frameRate); var clipLength = animeClip.length;
StartCoroutine(CaptureToGIFCustom.Instance.Encode(animeClip.frameRate, quality)); var frameRate = animeClip.frameRate;
var clipFrameCount = Mathf.RoundToInt(clipLength * frameRate);
StartCoroutine(CaptureToGIFCustom.Instance.Encode(frameRate, quality));
AnimationSettings.ChangeSpeed(0); AnimationSettings.ChangeSpeed(0);
AnimationSettings.ChangeProgress(0); AnimationSettings.ChangeProgress(0);
@@ -93,6 +93,70 @@ public class Screenshot : MonoBehaviour
ScreenshotSettings.GifButton.interactable = true; ScreenshotSettings.GifButton.interactable = true;
} }
public void BeginRecordImageSequence()
{
if (recording || UmaViewerBuilder.Instance.CurrentUMAContainer == null) return;
recording = true;
ScreenshotSettings.SequenceButton.interactable = false;
StartCoroutine(RecordSequence(ScreenshotSettings.GifWidth, ScreenshotSettings.GifHeight, ScreenshotSettings.GifTransparent, 10, ScreenshotSettings.GifFPS)); //(int)UmaViewerUI.Instance.GifQuality.value));
}
private IEnumerator RecordSequence(int width, int height, bool transparent, int quality, int frameRateOverride = -1)
{
ScreenshotSettings.SequenceButton.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = "Recording...";
string folderName = string.Format("UmaViewer_{0}", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff"));
#if UNITY_ANDROID && !UNITY_EDITOR
string fileDirectory = Application.persistentDataPath + "/../Screenshots/" + folderName;
#else
string fileDirectory = Application.dataPath + "/../Screenshots/" + folderName;
#endif
Directory.CreateDirectory(fileDirectory);
var camera = GetActiveCamera();
var ppLayer = camera.GetComponent<PostProcessLayer>();
bool oldPpState = ppLayer.enabled;
ppLayer.enabled = false;
var uma = UmaViewerBuilder.Instance.CurrentUMAContainer;
var animator = uma.UmaAnimator;
if (animator == null) yield break;
int frame = 0;
var animeClip = uma.OverrideController["clip_2"];
var clipLength = animeClip.length;
var frameRate = animeClip.frameRate;
var clipFrameCount = Mathf.RoundToInt(clipLength * (frameRateOverride == -1? frameRate : frameRateOverride));
var maxNameLength = clipFrameCount.ToString().Length + 1;
AnimationSettings.ChangeSpeed(0);
AnimationSettings.ChangeProgress(0);
yield return new WaitForSeconds(1); //wait for dynamicBone to settle;
while (frame < clipFrameCount)
{
AnimationSettings.ChangeProgress((float)frame / clipFrameCount);
yield return new WaitForEndOfFrame();
var tex = GrabFrame(camera, width, height, transparent);
var bytes = tex.EncodeToPNG();
var fileName = $"{fileDirectory}/{frame.ToString().PadLeft(maxNameLength, '0')}.png";
File.WriteAllBytes(fileName, bytes);
Destroy(tex);
frame++;
}
recording = false;
AnimationSettings.ChangeProgress(0);
AnimationSettings.ChangeSpeed(1);
ppLayer.enabled = oldPpState;
ScreenshotSettings.SequenceButton.interactable = true;
ScreenshotSettings.SequenceButton.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = "Record PNG Sequence";
var fullpath = Path.GetFullPath(folderName);
UmaViewerUI.Instance.ShowMessage($"PNG Sequence saved: {fullpath}", UIMessageType.Success);
}
public static Texture2D GrabFrame(Camera cam, int width, int height, bool transparent = true) public static Texture2D GrabFrame(Camera cam, int width, int height, bool transparent = true)
{ {
var dimensions = GetResolution(width, height); var dimensions = GetResolution(width, height);
@@ -11,6 +11,8 @@ public class UISettingsScreenshot : MonoBehaviour
public TMP_InputField _gifHeight; public TMP_InputField _gifHeight;
public Toggle _gifTransparent; public Toggle _gifTransparent;
public Button GifButton; public Button GifButton;
public Button SequenceButton;
public TMP_Dropdown SequenceFPSDropdown;
public int Width => int.Parse(_width.text); public int Width => int.Parse(_width.text);
public int Height => int.Parse(_height.text); public int Height => int.Parse(_height.text);
@@ -19,4 +21,16 @@ public class UISettingsScreenshot : MonoBehaviour
public int GifWidth => int.Parse(_gifWidth.text); public int GifWidth => int.Parse(_gifWidth.text);
public int GifHeight => int.Parse(_gifHeight.text); public int GifHeight => int.Parse(_gifHeight.text);
public bool GifTransparent => _gifTransparent.isOn; public bool GifTransparent => _gifTransparent.isOn;
public int GifFPS {
get
{
switch (SequenceFPSDropdown.value)
{
case 1: return 30;
case 2: return 60;
case 3: return 120;
default: return -1;
}
}
}
} }
+1 -1
View File
@@ -1055,7 +1055,7 @@ public class UmaContainerCharacter : UmaContainer
if (UmaAnimator) if (UmaAnimator)
{ {
Debug.Log("LiveTime" + UI.LiveTime.ToString()); //Debug.Log("LiveTime" + UI.LiveTime.ToString());
aClip.name = entry.Name; // Need a complete path to find dependencies aClip.name = entry.Name; // Need a complete path to find dependencies
LoadAnimation(aClip); LoadAnimation(aClip);
return; return;