using UnityEngine; using UnityEngine.UI; using Games.Events; using Games.Scene; using Module.Log; public class UICameraCopy : MonoBehaviour { #region static private static RenderTexture _CameraTexture; //private static Texture2D _CameraTexture2D; #endregion public RawImage _BackImage; public int _BlurRange; public void ShowBlur() { ControllerCamera(); } public void HideBlur() { ReleaseCamera(); } #region copy private bool ControllerCamera() { var result = false; if (!gameObject.activeInHierarchy) LogModule.ErrorLog("Try to show blur background while the UICameraCopy gameObject is not active!"); // 已经处于开启状态,就不做额外的处理 else if (_CameraTexture == null) { if (SceneLogic.CameraController != null) { var mainCamera = SceneLogic.CameraController.MainCamera; if (mainCamera != null) { _CameraTexture = RenderTexturePool.Instance.GetRenderTexture(new Vector2(Screen.width * 0.1f, Screen.height * 0.1f)); var temp = mainCamera.targetTexture; mainCamera.targetTexture = _CameraTexture; var mainCameraState = mainCamera.enabled; mainCamera.enabled = true; mainCamera.Render(); mainCamera.enabled = mainCameraState; _BackImage.texture = _CameraTexture; _BackImage.enabled = true; mainCamera.targetTexture = temp; result = true; EventDispatcher.Instance.Dispatch(Games.Events.EventId.UiCameraCopy, true); } } } return result; } private void ReleaseCamera() { if (_CameraTexture != null) { _BackImage.texture = null; _BackImage.enabled = false; RenderTexturePool.Instance.ReleaseRenderTexture(_CameraTexture); _CameraTexture = null; EventDispatcher.Instance.Dispatch(Games.Events.EventId.UiCameraCopy, false); } } #endregion #region camera culling private void SetCameraCulling() { _BackImage.enabled = false; if (SceneLogic.CameraController != null) SceneLogic.CameraController.RemoveLayerMask(LayerMask.GetMask("MainPlayer", "LogicObj")); } private void ResetCameraCulling() { if (SceneLogic.CameraController != null) SceneLogic.CameraController.ResetLayerMask(); } #endregion #region image blur public Texture2D ImageBlur(Texture2D orgTexture2d, int range) { float sumR = 0; float sumG = 0; float sumB = 0; int culTimes = 0; //Texture2D orgTexture2d = new Texture2D(orgTexture.width, orgTexture.height); //orgTexture2d.ReadPixels(new Rect(0, 0, orgTexture.width, orgTexture.height), 0, 0); //orgTexture2d.Apply(); Texture2D newTexture = new Texture2D(orgTexture2d.width, orgTexture2d.height); for (int width = 0; width < orgTexture2d.width; ++width) { for (int height = 0; height < orgTexture2d.height; ++height) { culTimes = 0; for (int i = width - range; i <= width + range; ++i) { for (int j = height - range; j <= height + range; ++j) { if (i >= 0 && i < orgTexture2d.width && j >= 0 && j < orgTexture2d.height) { ++culTimes; sumR += orgTexture2d.GetPixel(i, j).r; sumG += orgTexture2d.GetPixel(i, j).g; sumB += orgTexture2d.GetPixel(i, j).b; } } } newTexture.SetPixel(width, height, new Color(sumR / culTimes, sumG / culTimes, sumB / culTimes)); } } newTexture.Apply(); return newTexture; } #endregion }