debug video error

main
Yogi 2024-12-04 17:21:36 +08:00
parent 01449764af
commit 11e215f7da
5 changed files with 103 additions and 64 deletions

View File

@ -1,5 +1,5 @@
using System; using System;
using System.Threading.Tasks; using System.Collections;
using UnityEngine; using UnityEngine;
using UnityEngine.Video; using UnityEngine.Video;
@ -14,7 +14,7 @@ public class AudioPlayControl : MonoBehaviour
public VideoClip[] mp4Clips; public VideoClip[] mp4Clips;
private int currentMP4Index = -1; private int currentMP4Index = -1;
public bool isPlaying = false; public bool isPlaying = false;
public Action OnMP4PlayFinished; public Action OnPlayFinished;
private void Awake() private void Awake()
{ {
@ -22,11 +22,16 @@ public class AudioPlayControl : MonoBehaviour
player.playOnAwake = false; player.playOnAwake = false;
this.gameObject.SetActive(false); this.gameObject.SetActive(false);
} }
private void OnDisable()
{
this.StopAllCoroutines();
}
public void ResetMP4Index() public void ResetMP4Index()
{ {
this.currentMP4Index = -1; this.currentMP4Index = -1;
} }
private async Task PlayClipThenStop(VideoClip clip) Coroutine stopCoroutine;
private void PlayClipThenStop(VideoClip clip)
{ {
if (this == null || this.gameObject == null) if (this == null || this.gameObject == null)
return; return;
@ -34,19 +39,22 @@ public class AudioPlayControl : MonoBehaviour
this.gameObject.SetActive(true); this.gameObject.SetActive(true);
player.Play(); player.Play();
isPlaying = true; isPlaying = true;
await Task.Delay((int)(1000 * player.clip.length));
StopPlay();
isPlaying = false;
}
public async Task PlayStartVideo()
{
await PlayClipThenStop(startAudio);
}
public async Task PlayEndVideo()
{
await PlayClipThenStop(endAudio);
}
stopCoroutine = StartCoroutine(WaitForSeconedToStop());
}
IEnumerator WaitForSeconedToStop()
{
yield return new WaitForSeconds((float)player.clip.length);
StopPlay();
}
public void PlayStartVideo()
{
PlayClipThenStop(startAudio);
}
public void PlayEndVideo()
{
PlayClipThenStop(endAudio);
}
public void StopPlay() public void StopPlay()
{ {
if (this == null || this.gameObject == null) if (this == null || this.gameObject == null)
@ -54,19 +62,19 @@ public class AudioPlayControl : MonoBehaviour
isPlaying = false; isPlaying = false;
player.Stop(); player.Stop();
this.gameObject.SetActive(false); this.gameObject.SetActive(false);
OnPlayFinished?.Invoke();
this.StopAllCoroutines();
} }
public bool IsCanPlayMP4() public bool IsCanPlayMP4()
{ {
if (currentMP4Index + 1 == mp4Clips.Length) if (currentMP4Index + 1 == mp4Clips.Length)
return false; return false;
return true; return true;
} }
public async void PlayMP4() public void PlayMP4()
{ {
currentMP4Index++; currentMP4Index++;
var needPlayClip = mp4Clips[currentMP4Index]; var needPlayClip = mp4Clips[currentMP4Index];
await PlayClipThenStop(needPlayClip); PlayClipThenStop(needPlayClip);
OnMP4PlayFinished?.Invoke();
} }
} }

View File

