diff options
author | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
commit | 6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch) | |
tree | b38119d2acf0a982cb67e381f146924b9bfc3b3f /UnityEngine.PostProcessing/GrainComponent.cs |
+init
Diffstat (limited to 'UnityEngine.PostProcessing/GrainComponent.cs')
-rw-r--r-- | UnityEngine.PostProcessing/GrainComponent.cs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/UnityEngine.PostProcessing/GrainComponent.cs b/UnityEngine.PostProcessing/GrainComponent.cs new file mode 100644 index 0000000..c01dcda --- /dev/null +++ b/UnityEngine.PostProcessing/GrainComponent.cs @@ -0,0 +1,52 @@ +namespace UnityEngine.PostProcessing; + +public sealed class GrainComponent : PostProcessingComponentRenderTexture<GrainModel> +{ + private static class Uniforms + { + internal static readonly int _Grain_Params1 = Shader.PropertyToID("_Grain_Params1"); + + internal static readonly int _Grain_Params2 = Shader.PropertyToID("_Grain_Params2"); + + internal static readonly int _GrainTex = Shader.PropertyToID("_GrainTex"); + + internal static readonly int _Phase = Shader.PropertyToID("_Phase"); + } + + private RenderTexture m_GrainLookupRT; + + public override bool active => base.model.enabled && base.model.settings.intensity > 0f && SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf) && !context.interrupted; + + public override void OnDisable() + { + GraphicsUtils.Destroy(m_GrainLookupRT); + m_GrainLookupRT = null; + } + + public override void Prepare(Material uberMaterial) + { + GrainModel.Settings settings = base.model.settings; + uberMaterial.EnableKeyword("GRAIN"); + float realtimeSinceStartup = Time.realtimeSinceStartup; + float value = Random.value; + float value2 = Random.value; + if (m_GrainLookupRT == null || !m_GrainLookupRT.IsCreated()) + { + GraphicsUtils.Destroy(m_GrainLookupRT); + m_GrainLookupRT = new RenderTexture(192, 192, 0, RenderTextureFormat.ARGBHalf) + { + filterMode = FilterMode.Bilinear, + wrapMode = TextureWrapMode.Repeat, + anisoLevel = 0, + name = "Grain Lookup Texture" + }; + m_GrainLookupRT.Create(); + } + Material material = context.materialFactory.Get("Hidden/Post FX/Grain Generator"); + material.SetFloat(Uniforms._Phase, realtimeSinceStartup / 20f); + Graphics.Blit(null, m_GrainLookupRT, material, settings.colored ? 1 : 0); + uberMaterial.SetTexture(Uniforms._GrainTex, m_GrainLookupRT); + uberMaterial.SetVector(Uniforms._Grain_Params1, new Vector2(settings.luminanceContribution, settings.intensity * 20f)); + uberMaterial.SetVector(Uniforms._Grain_Params2, new Vector4((float)context.width / (float)m_GrainLookupRT.width / settings.size, (float)context.height / (float)m_GrainLookupRT.height / settings.size, value, value2)); + } +} |