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