2024-12-01 16:22:17 +08:00
|
|
|
|
using System.Text;
|
2024-12-01 17:25:54 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2024-12-01 15:24:10 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class GameManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public string startMsg = "START";
|
|
|
|
|
public string stopMsg = "STOP";
|
|
|
|
|
|
|
|
|
|
public AudioPlayControl videoControl;
|
|
|
|
|
private bool isCanControlModel = false;
|
|
|
|
|
|
|
|
|
|
public Transform brainTrans;
|
|
|
|
|
public float dt = 0.1f;
|
|
|
|
|
|
|
|
|
|
public bool isTestInput = false;
|
|
|
|
|
float angleX, angleY, angleZ;
|
|
|
|
|
|
2024-12-01 17:34:51 +08:00
|
|
|
|
public float smoothValue = 100;
|
|
|
|
|
|
2024-12-01 15:24:10 +08:00
|
|
|
|
public void Awake()
|
|
|
|
|
{
|
|
|
|
|
SampleMessageListener.OnReceivedMSG += OnReceivedMessage;
|
2024-12-01 15:48:43 +08:00
|
|
|
|
brainTrans.gameObject.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
SampleMessageListener.OnReceivedMSG -= OnReceivedMessage;
|
2024-12-01 15:24:10 +08:00
|
|
|
|
}
|
|
|
|
|
private void OnReceivedMessage(string msgStr)
|
|
|
|
|
{
|
2024-12-01 17:25:54 +08:00
|
|
|
|
|
2024-12-01 15:24:10 +08:00
|
|
|
|
if (msgStr == startMsg)
|
|
|
|
|
{
|
|
|
|
|
print($"<22><>ʼ<EFBFBD><CABC>Ϸ");
|
|
|
|
|
GamePlay();
|
|
|
|
|
}
|
|
|
|
|
else if (msgStr == stopMsg)
|
|
|
|
|
{
|
|
|
|
|
print($"<22>˳<EFBFBD><CBB3><EFBFBD>Ϸ");
|
|
|
|
|
Application.Quit();
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-01 16:22:17 +08:00
|
|
|
|
sb?.AppendLine(msgStr);
|
2024-12-01 15:24:10 +08:00
|
|
|
|
var arr = msgStr.Split(',');
|
|
|
|
|
//<2F><><EFBFBD>ٶ<EFBFBD>
|
|
|
|
|
var ax = float.Parse(arr[0]);
|
|
|
|
|
var ay = float.Parse(arr[1]);
|
|
|
|
|
var az = float.Parse(arr[2]);
|
|
|
|
|
//<2F><><EFBFBD>ٶ<EFBFBD>
|
|
|
|
|
var gx = float.Parse(arr[3]);
|
|
|
|
|
var gy = float.Parse(arr[4]);
|
|
|
|
|
var gz = float.Parse(arr[5]);
|
|
|
|
|
print($"<22><><EFBFBD>ü<EFBFBD><C3BC>ٶ<EFBFBD>:{ax},{ay},{az}");
|
|
|
|
|
print($"<22><><EFBFBD>ý<EFBFBD><C3BD>ٶ<EFBFBD>:{gx},{gy},{gz}");
|
2024-12-01 17:25:54 +08:00
|
|
|
|
//this.GetComponent<Rigidbody>().angularVelocity
|
2024-12-01 15:24:10 +08:00
|
|
|
|
if (isCanControlModel)
|
|
|
|
|
{
|
2024-12-01 17:25:54 +08:00
|
|
|
|
//ControlModelRotate(new Vector3(filteredRoll, filteredPitch, angleZ)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD><CBBA><EFBFBD>ֵ
|
2024-12-01 17:34:51 +08:00
|
|
|
|
var result = CalculateRotation(brainTrans.rotation, ax, ay, az, gx, gy, gz);
|
|
|
|
|
brainTrans.rotation = Quaternion.Slerp(brainTrans.rotation, result, Time.deltaTime * smoothValue);
|
2024-12-01 15:24:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private async void GamePlay()
|
|
|
|
|
{
|
|
|
|
|
await videoControl.PlayStartVideo();
|
|
|
|
|
JustGamePlayNoAnim();
|
|
|
|
|
}
|
|
|
|
|
private void JustGamePlayNoAnim()
|
|
|
|
|
{
|
|
|
|
|
isCanControlModel = true;
|
|
|
|
|
brainTrans.gameObject.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
private void ControlModelRotate(Vector3 angleModify)
|
|
|
|
|
{
|
|
|
|
|
brainTrans.rotation = Quaternion.Euler(angleModify);
|
|
|
|
|
}
|
2024-12-01 17:25:54 +08:00
|
|
|
|
private Quaternion CalculateRotation(Quaternion rotation, float ax, float ay, float az, float gx, float gy, float gz)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE>г<EFBFBD>ʼ<EFBFBD><CABC>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
Quaternion q = rotation;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
Quaternion gyroQuat = new Quaternion(0, gx * dt, gy * dt, gz * dt);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>˻<EFBFBD>
|
|
|
|
|
q = q * gyroQuat;
|
2024-12-01 15:24:10 +08:00
|
|
|
|
|
2024-12-01 17:25:54 +08:00
|
|
|
|
// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
q.Normalize();
|
|
|
|
|
|
|
|
|
|
// ʹ<>ü<EFBFBD><C3BC>ٶȼ<D9B6><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
Quaternion accelQuat = new Quaternion(ax, ay, az, 0);
|
|
|
|
|
q = Quaternion.Slerp(q, accelQuat, 0.01f); // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
// <20><><EFBFBD>յõ<D5B5><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
|
|
|
|
|
return q;
|
|
|
|
|
}
|
2024-12-01 16:22:17 +08:00
|
|
|
|
|
|
|
|
|
private StringBuilder sb;
|
2024-12-01 15:24:10 +08:00
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (isTestInput)
|
|
|
|
|
{
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
|
|
|
{
|
|
|
|
|
OnReceivedMessage(startMsg);
|
|
|
|
|
}
|
2024-12-01 15:48:43 +08:00
|
|
|
|
if (Input.GetKey(KeyCode.Alpha2))
|
2024-12-01 15:24:10 +08:00
|
|
|
|
{
|
2024-12-01 15:48:43 +08:00
|
|
|
|
float randomF() => Random.Range(0.0f, 1.0f);
|
|
|
|
|
OnReceivedMessage($"{randomF()},{randomF()},{randomF()},{randomF()},{randomF()},{randomF()}");
|
2024-12-01 15:24:10 +08:00
|
|
|
|
}
|
|
|
|
|
else if (Input.GetKeyDown(KeyCode.Alpha3))
|
|
|
|
|
{
|
|
|
|
|
OnReceivedMessage(stopMsg);
|
2024-12-01 15:48:43 +08:00
|
|
|
|
}
|
|
|
|
|
else if (Input.GetKeyDown(KeyCode.F))
|
2024-12-01 15:24:10 +08:00
|
|
|
|
{
|
|
|
|
|
videoControl.StopPlay();
|
|
|
|
|
JustGamePlayNoAnim();
|
|
|
|
|
}
|
2024-12-01 16:22:17 +08:00
|
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.Z))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("<22><>ʼ¼<CABC><C2BC>");
|
|
|
|
|
sb = new StringBuilder();
|
|
|
|
|
}
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.X))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"¼<>ƽ<EFBFBD><C6BD><EFBFBD>! <20><><EFBFBD><EFBFBD>:\n{sb}");
|
|
|
|
|
sb = null;
|
|
|
|
|
}
|
2024-12-01 17:25:54 +08:00
|
|
|
|
if (Input.GetKeyDown(KeyCode.C))
|
|
|
|
|
{
|
|
|
|
|
print(sendMsgDataArr);
|
|
|
|
|
SendAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void SendAsync()
|
|
|
|
|
{
|
|
|
|
|
var rows = sendMsgDataArr.Split("\r\n");
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (index < rows.Length)
|
|
|
|
|
{
|
|
|
|
|
var row = rows[index];
|
|
|
|
|
OnReceivedMessage(row);
|
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 15:24:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-01 17:25:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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";
|
2024-12-01 15:24:10 +08:00
|
|
|
|
}
|