DitheringComponent.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. namespace UnityEngine.PostProcessing
  3. {
  4. public sealed class DitheringComponent : PostProcessingComponentRenderTexture<DitheringModel>
  5. {
  6. public override bool active
  7. {
  8. get
  9. {
  10. return base.model.enabled && !this.context.interrupted;
  11. }
  12. }
  13. public override void OnDisable()
  14. {
  15. this.noiseTextures = null;
  16. }
  17. private void LoadNoiseTextures()
  18. {
  19. this.noiseTextures = new Texture2D[64];
  20. for (int i = 0; i < 64; i++)
  21. {
  22. this.noiseTextures[i] = Resources.Load<Texture2D>("Bluenoise64/LDR_LLL1_" + i);
  23. }
  24. }
  25. public override void Prepare(Material uberMaterial)
  26. {
  27. if (++this.textureIndex >= 64)
  28. {
  29. this.textureIndex = 0;
  30. }
  31. float value = Random.value;
  32. float value2 = Random.value;
  33. if (this.noiseTextures == null)
  34. {
  35. this.LoadNoiseTextures();
  36. }
  37. Texture2D texture2D = this.noiseTextures[this.textureIndex];
  38. uberMaterial.EnableKeyword("DITHERING");
  39. uberMaterial.SetTexture(DitheringComponent.Uniforms._DitheringTex, texture2D);
  40. uberMaterial.SetVector(DitheringComponent.Uniforms._DitheringCoords, new Vector4((float)this.context.width / (float)texture2D.width, (float)this.context.height / (float)texture2D.height, value, value2));
  41. }
  42. private Texture2D[] noiseTextures;
  43. private int textureIndex;
  44. private const int k_TextureCount = 64;
  45. private static class Uniforms
  46. {
  47. internal static readonly int _DitheringTex = Shader.PropertyToID("_DitheringTex");
  48. internal static readonly int _DitheringCoords = Shader.PropertyToID("_DitheringCoords");
  49. }
  50. }
  51. }