using UnityEngine; namespace XGame.Framework.Map { /// /// 平行相机跟随范围 /// 相机跟随目标限制在该范围内 /// [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}"; } } /// /// 目标平面垂直于相机的直线距离 /// [SerializeField] private float _distance = 16; /// /// 可移动区域相对于视口的缩放 /// [SerializeField] private Vector2 _viewportScale = Vector2.one; /// /// 可移动区域对齐点 /// [SerializeField] private Vector2 _viewportPivot = Vector2.one * 0.5f; /// /// 指定距离对应的视口大小 /// 下负上正,左负右正 /// 不可修改 /// //[SerializeField] private Rect _viewportRect; /// /// 目标移动区域 /// 下负上正,左负右正 /// 不可修改 /// //[SerializeField] private Rect _targetRect; //[SerializeField] private Camera _camera; //[SerializeField] private IParallelTarget _target; //[SerializeField] private Vector3 _offsetUp; //[SerializeField] private Vector3 _offsetDown; /// /// 目标的屏幕区域 /// 左下(0,0),右上(width,height) /// private Rect _screenRect; /// /// 显示区域顶部中心位置转世界坐标 /// /// 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() { _camera = GetComponent(); 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) { 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; } } else {// 没有移动 if (tarScreenPos.y > _screenRect.y) { // 自动移动到显示区域底部 var pos = transform.position; pos.y += millisecond * 0.001f * 2; transform.position = pos; } } //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; } /// /// 计算点到平面的垂直距离 /// 法线向量不能为零 /// /// 待计算距离的点 /// 平面上的一个点 /// 平面的法线向量 /// 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)) //{ //} _camera = GetComponent(); 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 } }