This commit is contained in:
2024-04-22 00:00:51 +02:00
parent cf04700131
commit 11fd1a3d92
54 changed files with 11144 additions and 1416 deletions

View File

@@ -0,0 +1,6 @@
using UnityEngine;
public interface IAnimated
{
public Animator GetAnimator();
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 14fe157a3aea9784d892f9bc75881dc9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -169,7 +169,7 @@ public class ObjectContainer : MonoBehaviour, IKeyframeSetter
public virtual void SetKeyframe(int frameNum = -1)
{
if (DoNotSave || !_setKeyframe) return;
Debug.Log("Setting frame for " + name);
//Debug.Log("Setting frame for " + name);
//if (Frames.Count == 0) SetDefaultFrame();
if (frameNum == -1) frameNum = TimelineController.Instance.CurrentFrame;

View File

@@ -11,187 +11,187 @@ using UnityEngine.Networking;
public class ModelViewerDownloader
{
public static string UrlBase = "";
public static string SaveLocation = "/idbfs/ModelViewer/";
// public static string UrlBase = "";
// public static string SaveLocation = "/idbfs/ModelViewer/";
public static ModelViewerDownloader Instance;
public bool SyncRequired;
// public static ModelViewerDownloader Instance;
// public bool SyncRequired;
public ModelViewerDownloader() : this(UrlBase, "/idbfs/ModelViewer/") { }
// public ModelViewerDownloader() : this(UrlBase, "/idbfs/ModelViewer/") { }
public ModelViewerDownloader(string baseUrl, string saveLocation)
{
UrlBase = baseUrl;
SaveLocation = saveLocation;
ModelViewerMain.GetInstance().StartCoroutine(Loop());
}
// public ModelViewerDownloader(string baseUrl, string saveLocation)
// {
// UrlBase = baseUrl;
// SaveLocation = saveLocation;
// ModelViewerMain.GetInstance().StartCoroutine(Loop());
// }
public class DownloadTask
{
public string Name;
public bool Redownload = false;
public bool Finished = false;
public bool Success = false;
public int Priority = 0;
public string ResultText;
public byte[] ResultBytes;
public System.Action OnDownloadSuccess;
// public class DownloadTask
// {
// public string Name;
// public bool Redownload = false;
// public bool Finished = false;
// public bool Success = false;
// public int Priority = 0;
// public string ResultText;
// public byte[] ResultBytes;
// public System.Action OnDownloadSuccess;
public DownloadTask(string name, int priority, bool redownload)
{
Name = name;
Priority = priority;
Redownload = redownload;
}
}
// public DownloadTask(string name, int priority, bool redownload)
// {
// Name = name;
// Priority = priority;
// Redownload = redownload;
// }
// }
private static List<DownloadTask> _downloadRunning = new List<DownloadTask>();
private static List<DownloadTask> _downloadQueue = new List<DownloadTask>();
// private static List<DownloadTask> _downloadRunning = new List<DownloadTask>();
// private static List<DownloadTask> _downloadQueue = new List<DownloadTask>();
public IEnumerator Loop()
{
float lastSync = 5;
while (true)
{
if(_downloadQueue.Count > 0)
{
_downloadRunning.AddRange(_downloadQueue);
_downloadRunning = _downloadRunning.OrderByDescending(d=>d.Priority).ToList();
_downloadQueue.Clear();
}
if(_downloadRunning.Count > 0)
{
var fileToDownload = _downloadRunning[0];
_downloadRunning.RemoveAt(0);
yield return DownloadFile(fileToDownload);
if(!_downloadRunning.Any() && !_downloadQueue.Any())
{
SyncRequired = true;
lastSync = 0;
}
}
if(SyncRequired)
{
lastSync += Time.deltaTime;
if(lastSync >= 5)
{
CommitChanges();
}
}
yield return 0;
}
}
// public IEnumerator Loop()
// {
// float lastSync = 5;
// while (true)
// {
// if(_downloadQueue.Count > 0)
// {
// _downloadRunning.AddRange(_downloadQueue);
// _downloadRunning = _downloadRunning.OrderByDescending(d=>d.Priority).ToList();
// _downloadQueue.Clear();
// }
// if(_downloadRunning.Count > 0)
// {
// var fileToDownload = _downloadRunning[0];
// _downloadRunning.RemoveAt(0);
// yield return DownloadFile(fileToDownload);
// if(!_downloadRunning.Any() && !_downloadQueue.Any())
// {
// SyncRequired = true;
// lastSync = 0;
// }
// }
// if(SyncRequired)
// {
// lastSync += Time.deltaTime;
// if(lastSync >= 5)
// {
// CommitChanges();
// }
// }
// yield return 0;
// }
// }
public static IEnumerator WaitForDownload(string name, int priority = 0, bool redownload = false, System.Action<string[]> lines = null, System.Action<byte[]> bytes = null, System.Action<string> text = null, System.Action onDownloadSuccess = null)
{
var task = _downloadQueue.Concat(_downloadRunning).FirstOrDefault(t => t.Name == name);
// public static IEnumerator WaitForDownload(string name, int priority = 0, bool redownload = false, System.Action<string[]> lines = null, System.Action<byte[]> bytes = null, System.Action<string> text = null, System.Action onDownloadSuccess = null)
// {
// var task = _downloadQueue.Concat(_downloadRunning).FirstOrDefault(t => t.Name == name);
if(task == null)
{
task = new DownloadTask(name, priority, redownload);
task.OnDownloadSuccess = onDownloadSuccess;
_downloadQueue.Add(task);
}
// if(task == null)
// {
// task = new DownloadTask(name, priority, redownload);
// task.OnDownloadSuccess = onDownloadSuccess;
// _downloadQueue.Add(task);
// }
while (!task.Finished)
{
yield return 0;
}
// while (!task.Finished)
// {
// yield return 0;
// }
lines?.Invoke(task.ResultText.Split('\n').Select(s => s.Trim()).ToArray());
bytes?.Invoke(task.ResultBytes);
text?.Invoke(task.ResultText);
}
// lines?.Invoke(task.ResultText.Split('\n').Select(s => s.Trim()).ToArray());
// bytes?.Invoke(task.ResultBytes);
// text?.Invoke(task.ResultText);
// }
private IEnumerator DownloadFile(DownloadTask task)
{
#if UNITY_WEBGL && !UNITY_EDITOR
string fileSavePath = $"{SaveLocation}{task.Name}";
#else
string fileSavePath = Application.persistentDataPath + " /" + task.Name;
#endif
// private IEnumerator DownloadFile(DownloadTask task)
// {
//#if UNITY_WEBGL && !UNITY_EDITOR
// string fileSavePath = $"{SaveLocation}{task.Name}";
//#else
// string fileSavePath = Application.persistentDataPath + " /" + task.Name;
//#endif
if (!task.Redownload && LoadFromIdbfs(fileSavePath, out var data))
{
//Debug.Log("Loaded from " + fileSavePath);
task.Success = true;
task.ResultText = Encoding.UTF8.GetString(data);
task.ResultBytes = data;
task.Finished = true;
yield break;
}
// if (!task.Redownload && LoadFromIdbfs(fileSavePath, out var data))
// {
// //Debug.Log("Loaded from " + fileSavePath);
// task.Success = true;
// task.ResultText = Encoding.UTF8.GetString(data);
// task.ResultBytes = data;
// task.Finished = true;
// yield break;
// }
string url = UrlBase + task.Name + (task.Redownload ? "?time=" + DateTime.Now.Ticks.ToString() : "");
//var downloadPanel = KFKViewerUI.Instance.DownloadPanel;
// string url = UrlBase + task.Name + (task.Redownload ? "?time=" + DateTime.Now.Ticks.ToString() : "");
// //var downloadPanel = KFKViewerUI.Instance.DownloadPanel;
//Msg.Log(url);
// //Msg.Log(url);
using (var www = UnityWebRequest.Get(url))
{
//downloadPanel.gameObject.SetActive(true);
// using (var www = UnityWebRequest.Get(url))
// {
// //downloadPanel.gameObject.SetActive(true);
var operation = www.SendWebRequest();
// var operation = www.SendWebRequest();
do
{
//downloadPanel.SetStatus(task.Name, www.downloadProgress);
yield return null;
}
while (!operation.isDone);
// do
// {
// //downloadPanel.SetStatus(task.Name, www.downloadProgress);
// yield return null;
// }
// while (!operation.isDone);
if (www.result != UnityWebRequest.Result.Success)
{
Error.Log(Color.red, www.error);
task.Success = false;
task.ResultText = "";
task.ResultBytes = new byte[0];
}
else
{
SaveToIdbfs(fileSavePath, www.downloadHandler.data);
task.OnDownloadSuccess?.Invoke();
task.Success = true;
task.ResultText = www.downloadHandler.text;
task.ResultBytes = www.downloadHandler.data;
}
}
// if (www.result != UnityWebRequest.Result.Success)
// {
// Error.Log(Color.red, www.error);
// task.Success = false;
// task.ResultText = "";
// task.ResultBytes = new byte[0];
// }
// else
// {
// SaveToIdbfs(fileSavePath, www.downloadHandler.data);
// task.OnDownloadSuccess?.Invoke();
// task.Success = true;
// task.ResultText = www.downloadHandler.text;
// task.ResultBytes = www.downloadHandler.data;
// }
// }
//downloadPanel.SetStatus(task.Name, 0);
//downloadPanel.gameObject.SetActive(false);
// //downloadPanel.SetStatus(task.Name, 0);
// //downloadPanel.gameObject.SetActive(false);
task.Finished = true;
}
// task.Finished = true;
// }
public static bool LoadFromIdbfs(string name, out byte[] data)
{
if(File.Exists(name))
{
data = File.ReadAllBytes(name);
return true;
}
else
{
data = null;
return false;
}
}
// public static bool LoadFromIdbfs(string name, out byte[] data)
// {
// if(File.Exists(name))
// {
// data = File.ReadAllBytes(name);
// return true;
// }
// else
// {
// data = null;
// return false;
// }
// }
public static void SaveToIdbfs(string path, byte[] data)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllBytes(path, data);
//Debug.Log("Saved " + path);
}
// public static void SaveToIdbfs(string path, byte[] data)
// {
// Directory.CreateDirectory(Path.GetDirectoryName(path));
// File.WriteAllBytes(path, data);
// //Debug.Log("Saved " + path);
// }
private static void CommitChanges()
{
Instance.SyncRequired = false;
Debug.Log("Database Updated");
#if UNITY_WEBGL && !UNITY_EDITOR
SyncFiles();
#endif
}
// private static void CommitChanges()
// {
// Instance.SyncRequired = false;
// Debug.Log("Database Updated");
//#if UNITY_WEBGL && !UNITY_EDITOR
// SyncFiles();
//#endif
// }
[DllImport("__Internal")]
private static extern void SyncFiles();
// [DllImport("__Internal")]
// private static extern void SyncFiles();
}

View File

@@ -26,6 +26,9 @@ public class ModelViewerInterface : MonoBehaviour
public RectTransform Tooltip;
public TMPro.TextMeshProUGUI TooltipText;
private Vector2 _previousResolution = Vector2.zero;
private int _resolutionUpdateCountdown;
public static ModelViewerInterface GetInstance()
{
return _mainInstance;
@@ -36,6 +39,29 @@ public class ModelViewerInterface : MonoBehaviour
return _mainInstance as T;
}
protected void Update()
{
var resolution = new Vector2(Screen.width, Screen.height);
if (resolution != _previousResolution)
{
_previousResolution = resolution;
_resolutionUpdateCountdown = 10;
}
if (_resolutionUpdateCountdown > 0)
{
if (--_resolutionUpdateCountdown == 0)
{
var draggables = GameObject.FindObjectsOfType<UIElementDragger>();
foreach (var draggable in draggables)
{
draggable.transform.position = draggable.RecalculatePosition(draggable.transform.position);
};
}
}
}
public static void SetTooltip(int index, string tooltip)
{
var tooltips = GetInstance().TooltipLabels;
@@ -45,7 +71,6 @@ public class ModelViewerInterface : MonoBehaviour
}
}
public static void ToggleVisible(GameObject go)
{
ToggleVisible(go, false);

View File

@@ -1,6 +1,7 @@
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using static UnityEngine.Rendering.VirtualTexturing.Debugging;
using UnityEngine.UI;
[RequireComponent(typeof(CanvasGroup))]
public class UIElementDragger : MonoBehaviour, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler
@@ -48,12 +49,12 @@ public class UIElementDragger : MonoBehaviour, IDragHandler, IEndDragHandler, IP
}
}
private void OnEnable()
private void Start()
{
var handle = HandlePosition.GetComponent<RectTransform>();
transform.position = new Vector2(
Mathf.Clamp(transform.position.x, 0, Screen.width - handle.rect.width),
Mathf.Clamp(transform.position.y, handle.sizeDelta.y, Screen.height));
if (Proxy == null)
{
transform.position = RecalculatePosition(transform.position);
}
}
public void SetProxyMode(UIDraggableProxy proxy)
@@ -81,18 +82,19 @@ public class UIElementDragger : MonoBehaviour, IDragHandler, IEndDragHandler, IP
}
else
{
var canvas = ModelViewerInterface.GetInstance().MainCanvas;
var proxyRect = Proxy.transform as RectTransform;
var handleRect = HandlePosition.transform as RectTransform;
var handleOffset = handleRect.sizeDelta.y * Vector3.up;
var handleOffset = handleRect.sizeDelta.y * canvas.scaleFactor * Vector3.up;
if (Proxy.GetComponentInParent<UIToolbar>().Layout == UIToolbar.LayoutMode.Vertical)
{
//anchor to top-right of button
transform.position = proxyRect.position + proxyRect.sizeDelta.x * Vector3.right + handleOffset;
transform.position = proxyRect.position + proxyRect.sizeDelta.x * canvas.scaleFactor * Vector3.right + handleOffset;
}
else
{
//anchor to bottom-left of button
transform.position = proxyRect.position + proxyRect.sizeDelta.y * Vector3.up - handleOffset;
transform.position = proxyRect.position + proxyRect.sizeDelta.y * canvas.scaleFactor * Vector3.up - handleOffset;
}
gameObject.SetActive(true);
}
@@ -126,11 +128,7 @@ public class UIElementDragger : MonoBehaviour, IDragHandler, IEndDragHandler, IP
}
case DragAction.Position:
{
var handle = HandlePosition.GetComponent<RectTransform>();
transform.position = new Vector2(
Mathf.Clamp(Input.mousePosition.x + mouseOffset.x, 0, Screen.width - handle.rect.width),
Mathf.Clamp(Input.mousePosition.y + mouseOffset.y, handle.sizeDelta.y, Screen.height));
transform.position = RecalculatePosition((Vector2)Input.mousePosition + mouseOffset);
break;
}
}
@@ -184,4 +182,16 @@ public class UIElementDragger : MonoBehaviour, IDragHandler, IEndDragHandler, IP
{
OnEndDrag(null);
}
public Vector2 RecalculatePosition(Vector2 pos)
{
var handle = HandlePosition.transform as RectTransform;
var tsf = transform as RectTransform;
var canvas = ModelViewerInterface.GetInstance().MainCanvas;
var xPos = Mathf.Clamp(pos.x, 0, Screen.width - tsf.sizeDelta.x * canvas.scaleFactor);
var yPos = Mathf.Clamp(pos.y, handle.sizeDelta.y * canvas.scaleFactor, Screen.height);
return new Vector2 (xPos, yPos);
}
}

