summaryrefslogtreecommitdiff
path: root/UnityEngine.PostProcessing/GrainComponent.cs
blob: c01dcda20ba7f1a544d6e413dbcb3142124643c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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));
	}
}