FogEffectManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. public class FogEffectManager : IEffectManager
  5. {
  6. public const string header = "EFFECT_FOG";
  7. private GlobalFog Fog { get; set; }
  8. public bool Ready { get; private set; }
  9. public bool Active { get; private set; }
  10. private readonly float initialDistance = 4f;
  11. private readonly float initialDensity = 1f;
  12. private readonly float initialHeightScale = 1f;
  13. private readonly float initialHeight = 0f;
  14. private readonly Color initialColour = Color.white;
  15. private float distance;
  16. public float Distance
  17. {
  18. get => distance;
  19. set => distance = Fog.startDistance = value;
  20. }
  21. private float density;
  22. public float Density
  23. {
  24. get => density;
  25. set => density = Fog.globalDensity = value;
  26. }
  27. private float heightScale;
  28. public float HeightScale
  29. {
  30. get => heightScale;
  31. set => heightScale = Fog.heightScale = value;
  32. }
  33. private float height;
  34. public float Height
  35. {
  36. get => height;
  37. set => height = Fog.height = value;
  38. }
  39. public float FogColourRed
  40. {
  41. get => FogColour.r;
  42. set
  43. {
  44. Color fogColour = FogColour;
  45. FogColour = new Color(value, fogColour.g, fogColour.b);
  46. }
  47. }
  48. public float FogColourGreen
  49. {
  50. get => FogColour.g;
  51. set
  52. {
  53. Color fogColour = FogColour;
  54. FogColour = new Color(fogColour.r, value, fogColour.b);
  55. }
  56. }
  57. public float FogColourBlue
  58. {
  59. get => FogColour.b;
  60. set
  61. {
  62. Color fogColour = FogColour;
  63. FogColour = new Color(fogColour.r, fogColour.g, value);
  64. }
  65. }
  66. private Color fogColour;
  67. public Color FogColour
  68. {
  69. get => fogColour;
  70. set => fogColour = Fog.globalFogColor = value;
  71. }
  72. public void Serialize(System.IO.BinaryWriter binaryWriter)
  73. {
  74. binaryWriter.Write(header);
  75. binaryWriter.Write(Distance);
  76. binaryWriter.Write(Density);
  77. binaryWriter.Write(HeightScale);
  78. binaryWriter.Write(Height);
  79. binaryWriter.WriteColour(FogColour);
  80. binaryWriter.Write(Active);
  81. }
  82. public void Deserialize(System.IO.BinaryReader binaryReader)
  83. {
  84. Distance = binaryReader.ReadSingle();
  85. Density = binaryReader.ReadSingle();
  86. HeightScale = binaryReader.ReadSingle();
  87. Height = binaryReader.ReadSingle();
  88. FogColour = binaryReader.ReadColour();
  89. SetEffectActive(binaryReader.ReadBoolean());
  90. }
  91. public void Activate()
  92. {
  93. if (Fog == null)
  94. {
  95. Ready = true;
  96. Fog = GameMain.Instance.MainCamera.GetOrAddComponent<GlobalFog>();
  97. if (Fog.fogShader == null) Fog.fogShader = Shader.Find("Hidden/GlobalFog");
  98. Distance = initialDistance;
  99. Density = initialDensity;
  100. HeightScale = initialHeightScale;
  101. Height = initialHeight;
  102. FogColour = initialColour;
  103. }
  104. SetEffectActive(false);
  105. }
  106. public void Deactivate()
  107. {
  108. Distance = initialDistance;
  109. Density = initialDensity;
  110. HeightScale = initialHeightScale;
  111. Height = initialHeight;
  112. FogColour = initialColour;
  113. Fog.enabled = false;
  114. Active = false;
  115. }
  116. public void Reset()
  117. {
  118. Fog.startDistance = initialDistance;
  119. Fog.globalDensity = initialDensity;
  120. Fog.heightScale = initialHeightScale;
  121. Fog.height = initialHeight;
  122. Fog.globalFogColor = initialColour;
  123. }
  124. public void SetEffectActive(bool active)
  125. {
  126. Fog.enabled = active;
  127. if (Active = active)
  128. {
  129. Fog.startDistance = Distance;
  130. Fog.globalDensity = Density;
  131. Fog.heightScale = HeightScale;
  132. Fog.height = Height;
  133. Fog.globalFogColor = FogColour;
  134. }
  135. else Reset();
  136. }
  137. public void Update() { }
  138. }
  139. }