From bbc2cbb1713e7090d37a5537a5d30415d37ae9e4 Mon Sep 17 00:00:00 2001 From: CroakFang <1605296300@qq.com> Date: Sat, 29 Jul 2023 23:56:55 +0800 Subject: [PATCH] add live vmd recording support (wip) --- Assets/Scenes/Version2.unity | 364 +++++++++++++++++- .../VMDRecorder/UnityCameraVMDRecorder.cs | 246 +++++++++++- .../VMDRecorder/UnityHumanoidVMDRecorder.cs | 86 +++-- Assets/Scripts/UmaContainer.cs | 1 + Assets/Scripts/UmaViewerAudio.cs | 2 +- Assets/Scripts/UmaViewerBuilder.cs | 3 +- Assets/Scripts/UmaViewerUI.cs | 127 +++--- .../Gallop/Live/Cutt/LiveTimelineControl.cs | 38 +- .../Scripts/umamusume/Gallop/Live/Director.cs | 75 +++- 9 files changed, 816 insertions(+), 126 deletions(-) diff --git a/Assets/Scenes/Version2.unity b/Assets/Scenes/Version2.unity index fb6b6e0..b8b7464 100644 --- a/Assets/Scenes/Version2.unity +++ b/Assets/Scenes/Version2.unity @@ -930,7 +930,7 @@ RectTransform: m_Children: - {fileID: 887532140} m_Father: {fileID: 523631780} - m_RootOrder: 7 + m_RootOrder: 8 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0} m_AnchorMax: {x: 0.5, y: 0} @@ -2964,7 +2964,7 @@ RectTransform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 523631780} - m_RootOrder: 4 + m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 1} m_AnchorMax: {x: 0.5, y: 1} @@ -4738,6 +4738,81 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 197132282} m_CullTransparentMesh: 1 +--- !u!1 &202988918 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 202988919} + - component: {fileID: 202988921} + - component: {fileID: 202988920} + m_Layer: 5 + m_Name: Checkmark + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &202988919 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 202988918} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 922614215} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &202988920 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 202988918} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 10901, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 0 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &202988921 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 202988918} + m_CullTransparentMesh: 1 --- !u!1 &203967664 GameObject: m_ObjectHideFlags: 0 @@ -14260,6 +14335,104 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 504748474} m_CullTransparentMesh: 1 +--- !u!1 &506660741 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 506660742} + - component: {fileID: 506660743} + m_Layer: 5 + m_Name: Toggle + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &506660742 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 506660741} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 1556377413} + - {fileID: 922614215} + m_Father: {fileID: 523631780} + m_RootOrder: 4 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 261.13794, y: 62.5} + m_SizeDelta: {x: 227.715, y: 35} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &506660743 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 506660741} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9085046f02f69544eb97fd06b6048fe2, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Navigation: + m_Mode: 3 + m_WrapAround: 0 + m_SelectOnUp: {fileID: 0} + m_SelectOnDown: {fileID: 0} + m_SelectOnLeft: {fileID: 0} + m_SelectOnRight: {fileID: 0} + m_Transition: 1 + m_Colors: + m_NormalColor: {r: 1, g: 1, b: 1, a: 1} + m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1} + m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608} + m_ColorMultiplier: 1 + m_FadeDuration: 0.1 + m_SpriteState: + m_HighlightedSprite: {fileID: 0} + m_PressedSprite: {fileID: 0} + m_SelectedSprite: {fileID: 0} + m_DisabledSprite: {fileID: 0} + m_AnimationTriggers: + m_NormalTrigger: Normal + m_HighlightedTrigger: Highlighted + m_PressedTrigger: Pressed + m_SelectedTrigger: Selected + m_DisabledTrigger: Disabled + m_Interactable: 1 + m_TargetGraphic: {fileID: 922614216} + toggleTransition: 1 + graphic: {fileID: 202988920} + m_Group: {fileID: 0} + onValueChanged: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 1564959567} + m_TargetAssemblyTypeName: UmaViewerUI, umamusume + m_MethodName: SetLiveRecordMode + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + m_IsOn: 0 --- !u!1 &507044638 GameObject: m_ObjectHideFlags: 0 @@ -14780,8 +14953,8 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0.5} m_AnchorMax: {x: 1, y: 0.5} - m_AnchoredPosition: {x: 175, y: 80} - m_SizeDelta: {x: -450, y: 35} + m_AnchoredPosition: {x: 31.678, y: 80} + m_SizeDelta: {x: -626.645, y: 35} m_Pivot: {x: 0.5, y: 1} --- !u!114 &515484818 MonoBehaviour: @@ -14829,16 +15002,28 @@ MonoBehaviour: m_CaptionImage: {fileID: 0} m_ItemText: {fileID: 1312133766} m_ItemImage: {fileID: 0} - m_Value: 0 + m_Value: 1 m_Options: m_Options: - - m_Text: Preview Mode + - m_Text: Simple Mode m_Image: {fileID: 0} - m_Text: Full Live Mode(WIP) m_Image: {fileID: 0} m_OnValueChanged: m_PersistentCalls: - m_Calls: [] + m_Calls: + - m_Target: {fileID: 1564959567} + m_TargetAssemblyTypeName: UmaViewerUI, umamusume + m_MethodName: UpdateLiveMode + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 m_AlphaFadeSpeed: 0.15 --- !u!114 &515484819 MonoBehaviour: @@ -15037,6 +15222,7 @@ RectTransform: - {fileID: 1406840791} - {fileID: 1781049852} - {fileID: 515484817} + - {fileID: 506660742} - {fileID: 116348345} - {fileID: 605931264} - {fileID: 1280435371} @@ -17252,7 +17438,7 @@ RectTransform: - {fileID: 1834195527} - {fileID: 960399988} m_Father: {fileID: 523631780} - m_RootOrder: 5 + m_RootOrder: 6 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 1} m_AnchorMax: {x: 0.5, y: 1} @@ -24767,7 +24953,7 @@ MonoBehaviour: m_HorizontalOverflow: 0 m_VerticalOverflow: 0 m_LineSpacing: 1 - m_Text: Preview Mode + m_Text: Full Live Mode(WIP) --- !u!222 &839781340 CanvasRenderer: m_ObjectHideFlags: 0 @@ -26943,6 +27129,82 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 919036165} m_CullTransparentMesh: 1 +--- !u!1 &922614214 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 922614215} + - component: {fileID: 922614217} + - component: {fileID: 922614216} + m_Layer: 5 + m_Name: Background + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &922614215 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 922614214} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 202988919} + m_Father: {fileID: 506660742} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 1, y: 0.5} + m_AnchorMax: {x: 1, y: 0.5} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 35, y: 35} + m_Pivot: {x: 1, y: 0.5} +--- !u!114 &922614216 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 922614214} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &922614217 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 922614214} + m_CullTransparentMesh: 1 --- !u!1 &926505643 GameObject: m_ObjectHideFlags: 0 @@ -35168,7 +35430,7 @@ RectTransform: m_Children: - {fileID: 2061178672} m_Father: {fileID: 523631780} - m_RootOrder: 6 + m_RootOrder: 7 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0} m_AnchorMax: {x: 0.5, y: 0} @@ -42117,6 +42379,85 @@ RectTransform: type: 3} m_PrefabInstance: {fileID: 2141276146} m_PrefabAsset: {fileID: 0} +--- !u!1 &1556377412 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1556377413} + - component: {fileID: 1556377415} + - component: {fileID: 1556377414} + m_Layer: 5 + m_Name: Label + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1556377413 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1556377412} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 506660742} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: -15, y: -0.5} + m_SizeDelta: {x: -40, y: -3} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1556377414 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1556377412} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_FontData: + m_Font: {fileID: 12800000, guid: 036631af73b15ac41a9d10f75f5bbafe, type: 3} + m_FontSize: 17 + m_FontStyle: 0 + m_BestFit: 0 + m_MinSize: 2 + m_MaxSize: 40 + m_Alignment: 3 + m_AlignByGeometry: 0 + m_RichText: 1 + m_HorizontalOverflow: 0 + m_VerticalOverflow: 0 + m_LineSpacing: 1 + m_Text: Record VMD (Slow) +--- !u!222 &1556377415 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1556377412} + m_CullTransparentMesh: 1 --- !u!1 &1558324556 GameObject: m_ObjectHideFlags: 0 @@ -42395,7 +42736,8 @@ MonoBehaviour: EnableEyeTracking: 1 EnableFaceOverride: 1 LiveTime: 0 - LiveDropdown: {fileID: 515484818} + isRecordVMD: 0 + LiveRecoedToggle: {fileID: 0} LiveMode: 0 currentFaceDrivenKeyTarget: {fileID: 0} --- !u!114 &1564959568 diff --git a/Assets/Scripts/Exporters/VMDRecorder/UnityCameraVMDRecorder.cs b/Assets/Scripts/Exporters/VMDRecorder/UnityCameraVMDRecorder.cs index ef0f811..922dca8 100644 --- a/Assets/Scripts/Exporters/VMDRecorder/UnityCameraVMDRecorder.cs +++ b/Assets/Scripts/Exporters/VMDRecorder/UnityCameraVMDRecorder.cs @@ -19,6 +19,7 @@ public class UnityCameraVMDRecorder : MonoBehaviour public int KeyReductionLevel = 2; public List LocalPositions { get; private set; } = new List(); public List LocalRotations { get; private set; } = new List(); + public bool IsRecording { get; private set; } = false; public int FrameNumber { get; private set; } = 0; @@ -136,12 +137,12 @@ public class UnityCameraVMDRecorder : MonoBehaviour LastRotation = vmdRotation; } - Vector3 DeltaVector(Vector3 val ,Vector3 lastVal) + public static Vector3 DeltaVector(Vector3 val ,Vector3 lastVal) { return new Vector3(DeltaDegree(val.x, lastVal.x), DeltaDegree(val.y, lastVal.y), DeltaDegree(val.z, lastVal.z)); } - float DeltaDegree(float val,float lastVal) + public static float DeltaDegree(float val,float lastVal) { var deltaVal = val - lastVal; if (Mathf.Abs(deltaVal) < 180) @@ -323,6 +324,206 @@ public class UnityCameraVMDRecorder : MonoBehaviour Destroy(this); } + + public static void SaveLiveCameraVMD(List frames) + { + string directory = $"{Application.dataPath}/../VMDRecords/"; + Directory.CreateDirectory(directory); + var time = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); + string fileName = $"{directory}LiveCamera_POS_{time}.vmd"; + string fovFileName = $"{directory}LiveCamera_FOV_{time}.vmd"; + + + const string modelName = "カメラ・照明"; + + Debug.Log("Live Camera VMDファイル作成開始"); + + //ファイルの書き込み + using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) + using (BinaryWriter binaryWriter = new BinaryWriter(fileStream)) + { + try + { + const string ShiftJIS = "shift_jis"; + const int intByteLength = 4; + + //ファイルタイプの書き込み + const int fileTypeLength = 30; + const string RightFileType = "Vocaloid Motion Data 0002"; + byte[] fileTypeBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(RightFileType); + binaryWriter.Write(fileTypeBytes, 0, fileTypeBytes.Length); + binaryWriter.Write(new byte[fileTypeLength - fileTypeBytes.Length], 0, fileTypeLength - fileTypeBytes.Length); + + //モーション名の書き込み、Shift_JISで保存 + const int motionNameLength = 20; + byte[] motionNameBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("CameraAttach to 全ての親"); + binaryWriter.Write(motionNameBytes, 0, Mathf.Min(motionNameBytes.Length, 20)); + if (motionNameLength - motionNameBytes.Length > 0) + { + binaryWriter.Write(new byte[motionNameLength - motionNameBytes.Length], 0, motionNameLength - motionNameBytes.Length); + } + + //全ボーンフレーム数の書き込み + byte[] allKeyFrameNumberByte = BitConverter.GetBytes((uint)frames.Count); + binaryWriter.Write(allKeyFrameNumberByte, 0, intByteLength); + + foreach (var frame in frames) + { + const int boneNameLength = 15; + string boneNameString = "全ての親"; + + byte[] boneNameBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(boneNameString); + binaryWriter.Write(boneNameBytes, 0, boneNameBytes.Length); + binaryWriter.Write(new byte[boneNameLength - boneNameBytes.Length], 0, boneNameLength - boneNameBytes.Length); + + byte[] frameNumberByte = BitConverter.GetBytes((ulong)(frame.frameIndex/2)); + binaryWriter.Write(frameNumberByte, 0, intByteLength); + + Vector3 position = frame.Position * WorldScaleFix; + byte[] positionX = BitConverter.GetBytes(position.x); + binaryWriter.Write(positionX, 0, intByteLength); + byte[] positionY = BitConverter.GetBytes(position.y); + binaryWriter.Write(positionY, 0, intByteLength); + byte[] positionZ = BitConverter.GetBytes(position.z); + binaryWriter.Write(positionZ, 0, intByteLength); + Quaternion rotation = frame.Rotation; + byte[] rotationX = BitConverter.GetBytes(rotation.x); + binaryWriter.Write(rotationX, 0, intByteLength); + byte[] rotationY = BitConverter.GetBytes(rotation.y); + binaryWriter.Write(rotationY, 0, intByteLength); + byte[] rotationZ = BitConverter.GetBytes(rotation.z); + binaryWriter.Write(rotationZ, 0, intByteLength); + byte[] rotationW = BitConverter.GetBytes(rotation.w); + binaryWriter.Write(rotationW, 0, intByteLength); + + byte[] interpolateBytes = new byte[64]; + binaryWriter.Write(interpolateBytes, 0, 64); + } + + //全モーフフレーム数の書き込み + byte[] faceFrameCount = BitConverter.GetBytes(0); + binaryWriter.Write(faceFrameCount, 0, intByteLength); + + //カメラの書き込み + byte[] cameraFrameCount = BitConverter.GetBytes(0); + binaryWriter.Write(cameraFrameCount, 0, intByteLength); + + //照明の書き込み + byte[] lightFrameCount = BitConverter.GetBytes(0); + binaryWriter.Write(lightFrameCount, 0, intByteLength); + + //セルフシャドウの書き込み + byte[] selfShadowCount = BitConverter.GetBytes(0); + binaryWriter.Write(selfShadowCount, 0, intByteLength); + + //IKの書き込み + byte[] ikCount = BitConverter.GetBytes(0); + binaryWriter.Write(ikCount, 0, intByteLength); + } + catch (Exception ex) + { + Debug.Log("VMD書き込みエラー" + ex.Message); + } + finally + { + binaryWriter.Close(); + } + } + + using (FileStream fileStream = new FileStream(fovFileName, FileMode.Create)) + using (BinaryWriter binaryWriter = new BinaryWriter(fileStream)) + { + try + { + const string ShiftJIS = "shift_jis"; + const int intByteLength = 4; + + //ファイルタイプの書き込み + const int fileTypeLength = 30; + const string RightFileType = "Vocaloid Motion Data 0002"; + byte[] fileTypeBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(RightFileType); + binaryWriter.Write(fileTypeBytes, 0, fileTypeBytes.Length); + binaryWriter.Write(new byte[fileTypeLength - fileTypeBytes.Length], 0, fileTypeLength - fileTypeBytes.Length); + + //モーション名の書き込み、Shift_JISで保存 + const int motionNameLength = 20; + byte[] motionNameBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(modelName); + binaryWriter.Write(motionNameBytes, 0, Mathf.Min(motionNameBytes.Length, 20)); + if (motionNameLength - motionNameBytes.Length > 0) + { + binaryWriter.Write(new byte[motionNameLength - motionNameBytes.Length], 0, motionNameLength - motionNameBytes.Length); + } + + //全ボーンフレーム数の書き込み + byte[] allKeyFrameNumberByte = BitConverter.GetBytes(0); + binaryWriter.Write(allKeyFrameNumberByte, 0, intByteLength); + + //全モーフフレーム数の書き込み + byte[] faceFrameCount = BitConverter.GetBytes(0); + binaryWriter.Write(faceFrameCount, 0, intByteLength); + + var fovkeys = frames.FindAll(k => k.FovVaild); + //カメラの書き込み + byte[] cameraFrameCount = BitConverter.GetBytes((uint)fovkeys.Count); + binaryWriter.Write(cameraFrameCount, 0, intByteLength); + + foreach (var frame in fovkeys) + { + byte[] frameNumberByte = BitConverter.GetBytes((ulong)frame.frameIndex/2); + binaryWriter.Write(frameNumberByte, 0, intByteLength); + + byte[] cameraDistanceByte = BitConverter.GetBytes(0); + binaryWriter.Write(cameraDistanceByte, 0, intByteLength); + + Vector3 position = Vector3.zero; + byte[] positionX = BitConverter.GetBytes(position.x); + binaryWriter.Write(positionX, 0, intByteLength); + byte[] positionY = BitConverter.GetBytes(position.y); + binaryWriter.Write(positionY, 0, intByteLength); + byte[] positionZ = BitConverter.GetBytes(position.z); + binaryWriter.Write(positionZ, 0, intByteLength); + + Vector3 rotation = new Vector3(0,180,0) * Mathf.Deg2Rad; + byte[] rotationX = BitConverter.GetBytes(rotation.x); + binaryWriter.Write(rotationX, 0, intByteLength); + byte[] rotationY = BitConverter.GetBytes(rotation.y); + binaryWriter.Write(rotationY, 0, intByteLength); + byte[] rotationZ = BitConverter.GetBytes(rotation.z); + binaryWriter.Write(rotationZ, 0, intByteLength); + + byte[] interpolateBytes = new byte[24]; + binaryWriter.Write(interpolateBytes, 0, 24); + + byte[] viewAngleByte = BitConverter.GetBytes((ulong)frame.Fov); + binaryWriter.Write(viewAngleByte, 0, intByteLength); + + byte perspectiveByte = Convert.ToByte(frame.isOrthographic); + byte[] perspectiveBytes = new byte[] { perspectiveByte }; + binaryWriter.Write(new byte[] { perspectiveByte }, 0, perspectiveBytes.Length); + } + + //照明の書き込み + byte[] lightFrameCount = BitConverter.GetBytes(0); + binaryWriter.Write(lightFrameCount, 0, intByteLength); + + //セルフシャドウの書き込み + byte[] selfShadowCount = BitConverter.GetBytes(0); + binaryWriter.Write(selfShadowCount, 0, intByteLength); + + //IKの書き込み + byte[] ikCount = BitConverter.GetBytes(0); + binaryWriter.Write(ikCount, 0, intByteLength); + } + catch (Exception ex) + { + Debug.Log("FOV VMD書き込みエラー" + ex.Message); + } + finally + { + binaryWriter.Close(); + } + } + } /// /// VMDを作成する /// 呼び出す際は先にStopRecordingを呼び出すこと @@ -334,4 +535,45 @@ public class UnityCameraVMDRecorder : MonoBehaviour KeyReductionLevel = keyReductionLevel; SaveVMD(); } +} + +public class LiveCameraFrame +{ + public int frameIndex; + public Vector3 Position; + public Quaternion Rotation; + public Vector3 CameraRotation; + public float Fov; + public bool FovVaild; + public bool isOrthographic; + + public LiveCameraFrame(int frameIndex, Transform transform, float fov, LiveCameraFrame lastframe = null, bool isOrthographic = false) + { + this.frameIndex = frameIndex; + + var fixedPosition = transform.position; + var vmdPosition = new Vector3(-fixedPosition.x, fixedPosition.y, -fixedPosition.z); + Position = vmdPosition; + + var fixedQuatenion = transform.rotation; + var vmdQuatenion = new Quaternion(-fixedQuatenion.x, fixedQuatenion.y, -fixedQuatenion.z, fixedQuatenion.w); + Rotation = vmdQuatenion; + + Vector3 vmdRotation = new Vector3(fixedQuatenion.eulerAngles.x, 180 - fixedQuatenion.eulerAngles.y, fixedQuatenion.eulerAngles.z); + + var finalRotation = vmdRotation; + if (lastframe == null) + { + finalRotation = vmdRotation; + } + else + { + finalRotation = lastframe.CameraRotation + UnityCameraVMDRecorder.DeltaVector(vmdRotation, lastframe.CameraRotation); + } + + CameraRotation = finalRotation; + + Fov = fov; + this.isOrthographic = isOrthographic; + } } \ No newline at end of file diff --git a/Assets/Scripts/Exporters/VMDRecorder/UnityHumanoidVMDRecorder.cs b/Assets/Scripts/Exporters/VMDRecorder/UnityHumanoidVMDRecorder.cs index f9989cd..a519fa8 100644 --- a/Assets/Scripts/Exporters/VMDRecorder/UnityHumanoidVMDRecorder.cs +++ b/Assets/Scripts/Exporters/VMDRecorder/UnityHumanoidVMDRecorder.cs @@ -54,6 +54,7 @@ public class UnityHumanoidVMDRecorder : MonoBehaviour Dictionary> positionDictionarySaved = new Dictionary>(); Dictionary> rotationDictionary = new Dictionary>(); Dictionary> rotationDictionarySaved = new Dictionary>(); + Dictionary visitableDictionary = new Dictionary(); //ボーン移動量の補正係数 //この値は大体の値、正確ではない const float DefaultBoneAmplifier = 12.5f; @@ -66,10 +67,12 @@ public class UnityHumanoidVMDRecorder : MonoBehaviour public MorphRecorder morphRecorder; public MorphRecorder morphRecorderSaved; + private UmaContainer container; float aposeDegress = 38.5f; public void Initialize() { Time.fixedDeltaTime = FPSs; + container = GetComponentInParent(); List objs = GetComponentsInChildren().ToList(); BoneDictionary = new Dictionary() { @@ -167,8 +170,6 @@ public class UnityHumanoidVMDRecorder : MonoBehaviour animator.Play(state.shortNameHash, 0, state.normalizedTime); } - - private void FixedUpdate() { if (IsRecording) @@ -178,11 +179,24 @@ public class UnityHumanoidVMDRecorder : MonoBehaviour } } + bool lastvisable; void SaveFrame() { if (boneGhost != null) { boneGhost.GhostAll(); } if (morphRecorder != null) { morphRecorder.RecrodAllMorph(); } + bool visable = container.LiveVisible; + if (visitableDictionary.Count == 0) + { + lastvisable = visable; + visitableDictionary.Add(0, visable); + } + else if(visable != lastvisable) + { + lastvisable = visable; + visitableDictionary.Add(FrameNumber, visable); + } + foreach (BoneNames boneName in BoneDictionary.Keys) { if (BoneDictionary[boneName] == null) @@ -521,34 +535,40 @@ public class UnityHumanoidVMDRecorder : MonoBehaviour //IKの書き込み //0フレームにキーフレーム一つだけ置く - byte[] ikCount = BitConverter.GetBytes(1); - byte[] ikFrameNumber = BitConverter.GetBytes(0); - byte modelDisplay = Convert.ToByte(1); - //右足IKと左足IKと右足つま先IKと左足つま先IKの4つ - byte[] ikNumber = BitConverter.GetBytes(4); - const int IKNameLength = 20; - byte[] leftIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("左足IK"); - byte[] rightIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("右足IK"); - byte[] leftToeIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("左つま先IK"); - byte[] rightToeIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("右つま先IK"); - byte ikOn = Convert.ToByte(1); - byte ikOff = Convert.ToByte(0); + byte[] ikCount = BitConverter.GetBytes(visitableDictionary.Count); binaryWriter.Write(ikCount, 0, intByteLength); - binaryWriter.Write(ikFrameNumber, 0, intByteLength); - binaryWriter.Write(modelDisplay); - binaryWriter.Write(ikNumber, 0, intByteLength); - binaryWriter.Write(leftIKName, 0, leftIKName.Length); - binaryWriter.Write(new byte[IKNameLength - leftIKName.Length], 0, IKNameLength - leftIKName.Length); - binaryWriter.Write(ikOff); - binaryWriter.Write(leftToeIKName, 0, leftToeIKName.Length); - binaryWriter.Write(new byte[IKNameLength - leftToeIKName.Length], 0, IKNameLength - leftToeIKName.Length); - binaryWriter.Write(ikOff); - binaryWriter.Write(rightIKName, 0, rightIKName.Length); - binaryWriter.Write(new byte[IKNameLength - rightIKName.Length], 0, IKNameLength - rightIKName.Length); - binaryWriter.Write(ikOff); - binaryWriter.Write(rightToeIKName, 0, rightToeIKName.Length); - binaryWriter.Write(new byte[IKNameLength - rightToeIKName.Length], 0, IKNameLength - rightToeIKName.Length); - binaryWriter.Write(ikOff); + + foreach(var visable in visitableDictionary) + { + byte[] ikFrameNumber = BitConverter.GetBytes(visable.Key); + byte modelDisplay = Convert.ToByte(visable.Value ? 1 : 0); + binaryWriter.Write(ikFrameNumber, 0, intByteLength); + binaryWriter.Write(modelDisplay); + + //右足IKと左足IKと右足つま先IKと左足つま先IKの4つ + byte[] ikNumber = BitConverter.GetBytes(4); + const int IKNameLength = 20; + byte[] leftIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("左足IK"); + byte[] rightIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("右足IK"); + byte[] leftToeIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("左つま先IK"); + byte[] rightToeIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("右つま先IK"); + byte ikOn = Convert.ToByte(1); + byte ikOff = Convert.ToByte(0); + + binaryWriter.Write(ikNumber, 0, intByteLength); + binaryWriter.Write(leftIKName, 0, leftIKName.Length); + binaryWriter.Write(new byte[IKNameLength - leftIKName.Length], 0, IKNameLength - leftIKName.Length); + binaryWriter.Write(ikOff); + binaryWriter.Write(leftToeIKName, 0, leftToeIKName.Length); + binaryWriter.Write(new byte[IKNameLength - leftToeIKName.Length], 0, IKNameLength - leftToeIKName.Length); + binaryWriter.Write(ikOff); + binaryWriter.Write(rightIKName, 0, rightIKName.Length); + binaryWriter.Write(new byte[IKNameLength - rightIKName.Length], 0, IKNameLength - rightIKName.Length); + binaryWriter.Write(ikOff); + binaryWriter.Write(rightToeIKName, 0, rightToeIKName.Length); + binaryWriter.Write(new byte[IKNameLength - rightToeIKName.Length], 0, IKNameLength - rightToeIKName.Length); + binaryWriter.Write(ikOff); + } } catch (Exception ex) { @@ -577,6 +597,14 @@ public class UnityHumanoidVMDRecorder : MonoBehaviour SaveVMD(modelName, fileName); } + public void SaveLiveVMD(string modelName, int keyReductionLevel = 3) + { + string fileName = Application.dataPath + "/../VMDRecords/" + string.Format("{0}_{1}.vmd", modelName, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")); + Directory.CreateDirectory(Application.dataPath + "/../VMDRecords"); + KeyReductionLevel = keyReductionLevel; + SaveVMD(modelName, fileName); + } + //裏で正規化されたモデル //(初期ポーズで各ボーンのlocalRotationがQuaternion.identityのモデル)を疑似的にアニメーションさせる class BoneGhost diff --git a/Assets/Scripts/UmaContainer.cs b/Assets/Scripts/UmaContainer.cs index 2d0f322..58bf677 100644 --- a/Assets/Scripts/UmaContainer.cs +++ b/Assets/Scripts/UmaContainer.cs @@ -82,6 +82,7 @@ public class UmaContainer : MonoBehaviour [Header("Live")] public bool IsLive = false; + public bool LiveVisible = true; public LiveTimelineCharaLocator LiveLocator; [Header("Other")] diff --git a/Assets/Scripts/UmaViewerAudio.cs b/Assets/Scripts/UmaViewerAudio.cs index e4c5dfb..c44f73c 100644 --- a/Assets/Scripts/UmaViewerAudio.cs +++ b/Assets/Scripts/UmaViewerAudio.cs @@ -22,7 +22,7 @@ public class UmaViewerAudio static public UmaSoundInfo getSoundPath(string name) { - Debug.Log(name); + //Debug.Log(name); UmaSoundInfo info; info.awb = Main.AbSounds.FirstOrDefault(a => a.Name.Contains(name) && a.Name.EndsWith("awb")); return info; diff --git a/Assets/Scripts/UmaViewerBuilder.cs b/Assets/Scripts/UmaViewerBuilder.cs index 0039e8c..fc8cef5 100644 --- a/Assets/Scripts/UmaViewerBuilder.cs +++ b/Assets/Scripts/UmaViewerBuilder.cs @@ -668,6 +668,7 @@ public class UmaViewerBuilder : MonoBehaviour GameObject MainLive = Instantiate(LiveControllerPrefab); Gallop.Live.Director mController = MainLive.GetComponentInChildren(); mController.live = live; + mController.IsRecordVMD = UI.isRecordVMD; List transferObjs = new List() { @@ -847,7 +848,7 @@ public class UmaViewerBuilder : MonoBehaviour public void LoadAsset(UmaDatabaseEntry entry, bool IsSubAsset = false, Transform SetParent = null) { - Debug.Log("Loading " + entry.Name); + //Debug.Log("Loading " + entry.Name); var bundle = UmaAssetManager.LoadAssetBundle(entry, isRecursive: false); LoadBundle(entry, bundle, IsSubAsset, SetParent); } diff --git a/Assets/Scripts/UmaViewerUI.cs b/Assets/Scripts/UmaViewerUI.cs index ca6a796..7965c81 100644 --- a/Assets/Scripts/UmaViewerUI.cs +++ b/Assets/Scripts/UmaViewerUI.cs @@ -10,7 +10,6 @@ using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; -using Debug = UnityEngine.Debug; public class UmaViewerUI : MonoBehaviour { @@ -135,8 +134,9 @@ public class UmaViewerUI : MonoBehaviour [Header("Live")] public bool LiveTime = false; - public Dropdown LiveDropdown; - public int LiveMode = 0; + public bool isRecordVMD; + public Dropdown LiveRecoedToggle; + public int LiveMode = 1; public FaceDrivenKeyTarget currentFaceDrivenKeyTarget; @@ -168,65 +168,6 @@ public class UmaViewerUI : MonoBehaviour private void Update() { - if (LiveMode != LiveDropdown.value) - { - LiveMode = LiveDropdown.value; - if (LiveSelectPannel.activeSelf && currentLive != null) - { - foreach (var select in LiveSelectList.content.GetComponentsInChildren()) - { - if(select != LiveSelectList.content.transform) - { - Destroy(select.gameObject); - } - } - - switch (LiveMode) - { - case 0: - LiveSelectImage.sprite = currentLive.Icon; - - int mainCount = 0; - foreach (var motion in Main.AbMotions.Where(a => a.Name.StartsWith($"3d/motion/live/body/son{currentLive.MusicId}") && Path.GetFileName(a.Name).Split('_').Length == 4)) - { - mainCount += 1; - } - - LiveSelectInfoText.text = $"{currentLive.SongName}\nMain Members Count: {mainCount}"; - for (int i = 0; i < mainCount; i++) - { - var chara = Instantiate(LiveSelectPrefab, LiveSelectList.content).GetComponent(); - chara.IndexText.text = (i + 1).ToString(); - chara.GetComponent