Files
2026-02-21 16:45:37 +08:00

146 lines
4.9 KiB
C#

using UnityEngine;
[AddComponentMenu("Image Effects/Lens Dirtiness")]
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class LensDirtiness : MonoBehaviour
{
private enum Pass
{
Threshold = 0,
Kawase = 1,
Compose = 2,
Gaussian = 3
}
private Shader Shader_Dirtiness;
private Material Material_Dirtiness;
private int ScreenX = 1280;
private int ScreenY = 720;
public bool ShowScreenControls;
public bool SceneTintsBloom = true;
public Texture2D DirtinessTexture;
public int Iterations = 4;
public int Downsampling = 4;
public float gain = 1f;
public float threshold = 1f;
public float BloomSize = 5f;
public float Dirtiness = 1f;
public float BloomIntensity = 1f;
public Color BloomColor = Color.white;
private void OnEnable()
{
Shader_Dirtiness = Shader.Find("Hidden/LensDirtiness");
if (Shader_Dirtiness == null)
{
Debug.Log("#ERROR# Hidden/LensDirtiness Shader not found");
}
Material_Dirtiness = new Material(Shader_Dirtiness);
Material_Dirtiness.hideFlags = HideFlags.HideAndDontSave;
SetKeyword();
}
private void SetKeyword()
{
if ((bool)Material_Dirtiness)
{
if (SceneTintsBloom)
{
Material_Dirtiness.EnableKeyword("_SCENE_TINTS_BLOOM");
}
else
{
Material_Dirtiness.DisableKeyword("_SCENE_TINTS_BLOOM");
}
}
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
ScreenX = source.width;
ScreenY = source.height;
int num = ScreenX;
int num2 = ScreenY;
Material_Dirtiness.SetFloat("_Gain", gain);
Material_Dirtiness.SetFloat("_Threshold", threshold);
Material_Dirtiness.SetFloat("_BloomIntensity", BloomIntensity);
RenderTexture renderTexture = RenderTexture.GetTemporary(ScreenX, ScreenY, 0, source.format);
Graphics.Blit(source, renderTexture, Material_Dirtiness, 0);
RenderTexture.ReleaseTemporary(renderTexture);
Material_Dirtiness.SetVector("_Offset", new Vector4(1f / (float)ScreenX, 1f / (float)ScreenY, 0f, 0f));
for (int i = 1; i <= Downsampling; i++)
{
num /= 2;
num2 /= 2;
RenderTexture renderTexture2 = RenderTexture.GetTemporary(num, num2, 0, source.format);
Graphics.Blit(renderTexture, renderTexture2, Material_Dirtiness, 1);
renderTexture = renderTexture2;
for (int j = 1; j <= Iterations; j++)
{
float x = (BloomSize + (float)(j / Iterations)) / (float)ScreenX;
float y = (BloomSize + (float)(j / Iterations)) / (float)ScreenY;
RenderTexture temporary = RenderTexture.GetTemporary(num, num2, 0, source.format);
Material_Dirtiness.SetVector("_Offset", new Vector4(0f, y, 0f, 0f));
Graphics.Blit(renderTexture2, temporary, Material_Dirtiness, 3);
RenderTexture.ReleaseTemporary(renderTexture2);
renderTexture2 = temporary;
temporary = RenderTexture.GetTemporary(num, num2, 0, source.format);
Material_Dirtiness.SetVector("_Offset", new Vector4(x, 0f, 0f, 0f));
Graphics.Blit(renderTexture2, temporary, Material_Dirtiness, 3);
RenderTexture.ReleaseTemporary(renderTexture2);
renderTexture2 = temporary;
}
RenderTexture.ReleaseTemporary(renderTexture2);
Material_Dirtiness.SetTexture("_Bloom", renderTexture2);
}
Material_Dirtiness.SetFloat("_Dirtiness", Dirtiness);
Material_Dirtiness.SetColor("_BloomColor", BloomColor);
Material_Dirtiness.SetTexture("_DirtinessTexture", DirtinessTexture);
Graphics.Blit(source, destination, Material_Dirtiness, 2);
}
private void OnGUI()
{
if (ShowScreenControls)
{
float x = 150f;
GUI.Box(new Rect(15f, 15f, 250f, 200f), string.Empty);
GUI.Label(new Rect(25f, 25f, 100f, 20f), "Gain= " + gain.ToString("0.0"));
gain = GUI.HorizontalSlider(new Rect(x, 30f, 100f, 20f), gain, 0f, 10f);
GUI.Label(new Rect(25f, 45f, 100f, 20f), "Threshold= " + threshold.ToString("0.0"));
threshold = GUI.HorizontalSlider(new Rect(x, 50f, 100f, 20f), threshold, 0f, 10f);
GUI.Label(new Rect(25f, 65f, 100f, 20f), "BloomSize= " + BloomSize.ToString("0.0"));
BloomSize = GUI.HorizontalSlider(new Rect(x, 70f, 100f, 20f), BloomSize, 0f, 10f);
GUI.Label(new Rect(25f, 85f, 100f, 20f), "Dirtiness= " + Dirtiness.ToString("0.0"));
Dirtiness = GUI.HorizontalSlider(new Rect(x, 90f, 100f, 20f), Dirtiness, 0f, 10f);
GUI.Label(new Rect(25f, 125f, 100f, 20f), "R= " + (BloomColor.r * 255f).ToString("0."));
GUI.color = new Color(BloomColor.r, 0f, 0f);
BloomColor.r = GUI.HorizontalSlider(new Rect(x, 130f, 100f, 20f), BloomColor.r, 0f, 1f);
GUI.color = Color.white;
GUI.Label(new Rect(25f, 145f, 100f, 20f), "G= " + (BloomColor.g * 255f).ToString("0."));
GUI.color = new Color(0f, BloomColor.g, 0f);
BloomColor.g = GUI.HorizontalSlider(new Rect(x, 150f, 100f, 20f), BloomColor.g, 0f, 1f);
GUI.color = Color.white;
GUI.Label(new Rect(25f, 165f, 100f, 20f), "R= " + (BloomColor.b * 255f).ToString("0."));
GUI.color = new Color(0f, 0f, BloomColor.b);
BloomColor.b = GUI.HorizontalSlider(new Rect(x, 170f, 100f, 20f), BloomColor.b, 0f, 1f);
GUI.color = Color.white;
}
}
}