123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using UnityEngine;
- namespace XGame.Framework.Map
- {
- /// <summary>
- /// 平行相机跟随范围
- /// 相机跟随目标限制在该范围内
- /// </summary>
- [RequireComponent(typeof(Camera))]
- public class ParallelCamera : MonoBehaviour
- {
- private struct Positions
- {
- public Vector3 leftDown;
- public Vector3 leftUp;
- public Vector3 rightDown;
- public Vector3 rightUp;
- public Vector3 CenterUp => (leftUp + rightUp) * 0.5f;
- public Vector3 CenterDown => (leftDown + rightDown) * 0.5f;
- public override string ToString()
- {
- return $"Positions leftDown:{leftDown} leftUp:{leftUp} rightDown:{rightDown} rightUp:{rightUp}";
- }
- }
- /// <summary>
- /// 相机视口修正参数
- /// </summary>
- [SerializeField]
- [Header("相机视口修正参数")]
- private CameraFieldOfViewOptions _fovOptions;
- /// <summary>
- /// 目标平面垂直于相机的直线距离
- /// </summary>
- [SerializeField]
- private float _distance = 16;
- /// <summary>
- /// 可移动区域相对于视口的缩放
- /// </summary>
- [SerializeField]
- private Vector2 _viewportScale = Vector2.one;
- /// <summary>
- /// 可移动区域对齐点
- /// </summary>
- [SerializeField]
- private Vector2 _viewportPivot = Vector2.one * 0.5f;
- /// <summary>
- /// 指定距离对应的视口大小
- /// 下负上正,左负右正
- /// 不可修改
- /// </summary>
- //[SerializeField]
- private Rect _viewportRect;
- /// <summary>
- /// 目标移动区域
- /// 下负上正,左负右正
- /// 不可修改
- /// </summary>
- //[SerializeField]
- private Rect _targetRect;
- //[SerializeField]
- private Camera _camera;
- //[SerializeField]
- private IParallelTarget _target;
- //[SerializeField]
- private Vector3 _offsetUp;
- //[SerializeField]
- private Vector3 _offsetDown;
- /// <summary>
- /// 目标的屏幕区域
- /// 左下(0,0),右上(width,height)
- /// </summary>
- private Rect _screenRect;
- /// <summary>
- /// 相机是否在移动
- /// 相机在玩家停止移动后会自动向下移动到显示区域底部,这时候UI需要同步刷新坐标
- /// </summary>
- public bool IsMoving { get; private set; }
- /// <summary>
- /// 显示区域顶部中心位置转世界坐标
- /// </summary>
- /// <returns></returns>
- public Vector3 TopCenterToWorldPosition()
- {
- var screenPos = _screenRect.center;
- screenPos.y = _screenRect.yMax;
- return _camera.ScreenToWorldPoint(screenPos);
- }
- public IParallelTarget Target
- {
- get => _target;
- set
- {
- if (_target != value)
- {
- _target = value;
- OnSetTarget();
- }
- }
- }
- private void Awake()
- {
- IsMoving = false;
- _camera = GetComponent<Camera>();
- _fovOptions.FixFieldOfView(_camera);
- CalculateViewport();
- }
- //private void LateUpdate()
- //{
- // if (_target != null)
- // {
- // if (_offset == Vector3.zero)
- // {
- // _offset = transform.position - _target.position;
- // }
- // transform.position = _target.position + _offset;
- // //Debug.Log($"LateUpdate distance: {PointToPlaneDistance(transform.position, target.position, Vector3.back)}");
- // }
- //}
- public void OnLateUpdate(int millisecond)
- {
- IsMoving = false;
- if (_target == null)
- return;
- var tarPosition = _target.Position;
- var tarScreenPos = _camera.WorldToScreenPoint(tarPosition);
- if (_target.IsMoving)
- {
- var moveDir = _target.MoveDir;
- if (tarScreenPos.y > _screenRect.yMax && moveDir.y > 0)
- { // 小于
- //Debug.Log($"OnLateUpdate Up :{tarScreenPos} _offsetUp:{_offsetUp}");
- transform.position = tarPosition + _offsetUp;
- }
- else if (tarScreenPos.y < _screenRect.y && moveDir.y < 0)
- {
- //Debug.Log($"OnLateUpdate Down :{tarScreenPos} _offsetDown:{_offsetDown}");
- transform.position = tarPosition + _offsetDown;
- }
- IsMoving = true;
- }
- else
- {// 没有移动
- if (tarScreenPos.y > _screenRect.y)
- { // 自动移动到显示区域底部
- var pos = transform.position;
- pos.y += millisecond * 0.001f * 2;
- transform.position = pos;
- IsMoving = true;
- }
- }
- //Debug.Log($"OnLateUpdate Screen:{tarScreenPos} tarPosition:{tarPosition}");
- }
- private void OnSetTarget()
- {
- if (_target == null)
- return;
- //_offset = transform.position - _target.position;
- _distance = PointToPlaneDistance(transform.position, _target.Position, Vector3.back);
- CalculateViewport();
- }
- private void CalculateViewport()
- {
- //Debug.Log($"Camera pixelRect:{_camera.pixelRect} rect:{_camera.rect} pixelWidth:{_camera.pixelWidth} pixelHeight:{_camera.pixelHeight}");
- var fovRadians = Mathf.Deg2Rad * _camera.fieldOfView; //fov从度数转弧度
- var viewportH = 2 * Mathf.Tan(fovRadians * 0.5f) * _distance;
- var viewportW = viewportH * _camera.aspect;
- _viewportRect = new Rect(-viewportW * 0.5f, -viewportH * 0.5f, viewportW, viewportH);
- _targetRect = new Rect(_viewportRect);
- _targetRect.size *= _viewportScale;
- _targetRect.center = _viewportRect.center + (_viewportPivot - Vector2.one * 0.5f) * _targetRect.size;
- var positions = RectToWorldPositions(_targetRect);
- _offsetUp = transform.position - positions.CenterUp;
- _offsetDown = transform.position - positions.CenterDown;
- var leftDown = _camera.WorldToScreenPoint(positions.leftDown);
- var rightUp = _camera.WorldToScreenPoint(positions.rightUp);
- _screenRect = new Rect(leftDown.x, leftDown.y, rightUp.x - leftDown.x, rightUp.y - leftDown.y);
- //Debug.Log($"World leftDown:{positions.leftDown} leftUp:{positions.leftUp} rightDown:{positions.rightDown} rightUp:{positions.rightUp}");
- //Log.Debug($"Screen leftDown:{leftDown} rightUp:{rightUp} screenRect:{_screenRect}");
- }
- private Positions RectToWorldPositions(Rect rect)
- {
- var normal = transform.forward;
- var center = transform.position + normal * _distance;
- var halfSize = rect.size * 0.5f;
- var offset = rect.center;
- var xAxis = Vector3.Normalize(Vector3.Cross(normal, Vector3.up));
- //Debug.Log($"center:{center} halfSize:{halfSize} offset:{offset} xAxis:{xAxis}");
- if (xAxis == Vector3.zero)
- { //预防当前方向与Y轴平行
- xAxis = Vector3.Cross(normal, Vector3.right);
- }
- var yAxis = Vector3.Cross(normal, xAxis);
- center = center - xAxis * offset.x - yAxis * offset.y;
- var positions = new Positions();
- positions.leftDown = center + xAxis * halfSize.x + yAxis * halfSize.y;
- positions.leftUp = center + xAxis * halfSize.x - yAxis * halfSize.y;
- positions.rightDown = center - xAxis * halfSize.x + yAxis * halfSize.y;
- positions.rightUp = center - xAxis * halfSize.x - yAxis * halfSize.y;
- //Log.Debug(positions);
- return positions;
- }
- /// <summary>
- /// 计算点到平面的垂直距离
- /// 法线向量不能为零
- /// </summary>
- /// <param name="point">待计算距离的点</param>
- /// <param name="pointOnPlane">平面上的一个点</param>
- /// <param name="planeNormal">平面的法线向量</param>
- /// <returns></returns>
- private float PointToPlaneDistance(Vector3 point, Vector3 pointOnPlane, Vector3 planeNormal)
- {
- var distance = Vector3.Dot(planeNormal, (point - pointOnPlane)) / planeNormal.magnitude;
- return distance;
- }
- public Vector3 WorldToScreenPoint(Vector3 point)
- {
- return _camera.WorldToScreenPoint(point);
- }
- #if UNITY_EDITOR
- void OnDrawGizmos()
- { //下负上正,左负右正
- //if (Physics.Raycast(transform.position, transform.forward, out var hitInfo))
- //{
- //}
- if (_camera == null)
- {
- _camera = GetComponent<Camera>();
- _fovOptions.FixFieldOfView(_camera);
- }
- CalculateViewport();
- var positions_1 = RectToWorldPositions(_viewportRect);
- Gizmos.color = Color.blue;
- Gizmos.DrawLine(positions_1.leftDown, positions_1.rightDown);
- Gizmos.DrawLine(positions_1.rightDown, positions_1.rightUp);
- Gizmos.DrawLine(positions_1.rightUp, positions_1.leftUp);
- Gizmos.DrawLine(positions_1.leftUp, positions_1.leftDown);
- Gizmos.color = Color.red;
- var positions_2 = RectToWorldPositions(_targetRect);
- Gizmos.DrawLine(positions_2.leftDown, positions_2.rightDown);
- Gizmos.DrawLine(positions_2.rightDown, positions_2.rightUp);
- Gizmos.DrawLine(positions_2.rightUp, positions_2.leftUp);
- Gizmos.DrawLine(positions_2.leftUp, positions_2.leftDown);
- Gizmos.DrawLine(transform.position, positions_2.CenterUp);
- Gizmos.DrawLine(transform.position, positions_2.CenterDown);
- //Gizmos.color = Color.green;
- //Vector3[] frustumCorners = new Vector3[4];
- //camera.CalculateFrustumCorners(new Rect(0, 0, 1, 1), distance, Camera.MonoOrStereoscopicEye.Mono, frustumCorners);
- //var offsetZ = transform.position.z;
- //for ( int i = 0; i < frustumCorners.Length; i++)
- //{
- // var pos = frustumCorners[i];
- // pos.z += offsetZ;
- // frustumCorners[i] = pos;
- //}
- //Gizmos.DrawLine(frustumCorners[0], frustumCorners[1]);
- //Gizmos.DrawLine(frustumCorners[1], frustumCorners[2]);
- //Gizmos.DrawLine(frustumCorners[2], frustumCorners[3]);
- //Gizmos.DrawLine(frustumCorners[3], frustumCorners[0]);
- //Gizmos.DrawFrustum(transform.position, fovRadians, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
- //var ray = camera.ScreenPointToRay(Vector3.zero);
- //Gizmos.DrawRay(ray);
- //Debug.Log($"PointToScreen LD:{PointToScreen(leftDown)} LU:{PointToScreen(leftUp)} RD:{PointToScreen(rightDown)} RU:{PointToScreen(rightUp)}");
- }
- #endif
- }
- }
|