GlitchEvent.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class GlitchEvent : CameraEffectEvent
  6. {
  7. public GlitchEvent(XftEventComponent owner) : base(CameraEffectEvent.EType.Glitch, owner)
  8. {
  9. this.m_random = new WaveRandom();
  10. this.Mask = owner.GlitchMask;
  11. this.GlitchShader = owner.GlitchShader;
  12. }
  13. public Material GlitchMaterial
  14. {
  15. get
  16. {
  17. if (this.m_material == null)
  18. {
  19. this.m_material = new Material(this.GlitchShader);
  20. this.m_material.hideFlags = HideFlags.HideAndDontSave;
  21. }
  22. return this.m_material;
  23. }
  24. }
  25. public override bool CheckSupport()
  26. {
  27. bool result = true;
  28. if (!SystemInfo.supportsImageEffects)
  29. {
  30. result = false;
  31. }
  32. if (!this.GlitchMaterial.shader.isSupported)
  33. {
  34. result = false;
  35. }
  36. return result;
  37. }
  38. public override void Initialize()
  39. {
  40. base.Initialize();
  41. this.m_random.Reset();
  42. }
  43. public override void Reset()
  44. {
  45. base.Reset();
  46. this.m_random.Reset();
  47. }
  48. public override void Update(float deltaTime)
  49. {
  50. this.m_elapsedTime += deltaTime;
  51. this.m_offset = this.m_random.GetRandom(this.m_owner.MinAmp, this.m_owner.MaxAmp, this.m_owner.MinRand, this.m_owner.MaxRand, this.m_owner.WaveLen);
  52. }
  53. public override void OnRenderImage(RenderTexture source, RenderTexture destination)
  54. {
  55. if (this.Mask == null)
  56. {
  57. return;
  58. }
  59. this.GlitchMaterial.SetVector("_displace", this.m_offset);
  60. this.GlitchMaterial.SetTexture("_Mask", this.Mask);
  61. Graphics.Blit(source, destination, this.GlitchMaterial);
  62. }
  63. protected Vector3 m_offset;
  64. protected WaveRandom m_random;
  65. protected Material m_material;
  66. public Shader GlitchShader;
  67. public Texture2D Mask;
  68. }
  69. }