LookAtCameraPartialAxis.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if ZED_HDRP || ZED_URP
  5. using UnityEngine.Rendering;
  6. #endif
  7. /// <summary>
  8. /// Makes the transform face any camera that renders, other than Unity Scene (editor) cameras.
  9. /// Will only turn on axes that you specify.
  10. /// <para>Used in the ZED MR calibration scene to make the 2D real-world screen always visible.</para>
  11. /// </summary>
  12. public class LookAtCameraPartialAxis : MonoBehaviour
  13. {
  14. /// <summary>
  15. /// Whether to rotate to follow the camera on the X axis.
  16. /// </summary>
  17. [Tooltip("Whether to follow the camera on the X axis.")]
  18. public bool followX = false;
  19. /// <summary>
  20. /// Whether to rotate to follow the camera on the Y axis.
  21. /// </summary>
  22. [Tooltip("Whether to follow the camera on the Y axis.")]
  23. public bool followY = true;
  24. /// <summary>
  25. /// Whether to rotate to follow the camera on the Z axis.
  26. /// </summary>
  27. [Tooltip("Whether to follow the camera on the Z axis.")]
  28. public bool followZ = false;
  29. private void Start()
  30. {
  31. #if ZED_HDRP || ZED_URP
  32. RenderPipelineManager.beginCameraRendering += LookAtCamera;
  33. #else
  34. Camera.onPreRender += LookAtCamera;
  35. #endif
  36. }
  37. private void OnDestroy()
  38. {
  39. #if ZED_HDRP || ZED_URP
  40. RenderPipelineManager.beginCameraRendering -= LookAtCamera;
  41. #else
  42. Camera.onPreRender -= LookAtCamera;
  43. #endif
  44. }
  45. #if ZED_URP || ZED_HDRP
  46. /// <summary>
  47. /// Rotates the transform to face the target camera on all enabled axes.
  48. /// </summary>
  49. /// <param name="cam"></param>
  50. void LookAtCamera(ScriptableRenderContext context, Camera cam)
  51. {
  52. //Camera cam = Camera.current;
  53. if (cam.name.Contains("Scene") || cam.name.Contains("Editor")) return;
  54. Quaternion lookrot = Quaternion.LookRotation(transform.position - cam.transform.position, Vector3.up);
  55. Vector3 lookeuler = lookrot.eulerAngles;
  56. float newx = followX ? lookeuler.x : transform.eulerAngles.x;
  57. float newy = followY ? lookeuler.y : transform.eulerAngles.y;
  58. float newz = followZ ? lookeuler.z : transform.eulerAngles.z;
  59. transform.rotation = Quaternion.Euler(newx, newy, newz);
  60. }
  61. #else
  62. /// <summary>
  63. /// Rotates the transform to face the target camera on all enabled axes.
  64. /// </summary>
  65. /// <param name="cam"></param>
  66. void LookAtCamera(Camera cam)
  67. {
  68. //Camera cam = Camera.current;
  69. if (cam.name.Contains("Scene") || cam.name.Contains("Editor")) return;
  70. Quaternion lookrot = Quaternion.LookRotation(transform.position - cam.transform.position, Vector3.up);
  71. Vector3 lookeuler = lookrot.eulerAngles;
  72. float newx = followX ? lookeuler.x : transform.eulerAngles.x;
  73. float newy = followY ? lookeuler.y : transform.eulerAngles.y;
  74. float newz = followZ ? lookeuler.z : transform.eulerAngles.z;
  75. transform.rotation = Quaternion.Euler(newx, newy, newz);
  76. }
  77. #endif
  78. }