首次提交

This commit is contained in:
Bob.Song
2026-03-05 18:07:55 +08:00
commit e125bb869e
4534 changed files with 563920 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
using FairyGUI;
using NBF;
using UnityEngine;
using UnityEngine.UI;
public class UIBlur : MonoBehaviour
{
[Header("Quality")] public RenderTexture blurRT;
[Range(1, 6)] public int iterations = 2; // 模糊迭代次数
[Range(0.5f, 4f)] public float blurSize = 1.2f;
[Header("Shader")] public Shader blurShader;
Material _mat;
RenderTexture _capturedRT;
public static UIBlur Instance { get; private set; }
void Awake()
{
blurRT = new RenderTexture((int)(Screen.width * 0.5f), (int)(Screen.height * 0.5f), 0,
RenderTextureFormat.ARGB32);
_mat = new Material(blurShader);
DontDestroyOnLoad(gameObject);
Instance = this;
}
public void CaptureAndBlurOnce()
{
if (blurRT == null) return;
var captureCamera = Camera.main;
if (captureCamera == null) return;
int w = blurRT.width;
int h = blurRT.width;
ReleaseRTs();
var oldEnabled = StageCamera.main.enabled;
StageCamera.main.enabled = false;
_capturedRT = new RenderTexture(w, h, 0, RenderTextureFormat.ARGB32);
_capturedRT.Create();
// 1) 抓摄像机画面到RT不做ReadPixels
var prevTarget = captureCamera.targetTexture;
captureCamera.targetTexture = _capturedRT;
captureCamera.Render();
captureCamera.targetTexture = prevTarget;
StageCamera.main.enabled = oldEnabled;
// 2) 模糊ping-pong
var rt1 = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32);
var rt2 = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32);
Graphics.Blit(_capturedRT, rt1);
_mat.SetFloat("_BlurSize", blurSize);
for (int i = 0; i < iterations; i++)
{
// 横向
_mat.SetVector("_Direction", new Vector2(1, 0));
Graphics.Blit(rt1, rt2, _mat);
// 纵向
_mat.SetVector("_Direction", new Vector2(0, 1));
Graphics.Blit(rt2, rt1, _mat);
}
Graphics.Blit(rt1, blurRT);
RenderTexture.ReleaseTemporary(rt1);
RenderTexture.ReleaseTemporary(rt2);
}
void ReleaseRTs()
{
if (_capturedRT != null)
{
_capturedRT.Release();
Destroy(_capturedRT);
_capturedRT = null;
}
}
void OnDestroy()
{
ReleaseRTs();
if (_mat) Destroy(_mat);
}
}