FogEffectManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. internal 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. public static float InitialDistance { get; private set; } = 4f;
  11. public static float InitialDensity { get; private set; } = 1f;
  12. public static float InitialHeightScale { get; private set; } = 1f;
  13. public static float InitialHeight { get; private set; } = 0f;
  14. public static Color InitialColour { get; private set; } = 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. Fog = GameMain.Instance.MainCamera.GetOrAddComponent<GlobalFog>();
  96. if (Fog.fogShader == null) Fog.fogShader = Shader.Find("Hidden/GlobalFog");
  97. Distance = InitialDistance;
  98. Density = InitialDensity;
  99. HeightScale = InitialHeightScale;
  100. Height = InitialHeight;
  101. FogColour = InitialColour;
  102. }
  103. }
  104. public void Deactivate()
  105. {
  106. Distance = InitialDistance;
  107. Density = InitialDensity;
  108. HeightScale = InitialHeightScale;
  109. Height = InitialHeight;
  110. FogColour = InitialColour;
  111. Fog.enabled = false;
  112. Active = false;
  113. }
  114. public void Reset()
  115. {
  116. Fog.startDistance = InitialDistance;
  117. Fog.globalDensity = InitialDensity;
  118. Fog.heightScale = InitialHeightScale;
  119. Fog.height = InitialHeight;
  120. Fog.globalFogColor = InitialColour;
  121. }
  122. public void SetEffectActive(bool active)
  123. {
  124. Fog.enabled = active;
  125. this.Active = active;
  126. if (this.Active)
  127. {
  128. Fog.startDistance = Distance;
  129. Fog.globalDensity = Density;
  130. Fog.heightScale = HeightScale;
  131. Fog.height = Height;
  132. Fog.globalFogColor = FogColour;
  133. }
  134. else Reset();
  135. }
  136. public void Update() { }
  137. }
  138. }