DragPointLight.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using UnityEngine;
  2. namespace MeidoPhotoStudio.Plugin
  3. {
  4. public class DragPointLight : DragPointGeneral
  5. {
  6. public static EnvironmentManager EnvironmentManager { private get; set; }
  7. private Light light;
  8. public enum MPSLightType
  9. {
  10. Normal, Spot, Point, Disabled
  11. }
  12. public enum LightProp
  13. {
  14. LightRotX, LightRotY, Intensity, ShadowStrength, SpotAngle, Range, Red, Green, Blue
  15. }
  16. public bool IsActiveLight { get; set; }
  17. public string Name { get; private set; } = string.Empty;
  18. public bool IsMain { get; set; }
  19. public MPSLightType SelectedLightType { get; private set; }
  20. public LightProperty CurrentLightProperty => LightProperties[(int)SelectedLightType];
  21. private readonly LightProperty[] LightProperties = new LightProperty[]
  22. {
  23. new LightProperty(),
  24. new LightProperty(),
  25. new LightProperty()
  26. };
  27. private bool isDisabled;
  28. public bool IsDisabled
  29. {
  30. get => isDisabled;
  31. set
  32. {
  33. isDisabled = value;
  34. light.gameObject.SetActive(!isDisabled);
  35. }
  36. }
  37. private bool isColourMode;
  38. public bool IsColourMode
  39. {
  40. get => IsMain && isColourMode && SelectedLightType == MPSLightType.Normal;
  41. set
  42. {
  43. if (!IsMain) return;
  44. light.color = value ? Color.white : LightColour;
  45. camera.backgroundColor = value ? LightColour : Color.black;
  46. isColourMode = value;
  47. LightColour = isColourMode ? camera.backgroundColor : light.color;
  48. EnvironmentManager.BGVisible = !IsColourMode;
  49. }
  50. }
  51. public Quaternion Rotation
  52. {
  53. get => CurrentLightProperty.Rotation;
  54. set => light.transform.rotation = CurrentLightProperty.Rotation = value;
  55. }
  56. public float Intensity
  57. {
  58. get => CurrentLightProperty.Intensity;
  59. set => light.intensity = CurrentLightProperty.Intensity = value;
  60. }
  61. public float Range
  62. {
  63. get => CurrentLightProperty.Range;
  64. set => light.range = CurrentLightProperty.Range = value;
  65. }
  66. public float SpotAngle
  67. {
  68. get => CurrentLightProperty.SpotAngle;
  69. set
  70. {
  71. light.spotAngle = CurrentLightProperty.SpotAngle = value;
  72. light.transform.localScale = Vector3.one * value;
  73. }
  74. }
  75. public float ShadowStrength
  76. {
  77. get => CurrentLightProperty.ShadowStrength;
  78. set => light.shadowStrength = CurrentLightProperty.ShadowStrength = value;
  79. }
  80. public float LightColorRed
  81. {
  82. get => IsColourMode ? camera.backgroundColor.r : CurrentLightProperty.LightColour.r;
  83. set
  84. {
  85. Color color = IsColourMode ? camera.backgroundColor : light.color;
  86. LightColour = new Color(value, color.g, color.b);
  87. }
  88. }
  89. public float LightColorGreen
  90. {
  91. get => IsColourMode ? camera.backgroundColor.g : CurrentLightProperty.LightColour.r;
  92. set
  93. {
  94. Color color = IsColourMode ? camera.backgroundColor : light.color;
  95. LightColour = new Color(color.r, value, color.b);
  96. }
  97. }
  98. public float LightColorBlue
  99. {
  100. get => IsColourMode ? camera.backgroundColor.b : CurrentLightProperty.LightColour.r;
  101. set
  102. {
  103. Color color = IsColourMode ? camera.backgroundColor : light.color;
  104. LightColour = new Color(color.r, color.g, value);
  105. }
  106. }
  107. public Color LightColour
  108. {
  109. get => IsColourMode ? camera.backgroundColor : CurrentLightProperty.LightColour;
  110. set
  111. {
  112. Color colour = CurrentLightProperty.LightColour = value;
  113. if (IsColourMode) camera.backgroundColor = colour;
  114. else light.color = colour;
  115. }
  116. }
  117. public static void SetLightProperties(Light light, LightProperty prop)
  118. {
  119. light.transform.rotation = prop.Rotation;
  120. light.intensity = prop.Intensity;
  121. light.range = prop.Range;
  122. light.spotAngle = prop.SpotAngle;
  123. light.shadowStrength = prop.ShadowStrength;
  124. light.color = prop.LightColour;
  125. if (light.type == LightType.Spot) light.transform.localScale = Vector3.one * prop.SpotAngle;
  126. else if (light.type == LightType.Point) light.transform.localScale = Vector3.one * prop.Range;
  127. }
  128. public override void Set(Transform myObject)
  129. {
  130. base.Set(myObject);
  131. light = myObject.gameObject.GetOrAddComponent<Light>();
  132. light.transform.position = LightProperty.DefaultPosition;
  133. light.transform.rotation = LightProperty.DefaultRotation;
  134. SetLightType(MPSLightType.Normal);
  135. ScaleFactor = 50f;
  136. DefaultRotation = LightProperty.DefaultRotation;
  137. DefaultPosition = LightProperty.DefaultPosition;
  138. }
  139. protected override void OnDestroy()
  140. {
  141. if (!IsMain) Destroy(light.gameObject);
  142. base.OnDestroy();
  143. }
  144. protected override void OnRotate()
  145. {
  146. CurrentLightProperty.Rotation = light.transform.rotation;
  147. base.OnRotate();
  148. }
  149. protected override void OnScale()
  150. {
  151. float value = light.transform.localScale.x;
  152. if (SelectedLightType == MPSLightType.Point) Range = value;
  153. else if (SelectedLightType == MPSLightType.Spot) SpotAngle = value;
  154. base.OnScale();
  155. }
  156. protected override void ApplyDragType()
  157. {
  158. if (Selecting || Moving) ApplyProperties(true, true, false);
  159. else if (SelectedLightType != MPSLightType.Point && Rotating) ApplyProperties(true, true, false);
  160. else if (SelectedLightType != MPSLightType.Normal && Scaling) ApplyProperties(true, true, false);
  161. else if (!IsMain && Deleting) ApplyProperties(true, true, false);
  162. else ApplyProperties(false, false, false);
  163. ApplyColours();
  164. }
  165. public void SetLightType(MPSLightType type)
  166. {
  167. LightType lightType = LightType.Directional;
  168. string name = "normal";
  169. SelectedLightType = type;
  170. if (type == MPSLightType.Spot)
  171. {
  172. lightType = LightType.Spot;
  173. name = "spot";
  174. }
  175. else if (type == MPSLightType.Point)
  176. {
  177. lightType = LightType.Point;
  178. name = "point";
  179. }
  180. light.type = lightType;
  181. Name = IsMain ? "main" : name;
  182. if (IsMain)
  183. {
  184. EnvironmentManager.BGVisible = !(IsColourMode && SelectedLightType == MPSLightType.Normal);
  185. }
  186. SetProps();
  187. ApplyDragType();
  188. }
  189. public void SetRotation(float x, float y) => Rotation = Quaternion.Euler(x, y, Rotation.eulerAngles.z);
  190. public void SetProp(LightProp prop, float value)
  191. {
  192. switch (prop)
  193. {
  194. case LightProp.Intensity:
  195. Intensity = value;
  196. break;
  197. case LightProp.ShadowStrength:
  198. ShadowStrength = value;
  199. break;
  200. case LightProp.SpotAngle:
  201. SpotAngle = value;
  202. break;
  203. case LightProp.Range:
  204. Range = value;
  205. break;
  206. case LightProp.Red:
  207. LightColorRed = value;
  208. break;
  209. case LightProp.Green:
  210. LightColorGreen = value;
  211. break;
  212. case LightProp.Blue:
  213. LightColorBlue = value;
  214. break;
  215. }
  216. }
  217. public void ResetLightProps()
  218. {
  219. LightProperties[(int)SelectedLightType] = new LightProperty();
  220. SetProps();
  221. }
  222. public void ResetLightPosition() => light.transform.position = LightProperty.DefaultPosition;
  223. private void SetProps()
  224. {
  225. SetLightProperties(light, CurrentLightProperty);
  226. if (IsColourMode)
  227. {
  228. light.color = Color.white;
  229. camera.backgroundColor = CurrentLightProperty.LightColour;
  230. }
  231. }
  232. }
  233. public class LightProperty
  234. {
  235. public static readonly Vector3 DefaultPosition = new(0f, 1.9f, 0.4f);
  236. public static readonly Quaternion DefaultRotation = Quaternion.Euler(40f, 180f, 0f);
  237. public Quaternion Rotation { get; set; } = DefaultRotation;
  238. public float Intensity { get; set; } = 0.95f;
  239. public float Range { get; set; } = GameMain.Instance.MainLight.GetComponent<Light>().range;
  240. public float SpotAngle { get; set; } = 50f;
  241. public float ShadowStrength { get; set; } = 0.10f;
  242. public Color LightColour { get; set; } = Color.white;
  243. }
  244. }