bug fix
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
public class UnityCameraVMDRecorder : MonoBehaviour
|
||||
@@ -167,7 +165,7 @@ public class UnityCameraVMDRecorder : MonoBehaviour
|
||||
/// 呼び出す際は先にStopRecordingを呼び出すこと
|
||||
/// </summary>
|
||||
/// <param name="filePath">保存先の絶対ファイルパス</param>
|
||||
public async void SaveVMD()
|
||||
public void SaveVMD()
|
||||
{
|
||||
string fileName = Application.dataPath + "/../VMDRecords/" + string.Format("Camera_{0}.vmd", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
|
||||
if (IsRecording)
|
||||
@@ -184,104 +182,103 @@ public class UnityCameraVMDRecorder : MonoBehaviour
|
||||
bool isOrthographic = targetCamera.orthographic;
|
||||
|
||||
Debug.Log(targetCameraTransform.name + "VMDファイル作成開始");
|
||||
await Task.Run(() =>
|
||||
|
||||
//ファイルの書き込み
|
||||
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
|
||||
using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
|
||||
{
|
||||
//ファイルの書き込み
|
||||
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
|
||||
using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
|
||||
try
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
//カメラの書き込み
|
||||
byte[] cameraFrameCount = BitConverter.GetBytes((uint)(frameNumberSaved / KeyReductionLevel));
|
||||
binaryWriter.Write(cameraFrameCount, 0, intByteLength);
|
||||
for (int i = 0; i < frameNumberSaved; i++)
|
||||
{
|
||||
if ((i % KeyReductionLevel) != 0) { continue; }
|
||||
|
||||
byte[] frameNumberByte = BitConverter.GetBytes((ulong)i);
|
||||
binaryWriter.Write(frameNumberByte, 0, intByteLength);
|
||||
|
||||
byte[] cameraDistanceByte = BitConverter.GetBytes(0);
|
||||
binaryWriter.Write(cameraDistanceByte, 0, intByteLength);
|
||||
|
||||
Vector3 position = positionsSaved[i] * 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);
|
||||
|
||||
Vector3 rotation = rotationsSaved[i] * 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)fieldOfView);
|
||||
binaryWriter.Write(viewAngleByte, 0, intByteLength);
|
||||
|
||||
byte perspectiveByte = Convert.ToByte(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);
|
||||
binaryWriter.Write(new byte[motionNameLength - motionNameBytes.Length], 0, motionNameLength - motionNameBytes.Length);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
//全ボーンフレーム数の書き込み
|
||||
byte[] allKeyFrameNumberByte = BitConverter.GetBytes(0);
|
||||
binaryWriter.Write(allKeyFrameNumberByte, 0, intByteLength);
|
||||
|
||||
//全モーフフレーム数の書き込み
|
||||
byte[] faceFrameCount = BitConverter.GetBytes(0);
|
||||
binaryWriter.Write(faceFrameCount, 0, intByteLength);
|
||||
|
||||
//カメラの書き込み
|
||||
byte[] cameraFrameCount = BitConverter.GetBytes((uint)(frameNumberSaved / KeyReductionLevel));
|
||||
binaryWriter.Write(cameraFrameCount, 0, intByteLength);
|
||||
for (int i = 0; i < frameNumberSaved; i++)
|
||||
{
|
||||
Debug.Log("VMD書き込みエラー" + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
binaryWriter.Close();
|
||||
if ((i % KeyReductionLevel) != 0) { continue; }
|
||||
|
||||
byte[] frameNumberByte = BitConverter.GetBytes((ulong)i);
|
||||
binaryWriter.Write(frameNumberByte, 0, intByteLength);
|
||||
|
||||
byte[] cameraDistanceByte = BitConverter.GetBytes(0);
|
||||
binaryWriter.Write(cameraDistanceByte, 0, intByteLength);
|
||||
|
||||
Vector3 position = positionsSaved[i] * 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);
|
||||
|
||||
Vector3 rotation = rotationsSaved[i] * 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)fieldOfView);
|
||||
binaryWriter.Write(viewAngleByte, 0, intByteLength);
|
||||
|
||||
byte perspectiveByte = Convert.ToByte(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("VMD書き込みエラー" + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
binaryWriter.Close();
|
||||
}
|
||||
}
|
||||
Debug.Log(targetCameraTransform.name + "VMDファイル作成終了");
|
||||
Destroy(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user