View File

@@ -12,9 +12,13 @@ public class Screenshot : MonoBehaviour
bool recording = false;
public Button RecordButton;
public TMPro.TMP_InputField ScreenshotWidth;
public TMPro.TMP_InputField ScreenshotHeight;
public Toggle ScreenshotTransparent;
public TMPro.TMP_InputField
ScreenshotWidth,
ScreenshotHeight,
GifWidth,
GifHeight;
public TMPro.TMP_Dropdown CaptureAntialiasing;
public Toggle CaptureTransparent;
private void Awake()
{
@@ -24,11 +28,11 @@ public class Screenshot : MonoBehaviour
public void TakeScreenshot()
{
var camera = ModelViewerMain.GetInstance().GetCamera().Cam;
int width = int.Parse(ScreenshotWidth.text);
int height = int.Parse(ScreenshotHeight.text);
width = width == -1 ? Screen.width : width;
height = height == -1 ? Screen.height : height;
var image = GrabFrame(camera, width, height, ScreenshotTransparent.isOn);
int width = string.IsNullOrEmpty(ScreenshotWidth.text)? 0 : int.Parse(ScreenshotWidth.text);
int height = string.IsNullOrEmpty(ScreenshotHeight.text) ? 0 : int.Parse(ScreenshotHeight.text);
width = width == 0 ? Screen.width : width;
height = height == 0 ? Screen.height : height;
var image = GrabFrame(camera, width, height, CaptureTransparent.isOn, GetAntialiasing());
string fileName = Application.dataPath + "/../Screenshots/" + string.Format("UniViewer_{0}", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff"));
byte[] pngShot = ImageConversion.EncodeToPNG(image);
@@ -41,55 +45,53 @@ public class Screenshot : MonoBehaviour
public void BeginRecordGif()
{
if (recording || ModelViewerMain.GetInstance().SelectedObject == null) return;
if (recording || ModelViewerMain.GetInstance().SelectedObject == null)
{
Error.Log(Color.yellow, "Target not found");
return;
}
recording = true;
RecordButton.interactable = false;
StartCoroutine(RecordGif(int.Parse(ScreenshotWidth.text), int.Parse(ScreenshotHeight.text), ScreenshotTransparent.isOn, 10)); //(int)UmaViewerUI.Instance.GifQuality.value));
int width = string.IsNullOrEmpty(GifWidth.text) ? 0 : int.Parse(GifWidth.text);
int height = string.IsNullOrEmpty(GifHeight.text) ? 0 : int.Parse(GifHeight.text);
ModelViewerMain.GetInstance().StartCoroutine(RecordGif(width, height, CaptureTransparent.isOn, GetAntialiasing())); //(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 antialiasing)
{
yield break;
//var camera = GetActiveCamera();
//var ppLayer = camera.GetComponent<PostProcessLayer>();
//bool oldPpState = ppLayer.enabled;
//ppLayer.enabled = false;
var camera = ModelViewerMain.GetInstance().GetCamera().Cam;
//var uma = UmaViewerBuilder.Instance.CurrentUMAContainer;
//var animator = uma.UmaAnimator;
//if (animator == null) yield break;
var currentCharacter = ModelViewerMain.GetInstance().SelectedObject;
//int frame = 0;
//var animeClip = uma.OverrideController["clip_2"];
//var clipFrameCount = Mathf.RoundToInt(animeClip.length * animeClip.frameRate);
//StartCoroutine(CaptureToGIFCustom.Instance.Encode(animeClip.frameRate, quality));
if (currentCharacter as IAnimated == null) yield break;
//UmaViewerUI.Instance.AnimationSpeedChange(0);
//UmaViewerUI.Instance.AnimationProgressChange(0);
//yield return new WaitForSeconds(1); //wait for dynamicBone to settle;
var animator = (currentCharacter as IAnimated).GetAnimator();
//while (frame < clipFrameCount)
//{
// UmaViewerUI.Instance.GifSlider.value = (float)frame / clipFrameCount;
// UmaViewerUI.Instance.AnimationProgressChange((float)frame / clipFrameCount);
// yield return new WaitForEndOfFrame();
// var tex = GrabFrame(camera, width, height, transparent, transparent);
// CaptureToGIFCustom.Instance.Frames.Add(new Image(tex));
// Destroy(tex);
// frame++;
//}
if (animator == null) yield break;
//recording = false;
//UmaViewerUI.Instance.AnimationProgressChange(0);
//UmaViewerUI.Instance.AnimationSpeedChange(1);
//ppLayer.enabled = oldPpState;
//CaptureToGIFCustom.Instance.stop = true;
//UmaViewerUI.Instance.GifButton.interactable = true;
//UmaViewerUI.Instance.GifSlider.value = 1;
int frame = 0;
var animeClip = (animator.runtimeAnimatorController as AnimatorOverrideController)["Anim"];
var clipFrameCount = Mathf.RoundToInt(animeClip.length * animeClip.frameRate);
ModelViewerMain.GetInstance().StartCoroutine(CaptureToGIFCustom.Instance.Encode(animeClip.frameRate, 10));
animator.speed = 0;
while (frame < clipFrameCount)
{
animator.Play("Anim", 0, (float)frame / clipFrameCount);
yield return new WaitForEndOfFrame();
var tex = GrabFrame(camera, width, height, transparent, antialiasing, transparent);
CaptureToGIFCustom.Instance.Frames.Add(new Image(tex));
Destroy(tex);
frame++;
}
recording = false;
CaptureToGIFCustom.Instance.stop = true;
RecordButton.interactable = true;
}
public static Texture2D GrabFrame(Camera cam, int width, int height, bool transparent = true, bool gifBackground = false)
public static Texture2D GrabFrame(Camera cam, int width, int height, bool transparent = true, int antialiasing = 2, bool gifBackground = false)
{
var dimensions = GetResolution(width, height);
width = dimensions.x;
@@ -111,7 +113,7 @@ public class Screenshot : MonoBehaviour
}
var tex_color = new Texture2D(width, height, TextureFormat.ARGB32, false);
var render_texture = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, 8);
var render_texture = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, antialiasing);
var grab_area = new Rect(0, 0, width, height);
RenderTexture.active = render_texture;
@@ -150,4 +152,9 @@ public class Screenshot : MonoBehaviour
}
return new Vector2Int(width, height);
}
private int GetAntialiasing()
{
return int.Parse(CaptureAntialiasing.options[CaptureAntialiasing.value].text);
}
}

View File

@@ -1,3 +1,14 @@
#if !UNITY_EDITOR
using UnityEngine;
[System.AttributeUsage(System.AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
public sealed class ShowIfAttribute : PropertyAttribute
{
public ShowIfAttribute(string ConditionalSourceField, bool expectedValue, bool HideInInspector = false)
{
}
}
#else
/* Title : Attribute for show a field if other field is true or false.
* Author : Anth
*/
@@ -107,4 +118,6 @@ public class ConditionalHidePropertyDrawer : PropertyDrawer
return false;
#endif
}
}
}
#endif

View File

@@ -2,27 +2,32 @@
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using System;
namespace uGIF
{
public class CaptureToGIFCustom : MonoBehaviour
public class CaptureToGIFCustom
{
public static CaptureToGIFCustom Instance;
private static CaptureToGIFCustom _instance;
public static CaptureToGIFCustom Instance
{
get
{
if(_instance == null)
{
_instance = new CaptureToGIFCustom();
}
return _instance;
}
}
public List<Image> Frames = new List<Image>();
public bool stop = false;
[System.NonSerialized]
public byte[] bytes = null;
private void Awake()
{
Instance = this;
}
public IEnumerator Encode (float fps, int quality)
{
bytes = null;