CameraEditorUtils.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine;
  2. using Object = UnityEngine.Object;
  3. namespace UnityEditor.Rendering
  4. {
  5. /// <summary>
  6. /// Utility functions for cameras in the editor.
  7. /// </summary>
  8. public static class CameraEditorUtils
  9. {
  10. /// <summary>Delegate that must give an initialized preview camera</summary>
  11. /// <param name="sourceCamera">The initial Camera we want a preview from</param>
  12. /// <param name="previewSize">The size of the preview</param>
  13. /// <returns>The Preview camera, initialized</returns>
  14. public delegate Camera GetPreviewCamera(Camera sourceCamera, Vector2 previewSize);
  15. const float k_PreviewNormalizedSize = 0.2f;
  16. internal static Material s_GUITextureBlit2SRGBMaterial;
  17. /// <summary>
  18. /// The material used to display a texture into SRGB
  19. /// </summary>
  20. public static Material GUITextureBlit2SRGBMaterial
  21. {
  22. get
  23. {
  24. if (!s_GUITextureBlit2SRGBMaterial)
  25. {
  26. Shader shader = EditorGUIUtility.LoadRequired("SceneView/GUITextureBlit2SRGB.shader") as Shader;
  27. s_GUITextureBlit2SRGBMaterial = new Material(shader);
  28. s_GUITextureBlit2SRGBMaterial.hideFlags = HideFlags.HideAndDontSave;
  29. }
  30. s_GUITextureBlit2SRGBMaterial.SetFloat("_ManualTex2SRGB", QualitySettings.activeColorSpace == ColorSpace.Linear ? 1.0f : 0.0f);
  31. return s_GUITextureBlit2SRGBMaterial;
  32. }
  33. }
  34. /// <summary>
  35. /// Draw the overlay of a Camera
  36. /// </summary>
  37. /// <param name="target">The Camera that we want a preview</param>
  38. /// <param name="sceneView">The scene view where to draw it</param>
  39. /// <param name="previewCameraGetter">The way to get the preview camera corresponding to the target</param>
  40. public static void DrawCameraSceneViewOverlay(Object target, SceneView sceneView, GetPreviewCamera previewCameraGetter)
  41. {
  42. if (target == null) return;
  43. // cache some deep values
  44. var c = (Camera)target;
  45. var previewSize = Handles.GetMainGameViewSize();
  46. if (previewSize.x < 0f)
  47. {
  48. // Fallback to Scene View of not a valid game view size
  49. previewSize.x = sceneView.position.width;
  50. previewSize.y = sceneView.position.height;
  51. }
  52. // Apply normalizedviewport rect of camera
  53. var normalizedViewPortRect = c.rect;
  54. previewSize.x *= Mathf.Max(normalizedViewPortRect.width, 0f);
  55. previewSize.y *= Mathf.Max(normalizedViewPortRect.height, 0f);
  56. // Prevent using invalid previewSize
  57. if (previewSize.x <= 0f || previewSize.y <= 0f)
  58. return;
  59. var aspect = previewSize.x / previewSize.y;
  60. // Scale down (fit to scene view)
  61. previewSize.y = k_PreviewNormalizedSize * sceneView.position.height;
  62. previewSize.x = previewSize.y * aspect;
  63. if (previewSize.y > sceneView.position.height * 0.5f)
  64. {
  65. previewSize.y = sceneView.position.height * 0.5f;
  66. previewSize.x = previewSize.y * aspect;
  67. }
  68. if (previewSize.x > sceneView.position.width * 0.5f)
  69. {
  70. previewSize.x = sceneView.position.width * 0.5f;
  71. previewSize.y = previewSize.x / aspect;
  72. }
  73. // Get and reserve rect
  74. Rect cameraRect = GUILayoutUtility.GetRect(previewSize.x, previewSize.y);
  75. if (Event.current.type == EventType.Repaint)
  76. {
  77. var previewCamera = previewCameraGetter(c, previewSize);
  78. if (previewCamera.targetTexture == null)
  79. {
  80. Debug.LogError("The preview camera must render in a render target");
  81. return;
  82. }
  83. bool drawGizmo = sceneView.drawGizmos;
  84. sceneView.drawGizmos = false;
  85. previewCamera.Render();
  86. sceneView.drawGizmos = drawGizmo;
  87. Graphics.DrawTexture(cameraRect, previewCamera.targetTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, GUI.color, GUITextureBlit2SRGBMaterial);
  88. // We set target texture to null after this call otherwise if both sceneview and gameview are visible and we have a preview camera wwe
  89. // get this error: "Releasing render texture that is set as Camera.targetTexture!"
  90. previewCamera.targetTexture = null;
  91. }
  92. }
  93. /// <summary>
  94. /// Check if the view port rect have a positive size
  95. /// </summary>
  96. /// <param name="normalizedViewPortRect">The rect to check</param>
  97. /// <returns>True: the rect have positive size</returns>
  98. public static bool IsViewPortRectValidToRender(Rect normalizedViewPortRect)
  99. {
  100. if (normalizedViewPortRect.width <= 0f || normalizedViewPortRect.height <= 0f)
  101. return false;
  102. if (normalizedViewPortRect.x >= 1f || normalizedViewPortRect.xMax <= 0f)
  103. return false;
  104. if (normalizedViewPortRect.y >= 1f || normalizedViewPortRect.yMax <= 0f)
  105. return false;
  106. return true;
  107. }
  108. }
  109. }