using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent(typeof(Camera))]
public class CompassGizmoControl : MonoBehaviour
{
//public CameraMovement cameraMovement;
public CameraMove cameraMovement;
[HideInInspector]
public bool IsGizmoClick = false;
private float ScreenWidth = 400;
private float ScreenHeight = 300;
public float xPos = 750;
public float yPos = 300f;
private Vector2 lastScreenSize;
private const float CAMERA_MOVE_TIME_DURATION = 0.5f;
private const float DEFAULT_CAMERA_PIVOT_DISTANT = 10F; 축을 어디로 둘지 정하는 변수
private Camera cam_Gizmo;
private Transform cam_Gizmo_Pivot;
private GameObject mainCameraTargetObject;
private Coroutine c;
private Rect viewportRect;
private float viewportX = 0.88f;
void Reset()
{
cam_Gizmo = gameObject.GetComponent<Camera>();
cam_Gizmo.gameObject.name = "Camera_Gizmo";
cam_Gizmo.clearFlags = CameraClearFlags.Depth;
cam_Gizmo.depth = 100;
cam_Gizmo.pixelRect = new Rect(Screen.width - xPos, Screen.height - yPos, ScreenWidth, ScreenHeight);
cam_Gizmo.fieldOfView = 25f;
cam_Gizmo.nearClipPlane = 0.01f;
cam_Gizmo.farClipPlane = 10f;
cam_Gizmo.transform.localPosition = transform.parent.transform.forward * -8f;//new Vector3(4f, 4f, 6f);
cam_Gizmo.transform.LookAt(transform.parent.transform);
}
void Start()
{
cam_Gizmo = gameObject.GetComponent<Camera>();
cam_Gizmo_Pivot = transform.parent.transform;
mainCameraTargetObject = new GameObject();
viewportRect = cam_Gizmo.rect;
}
void Update()
{
if (Camera.main != null)
{
cam_Gizmo_Pivot.localRotation = Camera.main.transform.rotation;
//ResizeScreenCheck();
}
}
private void ResizeScreenCheck()
{
//Vector2 screenSize = new Vector2(Screen.width, Screen.height);
//if (this.lastScreenSize != screenSize)
//{
// this.lastScreenSize = screenSize;
// cam_Gizmo.pixelRect = new Rect(Screen.width - xPos, Screen.height - yPos, ScreenWidth, ScreenHeight);
//}
}
/// called from CompassMouseEvent.cs
public void clickedGizmoObject(Transform clickedGizmoObject)
{
if (EventSystem.current.IsPointerOverGameObject())
return;
IsGizmoClick = true;
Camera.main.transform.parent = null;
Vector3 normalVector3 = Vector3.Normalize(clickedGizmoObject.localPosition);
Vector3 MainCameraPivot = findMainCameraPivot();
mainCameraTargetObject.transform.position = MainCameraPivot + (normalVector3 * Vector3.Distance(Camera.main.transform.position, MainCameraPivot));
mainCameraTargetObject.transform.LookAt(MainCameraPivot);
if (c != null)
{
StopCoroutine(c);
c = null;
}
c = StartCoroutine(moveCamera(mainCameraTargetObject.transform));
}
private IEnumerator moveCamera(Transform target_Point)
{
Vector3 destposition = target_Point.position;
Quaternion destrotation = target_Point.rotation;
Transform cam_transform = Camera.main.transform;
float time = 0f;
float duration_lerf = 0f;
while (cam_transform.position != destposition && cam_transform.rotation != destrotation)
{
time = Mathf.Clamp(time, 0, CAMERA_MOVE_TIME_DURATION);
duration_lerf = time / CAMERA_MOVE_TIME_DURATION;
cam_transform.position = Vector3.Lerp(cam_transform.position, destposition, duration_lerf);
cam_transform.rotation = Quaternion.Lerp(cam_transform.rotation, destrotation, duration_lerf);
yield return null;
time += Time.deltaTime;
cameraMovement.SetTarget();
}
cameraMovement.SetTarget();
IsGizmoClick = false;
}
private Vector3 findMainCameraPivot()
{
Vector3 v3 = Camera.main.transform.position + Camera.main.transform.forward * DEFAULT_CAMERA_PIVOT_DISTANT;// 레이 타겟이 없을때 카메라 전방 10m
Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hitpoint;
if (Physics.Raycast(ray, out hitpoint))
{
v3 = hitpoint.point;
}
return v3;
}
private float findMainCameraPivotDist()
{
Vector3 MainCameraPivot = findMainCameraPivot();
return (Vector3.Distance(MainCameraPivot, Camera.main.transform.position));
}
public void change_camera_OrthographicMode()
{
Camera.main.orthographic = true;
float camOrthographicSize = (Mathf.Tan(Camera.main.fieldOfView / 2f * Mathf.Deg2Rad) * findMainCameraPivotDist());
Camera.main.orthographicSize = camOrthographicSize;
}
public void change_camera_PerspectiveMode()
{
Camera.main.orthographic = false;
}
public void OnValueChangedEditToggle(bool value)
{
if (value)
{
cam_Gizmo.rect = viewportRect;
}
else
{
cam_Gizmo.rect = new Rect(viewportX, viewportRect.yMin, viewportRect.xMax, viewportRect.yMax);
}
}
}