@ -23,16 +23,16 @@ public class GameManager : MonoBehaviour
{ {
SampleMessageListener.OnReceivedMSG += OnReceivedMessage; SampleMessageListener.OnReceivedMSG += OnReceivedMessage;
brainTrans.gameObject.SetActive(false); brainTrans.gameObject.SetActive(false);
videoControl.OnMP4PlayFinished += async () => //videoControl.OnMP4PlayFinished += async () =>
{ //{
if (!videoControl.IsCanPlayMP4()) // if (!videoControl.IsCanPlayMP4())
{ // {
await videoControl.PlayEndVideo(); // await videoControl.PlayEndVideo();
brainTrans.gameObject.SetActive(false); // brainTrans.gameObject.SetActive(false);
videoControl.ResetMP4Index(); // videoControl.ResetMP4Index();
isGameStart = false; // isGameStart = false;
} // }
}; //};
} }
private void OnDestroy() private void OnDestroy()
{ {
@ -43,12 +43,12 @@ public class GameManager : MonoBehaviour
if (msgStr == startMsg) if (msgStr == startMsg)
{ {
print($"开始游戏"); print($"开始游戏");
GamePlay(); GamePlay();
} }
else if (msgStr == stopMsg) else if (msgStr == stopMsg)
{ {
print($"退出游戏"); print($"退出游戏");
Application.Quit(); Application.Quit();
#if UNITY_EDITOR #if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false; UnityEditor.EditorApplication.isPlaying = false;
@ -58,33 +58,34 @@ public class GameManager : MonoBehaviour
{ {
sb?.AppendLine(msgStr); sb?.AppendLine(msgStr);
var arr = msgStr.Split(','); var arr = msgStr.Split(',');
//加速度 //加速度
var ax = float.Parse(arr[0]); var ax = float.Parse(arr[0]);
var ay = float.Parse(arr[1]); var ay = float.Parse(arr[1]);
var az = float.Parse(arr[2]); var az = float.Parse(arr[2]);
//角速度 //角速度
var gx = float.Parse(arr[3]); var gx = float.Parse(arr[3]);
var gy = float.Parse(arr[4]); var gy = float.Parse(arr[4]);
var gz = float.Parse(arr[5]); var gz = float.Parse(arr[5]);
print($"获得加速度:{ax},{ay},{az}"); print($"获得加速度:{ax},{ay},{az}");
print($"获得角速度:{gx},{gy},{gz}"); print($"获得角速度:{gx},{gy},{gz}");
//this.GetComponent<Rigidbody>().angularVelocity //this.GetComponent<Rigidbody>().angularVelocity
if (isCanControlModel) if (isCanControlModel)
{ {
//ControlModelRotate(new Vector3(filteredRoll, filteredPitch, angleZ)); // 传入过滤后的值 //ControlModelRotate(new Vector3(filteredRoll, filteredPitch, angleZ)); // 传入过滤后的值
//brainTrans.rotation = CalculateRotation(brainTrans.rotation, ax, ay, az, gx, gy, gz); //brainTrans.rotation = CalculateRotation(brainTrans.rotation, ax, ay, az, gx, gy, gz);
CalculateRotation(ax, ay, az, gx, gy, gz); CalculateRotation(ax, ay, az, gx, gy, gz);
} }
} }
} }
private async void GamePlay() private void GamePlay()
{ {
isGameStart = true; isGameStart = true;
await videoControl.PlayStartVideo(); videoControl.PlayStartVideo();
JustGamePlayNoAnim(); videoControl.OnPlayFinished += JustGamePlayNoAnim;
} }
private void JustGamePlayNoAnim() private void JustGamePlayNoAnim()
{ {
videoControl.OnPlayFinished -= JustGamePlayNoAnim;
isCanControlModel = true; isCanControlModel = true;
brainTrans.gameObject.SetActive(true); brainTrans.gameObject.SetActive(true);
} }
@ -95,46 +96,46 @@ public class GameManager : MonoBehaviour
private void CalculateRotation(float ax, float ay, float az, float gx, float gy, float gz) private void CalculateRotation(float ax, float ay, float az, float gx, float gy, float gz)
{ {
// 通过加速度计算姿态(俯仰角和滚转角) // 通过加速度计算姿态(俯仰角和滚转角)
float roll = Mathf.Atan2(ay, az) * Mathf.Rad2Deg; // 滚转角 float roll = Mathf.Atan2(ay, az) * Mathf.Rad2Deg; // 滚转角
float pitch = Mathf.Atan2(-ax, Mathf.Sqrt(ay * ay + az * az)) * Mathf.Rad2Deg; // 俯仰角 float pitch = Mathf.Atan2(-ax, Mathf.Sqrt(ay * ay + az * az)) * Mathf.Rad2Deg; // 俯仰角
// 计算角速度的变化(可以设定角速度的阈值或限制其过快变化) // 计算角速度的变化(可以设定角速度的阈值或限制其过快变化)
angleX += gx * dt; // 绕X轴旋转 angleX += gx * dt; // 绕X轴旋转
angleY += gy * dt; // 绕Y轴旋转 angleY += gy * dt; // 绕Y轴旋转
angleZ += gz * dt; // 绕Z轴旋转 angleZ += gz * dt; // 绕Z轴旋转
// 使用互补滤波来平滑姿态估计 // 使用互补滤波来平滑姿态估计
// 这里的系数决定了加速度和陀螺仪的权重通常设置为0.98 // 这里的系数决定了加速度和陀螺仪的权重通常设置为0.98
float alpha = 0.98f; // 互补滤波的权重 float alpha = 0.98f; // 互补滤波的权重
float filteredPitch = alpha * (angleX) + (1 - alpha) * pitch; // 过滤后的俯仰角 float filteredPitch = alpha * (angleX) + (1 - alpha) * pitch; // 过滤后的俯仰角
float filteredRoll = alpha * (angleY) + (1 - alpha) * roll; // 过滤后的滚转角 float filteredRoll = alpha * (angleY) + (1 - alpha) * roll; // 过滤后的滚转角
brainTrans.rotation = Quaternion.Euler(new Vector3(filteredRoll, filteredPitch, angleZ)); brainTrans.rotation = Quaternion.Euler(new Vector3(filteredRoll, filteredPitch, angleZ));
} }
private Quaternion CalculateRotation(Quaternion rotation, float ax, float ay, float az, float gx, float gy, float gz) private Quaternion CalculateRotation(Quaternion rotation, float ax, float ay, float az, float gx, float gy, float gz)
{ {
// 假设你已经有初始四元数 // 假设你已经有初始四元数
Quaternion q = rotation; Quaternion q = rotation;
// 将角速度转换为四元数 // 将角速度转换为四元数
Quaternion gyroQuat = new Quaternion(0, gx * dt, gy * dt, gz * dt); Quaternion gyroQuat = new Quaternion(0, gx * dt, gy * dt, gz * dt);
// 更新四元数:四元数乘积 // 更新四元数:四元数乘积
q = q * gyroQuat; q = q * gyroQuat;
// 归一化四元数,防止数值误差积累 // 归一化四元数,防止数值误差积累
q.Normalize(); q.Normalize();
// 使用加速度计数据修正四元数 // 使用加速度计数据修正四元数
Quaternion accelQuat = new Quaternion(ax, ay, az, 0); Quaternion accelQuat = new Quaternion(ax, ay, az, 0);
q = Quaternion.Slerp(q, accelQuat, 0.01f); // 使用球形插值修正四元数 q = Quaternion.Slerp(q, accelQuat, 0.01f); // 使用球形插值修正四元数
// 最终得到物体的旋转 // 最终得到物体的旋转
return q; return q;
} }
private StringBuilder sb; private StringBuilder sb;
private async void Update() private void Update()
{ {
if (isTestInput) if (isTestInput)
{ {
@ -156,15 +157,14 @@ public class GameManager : MonoBehaviour
videoControl.StopPlay(); videoControl.StopPlay();
JustGamePlayNoAnim(); JustGamePlayNoAnim();
} }
if (Input.GetKeyDown(KeyCode.Z)) if (Input.GetKeyDown(KeyCode.Z))
{ {
Debug.LogWarning("开始录制"); Debug.LogWarning("开始录制");
sb = new StringBuilder(); sb = new StringBuilder();
} }
if (Input.GetKeyDown(KeyCode.X)) if (Input.GetKeyDown(KeyCode.X))
{ {
Debug.LogWarning($"录制结束! 参数:\n{sb}"); Debug.LogWarning($"录制结束! 参数:\n{sb}");
sb = null; sb = null;
} }
if (Input.GetKeyDown(KeyCode.C)) if (Input.GetKeyDown(KeyCode.C))
@ -196,9 +196,21 @@ public class GameManager : MonoBehaviour
if (videoControl.IsCanPlayMP4()) if (videoControl.IsCanPlayMP4())
{ {
videoControl.PlayMP4(); videoControl.PlayMP4();
if (!videoControl.IsCanPlayMP4())// 播放到了最后一个视频, 自动播放End动画
{
brainTrans.gameObject.SetActive(false);
videoControl.PlayEndVideo();
videoControl.OnPlayFinished += ResetToGameStart;
}
} }
} }
} }
private void ResetToGameStart()
{
videoControl.OnPlayFinished -= ResetToGameStart;
videoControl.ResetMP4Index();
isGameStart = false;
}
private string sendMsgDataArr = "0.03,0.00,0.83,-5.42,-3.05,-2.17\r\n0.07,-0.03,0.80,-4.53,-1.27,1.87\r\n0.04,-0.02,0.82,-7.34,-0.99,-1.26\r\n0.04,-0.02,0.82,-1.22,-3.95,3.66\r\n0.03,0.02,0.81,-6.32,-4.70,-3.34\r\n0.05,-0.03,0.82,-6.53,-2.40,-2.28\r\n0.05,-0.00,0.82,-6.65,-3.69,6.26\r\n0.04,-0.01,0.83,-4.22,-4.48,4.00\r\n0.07,-0.03,0.81,-5.82,-2.50,-5.01\r\n0.05,-0.01,0.82,-4.89,-2.97,-1.27\r\n0.04,0.01,0.82,-6.31,-2.71,0.60\r\n0.07,-0.02,0.81,-1.50,-1.15,-7.15\r\n0.04,0.01,0.82,-7.13,3.05,-15.97\r\n0.06,-0.03,0.81,-3.79,1.00,-45.29\r\n-0.03,-0.06,0.87,-6.42,0.47,-123.16\r\n-0.01,0.02,0.78,-10.50,4.44,-90.85\r\n0.06,-0.01,0.81,-6.31,7.85,-80.14\r\n0.05,-0.04,0.82,-14.08,-0.04,-110.42\r\n-0.08,-0.09,0.85,-11.08,-3.77,-86.89\r\n0.02,-0.01,0.82,-9.47,3.86,-72.44\r\n0.03,-0.05,0.82,-12.89,6.39,-133.51\r\n-0.00,-0.10,0.83,-27.43,2.75,-189.82\r\n0.03,-0.09,0.82,-14.63,7.84,-137.10\r\n0.09,-0.10,0.79,-19.21,9.53,-175.67\r\n0.02,-0.14,0.81,-13.94,3.36,-197.25\r\n0.03,-0.10,0.79,-23.36,2.68,-112.65\r\n0.03,-0.11,0.86,-9.88,12.50,-140.66\r\n0.01,-0.22,0.81,-12.49,12.49,-125.18\r\n0.09,-0.06,0.84,-9.07,11.02,-110.91\r\n-0.24,-0.09,0.77,-15.20,43.73,-75.92\r\n0.13,-0.10,0.80,-10.72,17.12,-129.89\r\n0.05,-0.14,0.80,-11.35,4.04,6.60\r\n0.03,-0.17,0.82,-5.33,-3.23,-9.43\r\n0.01,-0.18,0.79,-3.86,-2.73,0.50\r\n0.06,-0.12,0.85,-5.76,-0.42,-9.27\r\n0.07,-0.15,0.81,-4.90,-6.82,2.41\r\n0.04,-0.18,0.80,-3.12,-0.34,19.65\r\n0.06,-0.14,0.82,-4.18,-2.79,-7.72\r\n0.05,-0.14,0.80,-6.60,-2.23,-0.33"; private string sendMsgDataArr = "0.03,0.00,0.83,-5.42,-3.05,-2.17\r\n0.07,-0.03,0.80,-4.53,-1.27,1.87\r\n0.04,-0.02,0.82,-7.34,-0.99,-1.26\r\n0.04,-0.02,0.82,-1.22,-3.95,3.66\r\n0.03,0.02,0.81,-6.32,-4.70,-3.34\r\n0.05,-0.03,0.82,-6.53,-2.40,-2.28\r\n0.05,-0.00,0.82,-6.65,-3.69,6.26\r\n0.04,-0.01,0.83,-4.22,-4.48,4.00\r\n0.07,-0.03,0.81,-5.82,-2.50,-5.01\r\n0.05,-0.01,0.82,-4.89,-2.97,-1.27\r\n0.04,0.01,0.82,-6.31,-2.71,0.60\r\n0.07,-0.02,0.81,-1.50,-1.15,-7.15\r\n0.04,0.01,0.82,-7.13,3.05,-15.97\r\n0.06,-0.03,0.81,-3.79,1.00,-45.29\r\n-0.03,-0.06,0.87,-6.42,0.47,-123.16\r\n-0.01,0.02,0.78,-10.50,4.44,-90.85\r\n0.06,-0.01,0.81,-6.31,7.85,-80.14\r\n0.05,-0.04,0.82,-14.08,-0.04,-110.42\r\n-0.08,-0.09,0.85,-11.08,-3.77,-86.89\r\n0.02,-0.01,0.82,-9.47,3.86,-72.44\r\n0.03,-0.05,0.82,-12.89,6.39,-133.51\r\n-0.00,-0.10,0.83,-27.43,2.75,-189.82\r\n0.03,-0.09,0.82,-14.63,7.84,-137.10\r\n0.09,-0.10,0.79,-19.21,9.53,-175.67\r\n0.02,-0.14,0.81,-13.94,3.36,-197.25\r\n0.03,-0.10,0.79,-23.36,2.68,-112.65\r\n0.03,-0.11,0.86,-9.88,12.50,-140.66\r\n0.01,-0.22,0.81,-12.49,12.49,-125.18\r\n0.09,-0.06,0.84,-9.07,11.02,-110.91\r\n-0.24,-0.09,0.77,-15.20,43.73,-75.92\r\n0.13,-0.10,0.80,-10.72,17.12,-129.89\r\n0.05,-0.14,0.80,-11.35,4.04,6.60\r\n0.03,-0.17,0.82,-5.33,-3.23,-9.43\r\n0.01,-0.18,0.79,-3.86,-2.73,0.50\r\n0.06,-0.12,0.85,-5.76,-0.42,-9.27\r\n0.07,-0.15,0.81,-4.90,-6.82,2.41\r\n0.04,-0.18,0.80,-3.12,-0.34,19.65\r\n0.06,-0.14,0.82,-4.18,-2.79,-7.72\r\n0.05,-0.14,0.80,-6.60,-2.23,-0.33";
} }

View File

@ -4,5 +4,8 @@
EditorBuildSettings: EditorBuildSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 2 serializedVersion: 2
m_Scenes: [] m_Scenes:
- enabled: 1
path: Assets/Scenes/MainScene.unity
guid: a17c0053f1e7b1f48a3a247da8346886
m_configObjects: {} m_configObjects: {}

View File

@ -0,0 +1,16 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &1
MonoBehaviour:
m_ObjectHideFlags: 53
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a287be6c49135cd4f9b2b8666c39d999, type: 3}
m_Name:
m_EditorClassIdentifier:
assetDefaultFramerate: 60
m_DefaultFrameRate: 60

View File

@ -4,7 +4,7 @@
UnityConnectSettings: UnityConnectSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 1 serializedVersion: 1
m_Enabled: 0 m_Enabled: 1
m_TestMode: 0 m_TestMode: 0
m_EventOldUrl: https://api.uca.cloud.unity3d.com/v1/events m_EventOldUrl: https://api.uca.cloud.unity3d.com/v1/events
m_EventUrl: https://cdp.cloud.unity3d.com/v1/events m_EventUrl: https://cdp.cloud.unity3d.com/v1/events