|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.XR.Interaction.Toolkit;
|
|
|
|
|
|
|
|
|
|
public class SyncTransform : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private XRGrabInteractable xRGrab;
|
|
|
|
|
private SyncTransformData syncTransformData;
|
|
|
|
|
private OriginTransformData originTransformData;
|
|
|
|
|
public SyncTransformData SyncTransformData
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
get => syncTransformData;
|
|
|
|
|
}
|
|
|
|
|
public int id = 0;
|
|
|
|
|
private Queue<SyncTransformData> syncQueue = new Queue<SyncTransformData>();
|
|
|
|
|
public Queue<SyncTransformData> SyncQueue
|
|
|
|
|
{
|
|
|
|
|
get => syncQueue;
|
|
|
|
|
set => syncQueue = value;
|
|
|
|
|
}
|
|
|
|
|
private void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
syncTransformData.pos = transform.position;
|
|
|
|
|
syncTransformData.rot = transform.eulerAngles;
|
|
|
|
|
syncTransformData.size = transform.localScale;
|
|
|
|
|
syncTransformData.ID = id;
|
|
|
|
|
syncTransformData.isGrip = false;
|
|
|
|
|
}
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
SyncManager.SyncTransformList.Add(this);
|
|
|
|
|
}
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
xRGrab = GetComponent<XRGrabInteractable>();
|
|
|
|
|
StartCoroutine(SyncTrans());
|
|
|
|
|
originTransformData.pos = transform.position;
|
|
|
|
|
originTransformData.rot = transform.eulerAngles;
|
|
|
|
|
originTransformData.size = transform.localScale;
|
|
|
|
|
originTransformData.ID = id;
|
|
|
|
|
Debug.Log(originTransformData.pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
UpdateSyncTrans();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateSyncTrans()
|
|
|
|
|
{
|
|
|
|
|
if (xRGrab!=null)
|
|
|
|
|
{
|
|
|
|
|
if (xRGrab.isSelected)
|
|
|
|
|
{
|
|
|
|
|
syncTransformData.isGrip = true;
|
|
|
|
|
syncTransformData.time = GetTimeStamp();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
syncTransformData.isGrip = false;
|
|
|
|
|
}
|
|
|
|
|
syncTransformData.pos = transform.position;
|
|
|
|
|
syncTransformData.rot = transform.eulerAngles;
|
|
|
|
|
syncTransformData.size = transform.localScale;
|
|
|
|
|
syncTransformData.ID = id;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private long GetTimeStamp()
|
|
|
|
|
{
|
|
|
|
|
DateTime currentTime = DateTime.Now;
|
|
|
|
|
long unixTimestampSeconds = (long)(currentTime - new DateTime(1970, 1, 1)).TotalSeconds;
|
|
|
|
|
return unixTimestampSeconds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator SyncTrans()
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
while (syncQueue.Count>0)
|
|
|
|
|
{
|
|
|
|
|
SyncTransformData data = syncQueue.Dequeue();
|
|
|
|
|
Debug.Log("data" + data.time);
|
|
|
|
|
if (data.time>= syncTransformData.time)
|
|
|
|
|
{
|
|
|
|
|
xRGrab.enabled = false;
|
|
|
|
|
float elapsedTime = 0f;
|
|
|
|
|
Vector3 startingPosition = transform.position;
|
|
|
|
|
Quaternion startRotation = transform.rotation;
|
|
|
|
|
while (elapsedTime < SyncManager.syncTime)
|
|
|
|
|
{
|
|
|
|
|
elapsedTime += Time.deltaTime;
|
|
|
|
|
float t = Mathf.Clamp01(elapsedTime / SyncManager.syncTime); // ȡ[0, 1]֮ IJ ֵ
|
|
|
|
|
transform.position = Vector3.Lerp(startingPosition, data.pos, t); // ʹ Բ ֵƽ ƶ
|
|
|
|
|
transform.rotation = Quaternion.Lerp(startRotation, Quaternion.Euler(data.rot), elapsedTime / SyncManager.syncTime);
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
transform.position = data.pos;
|
|
|
|
|
transform.eulerAngles = data.rot;
|
|
|
|
|
transform.localScale = data.size;
|
|
|
|
|
syncTransformData.time = data.time;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
xRGrab.enabled = true;
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public struct OriginTransformData
|
|
|
|
|
{
|
|
|
|
|
public Vector3 pos;
|
|
|
|
|
public Vector3 rot;
|
|
|
|
|
public Vector3 size;
|
|
|
|
|
public int ID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public struct SyncTransformData
|
|
|
|
|
{
|
|
|
|
|
public Vector3 pos;
|
|
|
|
|
public Vector3 rot;
|
|
|
|
|
public Vector3 size;
|
|
|
|
|
public bool isGrip;
|
|
|
|
|
public int ID;
|
|
|
|
|
public long time;
|
|
|
|
|
}
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public struct SyncTransformDatas
|
|
|
|
|
{
|
|
|
|
|
public List<SyncTransformData> syncTransforms;
|
|
|
|
|
}
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public struct SyncMessage
|
|
|
|
|
{
|
|
|
|
|
public List<SyncTransformDatas> Message;
|
|
|
|
|
}
|