MattiasCRT增加几个质量变体,优化MattiasCRT render代码效率

This commit is contained in:
ALIENJACK\alien 2024-12-26 15:20:11 +08:00
parent 9fb1812266
commit 710359f538
4 changed files with 55 additions and 7 deletions

View File

@ -22,7 +22,11 @@ namespace AxibugEmuOnline.Client
{ {
GetEditableFilterParamters(); GetEditableFilterParamters();
m_material = new Material(Shader.Find(ShaderName)); m_material = new Material(Shader.Find(ShaderName));
OnInit(m_material);
} }
protected virtual void OnInit(Material renderMat) { }
void GetEditableFilterParamters() void GetEditableFilterParamters()
{ {
var parameters = (from t in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public) var parameters = (from t in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)

View File

@ -9,7 +9,7 @@ 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(Screen.width, Screen.height, 0, 0)); renderMat.SetVector("_iResolution", new Vector4(result.width, result.height, 0, 0));
Graphics.Blit(src, result, renderMat); Graphics.Blit(src, result, renderMat);
} }
} }

View File

@ -1,5 +1,7 @@
using AxibugEmuOnline.Client; using Assets.Script.AppMain.Filter;
using AxibugEmuOnline.Client;
using UnityEngine; using UnityEngine;
using UnityEngine.Rendering;
public sealed class MattiasCRT : FilterEffect public sealed class MattiasCRT : FilterEffect
{ {
@ -7,9 +9,40 @@ public sealed class MattiasCRT : FilterEffect
protected override string ShaderName => "Filter/MattiasCRT"; protected override string ShaderName => "Filter/MattiasCRT";
public FilterParameter<EnumQuality> Quality = new FilterParameter<EnumQuality>(EnumQuality.High);
private LocalKeyword _kw_qualityLow;
private LocalKeyword _kw_qualityMid;
private LocalKeyword _kw_qualityHigh;
private int _pid_iResolution;
protected override void OnInit(Material renderMat)
{
_pid_iResolution = Shader.PropertyToID("_iResolution");
_kw_qualityLow = new LocalKeyword(renderMat.shader, "_QUALITY_LOW");
_kw_qualityMid = new LocalKeyword(renderMat.shader, "_QUALITY_MID");
_kw_qualityHigh = new LocalKeyword(renderMat.shader, "_QUALITY_HIGH");
}
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(_pid_iResolution, new Vector4(result.width, result.height, 0, 0));
renderMat.DisableKeyword(_kw_qualityLow);
renderMat.DisableKeyword(_kw_qualityMid);
renderMat.DisableKeyword(_kw_qualityHigh);
switch (Quality.GetValue())
{
case EnumQuality.Low: renderMat.EnableKeyword(_kw_qualityLow); break;
case EnumQuality.Mid: renderMat.EnableKeyword(_kw_qualityMid); break;
case EnumQuality.High: renderMat.EnableKeyword(_kw_qualityHigh); break;
}
Graphics.Blit(src, result, renderMat); Graphics.Blit(src, result, renderMat);
} }
public enum EnumQuality
{
Low,
Mid,
High
}
} }

View File

@ -1,4 +1,4 @@

Shader "Filter/MattiasCRT" Shader "Filter/MattiasCRT"
{ {
Properties Properties
@ -11,6 +11,7 @@ Shader "Filter/MattiasCRT"
{ {
CGPROGRAM CGPROGRAM
#pragma shader_feature_local _QUALITY_LOW _QUALITY_MID _QUALITY_HIGH
#pragma vertex vert_img #pragma vertex vert_img
#pragma fragment frag #pragma fragment frag
#include "UnityCG.cginc" #include "UnityCG.cginc"
@ -37,16 +38,26 @@ Shader "Filter/MattiasCRT"
float2 q = fragCoord.xy / _iResolution.xy; float2 q = fragCoord.xy / _iResolution.xy;
float2 uv = q; float2 uv = q;
uv = curve( uv ); uv = curve( uv );
float3 oricol = tex2D(_MainTex,uv).xyz;
float3 col;
float x = sin(0.3*_Time+uv.y*21.0)*sin(0.7*_Time+uv.y*29.0)*sin(0.3+0.33*_Time+uv.y*31.0)*0.0017; float x = sin(0.3*_Time+uv.y*21.0)*sin(0.7*_Time+uv.y*29.0)*sin(0.3+0.33*_Time+uv.y*31.0)*0.0017;
float3 col;
#if _QUALITY_LOW
col = tex2D(_MainTex,uv);
#elif _QUALITY_MID
col = tex2D(_MainTex,float2(x+uv.x+0.001,uv.y+0.001))+0.05;
float3 tmpColor2 = tex2D(_MainTex,0.75*float2(x+0.025, -0.027)+float2(uv.x+0.001,uv.y+0.001));
col.r+=tmpColor2.x*0.08;
col.g+=tmpColor2.y*0.05;
col.b+=tmpColor2.z*0.08;
#else
col.r = tex2D(_MainTex,float2(x+uv.x+0.001,uv.y+0.001)).x+0.05; col.r = tex2D(_MainTex,float2(x+uv.x+0.001,uv.y+0.001)).x+0.05;
col.g = tex2D(_MainTex,float2(x+uv.x+0.000,uv.y-0.002)).y+0.05; col.g = tex2D(_MainTex,float2(x+uv.x+0.000,uv.y-0.002)).y+0.05;
col.b = tex2D(_MainTex,float2(x+uv.x-0.002,uv.y+0.000)).z+0.05; col.b = tex2D(_MainTex,float2(x+uv.x-0.002,uv.y+0.000)).z+0.05;
col.r += 0.08*tex2D(_MainTex,0.75*float2(x+0.025, -0.027)+float2(uv.x+0.001,uv.y+0.001)).x; col.r += 0.08*tex2D(_MainTex,0.75*float2(x+0.025, -0.027)+float2(uv.x+0.001,uv.y+0.001)).x;
col.g += 0.05*tex2D(_MainTex,0.75*float2(x+-0.022, -0.02)+float2(uv.x+0.000,uv.y-0.002)).y;
col.b += 0.08*tex2D(_MainTex,0.75*float2(x+-0.02, -0.018)+float2(uv.x-0.002,uv.y+0.000)).z; col.b += 0.08*tex2D(_MainTex,0.75*float2(x+-0.02, -0.018)+float2(uv.x-0.002,uv.y+0.000)).z;
col.g += 0.05*tex2D(_MainTex,0.75*float2(x+-0.022, -0.02)+float2(uv.x+0.000,uv.y-0.002)).y;
#endif
col = clamp(col*0.6+0.4*col*col*1.0,0.0,1.0); col = clamp(col*0.6+0.4*col*col*1.0,0.0,1.0);