AR文件更新流程&AR网络延时问题
This commit is contained in:
1 parent
16cd2fd26b
commit
74ba5790f9
18 files changed
+867
-508
No files matched your search
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Assets.Scenes.Ride.Scripts;
|
||||
using Assets.Scripts.Apis.Models;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace Assets.AR
|
||||
{
|
||||
public class ARDownloader
|
||||
{
|
||||
private MapRoute _route;
|
||||
|
||||
private readonly string _localARDataPath;
|
||||
private readonly string _localARRoutePath;
|
||||
private readonly string _localVideoPath;
|
||||
private readonly string _serverARDataPath;
|
||||
private readonly string _serverARRoutePath;
|
||||
private readonly string _serverVideoPath;
|
||||
|
||||
public bool IsVideoDownloaded { get; private set; }
|
||||
public bool IsARDataDownloaded { get; private set; }
|
||||
public bool IsARRouteDownloaded { get; private set; }
|
||||
public float Total { get; private set; } = 0;
|
||||
|
||||
public ARDownloader(MapRoute route)
|
||||
{
|
||||
this._route = route;
|
||||
|
||||
var dataPath = $"{PFConstants.ARFolder}/{_route.Id}";
|
||||
var path = PFConstants.VideoFolder;
|
||||
Helper.CreateDirectoryIfNotExsit(dataPath);
|
||||
|
||||
_localARDataPath = $"{dataPath}/{_route.Id}.json";
|
||||
_localARRoutePath = $"{dataPath}/route-{_route.Id}.json";
|
||||
_localVideoPath = $"{path}/{_route.FileName}";
|
||||
|
||||
_serverARDataPath = _route.ARConfig;
|
||||
_serverARRoutePath = _route.VideoRoute;
|
||||
_serverVideoPath = _route.Url;
|
||||
}
|
||||
|
||||
public async UniTask<UnityWebRequest> DownLoadVideoAsync(IProgress<float> progress = null,
|
||||
CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
return await DownloadToFileAsync(_serverVideoPath, _localVideoPath, progress, cancellation);
|
||||
}
|
||||
|
||||
public async UniTask<UnityWebRequest> DownLoadARRouteAsync(IProgress<float> progress = null,
|
||||
CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
return await DownloadToFileAsync(_serverARRoutePath, _localARRoutePath, progress, cancellation);
|
||||
}
|
||||
|
||||
public async UniTask<UnityWebRequest> DownLoadARDataAsync(IProgress<float> progress = null,
|
||||
CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
return await DownloadToFileAsync(_serverARDataPath, _localARDataPath, progress, cancellation);
|
||||
}
|
||||
|
||||
public async Task<bool> CheckAllAsync()
|
||||
{
|
||||
IsARRouteDownloaded = await CheckARRouteAsync();
|
||||
IsARDataDownloaded = await CheckARDataAsync();
|
||||
IsVideoDownloaded = await CheckVideoAsync();
|
||||
|
||||
if (IsVideoDownloaded)
|
||||
Total += 1f;
|
||||
if (IsARRouteDownloaded)
|
||||
Total += 1f;
|
||||
if (IsARDataDownloaded)
|
||||
Total += 1f;
|
||||
|
||||
return IsVideoDownloaded || IsARRouteDownloaded || IsARDataDownloaded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return true if the video is needed to download
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> CheckVideoAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(_localVideoPath)) return true;
|
||||
var result = await HeadFileAsync(_serverVideoPath, _localVideoPath);
|
||||
return result.responseCode != 304;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return true if the video is needed to download
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> CheckARRouteAsync()
|
||||
{
|
||||
if (!File.Exists(_localARRoutePath)) return true;
|
||||
var result = await HeadFileAsync(_serverARRoutePath, _localARRoutePath);
|
||||
return result.responseCode != 304;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return true if the video is needed to download
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> CheckARDataAsync()
|
||||
{
|
||||
if (!File.Exists(_localARDataPath)) return true;
|
||||
var result = await HeadFileAsync(_serverARDataPath, _localARDataPath);
|
||||
return result.responseCode != 304;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 下载文件到本地(持续存入磁盘减少内存占用)
|
||||
/// </summary>
|
||||
/// <param name="downloadUrl">文件连接</param>
|
||||
/// <param name="fullPath">本地文件全路径</param>
|
||||
/// <param name="progress">下载进度</param>
|
||||
public static async UniTask<UnityWebRequest> DownloadToFileAsync(string downloadUrl, string fullPath,
|
||||
IProgress<float> progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
var dh = new DownloadHandlerFile(fullPath)
|
||||
{
|
||||
removeFileOnAbort = true
|
||||
};
|
||||
|
||||
var request = UnityWebRequest.Get(downloadUrl);
|
||||
request.downloadHandler = dh;
|
||||
request.method = UnityWebRequest.kHttpVerbGET;
|
||||
|
||||
var result = await request.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update,
|
||||
(cancellation?.Token ?? default(CancellationToken)));
|
||||
|
||||
var etag = result.GetResponseHeader("ETag");
|
||||
var path = fullPath + ".etag";
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
File.Create(path);
|
||||
}
|
||||
File.WriteAllText(path, etag);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// head request for file meta info such as etag.
|
||||
/// </summary>
|
||||
/// <param name="downloadUrl">download url</param>
|
||||
/// <param name="fullPath">local path for storage</param>
|
||||
/// <returns></returns>
|
||||
public static async UniTask<UnityWebRequest> HeadFileAsync(string downloadUrl, string fullPath,
|
||||
IProgress<float> progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
try
|
||||
{
|
||||
var etagFile = fullPath + ".etag";
|
||||
var etag = string.Empty;
|
||||
if (File.Exists(etagFile))
|
||||
{
|
||||
etag = File.ReadAllText(etagFile);
|
||||
}
|
||||
|
||||
var request = UnityWebRequest.Head(downloadUrl);
|
||||
if (!string.IsNullOrEmpty(etag))
|
||||
request.SetRequestHeader("If-None-Match", etag);
|
||||
|
||||
var result =
|
||||
await request
|
||||
.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update,
|
||||
(cancellation?.Token ?? default(CancellationToken)));
|
||||
|
||||
if (result.responseCode != 304)
|
||||
{
|
||||
etag = result.GetResponseHeader("ETag");
|
||||
File.WriteAllText(fullPath + ".etag", etag);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84a219eba2114884a2e246578ade9ab9
|
||||
timeCreated: 1684373675
|
||||
@@ -39,13 +39,16 @@ namespace Assets.AR
|
||||
|
||||
public int TestPower { get; set; }
|
||||
|
||||
public float ZOffset { get; set; }
|
||||
protected void Start()
|
||||
{
|
||||
this.Lane = route.Lane;//轨迹上的距离
|
||||
}
|
||||
|
||||
//重置碰撞检测参数
|
||||
public void ResetCollisionDetectionParameters()
|
||||
{
|
||||
this.StartPosition = 0;//开始位置
|
||||
this.Lane = 0.0f;//轨迹上的距离
|
||||
this.Lane = route.Lane;//轨迹上的距离
|
||||
this.DeltaLane = 0.0f;//差距
|
||||
this.LaneWidth = 0.0f;//轨迹宽度
|
||||
this.LaneCamera = 0.0f;//轨迹上摄像头的位置
|
||||
@@ -59,10 +62,10 @@ namespace Assets.AR
|
||||
{
|
||||
PositionOffset = Vector3.up * route.CameraHeight * (-1);
|
||||
var cameraRotation = this.GetCameraRotation(this.frame - this.FrameIndexDistanceCorrection);
|
||||
var normalizeCameraRotation = Quaternion.Euler(new Vector3(cameraRotation.eulerAngles.x, cameraRotation.eulerAngles.y, cameraRotation.eulerAngles.z+ZOffset));
|
||||
var normalizeCameraRotation = Quaternion.Euler(new Vector3(cameraRotation.eulerAngles.x+route.CameraRotationsOffset.x, cameraRotation.eulerAngles.y+route.CameraRotationsOffset.y, cameraRotation.eulerAngles.z+route.CameraRotationsOffset.z));
|
||||
|
||||
var filteredCameraPosition = this.GetFilteredCameraPosition(this.frame - this.FrameIndexDistanceCorrection);
|
||||
var vector3 = Vector3.left * (this.route.LeftHanded ? -1f : 1f) * (this.LaneWidth * (this.Lane + this.LaneCamera) - this.BaseOffset);
|
||||
var vector3 = Vector3.left * (this.route.LeftHanded ? -1f : 1f) * (this.LaneWidth * (this.Lane + this.LaneCamera + this.route.Lane) - this.BaseOffset);
|
||||
var targetPos = filteredCameraPosition + normalizeCameraRotation * (vector3 + this.PositionOffset);
|
||||
|
||||
this.transform.position = targetPos;
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Assets.AR
|
||||
{
|
||||
protected const float LaneWidth = 0.7f;
|
||||
protected const float StartRegionDistance = 200f;
|
||||
private const float StartRowsGap = 2.2f;
|
||||
private const float StartRowsGap = 1.0f;
|
||||
private const float StartRowsRandomDistance = 0.4f;
|
||||
private const float StartRowsRandomWidth = 0.15f;
|
||||
private const float MinCollisionDistance = 1.33f;
|
||||
@@ -34,7 +34,7 @@ namespace Assets.AR
|
||||
{
|
||||
var frame = 100f;
|
||||
this.StartRegionRouteWidth = this.GetRouteLeftOffset(frame) + this.GetRouteRightOffset(frame);
|
||||
this.RiderCountInStartRow = Mathf.Max(2, Mathf.CeilToInt(this.StartRegionRouteWidth / 0.7f));
|
||||
this.RiderCountInStartRow = Mathf.Max(1, Mathf.CeilToInt(this.StartRegionRouteWidth / LaneWidth));
|
||||
}
|
||||
|
||||
protected override void UpdateGameObjects(
|
||||
@@ -96,9 +96,9 @@ namespace Assets.AR
|
||||
this.CalculateOvertakingParameters();
|
||||
foreach (ARLaneGameObject collision in this.collisionList)
|
||||
{
|
||||
collision.LaneWidth = 0.7f;
|
||||
collision.LaneWidth = LaneWidth;
|
||||
var num6 = this.Route.LeftHanded ? this.GetRouteLeftOffset(collision.Frame) : this.GetRouteRightOffset(collision.Frame);
|
||||
var num7 = Mathf.Lerp(this.GetStartOffset(collision.StartPosition).x, 0.0f, collision.Distance / 200f);
|
||||
var num7 = Mathf.Lerp(this.GetStartOffset(collision.StartPosition).x, 0.0f, collision.Distance / StartRegionDistance);
|
||||
collision.BaseOffset = num6 + num7;
|
||||
}
|
||||
this.CalculateCameraAvoidance(cameraDistance);
|
||||
@@ -224,22 +224,19 @@ namespace Assets.AR
|
||||
Vector2 startOffset;
|
||||
if (this.randomizedStartOffsets.TryGetValue(atIndex, out startOffset))
|
||||
return startOffset;
|
||||
startOffset = new Vector2(UnityEngine.Random.Range(-0.15f, 0.15f), UnityEngine.Random.Range(-0.4f, 0.4f));
|
||||
startOffset = new Vector2(UnityEngine.Random.Range(-StartRowsRandomWidth, StartRowsRandomWidth), UnityEngine.Random.Range(-StartRowsRandomDistance,StartRowsRandomDistance));
|
||||
this.randomizedStartOffsets.Add(atIndex, startOffset);
|
||||
return startOffset;
|
||||
}
|
||||
|
||||
public float GetRouteDistance(int startPosition, float routeDistance)
|
||||
{
|
||||
if (!this.AllowStartOrder || (double)routeDistance >= 200.0)
|
||||
if (!this.AllowStartOrder || (double)routeDistance >= StartRegionDistance)
|
||||
return routeDistance;
|
||||
var start = (float)((RiderCountInStartRow == 0 ? 0 : (startPosition - 1) / this.RiderCountInStartRow) * 2.2000000476837158 + 6.0) + this.GetStartOffset(startPosition).y;
|
||||
var num = Mathf.Lerp(start , 0.0f, routeDistance / 200f);
|
||||
float num = Mathf.Lerp((float) ((double) ((startPosition - 1) / this.RiderCountInStartRow) * StartRowsGap + 6.0) + this.GetStartOffset(startPosition).y, 0.0f, routeDistance / StartRegionDistance);
|
||||
return routeDistance + num;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//计算碰撞距离
|
||||
protected static float GetCollisionParameter(
|
||||
float distSource,
|
||||
|
||||
+7
-10
@@ -59,8 +59,6 @@ namespace Assets.AR
|
||||
public int VideoFrameOffset { get; set; }
|
||||
|
||||
public float CameraHeight { get; set; }
|
||||
|
||||
public float ZOffset { get; set; }
|
||||
|
||||
public float RiderScale { get; set; } = 1f;
|
||||
|
||||
@@ -73,10 +71,15 @@ namespace Assets.AR
|
||||
public int FrameCount => this.CameraPositions.Length;
|
||||
|
||||
public double Lenght => this.FrameDistances[this.FrameCount - 1];
|
||||
|
||||
public VectorData CameraRotationsOffset { get; set; }
|
||||
|
||||
public float Lane { get; set; }
|
||||
|
||||
private float DefaultLeftSideOffset => !this.LeftHanded ? 4f : 0.0f;
|
||||
|
||||
private float DefaultRightSideOffset => !this.LeftHanded ? 0.0f : 4f;
|
||||
|
||||
|
||||
public int GetVisibilityAt(int frame)
|
||||
{
|
||||
@@ -431,17 +434,9 @@ namespace Assets.AR
|
||||
|
||||
public void LoadData(ARRouteData data)
|
||||
{
|
||||
// //todo:修改rotation z
|
||||
// foreach (var rotation in data.CameraRotations)
|
||||
// {
|
||||
// rotation.z += 4f;
|
||||
// }
|
||||
//
|
||||
// var str = JsonConvert.SerializeObject(data);
|
||||
this.Visibility = data.Visibility;
|
||||
this.VideoFrameOffset = data.VideoFrameOffset;
|
||||
this.LeftHanded = data.LeftHanded;
|
||||
this.ZOffset = data.Zoffset;
|
||||
this.SlamSegments = data.SlamSegments;
|
||||
this.CameraHeight = data.CameraHeight;
|
||||
this.RiderScale = (double)data.RiderScale != 0.0 ? data.RiderScale : 1f;
|
||||
@@ -454,6 +449,8 @@ namespace Assets.AR
|
||||
this.LeftSideOffsets = data.LeftSideOffsets;
|
||||
this.RightSideOffsetFrames = data.RightSideOffsetFrames;
|
||||
this.RightSideOffsets = data.RightSideOffsets;
|
||||
this.CameraRotationsOffset = data.CameraRotationsOffset;
|
||||
this.Lane = data.Lane;
|
||||
if (data.LightRotationFrames == null || data.LightRotationFrames.Length == 0)
|
||||
{
|
||||
this.LightRotationFrames = new float[0];
|
||||
|
||||
@@ -248,11 +248,12 @@ namespace Assets.AR
|
||||
public int VideoFrameOffsetMac = -1;
|
||||
public float CameraHeight;
|
||||
public float RiderScale = 1f;
|
||||
public float Zoffset = 0f;
|
||||
public int[] LeftSideOffsetFrames;
|
||||
public float[] LeftSideOffsets;
|
||||
public int[] RightSideOffsetFrames;
|
||||
public float[] RightSideOffsets;
|
||||
public VectorData CameraRotationsOffset;
|
||||
public float Lane;
|
||||
}
|
||||
public class VectorData
|
||||
{
|
||||
|
||||
@@ -91,17 +91,10 @@ namespace Assets.AR
|
||||
}
|
||||
private string LastAnimatorState { get; set; }
|
||||
|
||||
protected override void Start()
|
||||
{
|
||||
base.Start();
|
||||
SetZOffset();
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
if (this.route == null)
|
||||
return;
|
||||
SetMainRiderLane();
|
||||
base.Update();
|
||||
var num = this.Speed;
|
||||
this.Paused = this.Speed == 0;
|
||||
@@ -168,24 +161,6 @@ namespace Assets.AR
|
||||
this.lastVisibilitylevel = this.VisibilityLevel;
|
||||
}
|
||||
|
||||
private void SetZOffset()
|
||||
{
|
||||
var manager = FindObjectOfType<VideoGameManager>();
|
||||
var map = manager.GetMapRoute();
|
||||
this.ZOffset = map.Id == 6296 ? -4f : 0f;
|
||||
}
|
||||
|
||||
private void SetMainRiderLane()
|
||||
{
|
||||
if (IsMain)
|
||||
{
|
||||
var manager = FindObjectOfType<VideoGameManager>();
|
||||
var map = manager.GetMapRoute();
|
||||
if(map.Id == 6296)//TODO:这里写死溧阳AR骑行
|
||||
Lane = -2f;
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
//if (this.Paused || (double)this.Speed <= 0.0)
|
||||
|
||||
Reference in new issue
Block a user