DragPointLight.cs 8.4 KB

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