using Assets.AR; using DG.Tweening; using RenderHeads.Media.AVProVideo; using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; namespace Assets.AR { public class TestPlayer : ARLaneGameObjectsController { 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 collisionList = new List(); ///private Dictionary riders = new Dictionary(); ARVideoPlayerControl VideoPlayerControl; ARLaneGameObject[] rides; public ARLaneGameObject mainObject; private Light arLight; private Light arBackLight; public Text Fps; protected override void Start() { base.Start(); var txt = Resources.Load("UI/88019").text; var route = Resources.Load("UI/route-88019").text; aRData = Newtonsoft.Json.JsonConvert.DeserializeObject(txt); detail = Newtonsoft.Json.JsonConvert.DeserializeObject(route); videoPointsSync = new VideoPointsSync(detail.VideoPoints.Select(c => new VideoPoint() { Distance = (float)c.Distance, Time = (float)c.VideoTime })); videoSync = videoPointsSync; this.VideoPlayerControl = new ARVideoPlayerControl(); 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(); CreateBackLight(); } private void CreateBackLight() { GameObject gameObject1 = new GameObject("ARLight"); gameObject1.layer = this.gameObject.layer; gameObject1.transform.SetParent(this.transform); this.arLight = gameObject1.AddComponent(); 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(); 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 = 1f; double weight = 70; double bikeWeight = 20; private void Update() { timers -= Time.deltaTime; while (timers < 0) { foreach (var item in rides) { if (item.StartPosition == 1) { item.TestPower = new System.Random().Next(500, 720); } item.Speed = (float)(Assets.Scenes.Ride.Scripts.Helper.CalculateSpeed(0, 0, item.TestPower, weight, bikeWeight) / 3.6d); item.DeltaDistance = item.Speed; item.Route = Route; item.VideoSync = videoPointsSync; if (!this.riderObjects.ContainsKey(item.GetInstanceID())) this.riderObjects.Add(item.GetInstanceID(), item); } timers += 1f; } this.FrameIndexDistanceCorrection = this.VideoPlayerControl.FrameIndexDistanceCorrection; foreach (var item in rides) { item.Distance += item.DeltaDistance * Time.deltaTime; item.RouteDistance += item.DeltaDistance * Time.deltaTime; if (item.Distance >= Route.GetTotalDistance()) { item.IsAtFinish = true; } } this.UpdateCameraFollowDistance(); VideoPlayerControl.UpdateVideoPlayRate(mainObject.Speed, mainObject.distance); Fps.text = this.mediaPlayer.Control.GetCurrentTimeFrames().ToString(); this.FollowedRiderId = 1; base.Update(); this.UpdateRidersVisibility(); this.UpdateRidersLean(); this.UpdateBackLight(); if (Input.GetKeyDown(KeyCode.F)) this.SetVideoControlMode(VideoPlayMode.Front); if (Input.GetKeyDown(KeyCode.P)) this.SetVideoControlMode(VideoPlayMode.AutoPanorama); if (!Input.GetKeyDown(KeyCode.B)) return; this.SetVideoControlMode(VideoPlayMode.Rear); } public VideoPlayMode VideoControlMode { get; private set; } private float autoCameraSwitchTimeCounter = 80f; public void SetVideoControlMode(VideoPlayMode mode) { if (mode == this.VideoControlMode) return; if (!this.AR360Version) mode = VideoPlayMode.Front; this.VideoControlMode = mode; this.autoCameraSwitchTimeCounter = 80f; if (mode == VideoPlayMode.AutoPanorama) return; this.videoPlayer.IntendedRear = mode == VideoPlayMode.Rear; } public CameraDistance RiderCameraDistance { get; set; } //更新摄像机的位置 private void UpdateCameraFollowDistance() { this.RiderCameraDistance = AR.CameraDistance.Middle; this.NearViewMode = this.RiderCameraDistance == AR.CameraDistance.Near; var cameraFollowDistance = GetCameraFollowDistance(this.RiderCameraDistance); this.VideoPlayerControl.CameraFollowDistance = cameraFollowDistance; } protected override bool IsObjectActive(ARGameObject arObject) { var flag = base.IsObjectActive(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(CameraDistance distance) { switch (distance) { case AR.CameraDistance.FirstPerson: return 0.0f; case AR.CameraDistance.Near: return 2.5f; case AR.CameraDistance.Middle: return 5f; case AR.CameraDistance.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()) //{ // 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 (ARLaneGameObject 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); } #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 } } }