滤镜增加通用参数RenderScale

优化滤镜机制
This commit is contained in:
ALIENJACK\alien 2024-12-27 14:11:31 +08:00
parent 37bbbd7cfd
commit 59576be9ee
8 changed files with 67 additions and 37 deletions

View File

@ -9,17 +9,23 @@ namespace AxibugEmuOnline.Client
{ {
public abstract class FilterEffect public abstract class FilterEffect
{ {
public BoolParameter Enable = new BoolParameter(false); public bool Enable { get; set; }
[Range(0.1f, 4f)]
public FloatParameter RenderScale;
public abstract string Name { get; } public abstract string Name { get; }
public IReadOnlyCollection<EditableParamerter> EditableParam => m_editableParamList.AsReadOnly(); public IReadOnlyCollection<EditableParamerter> EditableParam => m_editableParamList.AsReadOnly();
List<EditableParamerter> m_editableParamList; List<EditableParamerter> m_editableParamList;
Material m_material; Material m_material;
protected virtual float m_defaultRenderScale { get => 1f; }
protected abstract string ShaderName { get; } protected abstract string ShaderName { get; }
private static int m_iResolutionID = Shader.PropertyToID("_iResolution");
public FilterEffect() public FilterEffect()
{ {
RenderScale = new FloatParameter(m_defaultRenderScale);
GetEditableFilterParamters(); GetEditableFilterParamters();
m_material = new Material(Shader.Find(ShaderName)); m_material = new Material(Shader.Find(ShaderName));
OnInit(m_material); OnInit(m_material);
@ -31,7 +37,6 @@ namespace AxibugEmuOnline.Client
{ {
var parameters = (from t in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public) var parameters = (from t in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)
where t.FieldType.IsSubclassOf(typeof(FilterParameter)) where t.FieldType.IsSubclassOf(typeof(FilterParameter))
where t.DeclaringType.IsSubclassOf(typeof(FilterEffect))
orderby t.MetadataToken orderby t.MetadataToken
select t); select t);
@ -54,7 +59,7 @@ namespace AxibugEmuOnline.Client
public void Render(Texture src, RenderTexture result) public void Render(Texture src, RenderTexture result)
{ {
m_material.SetVector("_iResolution", new Vector4(result.width, result.height)); m_material.SetVector(m_iResolutionID, new Vector4(result.width, result.height));
OnRenderer(m_material, src, result); OnRenderer(m_material, src, result);
} }

View File

@ -4,7 +4,9 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using UnityEngine; using UnityEngine;
using UnityEngine.UI;
using static AxibugEmuOnline.Client.FilterEffect; using static AxibugEmuOnline.Client.FilterEffect;
using static AxibugEmuOnline.Client.FilterManager;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
@ -48,34 +50,63 @@ namespace AxibugEmuOnline.Client
} }
private RenderTexture result = null; private RenderTexture result = null;
public Texture ExecuteFilterRender(Texture src) public void ExecuteFilterRender(Texture src, RawImage renderGraphic)
{ {
if (result == null) //获得激活的滤镜
{ Filter activeFilter = null;
result = RenderTexture.GetTemporary(Screen.width, Screen.height);
}
else if (result.width != Screen.width || result.height != Screen.height)
{
RenderTexture.ReleaseTemporary(result);
result = RenderTexture.GetTemporary(Screen.width, Screen.height);
}
bool anyFilterEnable = false;
foreach (var filter in Filters) foreach (var filter in Filters)
{ {
if (!filter.m_setting.Enable.GetValue()) continue; if (!filter.m_setting.Enable) continue;
filter.m_setting.Render(src, result); activeFilter = filter;
anyFilterEnable = true; break;
} }
if (anyFilterEnable) if (activeFilter == null)
return result; {
else renderGraphic.texture = src;
return src; return;
} }
var resolution = GetRawImageScreenResolution(renderGraphic);
int resWidth = (int)(resolution.x * activeFilter.m_setting.RenderScale.GetValue());
int resHeight = (int)(resolution.y * activeFilter.m_setting.RenderScale.GetValue());
if (result == null)
{
result = RenderTexture.GetTemporary(resWidth, resHeight);
}
else if (result.width != resWidth || result.height != resHeight)
{
RenderTexture.ReleaseTemporary(result);
result = RenderTexture.GetTemporary(resWidth, resHeight);
}
activeFilter.m_setting.Render(src, result);
renderGraphic.texture = result;
}
Vector2 GetRawImageScreenResolution(RawImage rawImage)
{
// 获取 RawImage 的 RectTransform
RectTransform rectTransform = rawImage.rectTransform;
// 获取 RawImage 在屏幕上的四个顶点的世界坐标
Vector3[] corners = new Vector3[4];
rectTransform.GetWorldCorners(corners);
// 左下角和右上角的屏幕坐标
Vector2 bottomLeft = RectTransformUtility.WorldToScreenPoint(rawImage.canvas.worldCamera, corners[0]);
Vector2 topRight = RectTransformUtility.WorldToScreenPoint(rawImage.canvas.worldCamera, corners[2]);
// 计算宽度和高度
float width = topRight.x - bottomLeft.x;
float height = topRight.y - bottomLeft.y;
return new Vector2(width, height);
}
/// <summary> 关闭滤镜预览 </summary> /// <summary> 关闭滤镜预览 </summary>
public void ShutDownFilterPreview() public void ShutDownFilterPreview()
{ {
@ -96,8 +127,8 @@ namespace AxibugEmuOnline.Client
{ {
foreach (var selfFiler in Filters) foreach (var selfFiler in Filters)
{ {
if (selfFiler != filter) selfFiler.m_setting.Enable.Override(false); if (selfFiler != filter) selfFiler.m_setting.Enable = false;
else selfFiler.m_setting.Enable.Override(true); else selfFiler.m_setting.Enable = true;
} }
} }
@ -108,7 +139,7 @@ namespace AxibugEmuOnline.Client
{ {
//关闭所有后处理效果 //关闭所有后处理效果
foreach (var filter in Filters) foreach (var filter in Filters)
filter.m_setting.Enable.Override(false); filter.m_setting.Enable = false;
} }
/// <summary> /// <summary>

View File

@ -37,7 +37,6 @@ public sealed class FixingPixelArtGrille : FilterEffect
protected override void OnRenderer(Material renderMat, Texture src, RenderTexture result) protected override void OnRenderer(Material renderMat, Texture src, RenderTexture result)
{ {
renderMat.SetVector("_iResolution", new Vector4(result.width, result.height, 0, 0));
renderMat.SetVector("_res", new Vector4(DrawResolution.GetValue().x, DrawResolution.GetValue().y, 0, 0)); renderMat.SetVector("_res", new Vector4(DrawResolution.GetValue().x, DrawResolution.GetValue().y, 0, 0));
renderMat.SetFloat("_hardScan", HardScan.GetValue()); renderMat.SetFloat("_hardScan", HardScan.GetValue());
renderMat.SetFloat("_hardPix", HardPix.GetValue()); renderMat.SetFloat("_hardPix", HardPix.GetValue());

View File

@ -9,7 +9,6 @@ public sealed class LCDPostEffect : FilterEffect
protected override void OnRenderer(Material renderMat, Texture src, RenderTexture result) protected override void OnRenderer(Material renderMat, Texture src, RenderTexture result)
{ {
renderMat.SetVector("_iResolution", new Vector4(result.width, result.height, 0, 0));
Graphics.Blit(src, result, renderMat); Graphics.Blit(src, result, renderMat);
} }
} }

View File

@ -13,11 +13,9 @@ public sealed class MattiasCRT : FilterEffect
private LocalKeyword _kw_qualityLow; private LocalKeyword _kw_qualityLow;
private LocalKeyword _kw_qualityMid; private LocalKeyword _kw_qualityMid;
private LocalKeyword _kw_qualityHigh; private LocalKeyword _kw_qualityHigh;
private int _pid_iResolution;
protected override void OnInit(Material renderMat) protected override void OnInit(Material renderMat)
{ {
_pid_iResolution = Shader.PropertyToID("_iResolution");
_kw_qualityLow = new LocalKeyword(renderMat.shader, "_QUALITY_LOW"); _kw_qualityLow = new LocalKeyword(renderMat.shader, "_QUALITY_LOW");
_kw_qualityMid = new LocalKeyword(renderMat.shader, "_QUALITY_MID"); _kw_qualityMid = new LocalKeyword(renderMat.shader, "_QUALITY_MID");
_kw_qualityHigh = new LocalKeyword(renderMat.shader, "_QUALITY_HIGH"); _kw_qualityHigh = new LocalKeyword(renderMat.shader, "_QUALITY_HIGH");
@ -25,7 +23,6 @@ public sealed class MattiasCRT : FilterEffect
protected override void OnRenderer(Material renderMat, Texture src, RenderTexture result) protected override void OnRenderer(Material renderMat, Texture src, RenderTexture result)
{ {
renderMat.SetVector(_pid_iResolution, new Vector4(result.width, result.height, 0, 0));
renderMat.DisableKeyword(_kw_qualityLow); renderMat.DisableKeyword(_kw_qualityLow);
renderMat.DisableKeyword(_kw_qualityMid); renderMat.DisableKeyword(_kw_qualityMid);
renderMat.DisableKeyword(_kw_qualityHigh); renderMat.DisableKeyword(_kw_qualityHigh);

View File

@ -1,4 +1,4 @@
using Assets.Script.AppMain.Filter; using Assets.Script.AppMain.Filter;
using AxibugEmuOnline.Client; using AxibugEmuOnline.Client;
using UnityEngine; using UnityEngine;
using UnityEngine.Rendering; using UnityEngine.Rendering;
@ -47,7 +47,6 @@ public class RetroArchMattiasCRTGlow : FilterEffect
m_multipPassCmd.Clear(); m_multipPassCmd.Clear();
m_multipPassCmd.GetTemporaryRT(m_wrapRT, result.width, result.height); m_multipPassCmd.GetTemporaryRT(m_wrapRT, result.width, result.height);
renderMat.SetVector("_iResolution", new Vector2(result.width, result.height));
renderMat.SetFloat(m_gamma_ID, InputGamma.GetValue()); renderMat.SetFloat(m_gamma_ID, InputGamma.GetValue());
renderMat.SetFloat(m_horiz_gauss_width_ID, GaussianWidth.GetValue()); renderMat.SetFloat(m_horiz_gauss_width_ID, GaussianWidth.GetValue());

View File

@ -16,6 +16,6 @@ public class UIFilterPreviewer : MonoBehaviour
private void Update() private void Update()
{ {
m_rawImg.texture = App.filter.ExecuteFilterRender(m_src); App.filter.ExecuteFilterRender(m_src, m_rawImg);
} }
} }

View File

@ -79,7 +79,7 @@ namespace AxibugEmuOnline.Client
public void ApplyFilterEffect() public void ApplyFilterEffect()
{ {
Image.texture = App.filter.ExecuteFilterRender(rt_gpu); App.filter.ExecuteFilterRender(rt_gpu, Image);
} }
private unsafe void PrepareUI(uint* screenData) private unsafe void PrepareUI(uint* screenData)