Example #1
0
        public virtual void OnOverlayGUI(Object target, SceneView sceneView)
        {
            if (target == null)
            {
                return;
            }

            var c = (Camera)target;

            // Do not render the Camera Preview overlay if the target camera GameObject is not part of the objects the SceneView is rendering
            if (!sceneView.IsGameObjectInThisSceneView(c.gameObject))
            {
                return;
            }

            Vector2 previewSize = c.targetTexture ? new Vector2(c.targetTexture.width, c.targetTexture.height) : PlayModeView.GetMainPlayModeViewTargetSize();

            if (previewSize.x < 0f)
            {
                // Fallback to Scene View of not a valid game view size
                previewSize.x = sceneView.position.width;
                previewSize.y = sceneView.position.height;
            }

            // Apply normalizedviewport rect of camera
            Rect normalizedViewPortRect = c.rect;

            // clamp normalized rect in [0,1]
            normalizedViewPortRect.xMin = Math.Max(normalizedViewPortRect.xMin, 0f);
            normalizedViewPortRect.yMin = Math.Max(normalizedViewPortRect.yMin, 0f);
            normalizedViewPortRect.xMax = Math.Min(normalizedViewPortRect.xMax, 1f);
            normalizedViewPortRect.yMax = Math.Min(normalizedViewPortRect.yMax, 1f);

            previewSize.x *= Mathf.Max(normalizedViewPortRect.width, 0f);
            previewSize.y *= Mathf.Max(normalizedViewPortRect.height, 0f);

            // Prevent using invalid previewSize
            if (previewSize.x < 1f || previewSize.y < 1f)
            {
                return;
            }

            float aspect = previewSize.x / previewSize.y;

            // Scale down (fit to scene view)
            previewSize.y = kPreviewNormalizedSize * sceneView.position.height;
            previewSize.x = previewSize.y * aspect;
            if (previewSize.y > sceneView.position.height * 0.5f)
            {
                previewSize.y = sceneView.position.height * 0.5f;
                previewSize.x = previewSize.y * aspect;
            }
            if (previewSize.x > sceneView.position.width * 0.5f)
            {
                previewSize.x = sceneView.position.width * 0.5f;
                previewSize.y = previewSize.x / aspect;
            }

            // Get and reserve rect
            Rect cameraRect = GUILayoutUtility.GetRect(previewSize.x, previewSize.y);

            cameraRect.width = Mathf.Floor(cameraRect.width);

            if (Event.current.type == EventType.Repaint)
            {
                Graphics.DrawTexture(cameraRect, Texture2D.whiteTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, Color.black);
            }

            var properWidth = cameraRect.height * aspect;

            cameraRect.x    += (cameraRect.width - properWidth) * 0.5f;
            cameraRect.width = properWidth;


            if (Event.current.type == EventType.Repaint)
            {
                // setup camera and render
                previewCamera.CopyFrom(c);

                // make sure the preview camera is rendering the same stage as the SceneView is
                if (sceneView.overrideSceneCullingMask != 0)
                {
                    previewCamera.overrideSceneCullingMask = sceneView.overrideSceneCullingMask;
                }
                else
                {
                    previewCamera.scene = sceneView.customScene;
                }

                // also make sure to sync any Skybox component on the preview camera
                var dstSkybox = previewCamera.GetComponent <Skybox>();
                if (dstSkybox)
                {
                    var srcSkybox = c.GetComponent <Skybox>();
                    if (srcSkybox && srcSkybox.enabled)
                    {
                        dstSkybox.enabled  = true;
                        dstSkybox.material = srcSkybox.material;
                    }
                    else
                    {
                        dstSkybox.enabled = false;
                    }
                }


                var previewTexture = GetPreviewTextureWithSize((int)cameraRect.width, (int)cameraRect.height);
                previewTexture.antiAliasing = Mathf.Max(1, QualitySettings.antiAliasing);
                previewCamera.targetTexture = previewTexture;
                previewCamera.pixelRect     = new Rect(0, 0, cameraRect.width, cameraRect.height);

                Handles.EmitGUIGeometryForCamera(c, previewCamera);

                if (c.usePhysicalProperties)
                {
                    // when sensor size is reduced, the previous frame is still visible behing so we need to clear the texture before rendering.
                    RenderTexture rt = RenderTexture.active;
                    RenderTexture.active = previewTexture;
                    GL.Clear(false, true, Color.clear);
                    RenderTexture.active = rt;
                }

                previewCamera.Render();
                Graphics.DrawTexture(cameraRect, previewTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, GUI.color, EditorGUIUtility.GUITextureBlit2SRGBMaterial);
            }
        }