fxvFogPresentation.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. namespace FXV.FogDemo
  6. {
  7. [ExecuteInEditMode]
  8. public class fxvFogPresentation : MonoBehaviour
  9. {
  10. [SerializeField]
  11. VolumeFog fog;
  12. TextMesh textMesh;
  13. float lightsFadeTarget = 1.0f;
  14. Light[] lights;
  15. void Start()
  16. {
  17. lights = GetComponentsInChildren<Light>();
  18. }
  19. void UpdateState()
  20. {
  21. if (textMesh == null)
  22. {
  23. textMesh = GetComponentInChildren<TextMesh>();
  24. }
  25. if (textMesh != null && fog)
  26. {
  27. string formatStr = System.Text.RegularExpressions.Regex.Replace(fog.GetFogType().ToString(), "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim();
  28. textMesh.text = "Fog: " + formatStr + " " + (fog.IsAffectedByLights() ? "Lit" : "Unlit");
  29. }
  30. }
  31. private void OnEnable()
  32. {
  33. UpdateState();
  34. }
  35. private void OnValidate()
  36. {
  37. UpdateState();
  38. }
  39. public void SetLightsFade(float target, float delay)
  40. {
  41. StartCoroutine(_SetLightsFade(target, delay));
  42. }
  43. IEnumerator _SetLightsFade(float target, float delay)
  44. {
  45. yield return new WaitForSeconds(delay);
  46. SetLightsFade(target);
  47. }
  48. public void SetAffectedByLights(bool affected)
  49. {
  50. if (lights == null)
  51. {
  52. lights = GetComponentsInChildren<Light>();
  53. }
  54. for (int i = 0; i < lights.Length; i++)
  55. {
  56. lights[i].gameObject.SetActive(affected);
  57. }
  58. fog.SetAffectedByLights(affected);
  59. UpdateState();
  60. }
  61. public void SetFogColor(Color c)
  62. {
  63. fog.SetFogColor(c);
  64. }
  65. public void SetLightsFade(float target, bool animated = true)
  66. {
  67. if (lights == null)
  68. {
  69. lights = GetComponentsInChildren<Light>();
  70. }
  71. lightsFadeTarget = target;
  72. if (!animated)
  73. {
  74. for (int i = 0; i < lights.Length; i++)
  75. {
  76. lights[i].intensity = target;
  77. if (lights[i].enabled && lights[i].intensity < 0.01f)
  78. {
  79. lights[i].enabled = false;
  80. }
  81. else if (!lights[i].enabled && lights[i].intensity > 0.01f)
  82. {
  83. lights[i].enabled = true;
  84. }
  85. }
  86. }
  87. }
  88. void Update()
  89. {
  90. for (int i = 0; i < lights.Length; i++)
  91. {
  92. lights[i].intensity = Mathf.MoveTowards(lights[i].intensity, lightsFadeTarget, Time.deltaTime * 2.0f);
  93. if (lights[i].enabled && lights[i].intensity < 0.01f)
  94. {
  95. lights[i].enabled = false;
  96. }
  97. else if (!lights[i].enabled && lights[i].intensity > 0.01f)
  98. {
  99. lights[i].enabled = true;
  100. }
  101. }
  102. }
  103. }
  104. }