add live vmd recording support (wip)

This commit is contained in:
CroakFang
2023-07-29 23:56:55 +08:00
parent fbcd27398f
commit bbc2cbb171
9 changed files with 816 additions and 126 deletions
@@ -19,6 +19,7 @@ public class UnityCameraVMDRecorder : MonoBehaviour
public int KeyReductionLevel = 2;
public List<Vector3> LocalPositions { get; private set; } = new List<Vector3>();
public List<Vector3> LocalRotations { get; private set; } = new List<Vector3>();
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<LiveCameraFrame> 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();
}
}
}
/// <summary>
/// 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;
}
}
@@ -54,6 +54,7 @@ public class UnityHumanoidVMDRecorder : MonoBehaviour
Dictionary<BoneNames, List<Vector3>> positionDictionarySaved = new Dictionary<BoneNames, List<Vector3>>();
Dictionary<BoneNames, List<Quaternion>> rotationDictionary = new Dictionary<BoneNames, List<Quaternion>>();
Dictionary<BoneNames, List<Quaternion>> rotationDictionarySaved = new Dictionary<BoneNames, List<Quaternion>>();
Dictionary<int, bool> visitableDictionary = new Dictionary<int, bool>();
//ボーン移動量の補正係数
//この値は大体の値、正確ではない
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<UmaContainer>();
List<Transform> objs = GetComponentsInChildren<Transform>().ToList();
BoneDictionary = new Dictionary<BoneNames, Transform>()
{
@@ -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