using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if ZED_HDRP || ZED_URP
using UnityEngine.Rendering;
#endif
///
/// Makes the transform face any camera that renders, other than Unity Scene (editor) cameras.
/// Will only turn on axes that you specify.
/// Used in the ZED MR calibration scene to make the 2D real-world screen always visible.
///
public class LookAtCameraPartialAxis : MonoBehaviour
{
///
/// Whether to rotate to follow the camera on the X axis.
///
[Tooltip("Whether to follow the camera on the X axis.")]
public bool followX = false;
///
/// Whether to rotate to follow the camera on the Y axis.
///
[Tooltip("Whether to follow the camera on the Y axis.")]
public bool followY = true;
///
/// Whether to rotate to follow the camera on the Z axis.
///
[Tooltip("Whether to follow the camera on the Z axis.")]
public bool followZ = false;
private void Start()
{
#if ZED_HDRP || ZED_URP
RenderPipelineManager.beginCameraRendering += LookAtCamera;
#else
Camera.onPreRender += LookAtCamera;
#endif
}
private void OnDestroy()
{
#if ZED_HDRP || ZED_URP
RenderPipelineManager.beginCameraRendering -= LookAtCamera;
#else
Camera.onPreRender -= LookAtCamera;
#endif
}
#if ZED_URP || ZED_HDRP
///
/// Rotates the transform to face the target camera on all enabled axes.
///
///
void LookAtCamera(ScriptableRenderContext context, Camera cam)
{
//Camera cam = Camera.current;
if (cam.name.Contains("Scene") || cam.name.Contains("Editor")) return;
Quaternion lookrot = Quaternion.LookRotation(transform.position - cam.transform.position, Vector3.up);
Vector3 lookeuler = lookrot.eulerAngles;
float newx = followX ? lookeuler.x : transform.eulerAngles.x;
float newy = followY ? lookeuler.y : transform.eulerAngles.y;
float newz = followZ ? lookeuler.z : transform.eulerAngles.z;
transform.rotation = Quaternion.Euler(newx, newy, newz);
}
#else
///
/// Rotates the transform to face the target camera on all enabled axes.
///
///
void LookAtCamera(Camera cam)
{
//Camera cam = Camera.current;
if (cam.name.Contains("Scene") || cam.name.Contains("Editor")) return;
Quaternion lookrot = Quaternion.LookRotation(transform.position - cam.transform.position, Vector3.up);
Vector3 lookeuler = lookrot.eulerAngles;
float newx = followX ? lookeuler.x : transform.eulerAngles.x;
float newy = followY ? lookeuler.y : transform.eulerAngles.y;
float newz = followZ ? lookeuler.z : transform.eulerAngles.z;
transform.rotation = Quaternion.Euler(newx, newy, newz);
}
#endif
}