powerfun-unity/Assets/AR/ARTrainingController.cs
2022-12-05 18:29:49 +08:00

382 lines
17 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RenderHeads.Media.AVProVideo;
using Assets.Scripts.Scenes.VideoRide;
using Assets.Core;
namespace Assets.AR
{
public class ARTrainingController : ARLaneObjectsController
{
public MediaPlayer mediaPlayer;
private ARData aRData;
private RouteDetailData detail;
private VideoPointsSync videoPointsSync;
private int index = 1;
private readonly List<ARLaneObject> collisionList = new List<ARLaneObject>();
VideoPlayerControl VideoPlayerControl;
ARLaneObject[] rides;
public ARLaneObject mainObject { get; set; }
private Light arLight;
private Light arBackLight;
public RiderCameraDistance RiderCameraDistance { get; set; }
public VideoControlMode VideoControlMode { get; private set; }
private float autoCameraSwitchTimeCounter = 80f;
float timers = 1f;
double weight = 70;
double bikeWeight = 20;
private VideoGameManager manager;
protected override void Start()
{
base.Start();
this.arObjects.Clear();
manager = GetComponent<VideoGameManager>();
}
public void Initialize(ARData arData, RouteDetailData routeDetailData)
{
//ar数据加载本地本地没有就下载
//var txt = Resources.Load<TextAsset>("UI/93824").text;
//var route = Resources.Load<TextAsset>("UI/route-93824").text;
this.aRData = arData; //Newtonsoft.Json.JsonConvert.DeserializeObject<ARData>(txt);
this.detail = routeDetailData;//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>();
SetUpLight();
}
protected override void Update()
{
if (!manager.IsStart())
return;
timers -= Time.deltaTime;
while (timers < 0)
{
foreach (var dic in manager.rideObjs)
{
var item = dic.Value;
item.Speed = (float)(dic.Key.Speed/3.6d);
item.DeltaDistance = item.Speed;
item.Route = Route;
item.VideoSync = videoPointsSync;
if(!this.arObjects.Contains(item))
this.arObjects.Add(item);
if (!this.riderObjects.ContainsKey(item.GetInstanceID()))
this.riderObjects.Add(item.GetInstanceID(), item);
}
if (mainObject != null)
{
mainObject.Speed = (float)(manager.CurrentPlayer.Speed / 3.6d);
mainObject.DeltaDistance = mainObject.Speed;
if (!this.riderObjects.ContainsKey(mainObject.GetInstanceID()))
this.riderObjects.Add(mainObject.GetInstanceID(), mainObject);
}
timers += 1f;
}
if (mainObject == null)
{
mainObject = manager.CurrentPlayer.gameObject.GetComponent<RiderRenderer>();
this.arObjects.Add(mainObject);
mainObject.Speed = (float)(manager.CurrentPlayer.Speed / 3.6d);
mainObject.DeltaDistance = mainObject.Speed;
mainObject.Route = Route;
mainObject.VideoSync = videoPointsSync;
if (!this.riderObjects.ContainsKey(mainObject.GetInstanceID()))
this.riderObjects.Add(mainObject.GetInstanceID(), mainObject);
}
foreach (var item in this.riderObjects.Values)
{
item.Distance += item.DeltaDistance * Time.deltaTime;
item.RouteDistance += item.DeltaDistance * Time.deltaTime;
if (item.Distance >= Route.GetTotalDistance())
{
item.IsAtFinish = true;
}
}
this.FrameIndexDistanceCorrection = this.VideoPlayerControl.FrameIndexDistanceCorrection;
//mainObject = manager.CurrentPlayer;
//this.FollowedRiderId = mainObject.Id;
this.UpdateCameraFollowDistance();
VideoPlayerControl.UpdateVideoPlaybackSpeed(mainObject.Speed, mainObject.distance);
base.Update();
this.UpdateRidersVisibility();
this.UpdateRidersLean();
this.UpdateBackLight();
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 void SetUpLight()
{
GameObject gameObject1 = new GameObject("ARLight");
gameObject1.layer = this.gameObject.layer;
gameObject1.transform.SetParent(this.transform);
this.arLight = gameObject1.AddComponent<Light>();
this.arLight.type = LightType.Directional;
this.arLight.transform.rotation = Quaternion.Euler(90f, 0.0f, 0.0f);
//if (this.BuildConfig.IsIOs || this.BuildConfig.IsTvOs)
//{
// this.arLight.shadows = LightShadows.Hard;
// this.arLight.shadowStrength = 0.5f;
//}
//else
{
this.arLight.shadows = LightShadows.Soft;
this.arLight.shadowStrength = 1f;
}
this.arLight.cullingMask = 512;
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;
}
//设置video显示模式
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;
}
//更新摄像机的位置
private void UpdateCameraFollowDistance()
{
this.RiderCameraDistance = RiderCameraDistance.Middle;
this.NearViewMode = this.RiderCameraDistance == RiderCameraDistance.Near;
float cameraFollowDistance = GetCameraFollowDistance(this.RiderCameraDistance);
this.VideoPlayerControl.CameraFollowDistance = 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 3f;
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);
}
}
//更新后背的光线
private void UpdateBackLight()
{
//if (this.SelectedPanorama != null)
//{
// this.arBackLight.transform.position = this.PanoramaCamera.transform.position;
// this.arBackLight.transform.rotation = this.PanoramaCamera.transform.rotation;
//}
//else
{
this.arBackLight.transform.position = this.UnityCamera.transform.position;
this.arBackLight.transform.rotation = this.UnityCamera.transform.rotation;
}
this.arLight.transform.rotation = this.Route.GetLightDirection(this.videoPlayer.CurrentFrame);
this.arLight.shadowStrength = this.Route.GetShadowIntensity(this.videoPlayer.CurrentFrame);
}
protected void LateUpdate() => this.UpdateRiderTitles();
private void UpdateRiderTitles()
{
//foreach (int key in this.riderNameViews.Keys)
//{
// ARLaneObject riderObject = this.riderObjects[key];
// UIRect riderNameView = this.riderNameViews[key];
// RiderValueView riderValueView;
// this.riderValueViews.TryGetValue(key, out riderValueView);
// float num1 = Vector3.Distance(riderObject.transform.position, this.UnityCamera.transform.position);
// if (!riderObject.gameObject.activeSelf || (double)num1 > (double)this.MaximumVisibilityDistance || this.SelectedPanorama == null && (double)num1 < 3.0)
// {
// riderNameView.Active = false;
// if ((UnityEngine.Object)riderValueView != (UnityEngine.Object)null)
// riderValueView.Active = false;
// }
// else
// {
// riderNameView.Active = true;
// if ((UnityEngine.Object)riderValueView != (UnityEngine.Object)null)
// riderValueView.Active = (double)num1 < 30.0;
// float nameViewAlpha = this.GetNameViewAlpha(num1);
// if ((double)nameViewAlpha < 1.0)
// riderNameView.Alpha = nameViewAlpha;
// float t = this.GetValueViewAlpha(num1);
// if ((double)t < 1.0 && (UnityEngine.Object)riderValueView != (UnityEngine.Object)null)
// riderValueView.Alpha = t;
// if (this.sportProfile == SportProfile.Running)
// {
// if ((UnityEngine.Object)riderValueView != (UnityEngine.Object)null)
// riderValueView.Active = false;
// t = 0.0f;
// }
// Vector3 vector3 = 5f * this.uiScale * Mathf.Max(0.1f, this.SelectedPanorama != null ? 1f : this.GetDistanceScale(num1));
// Quaternion rotation = this.UnityCamera.transform.rotation;
// if (this.SelectedPanorama != null)
// {
// rotation = this.PanoramaCamera.transform.rotation;
// }
// else
// {
// if ((double)riderObject.Distance < (double)this.CameraDistance)
// rotation *= Quaternion.Euler(0.0f, 180f, 0.0f);
// if (this.videoPlayer.IsRear)
// rotation *= Quaternion.Euler(0.0f, 180f, 0.0f);
// }
// float num2 = Mathf.Lerp(-0.5f * this.Route.RiderScale, 0.6f * this.Route.RiderScale, Mathf.Clamp01(VTMath.Lerp(ARObject.MaxDistanceVisibilityModels - 5f, 1f, ARObject.MaxDistanceVisibilityModels, 0.0f, num1)));
// float num3 = 14f;
// float num4 = Mathf.Lerp(num2 + vector3.y * (0.5f * riderNameView.Height), num2 + vector3.y * (float)((double)num3 + 0.5 * (double)riderNameView.Height + 5.0), t);
// float num5 = -this.Route.RiderScale * riderObject.PositionOffset.y;
// riderNameView.transform.localScale = vector3;
// riderNameView.transform.position = riderObject.transform.position + rotation * new Vector3(0.0f, num4 + num5, 0.0f);
// riderNameView.transform.rotation = rotation;
// if ((UnityEngine.Object)riderValueView != (UnityEngine.Object)null)
// {
// float num6 = num2 + vector3.y * (0.5f * num3);
// riderValueView.transform.localScale = vector3;
// riderValueView.transform.position = riderObject.transform.position + rotation * new Vector3(0.0f, num6 + num5, 0.0f);
// riderValueView.transform.rotation = rotation;
// }
// }
//}
}
}
}