GrainComponent.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. namespace UnityEngine.PostProcessing
  3. {
  4. public sealed class GrainComponent : PostProcessingComponentRenderTexture<GrainModel>
  5. {
  6. public override bool active
  7. {
  8. get
  9. {
  10. return base.model.enabled && base.model.settings.intensity > 0f && SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf) && !this.context.interrupted;
  11. }
  12. }
  13. public override void OnDisable()
  14. {
  15. GraphicsUtils.Destroy(this.m_GrainLookupRT);
  16. this.m_GrainLookupRT = null;
  17. }
  18. public override void Prepare(Material uberMaterial)
  19. {
  20. GrainModel.Settings settings = base.model.settings;
  21. uberMaterial.EnableKeyword("GRAIN");
  22. float realtimeSinceStartup = Time.realtimeSinceStartup;
  23. float value = Random.value;
  24. float value2 = Random.value;
  25. if (this.m_GrainLookupRT == null || !this.m_GrainLookupRT.IsCreated())
  26. {
  27. GraphicsUtils.Destroy(this.m_GrainLookupRT);
  28. this.m_GrainLookupRT = new RenderTexture(192, 192, 0, RenderTextureFormat.ARGBHalf)
  29. {
  30. filterMode = FilterMode.Bilinear,
  31. wrapMode = TextureWrapMode.Repeat,
  32. anisoLevel = 0,
  33. name = "Grain Lookup Texture"
  34. };
  35. this.m_GrainLookupRT.Create();
  36. }
  37. Material material = this.context.materialFactory.Get("Hidden/Post FX/Grain Generator");
  38. material.SetFloat(GrainComponent.Uniforms._Phase, realtimeSinceStartup / 20f);
  39. Graphics.Blit(null, this.m_GrainLookupRT, material, (!settings.colored) ? 0 : 1);
  40. uberMaterial.SetTexture(GrainComponent.Uniforms._GrainTex, this.m_GrainLookupRT);
  41. uberMaterial.SetVector(GrainComponent.Uniforms._Grain_Params1, new Vector2(settings.luminanceContribution, settings.intensity * 20f));
  42. uberMaterial.SetVector(GrainComponent.Uniforms._Grain_Params2, new Vector4((float)this.context.width / (float)this.m_GrainLookupRT.width / settings.size, (float)this.context.height / (float)this.m_GrainLookupRT.height / settings.size, value, value2));
  43. }
  44. private RenderTexture m_GrainLookupRT;
  45. private static class Uniforms
  46. {
  47. internal static readonly int _Grain_Params1 = Shader.PropertyToID("_Grain_Params1");
  48. internal static readonly int _Grain_Params2 = Shader.PropertyToID("_Grain_Params2");
  49. internal static readonly int _GrainTex = Shader.PropertyToID("_GrainTex");
  50. internal static readonly int _Phase = Shader.PropertyToID("_Phase");
  51. }
  52. }
  53. }