This commit is contained in:
左右看
2023-06-28 00:35:46 +08:00
parent cb5bab5ce9
commit 0d848e4d07
524 changed files with 78452 additions and 12 deletions
@@ -0,0 +1,468 @@
#define USE_ANIMATION_POINTER
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using GLTF.Schema;
using Unity.Profiling;
using UnityEngine;
using Object = UnityEngine.Object;
namespace UnityGLTF.Timeline
{
public class GLTFRecorder
{
public GLTFRecorder(Transform root, bool recordBlendShapes = true, bool recordRootInWorldSpace = false, bool recordAnimationPointer = false, bool addBoundsMarkerNodes = false)
{
if (!root)
throw new ArgumentNullException(nameof(root), "Please provide a root transform to record.");
this.root = root;
this.recordBlendShapes = recordBlendShapes;
this.recordRootInWorldSpace = recordRootInWorldSpace;
this.recordAnimationPointer = recordAnimationPointer;
this.addBoundsMarkerNodes = addBoundsMarkerNodes;
}
/// <summary>
/// Optionally assign a list of transforms to be recorded, other transforms will be ignored
/// </summary>
internal ICollection<Transform> recordingList = null;
private bool AllowRecordingTransform(Transform tr) => recordingList == null || recordingList.Contains(tr);
private Transform root;
private Dictionary<Transform, AnimationData> data = new Dictionary<Transform, AnimationData>(64);
private double startTime;
private double lastRecordedTime;
private bool isRecording;
private readonly bool recordBlendShapes;
private readonly bool recordRootInWorldSpace;
private readonly bool recordAnimationPointer;
private readonly bool addBoundsMarkerNodes;
public bool IsRecording => isRecording;
public double LastRecordedTime => lastRecordedTime;
internal class AnimationData
{
private Transform tr;
private SkinnedMeshRenderer smr;
private bool recordBlendShapes;
private bool inWorldSpace = false;
private bool recordAnimationPointer;
#if USE_ANIMATION_POINTER
private static List<ExportPlan> exportPlans;
private static MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();
internal List<Track> tracks = new List<Track>();
internal class ExportPlan
{
public string propertyName;
public Type dataType;
public Func<Transform, Object> GetTarget;
public Func<Transform, Object, AnimationData, object> GetData;
public ExportPlan(string propertyName, Type dataType, Func<Transform, Object> GetTarget, Func<Transform, Object, AnimationData, object> GetData)
{
this.propertyName = propertyName;
this.dataType = dataType;
this.GetTarget = GetTarget;
this.GetData = GetData;
}
public object Sample(AnimationData data)
{
var target = GetTarget(data.tr);
return GetData(data.tr, target, data);
}
}
#endif
public AnimationData(Transform tr, double time, bool zeroScale = false, bool recordBlendShapes = true, bool inWorldSpace = false, bool recordAnimationPointer = false)
{
this.tr = tr;
this.smr = tr.GetComponent<SkinnedMeshRenderer>();
this.recordBlendShapes = recordBlendShapes;
this.inWorldSpace = inWorldSpace;
this.recordAnimationPointer = recordAnimationPointer;
if (exportPlans == null)
{
exportPlans = new List<ExportPlan>();
exportPlans.Add(new ExportPlan("translation", typeof(Vector3), x => x, (tr0, _, options) => options.inWorldSpace ? tr0.position : tr0.localPosition));
exportPlans.Add(new ExportPlan("rotation", typeof(Quaternion), x => x, (tr0, _, options) =>
{
var q = options.inWorldSpace ? tr0.rotation : tr0.localRotation;
return new Quaternion(q.x, q.y, q.z, q.w);
}));
exportPlans.Add(new ExportPlan("scale", typeof(Vector3), x => x, (tr0, _, options) => options.inWorldSpace ? tr0.lossyScale : tr0.localScale));
exportPlans.Add(new ExportPlan("weights", typeof(float[]), x => x.GetComponent<SkinnedMeshRenderer>(), (tr0, x, options) =>
{
if (x is SkinnedMeshRenderer skinnedMesh && skinnedMesh.sharedMesh)
{
var mesh = skinnedMesh.sharedMesh;
var blendShapeCount = mesh.blendShapeCount;
if (blendShapeCount == 0) return null;
var weights = new float[blendShapeCount];
for (var i = 0; i < blendShapeCount; i++)
weights[i] = skinnedMesh.GetBlendShapeWeight(i);
return weights;
}
return null;
}));
if (recordAnimationPointer)
{
// TODO add other animation pointer export plans
exportPlans.Add(new ExportPlan("baseColorFactor", typeof(Color), x => x.GetComponent<MeshRenderer>() ? x.GetComponent<MeshRenderer>().sharedMaterial : null, (tr0, mat, options) =>
{
var r = tr0.GetComponent<Renderer>();
if (r.HasPropertyBlock())
{
r.GetPropertyBlock(materialPropertyBlock);
#if UNITY_2021_1_OR_NEWER
if (materialPropertyBlock.HasColor("_BaseColor")) return materialPropertyBlock.GetColor("_BaseColor");
if (materialPropertyBlock.HasColor("_Color")) return materialPropertyBlock.GetColor("_Color");
#else
var c = materialPropertyBlock.GetColor("_BaseColor");
if (c.r != 0 || c.g != 0 || c.b != 0 || c.a != 0) return c;
c = materialPropertyBlock.GetColor("_Color");
if (c.r != 0 || c.g != 0 || c.b != 0 || c.a != 0) return c;
// this leaves an edge case where someone is actually animating color to black:
// in that case, the un-animated color would now be exported...
#endif
}
if (mat is Material m && m)
{
if (m.HasProperty("_BaseColor")) return m.GetColor("_BaseColor");
if (m.HasProperty("_Color")) return m.GetColor("_Color");
}
return null;
}));
}
}
foreach (var plan in exportPlans)
{
if (plan.GetTarget(tr)) {
tracks.Add(new Track(this, plan, time));
}
}
}
internal class Track
{
public Object animatedObject => plan.GetTarget(tr.tr);
public string propertyName => plan.propertyName;
// TODO sample as floats?
public float[] times => samples.Keys.Select(x => (float) x).ToArray();
public object[] values => samples.Values.ToArray();
private AnimationData tr;
private ExportPlan plan;
private Dictionary<double, object> samples;
private object lastSample;
public Track(AnimationData tr, ExportPlan plan, double time)
{
this.tr = tr;
this.plan = plan;
samples = new Dictionary<double, object>();
SampleIfChanged(time);
}
public void SampleIfChanged(double time)
{
var value = plan.Sample(tr);
if (value == null || (value is Object o && !o))
return;
if (!value.Equals(lastSample))
{
samples[time] = value;
lastSample = value;
}
}
}
public void Update(double time)
{
foreach (var track in tracks)
{
track.SampleIfChanged(time);
}
}
}
public void StartRecording(double time)
{
startTime = time;
lastRecordedTime = time;
var trs = root.GetComponentsInChildren<Transform>(true);
data.Clear();
foreach (var tr in trs)
{
if (!AllowRecordingTransform(tr)) continue;
data.Add(tr, new AnimationData(tr, 0, !tr.gameObject.activeSelf, recordBlendShapes, recordRootInWorldSpace && tr == root, recordAnimationPointer));
}
isRecording = true;
}
public void UpdateRecording(double time)
{
if (!isRecording)
{
throw new InvalidOperationException($"{nameof(GLTFRecorder)} isn't recording, but {nameof(UpdateRecording)} was called. This is invalid.");
}
if (time <= lastRecordedTime)
{
Debug.LogWarning("Can't record backwards in time, please avoid this.");
return;
}
var currentTime = time - startTime;
var trs = root.GetComponentsInChildren<Transform>(true);
foreach (var tr in trs)
{
if (!AllowRecordingTransform(tr)) continue;
if (!data.ContainsKey(tr))
{
// insert "empty" frame with scale=0,0,0 because this object might have just appeared in this frame
var emptyData = new AnimationData(tr, lastRecordedTime, true, recordBlendShapes, recordAnimationPointer);
data.Add(tr, emptyData);
// insert actual keyframe
data[tr].Update(currentTime);
}
else
data[tr].Update(currentTime);
}
lastRecordedTime = time;
}
internal void EndRecording(out Dictionary<Transform, AnimationData> param)
{
param = null;
if (!isRecording) return;
isRecording = false;
param = data;
}
public void EndRecording(string filename, string sceneName = "scene", GLTFSettings settings = null)
{
if (!isRecording) return;
var dir = Path.GetDirectoryName(filename);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
using (var filestream = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
EndRecording(filestream, sceneName, settings);
}
}
public void EndRecording(Stream stream, string sceneName = "scene", GLTFSettings settings = null)
{
if (!isRecording) return;
isRecording = false;
Debug.Log("Gltf Recording saved. Tracks: " + data.Count + ", Total Keyframes: " + data.Sum(x => x.Value.tracks.Sum(y => y.values.Count())));
if (!settings)
{
var adjustedSettings = Object.Instantiate(GLTFSettings.GetOrCreateSettings());
adjustedSettings.ExportDisabledGameObjects = true;
adjustedSettings.ExportAnimations = false;
settings = adjustedSettings;
}
var logHandler = new StringBuilderLogHandler();
var exporter = new GLTFSceneExporter(new Transform[] { root }, new ExportOptions(settings)
{
AfterSceneExport = PostExport,
logger = new Logger(logHandler),
});
exporter.SaveGLBToStream(stream, sceneName);
logHandler.LogAndClear();
}
private void PostExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot)
{
// this would include animation from the original root
// exporter.ExportAnimationFromNode(ref root);
GLTFAnimation anim = new GLTFAnimation();
anim.Name = "Recording";
CollectAndProcessAnimation(exporter, anim, addBoundsMarkerNodes, out Bounds translationBounds);
if (addBoundsMarkerNodes)
{
Debug.Log("Animation bounds: " + translationBounds.center + " => " + translationBounds.size);
// create Cube primitive
var cube = GameObject.CreatePrimitive(PrimitiveType.Quad);
cube.transform.localScale = Vector3.one * 0.0001f;
cube.hideFlags = HideFlags.HideAndDontSave;
var collider = cube.GetComponent<MeshCollider>();
SafeDestroy(collider);
var boundsRoot = new GameObject("AnimationBounds");
boundsRoot.hideFlags = HideFlags.HideAndDontSave;
boundsRoot.transform.position = translationBounds.center;
var extremePointsOnBounds = new Vector3[6];
extremePointsOnBounds[0] = translationBounds.center + new Vector3(+translationBounds.extents.x, 0, 0);
extremePointsOnBounds[1] = translationBounds.center + new Vector3(-translationBounds.extents.x, 0, 0);
extremePointsOnBounds[2] = translationBounds.center + new Vector3(0, +translationBounds.extents.y, 0);
extremePointsOnBounds[3] = translationBounds.center + new Vector3(0, -translationBounds.extents.y, 0);
extremePointsOnBounds[4] = translationBounds.center + new Vector3(0, 0, +translationBounds.extents.z);
extremePointsOnBounds[5] = translationBounds.center + new Vector3(0, 0, -translationBounds.extents.z);
foreach (var point in extremePointsOnBounds)
{
var cubeInstance = Object.Instantiate(cube);
cubeInstance.name = "AnimationBounds";
cubeInstance.transform.position = point;
cubeInstance.transform.parent = boundsRoot.transform;
}
// export and add explicitly to the scene list, otherwise these nodes at the root level will be ignored
var nodeId = exporter.ExportNode(boundsRoot);
gltfRoot.Scenes[0].Nodes.Add(nodeId);
// move skinned meshes to the center of the bounds –
// they will be moved by their bones anyways, but this prevents wrong bounds calculations from them.
foreach (var skinnedMeshRenderer in root.GetComponentsInChildren<SkinnedMeshRenderer>())
skinnedMeshRenderer.transform.position = translationBounds.center;
SafeDestroy(boundsRoot);
SafeDestroy(cube);
}
// check if the root node is outside these bounds, move it to the center if necessary
if (anim.Channels.Count > 0 && anim.Samplers.Count > 0)
gltfRoot.Animations.Add(anim);
}
private static void SafeDestroy(Object obj)
{
if (Application.isEditor)
Object.DestroyImmediate(obj);
else
Object.Destroy(obj);
}
private static ProfilerMarker processAnimationMarker = new ProfilerMarker("Process Animation");
private static ProfilerMarker simplifyKeyframesMarker = new ProfilerMarker("Simplify Keyframes");
private static ProfilerMarker convertValuesMarker = new ProfilerMarker("Convert Values to Arrays");
private void CollectAndProcessAnimation(GLTFSceneExporter gltfSceneExporter, GLTFAnimation anim, bool calculateTranslationBounds, out Bounds translationBounds)
{
var gotFirstValue = false;
translationBounds = new Bounds();
foreach (var kvp in data)
{
processAnimationMarker.Begin();
foreach (var tr in kvp.Value.tracks)
{
if (tr.times.Length == 0) continue;
var times = tr.times;
var values = tr.values;
if (calculateTranslationBounds && tr.propertyName == "translation")
{
for (var i = 0; i < values.Length; i++)
{
var vec = (Vector3) values[i];
if (!gotFirstValue)
{
translationBounds = new Bounds(vec, Vector3.zero);
gotFirstValue = true;
}
else
{
translationBounds.Encapsulate(vec);
}
}
}
gltfSceneExporter.RemoveUnneededKeyframes(ref times, ref values);
gltfSceneExporter.AddAnimationData(tr.animatedObject, tr.propertyName, anim, times, values);
}
processAnimationMarker.End();
}
}
private class StringBuilderLogHandler : ILogHandler
{
private readonly StringBuilder sb = new StringBuilder();
private string LogTypeToLog(LogType logType)
{
#if UNITY_EDITOR
// create strings with <color> tags
switch (logType)
{
case LogType.Error:
return "<color=red>[" + logType + "]</color>";
case LogType.Assert:
return "<color=red>[" + logType + "]</color>";
case LogType.Warning:
return "<color=yellow>[" + logType + "]</color>";
case LogType.Log:
return "[" + logType + "]";
case LogType.Exception:
return "<color=red>[" + logType + "]</color>";
default:
return "[" + logType + "]";
}
#else
return "[" + logType + "]";
#endif
}
public void LogFormat(LogType logType, Object context, string format, params object[] args) => sb.AppendLine($"{LogTypeToLog(logType)} {string.Format(format, args)} [Context: {context}]");
public void LogException(Exception exception, Object context) => sb.AppendLine($"{LogTypeToLog(LogType.Exception)} {exception} [Context: {context}]");
public void LogAndClear()
{
if(sb.Length > 0)
{
var str = sb.ToString();
#if UNITY_2019_1_OR_NEWER
var logType = LogType.Log;
#if UNITY_EDITOR
if (str.IndexOf("[Error]", StringComparison.Ordinal) > -1 ||
str.IndexOf("[Exception]", StringComparison.Ordinal) > -1 ||
str.IndexOf("[Assert]", StringComparison.Ordinal) > -1)
logType = LogType.Error;
else if (str.IndexOf("[Warning]", StringComparison.Ordinal) > -1)
logType = LogType.Warning;
#endif
Debug.LogFormat(logType, LogOption.NoStacktrace, null, "Export Messages:" + "\n{0}", sb.ToString());
#else
Debug.Log(string.Format("Export Messages:" + "\n{0}", str));
#endif
}
sb.Clear();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5c86ab342c27009409e8735873d51306
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,129 @@
#if HAVE_TIMELINE
using UnityEngine;
using UnityEngine.Playables;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityGLTF.Timeline
{
public class GLTFRecorderBehaviour : PlayableBehaviour
{
private GLTFRecorder recorder = null;
private void BeginRecording(double getTime, Transform getExportRoot)
{
if (!getExportRoot)
{
Debug.LogError("Can't record: export root is null");
recorder = null;
return;
}
Time.captureFramerate = Clip.m_CaptureFrameRate;
// will be initialized lazy to ensure that the first frame is correct and not pre-timeline refresh
recorder = null;
}
private void StopRecording(double getTime)
{
recorder?.EndRecording(Clip.m_File);
}
private void ProcessRecording(double getTime, Transform getExportRoot)
{
if (recorder == null)
{
recorder = new GLTFRecorder(getExportRoot, Clip.m_RecordBlendShapes, Clip.m_RecordAnimationPointer);
recorder.StartRecording(getTime);
}
else if (getTime > recorder.LastRecordedTime)
{
recorder?.UpdateRecording(getTime);
}
}
public GLTFRecorderClip Clip;
private bool m_isPaused = false;
private static bool IsPlaying()
{
#if UNITY_EDITOR
return EditorApplication.isPlaying;
#else
return true;
#endif
}
public override void OnPlayableDestroy(Playable playable)
{
if (!IsPlaying())
{
return;
}
StopRecording(playable.GetTime());
}
public override void OnGraphStart(Playable playable)
{
if (!IsPlaying())
{
return;
}
BeginRecording(playable.GetTime(), Clip.GetExportRoot(playable.GetGraph()));
}
public override void OnGraphStop(Playable playable)
{
if (!IsPlaying())
{
return;
}
StopRecording(playable.GetTime());
}
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
if (!IsPlaying())
{
return;
}
double time = playable.GetTime();
GLTFRecorderHelper.Add(() => OnFrameEnd(time, playable, info, playerData));
}
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
m_isPaused = false;
}
public override void OnBehaviourPause(Playable playable, FrameData info)
{
m_isPaused = true;
}
public void OnFrameEnd(double time, Playable playable, FrameData info, object playerData)
{
if (!playable.IsValid())
{
return;
}
var root = Clip.GetExportRoot(playable.GetGraph());
if (!root || m_isPaused)
{
return;
}
ProcessRecording(time, root);
}
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5c90c158375298a4b931f2f013dbf111
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,35 @@
#if HAVE_TIMELINE
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
namespace UnityGLTF.Timeline
{
[System.ComponentModel.DisplayName("glTF Recorder Clip")]
public class GLTFRecorderClip : PlayableAsset, ITimelineClipAsset
{
public ExposedReference<Transform> m_exportRoot;
public string m_File = "Assets/Recording.glb";
public int m_CaptureFrameRate = 60;
public bool m_RecordBlendShapes = true;
public bool m_RecordAnimationPointer = false;
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
{
var ret = ScriptPlayable<GLTFRecorderBehaviour>.Create(graph);
var behaviour = ret.GetBehaviour();
behaviour.Clip = this; // TOD check if needed
return ret;
}
public ClipCaps clipCaps { get; }
public Transform GetExportRoot(PlayableGraph graph)
{
return m_exportRoot.Resolve(graph.GetResolver());
}
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a7ec2229f5635a54cbc23eec87b45654
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,59 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityGLTF.Timeline
{
[ExecuteInEditMode]
internal class GLTFRecorderHelper : MonoBehaviour
{
public static void Add(Action callback)
{
GetInstance().queuedActions.Add(callback);
}
private List<Action> queuedActions = new List<Action>();
private static GLTFRecorderHelper instance;
static GLTFRecorderHelper GetInstance()
{
if (instance != null) return instance;
instance = FindObjectOfType<GLTFRecorderHelper>();
if (instance != null) return instance;
var go = new GameObject
{
name = "GLTF Recorder Helper",
hideFlags = HideFlags.HideAndDontSave,
};
instance = go.AddComponent<GLTFRecorderHelper>();
return instance;
}
private IEnumerator WaitForEndOfFrame()
{
yield return new WaitForEndOfFrame();
foreach (var callback in queuedActions)
{
try
{
callback();
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}
queuedActions.Clear();
}
private void LateUpdate()
{
StartCoroutine(WaitForEndOfFrame());
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fba5af8565ac464587d0d4663f151239
timeCreated: 1646945202
@@ -0,0 +1,16 @@
#if HAVE_TIMELINE
using UnityEngine.Timeline;
namespace UnityGLTF.Timeline
{
[System.Serializable]
[TrackClipType(typeof(GLTFRecorderClip))]
[TrackColor(0.7f, 0.0f, 0.0f)]
public class GLTFRecorderTrack : TrackAsset
{
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5f4885dbb1716a34b84ab7851fb37ccd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: