368 lines
14 KiB
C#
368 lines
14 KiB
C#
using Assets.AR;
|
|
using DG.Tweening;
|
|
using RenderHeads.Media.AVProVideo;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace Assets.Scripts.Scenes.VideoRide
|
|
{
|
|
public class TestPlayer : ARLaneObjectsController
|
|
{
|
|
protected Transform LightTrans { get; set; }
|
|
public MediaPlayer mediaPlayer;
|
|
public Transform Player;
|
|
private ARData aRData;
|
|
private RouteDetailData detail;
|
|
private VideoPointsSync videoPointsSync;
|
|
private int index = 1;
|
|
private readonly List<ARLaneObject> collisionList = new List<ARLaneObject>();
|
|
///private Dictionary<int, IRouteRider> riders = new Dictionary<int, IRouteRider>();
|
|
VideoPlayerControl VideoPlayerControl;
|
|
ARLaneObject[] rides;
|
|
public ARLaneObject mainObject;
|
|
|
|
private Light arLight;
|
|
private Light arBackLight;
|
|
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
var txt = Resources.Load<TextAsset>("UI/88019").text;
|
|
var route = Resources.Load<TextAsset>("UI/route-88019").text;
|
|
aRData = Newtonsoft.Json.JsonConvert.DeserializeObject<ARData>(txt);
|
|
detail = Newtonsoft.Json.JsonConvert.DeserializeObject<RouteDetailData>(route);
|
|
|
|
videoPointsSync = new VideoPointsSync(detail.VideoPoints.Select(c => new VideoPoint()
|
|
{
|
|
Distance = (float)c.Distance,
|
|
Time = (float)c.VideoTime
|
|
}));
|
|
|
|
videoSync = videoPointsSync;
|
|
|
|
this.VideoPlayerControl = new VideoPlayerControl();
|
|
this.VideoPlayerControl.VideoSyncSource = videoSync;
|
|
this.videoPlayer = new AVProVideoPlayer();
|
|
this.videoPlayer.videoPlayer = mediaPlayer;
|
|
SetArRoute(new ARRoute(aRData.Route), videoSync, videoPlayer);
|
|
this.VideoPlayerControl.VideoFrameOffset = this.Route.VideoFrameOffset;
|
|
this.VideoPlayerControl.Route = Route;
|
|
this.VideoPlayerControl.SetVideoPlayer(videoPlayer);
|
|
rides = FindObjectsOfType<ARLaneObject>();
|
|
CreateBackLight();
|
|
}
|
|
|
|
private void CreateBackLight()
|
|
{
|
|
GameObject gameObject2 = new GameObject("ARBackLight");
|
|
gameObject2.layer = this.gameObject.layer;
|
|
gameObject2.transform.SetParent(this.transform);
|
|
this.arBackLight = gameObject2.AddComponent<Light>();
|
|
this.arBackLight.type = LightType.Directional;
|
|
this.arBackLight.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
|
|
this.arBackLight.shadows = LightShadows.None;
|
|
this.arBackLight.intensity = 0.5f;
|
|
this.arBackLight.cullingMask = 512;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
float timers = 0f;
|
|
double weight = 70;
|
|
double bikeWeight = 20;
|
|
|
|
private void Update()
|
|
{
|
|
timers -= Time.deltaTime;
|
|
|
|
while (timers < 0)
|
|
{
|
|
foreach (var item in rides)
|
|
{
|
|
item.Speed = (float)(Assets.Scenes.Ride.Scripts.Helper.CalculateSpeed(0, 0, item.TestPower, weight, bikeWeight) / 3.6d);
|
|
item.Distance += item.Speed;
|
|
item.RouteDistance = item.Distance;
|
|
item.Route = Route;
|
|
item.VideoSync = videoPointsSync;
|
|
if (!this.riderObjects.ContainsKey(item.GetInstanceID()))
|
|
this.riderObjects.Add(item.GetInstanceID(), item);
|
|
}
|
|
|
|
timers += 1f;
|
|
}
|
|
|
|
this.FollowedRiderId = 1;
|
|
this.UpdateCameraFollowDistance();
|
|
VideoPlayerControl.UpdateVideoPlaybackSpeed(mainObject.Speed, mainObject.distance-this.CameraDistance);
|
|
this.FrameIndexDistanceCorrection = this.VideoPlayerControl.FrameIndexDistanceCorrection;
|
|
base.Update();
|
|
this.UpdateRidersVisibility();
|
|
this.UpdateRidersLean();
|
|
|
|
if (Input.GetKeyDown(KeyCode.F))
|
|
this.SetVideoControlMode(VideoControlMode.Front);
|
|
if (Input.GetKeyDown(KeyCode.P))
|
|
this.SetVideoControlMode(VideoControlMode.AutoPanorama);
|
|
if (!Input.GetKeyDown(KeyCode.B))
|
|
return;
|
|
this.SetVideoControlMode(VideoControlMode.Rear);
|
|
}
|
|
|
|
private IEnumerator PlayPosition(ARLaneObject aRLaneObject,double prePos, double offset)
|
|
{
|
|
float t = 0;
|
|
while (t <= 1)
|
|
{
|
|
t += Time.deltaTime * 5f;
|
|
double result = prePos + offset * t;
|
|
aRLaneObject.distance = (float)result;
|
|
aRLaneObject.RouteDistance = (float)result ;
|
|
yield return null;
|
|
}
|
|
}
|
|
public VideoControlMode VideoControlMode { get; private set; }
|
|
private float autoCameraSwitchTimeCounter = 80f;
|
|
public void SetVideoControlMode(VideoControlMode mode)
|
|
{
|
|
if (mode == this.VideoControlMode)
|
|
return;
|
|
if (!this.AR360Version)
|
|
mode = VideoControlMode.Front;
|
|
this.VideoControlMode = mode;
|
|
this.autoCameraSwitchTimeCounter = 80f;
|
|
if (mode == VideoControlMode.AutoPanorama)
|
|
return;
|
|
this.videoPlayer.IntendedRear = mode == VideoControlMode.Rear;
|
|
}
|
|
|
|
public RiderCameraDistance RiderCameraDistance { get; set; }
|
|
//更新摄像机的位置
|
|
private void UpdateCameraFollowDistance()
|
|
{
|
|
this.RiderCameraDistance = RiderCameraDistance.Near;
|
|
this.NearViewMode = this.RiderCameraDistance == RiderCameraDistance.Near;
|
|
float cameraFollowDistance = GetCameraFollowDistance(this.RiderCameraDistance);
|
|
this.VideoPlayerControl.CameraFollowDistance = this.videoPlayer.IsRear ? cameraFollowDistance * -1f : cameraFollowDistance;
|
|
}
|
|
protected override bool IsArObjectActive(ARObject arObject)
|
|
{
|
|
bool flag = base.IsArObjectActive(arObject);
|
|
if ((UnityEngine.Object)arObject != (UnityEngine.Object)this.mainObject)
|
|
return flag;
|
|
if ((double)this.VideoPlayerControl.CameraFollowDistance > 0.0 || this.AR360Version)
|
|
return flag;
|
|
return flag && (double)this.VideoPlayerControl.CameraDistanceError > 5.0;
|
|
}
|
|
//获取摄像机的位置
|
|
public static float GetCameraFollowDistance(RiderCameraDistance distance)
|
|
{
|
|
switch (distance)
|
|
{
|
|
case RiderCameraDistance.FirstPerson:
|
|
return 0.0f;
|
|
case RiderCameraDistance.Near:
|
|
return 1.5f;
|
|
case RiderCameraDistance.Middle:
|
|
return 5f;
|
|
case RiderCameraDistance.Far:
|
|
return 10f;
|
|
default:
|
|
throw new ArgumentException("Unknown rider's camera distance.");
|
|
}
|
|
}
|
|
private void UpdateRidersVisibility()
|
|
{
|
|
//if (!this.videoPlayer.Initialized)
|
|
// return;
|
|
//this.UpdateVisibleRiders();
|
|
////删除当前道路上应该消失的骑手
|
|
//foreach (int num in this.riderObjects.Keys.ToArray<int>())
|
|
//{
|
|
// if (!this.visibleRiders.ContainsKey(num))
|
|
// {
|
|
// ARLaneObject riderObject = this.riderObjects[num];
|
|
// if (!riderObject.IsAtFinish || (double)riderObject.VisibilityLevel <= 0.0)
|
|
// {
|
|
// this.DestroyRider(num);
|
|
// this.DestroyRiderTitle(num);
|
|
// }
|
|
// }
|
|
//}
|
|
////新增应该显示的骑手
|
|
//foreach (TrainingARController.VisibleRiderItem visibleRiderItem in this.visibleRiders.Values)
|
|
//{
|
|
// IRouteRider rider = visibleRiderItem.Rider;
|
|
// bool showModel = visibleRiderItem.ShowModel;
|
|
// ARLaneObject arLaneObject;
|
|
// //二次检查
|
|
// if (this.riderObjects.TryGetValue(rider.Id, out arLaneObject))
|
|
// {
|
|
// if (arLaneObject is BaseRenderer)
|
|
// {
|
|
// if (!showModel)
|
|
// {
|
|
// float lane = arLaneObject.Lane;
|
|
// this.DestroyRider(rider.Id);
|
|
// this.CreateRiderObject(rider, lane);
|
|
// }
|
|
// }
|
|
// else if (showModel)
|
|
// {
|
|
// float lane = arLaneObject.Lane;
|
|
// this.DestroyRider(rider.Id);
|
|
// this.CreateRiderModel(rider, lane);
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// if (showModel)
|
|
// this.CreateRiderModel(rider, visibleRiderItem.Lane);
|
|
// else
|
|
// this.CreateRiderObject(rider, visibleRiderItem.Lane);
|
|
// this.CreateRiderTitleObject(rider);
|
|
// }
|
|
//}
|
|
}
|
|
//更新骑手的偏移角度
|
|
private void UpdateRidersLean()
|
|
{
|
|
foreach (ARLaneObject riderObject in this.riderObjects.Values)
|
|
{
|
|
float num1 = this.Route.GetTrajectoryCurvature(riderObject.Frame) + riderObject.Curvature;
|
|
float z1;
|
|
if ((double)num1 == 0.0)
|
|
{
|
|
z1 = 0.0f;
|
|
}
|
|
else
|
|
{
|
|
double speed = riderObject.Speed;
|
|
float f = 57.29578f * Mathf.Atan((float)(speed * speed * (double)num1 / 9.81));
|
|
float num2 = (double)f >= 0.0 ? 1f : -1f;
|
|
float num3 = Mathf.Abs(f);
|
|
if ((double)num3 > 20.0)
|
|
f = (float)((double)Mathf.Atan((float)(((double)num3 - 20.0) / 10.5)) * 10.5 + 20.0) * num2;
|
|
float num4 = Mathf.Clamp(f, -35f, 35f);
|
|
float z2 = riderObject.Lean.z;
|
|
z1 = Mathf.Clamp(num4, z2 - 60f * Time.deltaTime, z2 + 60f * Time.deltaTime);
|
|
}
|
|
float y = Mathf.Clamp(riderObject.LaneChangingDirection, -6f, 6f);
|
|
riderObject.Lean = new Vector3(0.0f, y, z1);
|
|
}
|
|
}
|
|
|
|
DateTime lastPlaybackSpeedChange = DateTime.UtcNow;
|
|
bool pausedByPlaybackSpeed = false;
|
|
bool pausedByTrainingPause = false;
|
|
float frame = 0f;
|
|
public bool SetPlaybackSpeed(float playbackSpeed)
|
|
{
|
|
if (this.mediaPlayer.Control.IsSeeking() || DateTime.UtcNow.Subtract(this.lastPlaybackSpeedChange).TotalSeconds < 1.0)
|
|
return false;
|
|
if ((double)playbackSpeed == 0.0 && !this.pausedByPlaybackSpeed)
|
|
{
|
|
this.pausedByPlaybackSpeed = true;
|
|
this.mediaPlayer.Pause();
|
|
}
|
|
else if ((double)playbackSpeed > 0.0 && this.pausedByPlaybackSpeed)
|
|
{
|
|
this.pausedByPlaybackSpeed = false;
|
|
if (!this.pausedByTrainingPause)
|
|
this.mediaPlayer.Play();
|
|
}
|
|
this.mediaPlayer.Control.SetPlaybackRate(playbackSpeed);
|
|
this.lastPlaybackSpeedChange = DateTime.UtcNow;
|
|
mediaPlayer.Play();
|
|
return true;
|
|
}
|
|
|
|
#endif
|
|
//人物状态
|
|
private string ConfigPath = Application.streamingAssetsPath + "/newdirection.txt";
|
|
public const int HERO_UP = 0;
|
|
public const int HERO_RIGHT = 1;
|
|
public const int HERO_DOWN = 2;
|
|
public const int HERO_LEFT = 3;
|
|
public const int UP = 4;
|
|
public const int DOWN = 5;
|
|
private Vector3 rotation = Vector3.zero;
|
|
private void keyBordControl()
|
|
{
|
|
#region 人物方向控制
|
|
var zdelta = 0.05f;
|
|
var delta = 0.2f;
|
|
if (Input.GetKey(KeyCode.W))
|
|
{
|
|
//mediaPlayer.Play();
|
|
//mediaPlayer.PlaybackRate = 1f;
|
|
var z = transform.position.z + zdelta;
|
|
//transform.DOMoveZ(z, 0);
|
|
}
|
|
|
|
if (Input.GetKey(KeyCode.A))
|
|
{
|
|
mediaPlayer.PlaybackRate = 1f;
|
|
mediaPlayer.Play();
|
|
var z = transform.position.x - zdelta;
|
|
//transform.DOMoveX(z, 0);
|
|
}
|
|
|
|
if (Input.GetKey(KeyCode.D))
|
|
{
|
|
mediaPlayer.PlaybackRate = 1f;
|
|
mediaPlayer.Play();
|
|
var z = transform.position.x + zdelta;
|
|
//transform.DOMoveX(z, 0);
|
|
}
|
|
if (Input.GetKey(KeyCode.S))
|
|
{
|
|
mediaPlayer.PlaybackRate = 1f;
|
|
mediaPlayer.Play();
|
|
var z = transform.position.z - zdelta;
|
|
//transform.DOMoveZ(z, 0);
|
|
}
|
|
|
|
//控制人物方向
|
|
if (Input.GetKey(KeyCode.Q))
|
|
{
|
|
rotation.y -= delta;
|
|
transform.DORotate(rotation, 0);
|
|
Camera.main.transform.DORotate(rotation, 0);
|
|
}
|
|
if (Input.GetKey(KeyCode.E))
|
|
{
|
|
rotation.y += delta;
|
|
transform.DORotate(rotation, 0);
|
|
Camera.main.transform.DORotate(rotation, 0);
|
|
}
|
|
//控制人物左右倾斜
|
|
if (Input.GetKey(KeyCode.J))
|
|
{
|
|
rotation.z += zdelta;
|
|
transform.DORotate(rotation, 0);
|
|
}
|
|
if (Input.GetKey(KeyCode.K))
|
|
{
|
|
rotation.z -= zdelta;
|
|
transform.DORotate(rotation, 0);
|
|
}
|
|
|
|
if (Input.GetKey(KeyCode.UpArrow))
|
|
{
|
|
var z = transform.position.y + zdelta;
|
|
transform.DOMoveY(z, 0);
|
|
}
|
|
|
|
if (Input.GetKey(KeyCode.DownArrow))
|
|
{
|
|
var z = transform.position.y - zdelta;
|
|
transform.DOMoveY(z, 0);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|
|
}
|