优化ui,谱面替换,以及科研模糊
This commit is contained in:
@@ -0,0 +1,414 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Qjjxk.Blur
|
||||
{
|
||||
[ExecuteAlways]
|
||||
[DisallowMultipleComponent]
|
||||
public class BlurRawImage : MonoBehaviour
|
||||
{
|
||||
[SerializeField, ReadOnly] private Shader blurShader;
|
||||
|
||||
public BlurType blurType;
|
||||
private BlurType _blurType;
|
||||
[Range(0f, 64f)] public float blurStrength;
|
||||
private float _blurStrength;
|
||||
[Range(0, 32)] public float blurRadius;
|
||||
private float _blurRadius;
|
||||
[Range(0, 6)] public int iterations = 1;
|
||||
private int _iterations;
|
||||
[Range(1, 8)] public float downSample = 1;
|
||||
private float _downSample;
|
||||
|
||||
private static readonly int _blurRadiusId = Shader.PropertyToID("_BlurRadius");
|
||||
private static readonly int _regionId = Shader.PropertyToID("_Region");
|
||||
private static readonly int _iterationId = Shader.PropertyToID("_Iteration");
|
||||
|
||||
private Scene _scene;
|
||||
private Canvas _canvas;
|
||||
private bool _needResize;
|
||||
private Vector3 _localPos;
|
||||
private Vector2 _selfSize;
|
||||
private Camera _mainCamera;
|
||||
private Vector2 _sizeDelta;
|
||||
private RawImage _rawImage;
|
||||
private int _initStep = -1;
|
||||
private int _width, _height;
|
||||
private bool _needRefresh = true;
|
||||
private MonitorDimension _monitor;
|
||||
private Material _blurMat, _blurMat2;
|
||||
private RectTransform _rectTransform;
|
||||
private RenderTexture _tempRT, _blurRT;
|
||||
private BlurCameraPass _blurCameraPass;
|
||||
private UniversalAdditionalCameraData _cameraData;
|
||||
private Material[] _blurMats;
|
||||
#if UNITY_EDITOR && UNITY_6000_0_OR_NEWER
|
||||
private RenderTexture _cameraRT;
|
||||
#endif
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_mainCamera = Camera.main;
|
||||
blurShader = Shader.Find("Qjjxk/BlurImage");
|
||||
var haveRawImage = TryGetComponent(out _rawImage);
|
||||
if (haveRawImage) _canvas = _rawImage.canvas;
|
||||
|
||||
if (_mainCamera && blurShader && _canvas) _initStep++;
|
||||
else _initStep--;
|
||||
if (_initStep < -1) return;
|
||||
|
||||
_scene = gameObject.scene;
|
||||
_blurType = blurType;
|
||||
_blurRadius = blurRadius;
|
||||
_iterations = iterations;
|
||||
downSample = 1;
|
||||
_downSample = downSample;
|
||||
_blurMat = new Material(blurShader);
|
||||
_blurMat2 = new Material(blurShader);
|
||||
_blurMats = new[]
|
||||
{
|
||||
new Material(blurShader),
|
||||
new Material(blurShader),
|
||||
new Material(blurShader),
|
||||
new Material(blurShader),
|
||||
new Material(blurShader),
|
||||
new Material(blurShader),
|
||||
};
|
||||
_rectTransform = _rawImage.rectTransform;
|
||||
_sizeDelta = _rectTransform.sizeDelta.Round();
|
||||
_localPos = _rectTransform.localPosition.Round();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (_initStep < 1 || !_scene.isLoaded) return;
|
||||
|
||||
if (_blurRT == null || !_blurRT.IsCreated())
|
||||
{
|
||||
RefreshPixelSize();
|
||||
_blurRT = new RenderTexture(_width, _height, 0) { name = "BlurRT" };
|
||||
_tempRT = new RenderTexture(_width, _height, 0) { name = "TempRT" };
|
||||
_blurRT.Create();
|
||||
_tempRT.Create();
|
||||
}
|
||||
|
||||
_rawImage.texture = _blurRT;
|
||||
_rawImage.enabled = true;
|
||||
RegisterMonitor(true);
|
||||
|
||||
// 每次重新创建 Pass 以更新内部 RTHandle
|
||||
_blurCameraPass = new BlurCameraPass(_blurType, _blurRT, _tempRT, _blurMats, _blurMat, _blurMat2, _iterations);
|
||||
|
||||
_cameraData = _mainCamera.GetUniversalAdditionalCameraData();
|
||||
if (iterations != 0) RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
downSample = 1;
|
||||
if (_initStep > -1)
|
||||
{
|
||||
blurShader = Shader.Find("Qjjxk/BlurImage");
|
||||
_rawImage.texture = _blurRT;
|
||||
return;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
EditorUtility.DisplayDialog("Error",
|
||||
"The MainCamera or blur Shader was not found, or the script has not been added to the component containing the RawImage.", "OK");
|
||||
EditorApplication.delayCall += () => { DestroyImmediate(this); };
|
||||
#endif
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (_initStep < 0) return;
|
||||
_initStep++;
|
||||
|
||||
RefreshPixelSize();
|
||||
|
||||
_tempRT = new RenderTexture(_width, _height, 0) { name = "TempRT" };
|
||||
_blurRT = new RenderTexture(_width, _height, 0) { name = "BlurRT" };
|
||||
#if UNITY_EDITOR && UNITY_6000_0_OR_NEWER
|
||||
_cameraRT = new RenderTexture(_mainCamera.pixelWidth, _mainCamera.pixelHeight, 16) { name = "CameraRT" };
|
||||
#endif
|
||||
|
||||
OnEnable();
|
||||
|
||||
SetBlurStrength();
|
||||
SetBlurRange();
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_initStep < 1) return;
|
||||
|
||||
if (_rawImage != null && (_rawImage.texture == null || _rawImage.texture != _blurRT))
|
||||
{
|
||||
_rawImage.texture = _blurRT;
|
||||
}
|
||||
|
||||
RefreshBlurType();
|
||||
|
||||
RefreshStrength();
|
||||
|
||||
RefreshIterations();
|
||||
|
||||
RefreshDownSample();
|
||||
|
||||
RefreshBlurRadius();
|
||||
|
||||
if (_rectTransform.hasChanged)
|
||||
{
|
||||
RefreshPixelSize();
|
||||
SetBlurRange();
|
||||
_rectTransform.hasChanged = false;
|
||||
}
|
||||
|
||||
ForceCameraRender();
|
||||
}
|
||||
|
||||
private void OnBeginCameraRendering(ScriptableRenderContext context, Camera tarCamera)
|
||||
{
|
||||
if (tarCamera != _mainCamera) return;
|
||||
if (_needResize) ResizeRT();
|
||||
if (_cameraData != null && _cameraData.scriptableRenderer != null && _blurCameraPass != null)
|
||||
_cameraData.scriptableRenderer.EnqueuePass(_blurCameraPass);
|
||||
}
|
||||
|
||||
private void RefreshBlurType()
|
||||
{
|
||||
if (_blurType == blurType) return;
|
||||
_blurType = blurType;
|
||||
if (_blurCameraPass != null) _blurCameraPass.blurType = _blurType;
|
||||
_needRefresh = true;
|
||||
RefreshStrength(true);
|
||||
}
|
||||
|
||||
private void RefreshStrength(bool force = false)
|
||||
{
|
||||
if (Mathf.Approximately(_blurStrength, blurStrength) && !force) return;
|
||||
_blurStrength = blurStrength;
|
||||
|
||||
if (_blurType == BlurType.Gaussian)
|
||||
{
|
||||
switch (_blurStrength)
|
||||
{
|
||||
case <= 2:
|
||||
iterations = 1;
|
||||
blurRadius = _blurStrength;
|
||||
break;
|
||||
case <= 10:
|
||||
iterations = 2;
|
||||
blurRadius = (_blurStrength + 0.5f) / Mathf.Sqrt(iterations);
|
||||
break;
|
||||
case <= 20:
|
||||
iterations = 3;
|
||||
blurRadius = _blurStrength / Mathf.Sqrt(iterations);
|
||||
break;
|
||||
case <= 50:
|
||||
iterations = 4;
|
||||
blurRadius = (_blurStrength - 0.5f) / Mathf.Sqrt(iterations);
|
||||
break;
|
||||
default:
|
||||
iterations = 5;
|
||||
blurRadius = (_blurStrength - 1) / Mathf.Sqrt(iterations);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (_blurType == BlurType.Kawase)
|
||||
{
|
||||
iterations = _blurStrength switch
|
||||
{
|
||||
<= 0 => 1,
|
||||
<= 10 => 3,
|
||||
<= 20 => 4,
|
||||
<= 30 => 5,
|
||||
_ => 6
|
||||
};
|
||||
blurRadius = _blurStrength / 2;
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterMonitor(bool isReg)
|
||||
{
|
||||
if (!_monitor) _monitor = _canvas.GetComponent<MonitorDimension>();
|
||||
if (!_monitor)
|
||||
{
|
||||
_monitor = _canvas.gameObject.AddComponent<MonitorDimension>();
|
||||
_monitor.hideFlags = HideFlags.HideInInspector;
|
||||
}
|
||||
|
||||
_monitor.Register(this, isReg);
|
||||
}
|
||||
|
||||
public void SetWindowSize()
|
||||
{
|
||||
if (_initStep < 1) return;
|
||||
|
||||
RefreshPixelSize();
|
||||
_needResize = true;
|
||||
SetBlurRange();
|
||||
}
|
||||
|
||||
private void RefreshPixelSize()
|
||||
{
|
||||
_width = Mathf.RoundToInt(_rectTransform.rect.width * _canvas.scaleFactor);
|
||||
_height = Mathf.RoundToInt(_rectTransform.rect.height * _canvas.scaleFactor);
|
||||
}
|
||||
|
||||
private void ResizeRT()
|
||||
{
|
||||
_needResize = false;
|
||||
if (_blurRT != null) _blurRT.Resize(_width / _downSample, _height / _downSample);
|
||||
if (_tempRT != null) _tempRT.Resize(_width / _downSample, _height / _downSample);
|
||||
#if UNITY_EDITOR && UNITY_6000_0_OR_NEWER
|
||||
if (_cameraRT != null) _cameraRT.Resize(_canvas.pixelRect.width, _canvas.pixelRect.height);
|
||||
#endif
|
||||
}
|
||||
|
||||
private void RefreshIterations()
|
||||
{
|
||||
if (_iterations == iterations) return;
|
||||
SetRenderPipeline();
|
||||
_iterations = iterations;
|
||||
if (_blurCameraPass != null) _blurCameraPass.iterations = _iterations;
|
||||
if (_blurMats != null && _blurMats.Length > 0 && _blurMats[0] != null) _blurMats[0].SetFloat(_iterationId, _iterations);
|
||||
_needRefresh = true;
|
||||
}
|
||||
|
||||
private void SetRenderPipeline()
|
||||
{
|
||||
if (_iterations != 0 && iterations == 0)
|
||||
{
|
||||
_blurRT.Release();
|
||||
_blurRT.Create();
|
||||
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
|
||||
}
|
||||
else if (_iterations == 0 && iterations != 0)
|
||||
{
|
||||
RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshDownSample()
|
||||
{
|
||||
if (Mathf.Approximately(_downSample, downSample)) return;
|
||||
_downSample = downSample;
|
||||
_needResize = true;
|
||||
SetBlurStrength();
|
||||
_needRefresh = true;
|
||||
}
|
||||
|
||||
private void RefreshBlurRadius()
|
||||
{
|
||||
if (Mathf.Approximately(_blurRadius, blurRadius)) return;
|
||||
_blurRadius = blurRadius;
|
||||
SetBlurStrength();
|
||||
_needRefresh = true;
|
||||
}
|
||||
|
||||
private void SetBlurStrength()
|
||||
{
|
||||
if (_blurMat != null) _blurMat.SetFloat(_blurRadiusId, _blurRadius / _downSample);
|
||||
if (_blurMat2 != null) _blurMat2.SetFloat(_blurRadiusId, _blurRadius / _downSample * 0.618f);
|
||||
|
||||
if (_blurMats != null)
|
||||
{
|
||||
for (var i = 0; i < _blurMats.Length; i++)
|
||||
{
|
||||
if (_blurMats[i] != null)
|
||||
_blurMats[i].SetFloat(_blurRadiusId, _blurRadius * 2 / _downSample * Mathf.Pow(0.5f, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetBlurRange()
|
||||
{
|
||||
if (_rectTransform == null || _canvas == null || _blurMat == null) return;
|
||||
|
||||
Vector3[] corners = new Vector3[4];
|
||||
_rectTransform.GetWorldCorners(corners);
|
||||
|
||||
Camera cam = _canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : _canvas.worldCamera;
|
||||
|
||||
Vector2 screenMin = RectTransformUtility.WorldToScreenPoint(cam, corners[0]);
|
||||
Vector2 screenMax = RectTransformUtility.WorldToScreenPoint(cam, corners[2]);
|
||||
|
||||
Rect canvasRect = _canvas.pixelRect;
|
||||
float xMin = screenMin.x / canvasRect.width;
|
||||
float xMax = screenMax.x / canvasRect.width;
|
||||
float yMin = screenMin.y / canvasRect.height;
|
||||
float yMax = screenMax.y / canvasRect.height;
|
||||
|
||||
_blurMat.SetVector(_regionId, new Vector4(xMin, yMin, xMax, yMax));
|
||||
_needRefresh = true;
|
||||
}
|
||||
|
||||
private void ForceCameraRender()
|
||||
{
|
||||
if (!Application.isEditor) return;
|
||||
if (!_needRefresh && Application.isPlaying) return;
|
||||
_needRefresh = false;
|
||||
#if UNITY_EDITOR && UNITY_6000_0_OR_NEWER
|
||||
if (_cameraRT == null)
|
||||
{
|
||||
if (_mainCamera != null)
|
||||
{
|
||||
_cameraRT = new RenderTexture(_mainCamera.pixelWidth, _mainCamera.pixelHeight, 16) { name = "CameraRT" };
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
RenderPipeline.SubmitRenderRequest(_mainCamera, new RenderPipeline.StandardRequest { destination = _cameraRT });
|
||||
#else
|
||||
_mainCamera.Render();
|
||||
#endif
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
|
||||
if (_initStep < 1 || !_scene.isLoaded) return;
|
||||
|
||||
_rawImage.texture = null;
|
||||
_rawImage.enabled = false;
|
||||
RegisterMonitor(false);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (_tempRT != null)
|
||||
{
|
||||
_tempRT.Release();
|
||||
DestroyImmediate(_tempRT);
|
||||
}
|
||||
if (_blurRT != null)
|
||||
{
|
||||
_blurRT.Release();
|
||||
DestroyImmediate(_blurRT);
|
||||
}
|
||||
#if UNITY_EDITOR && UNITY_6000_0_OR_NEWER
|
||||
if (_cameraRT != null)
|
||||
{
|
||||
_cameraRT.Release();
|
||||
DestroyImmediate(_cameraRT);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public enum BlurType
|
||||
{
|
||||
Gaussian,
|
||||
Kawase,
|
||||
Grainy
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a730e8f3020586546889222d57c607a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 308958
|
||||
packageName: Blur RawImage UGUI
|
||||
packageVersion: 1.4
|
||||
assetPath: Assets/BlurRawImage/Scripts/BlurRawImage.cs
|
||||
uploadId: 761342
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59fb22bfcf919e048bce3711531e9693
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,201 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
using UnityEngine.Rendering.RenderGraphModule;
|
||||
#endif
|
||||
|
||||
namespace Qjjxk.Blur
|
||||
{
|
||||
public class BlurCameraPass : ScriptableRenderPass
|
||||
{
|
||||
private readonly RTHandle _blurRT, _tempRT;
|
||||
private readonly Material _blurMat, _blurMat2;
|
||||
public int iterations;
|
||||
public BlurType blurType;
|
||||
|
||||
private readonly Material[] _blurMats;
|
||||
|
||||
public BlurCameraPass(BlurType blurType, RenderTexture blurRT, RenderTexture tempRT, Material[] blurMats, Material mat, Material mat2,
|
||||
int iteration)
|
||||
{
|
||||
renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
|
||||
this.blurType = blurType;
|
||||
_tempRT = RTHandles.Alloc(tempRT);
|
||||
_blurRT = RTHandles.Alloc(blurRT);
|
||||
_blurMats = blurMats;
|
||||
_blurMat = mat;
|
||||
_blurMat2 = mat2;
|
||||
iterations = iteration;
|
||||
}
|
||||
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
[System.Obsolete]
|
||||
#endif
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
var commandBuffer = CommandBufferPool.Get("CameraCapture");
|
||||
commandBuffer.Clear();
|
||||
#if UNITY_2022
|
||||
var source = renderingData.cameraData.renderer.cameraColorTargetHandle;
|
||||
#else
|
||||
var source = renderingData.cameraData.renderer.cameraColorTarget;
|
||||
#endif
|
||||
|
||||
if (blurType == BlurType.Gaussian)
|
||||
{
|
||||
Blit(commandBuffer, source, _blurRT, _blurMat);
|
||||
for (var i = 0; i < iterations; i++)
|
||||
{
|
||||
var blurMat = i == 1 ? _blurMat2 : _blurMat;
|
||||
Blit(commandBuffer, _blurRT, _tempRT, blurMat, 1);
|
||||
Blit(commandBuffer, _tempRT, _blurRT, blurMat, 2);
|
||||
}
|
||||
}
|
||||
else if (blurType == BlurType.Kawase)
|
||||
{
|
||||
Blit(commandBuffer, source, _tempRT, _blurMat);
|
||||
for (var i = 0; i < 2 * iterations - 1; i++)
|
||||
{
|
||||
var index = i < iterations ? i : 2 * iterations - i - 2;
|
||||
if (index % 2 == 0) Blit(commandBuffer, _tempRT, _blurRT, _blurMats[index], 4);
|
||||
else Blit(commandBuffer, _blurRT, _tempRT, _blurMats[index], 4);
|
||||
}
|
||||
}
|
||||
else if (blurType == BlurType.Grainy)
|
||||
{
|
||||
Blit(commandBuffer, source, _tempRT, _blurMat);
|
||||
Blit(commandBuffer, _tempRT, _blurRT, _blurMats[0], 3);
|
||||
}
|
||||
|
||||
context.ExecuteCommandBuffer(commandBuffer);
|
||||
CommandBufferPool.Release(commandBuffer);
|
||||
}
|
||||
|
||||
#if UNITY_6000_0_OR_NEWER
|
||||
private class BlurPassData
|
||||
{
|
||||
public TextureHandle inputTh;
|
||||
public Material blurMat;
|
||||
public int pass;
|
||||
}
|
||||
|
||||
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
|
||||
{
|
||||
if (_blurRT == null || _blurRT.rt == null || _tempRT == null || _tempRT.rt == null)
|
||||
return;
|
||||
|
||||
if (_blurMat == null || _blurMat2 == null)
|
||||
return;
|
||||
|
||||
var resourceData = frameData.Get<UniversalResourceData>();
|
||||
|
||||
var blurDesc = _blurRT.rt.descriptor;
|
||||
var blurInfo = new RenderTargetInfo
|
||||
{
|
||||
width = blurDesc.width,
|
||||
height = blurDesc.height,
|
||||
volumeDepth = blurDesc.volumeDepth,
|
||||
msaaSamples = blurDesc.msaaSamples,
|
||||
format = blurDesc.graphicsFormat,
|
||||
};
|
||||
|
||||
var tempDesc = _tempRT.rt.descriptor;
|
||||
var tempInfo = new RenderTargetInfo
|
||||
{
|
||||
width = tempDesc.width,
|
||||
height = tempDesc.height,
|
||||
volumeDepth = tempDesc.volumeDepth,
|
||||
msaaSamples = tempDesc.msaaSamples,
|
||||
format = tempDesc.graphicsFormat,
|
||||
};
|
||||
|
||||
var blurHandle = renderGraph.ImportTexture(_blurRT, blurInfo);
|
||||
var tempHandle = renderGraph.ImportTexture(_tempRT, tempInfo);
|
||||
|
||||
if (blurType == BlurType.Gaussian)
|
||||
{
|
||||
for (var i = 0; i < iterations * 2 + 1; i++)
|
||||
{
|
||||
using var builder = renderGraph.AddRasterRenderPass<BlurPassData>("BlurCameraPass", out var passData);
|
||||
passData.blurMat = i is 3 or 4 ? _blurMat2 : _blurMat;
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
passData.pass = 0;
|
||||
passData.inputTh = resourceData.activeColorTexture;
|
||||
}
|
||||
else if (i % 2 == 1)
|
||||
{
|
||||
passData.pass = 1;
|
||||
passData.inputTh = blurHandle;
|
||||
}
|
||||
else
|
||||
{
|
||||
passData.pass = 2;
|
||||
passData.inputTh = tempHandle;
|
||||
}
|
||||
|
||||
builder.SetRenderAttachment(i % 2 == 0 ? blurHandle : tempHandle, 0, AccessFlags.WriteAll);
|
||||
builder.SetRenderFunc((BlurPassData data, RasterGraphContext context) =>
|
||||
{
|
||||
if (data.blurMat != null)
|
||||
Blitter.BlitTexture(context.cmd, data.inputTh, new Vector4(1, 1, 0, 0), data.blurMat, data.pass);
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (blurType == BlurType.Kawase)
|
||||
{
|
||||
if (_blurMats == null) return;
|
||||
|
||||
for (var i = -1; i < 2 * iterations - 1; i++)
|
||||
{
|
||||
var index = i < iterations ? i : 2 * iterations - i - 2;
|
||||
|
||||
using var builder = renderGraph.AddRasterRenderPass<BlurPassData>("BlurCameraPass", out var passData);
|
||||
passData.blurMat = index == -1 ? _blurMat : (index >= 0 && index < _blurMats.Length ? _blurMats[index] : null);
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
passData.pass = 0;
|
||||
passData.inputTh = resourceData.activeColorTexture;
|
||||
}
|
||||
else if (index % 2 == 0)
|
||||
{
|
||||
passData.pass = 4;
|
||||
passData.inputTh = tempHandle;
|
||||
}
|
||||
else
|
||||
{
|
||||
passData.pass = 4;
|
||||
passData.inputTh = blurHandle;
|
||||
}
|
||||
|
||||
builder.SetRenderAttachment(i % 2 == 0 ? blurHandle : tempHandle, 0, AccessFlags.WriteAll);
|
||||
builder.SetRenderFunc((BlurPassData data, RasterGraphContext context) =>
|
||||
{
|
||||
if (data.blurMat != null)
|
||||
Blitter.BlitTexture(context.cmd, data.inputTh, new Vector4(1, 1, 0, 0), data.blurMat, data.pass);
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (blurType == BlurType.Grainy)
|
||||
{
|
||||
if (_blurMats == null || _blurMats.Length == 0) return;
|
||||
|
||||
using var builder = renderGraph.AddRasterRenderPass<BlurPassData>("BlurCameraPass", out var passData);
|
||||
passData.blurMat = _blurMats[0];
|
||||
passData.pass = 3;
|
||||
passData.inputTh = resourceData.activeColorTexture;
|
||||
|
||||
builder.SetRenderAttachment(blurHandle, 0, AccessFlags.WriteAll);
|
||||
builder.SetRenderFunc((BlurPassData data, RasterGraphContext context) =>
|
||||
{
|
||||
if (data.blurMat != null)
|
||||
Blitter.BlitTexture(context.cmd, data.inputTh, new Vector4(1, 1, 0, 0), data.blurMat, data.pass);
|
||||
});
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c52055676d99ddb4cb5267256e942cb1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,49 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Qjjxk.Blur
|
||||
{
|
||||
public static class ExtensionMethod
|
||||
{
|
||||
private static void Resize(this RenderTexture rt, int width, int height)
|
||||
{
|
||||
if (rt == null) return;
|
||||
rt.Release();
|
||||
rt.width = width;
|
||||
rt.height = height;
|
||||
rt.Create();
|
||||
}
|
||||
|
||||
public static void Resize(this RenderTexture rt, float width, float height)
|
||||
{
|
||||
rt.Resize((int)width, (int)height);
|
||||
}
|
||||
|
||||
public static Vector3 Round(this Vector3 tarVector)
|
||||
{
|
||||
return new Vector3(Mathf.Round(tarVector.x), Mathf.Round(tarVector.y), Mathf.Round(tarVector.z));
|
||||
}
|
||||
|
||||
public static Vector2 Round(this Vector2 tarVector)
|
||||
{
|
||||
return new Vector2(Mathf.Round(tarVector.x), Mathf.Round(tarVector.y));
|
||||
}
|
||||
|
||||
public static void TryAdd<T>(this List<T> list, T t)
|
||||
{
|
||||
if (list.Contains(t)) return;
|
||||
list.Add(t);
|
||||
}
|
||||
|
||||
public static void Destroy(this Component tarComp)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (Application.isPlaying) Object.Destroy(tarComp);
|
||||
else EditorApplication.delayCall += () => Object.DestroyImmediate(tarComp);
|
||||
#else
|
||||
Object.Destroy(tarComp);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b098834905d26e44bdb35ccf083a438
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 308958
|
||||
packageName: Blur RawImage UGUI
|
||||
packageVersion: 1.4
|
||||
assetPath: Assets/BlurRawImage/Scripts/Utils/ExtensionMethod.cs
|
||||
uploadId: 761342
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Qjjxk.Blur
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class ReadOnlyAttribute : PropertyAttribute
|
||||
{
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
|
||||
public class ReadOnlyDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
GUI.enabled = false;
|
||||
EditorGUI.PropertyField(position, property, label);
|
||||
GUI.enabled = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca85a5a523f3f274a8eb06a3c1a4ed3c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 308958
|
||||
packageName: Blur RawImage UGUI
|
||||
packageVersion: 1.4
|
||||
assetPath: Assets/BlurRawImage/Scripts/Utils/MiracleAttributes.cs
|
||||
uploadId: 761342
|
||||
@@ -0,0 +1,51 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace Qjjxk.Blur
|
||||
{
|
||||
[ExecuteAlways]
|
||||
public class MonitorDimension : MonoBehaviour
|
||||
{
|
||||
public RenderTexture captureRT;
|
||||
public CameraEvent cameraEvent;
|
||||
private List<BlurRawImage> _blurList;
|
||||
private CommandBuffer _cmdBuffer;
|
||||
private Camera _mainCamera;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_mainCamera = Camera.main!;
|
||||
captureRT = new RenderTexture(_mainCamera.pixelWidth, _mainCamera.pixelHeight, 0) { name = "CaptureRT" };
|
||||
_cmdBuffer = new CommandBuffer { name = "Capture" };
|
||||
_cmdBuffer.Blit(BuiltinRenderTextureType.CurrentActive, captureRT);
|
||||
cameraEvent = Application.isEditor ? CameraEvent.BeforeImageEffects : CameraEvent.AfterEverything;
|
||||
_mainCamera!.AddCommandBuffer(cameraEvent, _cmdBuffer);
|
||||
}
|
||||
|
||||
public void Register(BlurRawImage blurRawImage, bool isReg = true)
|
||||
{
|
||||
_blurList ??= new List<BlurRawImage>();
|
||||
if (isReg)
|
||||
{
|
||||
_blurList.TryAdd(blurRawImage);
|
||||
}
|
||||
else
|
||||
{
|
||||
_blurList.Remove(blurRawImage);
|
||||
if (_blurList.Count != 0) return;
|
||||
this.Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRectTransformDimensionsChange()
|
||||
{
|
||||
if (!gameObject.scene.isLoaded || _blurList == null || _blurList.Count == 0) return;
|
||||
captureRT.Resize(_mainCamera.pixelWidth, _mainCamera.pixelHeight);
|
||||
foreach (var blurRawImage in _blurList)
|
||||
{
|
||||
blurRawImage.SetWindowSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 78f09e0a818ad7c4cbdabcb2055e725d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 308958
|
||||
packageName: Blur RawImage UGUI
|
||||
packageVersion: 1.4
|
||||
assetPath: Assets/BlurRawImage/Scripts/Utils/MonitorDimension.cs
|
||||
uploadId: 761342
|
||||
Reference in New Issue
Block a user