EdgeCheckAnimation.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using UnityEngine;
  3. public class EdgeCheckAnimation : MonoBehaviour
  4. {
  5. private float delta = 0.04f;
  6. public Color edgeColor = new Color(0f, 1f, 1f, 1f);
  7. private bool isRuning = false;
  8. private Color lastEdgeColor;
  9. private Material m_Material;
  10. private float offset = 0.0015f;
  11. public Shader shader;
  12. public float speed = 3f;
  13. private float startTime = 0f;
  14. [Range(0f, 8.5f)]
  15. private float valueWave = 0f;
  16. [Range(0f, 8.5f)]
  17. private float valueWave1 = 4f;
  18. public Texture2D waveTexture;
  19. private void OnDestroy()
  20. {
  21. if (this.m_Material != null)
  22. {
  23. UnityEngine.Object.DestroyImmediate(this.m_Material);
  24. }
  25. this.isRuning = false;
  26. }
  27. private void OnEnable()
  28. {
  29. this.startTime = Time.time;
  30. }
  31. private void OnRenderImage(RenderTexture source, RenderTexture destination)
  32. {
  33. if (this.isRuning)
  34. {
  35. Graphics.Blit(source, destination, this.material);
  36. }
  37. else if (material)
  38. {
  39. Graphics.Blit(source, destination, material);
  40. }
  41. else
  42. {
  43. Graphics.Blit(source, destination);
  44. }
  45. }
  46. private void Start()
  47. {
  48. if (!SystemInfo.supportsImageEffects)
  49. {
  50. //base.enabled = false;
  51. }
  52. if (this.shader == null)
  53. {
  54. this.shader = Shader.Find("Hidden/EdgeCheck");
  55. }
  56. if ((this.shader == null) || !this.shader.isSupported)
  57. {
  58. //base.enabled = false;
  59. }
  60. if (this.waveTexture == null)
  61. {
  62. //base.enabled = false;
  63. Debug.LogError("There is No waveTexture in EdgeCheck!", this);
  64. }
  65. this.material.SetFloat("_delta", this.delta);
  66. this.material.SetTexture("_WaveTex", this.waveTexture);
  67. this.material.SetColor("_Color", this.edgeColor);
  68. this.lastEdgeColor = this.edgeColor;
  69. this.isRuning = true;
  70. this.startTime = Time.time;
  71. }
  72. private void OnStartShow()
  73. {
  74. enabled = false;
  75. }
  76. private void OnChangeTrackState()
  77. {
  78. enabled = true;
  79. }
  80. private void Update()
  81. {
  82. if (this.lastEdgeColor != this.edgeColor)
  83. {
  84. this.material.SetColor("_Color", this.edgeColor);
  85. this.lastEdgeColor = this.edgeColor;
  86. }
  87. this.material.SetFloat("_Offset", this.offset);
  88. this.material.SetVector("_WaveTex_ST", new Vector4(1f / this.valueWave, 1f / this.valueWave, (1f - (1f / this.valueWave)) / 2f, (1f - (1f / this.valueWave)) / 2f));
  89. this.material.SetVector("_WaveTex1_ST", new Vector4(1f / this.valueWave1, 1f / this.valueWave1, (1f - (1f / this.valueWave1)) / 2f, (1f - (1f / this.valueWave1)) / 2f));
  90. this.valueWave = Mathf.Repeat((Time.time - this.startTime) * this.speed, 9f) + 0.5f;
  91. this.valueWave1 = Mathf.Repeat(((Time.time - this.startTime) * this.speed) + 4f, 9f) + 0.5f;
  92. }
  93. public Color EdgeColor
  94. {
  95. get
  96. {
  97. return this.edgeColor;
  98. }
  99. set
  100. {
  101. this.edgeColor = value;
  102. }
  103. }
  104. protected Material material
  105. {
  106. get
  107. {
  108. if (this.m_Material == null)
  109. {
  110. this.m_Material = new Material(this.shader);
  111. this.m_Material.hideFlags = HideFlags.HideAndDontSave;// 0x3d;
  112. }
  113. return this.m_Material;
  114. }
  115. }
  116. }