123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace SC.XR.Unity
- {
- public class CameraFollowerEdustry : FollowerBase
- {
- [Header("Following Settings")]
- public bool InstantFollowing;//是否无延迟跟随
- public bool LinearFollowing;//是否线性移动
- public Vector2 menu_size;//面板的长宽,默认面板的position在中心。
- private Transform _slamHead;
- Vector2 initViewPoint;//相机画面的中点,坐标为(0.5f,0.5f)
- Vector3 viewPoint = Vector3.zero;// 面板坐标相对于相机画面的坐标。
- Vector3 viewPoint_right = Vector3.zero;// 面板的上下左右端相对于相机画面的坐标。
- Vector3 viewPoint_left = Vector3.zero;
- Vector3 viewPoint_top = Vector3.zero;
- Vector3 viewPoint_bot = Vector3.zero;
- bool isFollower = false;
- Vector3 originPos = Vector3.zero;
- Vector3 desPos = Vector3.zero;
- float journeyLength = 0f;
- public Camera Cam;
- void Start()
- {
- _slamHead = Cam.transform;
- initViewPoint = Cam.WorldToViewportPoint(_slamHead.position + (_slamHead.forward * WindowDistance));
- InstantFollow();
- }
- //Nonlinear Following by default
- protected override void Follow()
- {
- if (Cam == null)
- {
- return;
- }
- transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(Cam.transform), WindowFollowSpeed * Time.deltaTime);
- transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Cam.transform), WindowFollowSpeed * Time.deltaTime);
- transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
- }
- //Following camera without latency
- protected void InstantFollow()
- {
- transform.position = CalculateWindowPosition(Cam.transform);
- transform.rotation = CalculateWindowRotation(Cam.transform);
- transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
- }
- //For Linear Following, turn down the WindowFollowSpeed to around 0.5 at distance = 1 for best experience
- protected void LinearFollow()
- {
- originPos = transform.position;
- desPos = CalculateWindowPosition(Cam.transform);
- journeyLength = Vector3.Distance(originPos, desPos);
- transform.position = Vector3.Lerp(originPos, desPos, (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
- transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Cam.transform), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
- transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
- }
- protected override void LateUpdate()
- {
- if ((StopFollower == false)&& !InstantFollowing)
- {
- if (IsFollower() && !LinearFollowing)
- {
- Follow();
- }
- else if (IsFollower() && LinearFollowing)
- {
- LinearFollow();
- }
- else
- {
- Cam.transform.position = OpenXRCamera.Instance.head.position;
- Cam.transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.transform.eulerAngles.y, 0);
- }
- }
- else if ((StopFollower == false) && InstantFollowing)
- {
- InstantFollow();
- }else
- {
- Cam.transform.position = OpenXRCamera.Instance.head.position;
- Cam.transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.transform.eulerAngles.y, 0);
- }
- }
- protected bool IsFollower()
- {
- if (Cam == null || Cam == null)
- {
- return false;
- }
- viewPoint = Cam.WorldToViewportPoint(transform.position);// 面板在相机画面上的坐标
- viewPoint_right = Cam.WorldToViewportPoint(transform.TransformPoint(new Vector3(menu_size.x/2, 0, 0)));// 面板边界在相机画面上的坐标
- viewPoint_left = Cam.WorldToViewportPoint(transform.TransformPoint(new Vector3(-menu_size.x/2, 0, 0)));
- viewPoint_top = Cam.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, menu_size.y/2, 0)));
- viewPoint_bot = Cam.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, -menu_size.y/2, 0)));
- if (viewPoint_right.x > 1 || viewPoint_left.x < 0)//边界判断// || viewPoint_bot.y < 0 || viewPoint_top.y > 1
- {
- isFollower = true;
- }
- else if (Mathf.Abs(viewPoint.x - initViewPoint.x) < 0.05f && Mathf.Abs(viewPoint.y - initViewPoint.y) < 0.05f)//停止跟随的边界判断
- {
- isFollower = false;
- }
- return isFollower;
- }
- }
- }
|