summaryrefslogtreecommitdiff
path: root/UnityEngine.PostProcessing/DitheringComponent.cs
blob: a9f055473b7a31d09ef6230164a662a4334539fe (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
namespace UnityEngine.PostProcessing;

public sealed class DitheringComponent : PostProcessingComponentRenderTexture<DitheringModel>
{
	private static class Uniforms
	{
		internal static readonly int _DitheringTex = Shader.PropertyToID("_DitheringTex");

		internal static readonly int _DitheringCoords = Shader.PropertyToID("_DitheringCoords");
	}

	private Texture2D[] noiseTextures;

	private int textureIndex;

	private const int k_TextureCount = 64;

	public override bool active => base.model.enabled && !context.interrupted;

	public override void OnDisable()
	{
		noiseTextures = null;
	}

	private void LoadNoiseTextures()
	{
		noiseTextures = new Texture2D[64];
		for (int i = 0; i < 64; i++)
		{
			noiseTextures[i] = Resources.Load<Texture2D>("Bluenoise64/LDR_LLL1_" + i);
		}
	}

	public override void Prepare(Material uberMaterial)
	{
		if (++textureIndex >= 64)
		{
			textureIndex = 0;
		}
		float value = Random.value;
		float value2 = Random.value;
		if (noiseTextures == null)
		{
			LoadNoiseTextures();
		}
		Texture2D texture2D = noiseTextures[textureIndex];
		uberMaterial.EnableKeyword("DITHERING");
		uberMaterial.SetTexture(Uniforms._DitheringTex, texture2D);
		uberMaterial.SetVector(Uniforms._DitheringCoords, new Vector4((float)context.width / (float)texture2D.width, (float)context.height / (float)texture2D.height, value, value2));
	}
}