MPSLight.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. internal class MPSLight
  6. {
  7. private static Camera camera = GameMain.Instance.MainCamera.GetComponent<Camera>();
  8. private Light light;
  9. public DragDogu DragLight { get; private set; }
  10. public event EventHandler Rotate;
  11. public event EventHandler Scale;
  12. public event EventHandler Delete;
  13. public event EventHandler Select;
  14. public bool isActiveLight = false;
  15. public bool IsMain { get; private set; } = false;
  16. private bool isDisabled = false;
  17. public string Name { get; private set; }
  18. public bool IsDisabled
  19. {
  20. get => isDisabled;
  21. set
  22. {
  23. this.isDisabled = value;
  24. this.light.gameObject.SetActive(!this.isDisabled);
  25. }
  26. }
  27. public LightProperty[] LightProperties = new LightProperty[]
  28. {
  29. new LightProperty(),
  30. new LightProperty(),
  31. new LightProperty()
  32. };
  33. private bool isColourMode = false;
  34. public bool IsColourMode
  35. {
  36. get => isColourMode && SelectedLightType == MPSLightType.Normal;
  37. set
  38. {
  39. this.light.color = value ? Color.white : LightColour;
  40. camera.backgroundColor = value ? LightColour : Color.black;
  41. this.isColourMode = value;
  42. LightColour = this.isColourMode ? camera.backgroundColor : light.color;
  43. }
  44. }
  45. public MPSLightType SelectedLightType { get; private set; } = MPSLightType.Normal;
  46. public LightProperty CurrentLightProperty => LightProperties[(int)SelectedLightType];
  47. public Quaternion Rotation
  48. {
  49. get => CurrentLightProperty.Rotation;
  50. set => this.light.transform.rotation = CurrentLightProperty.Rotation = value;
  51. }
  52. public float Intensity
  53. {
  54. get => CurrentLightProperty.Intensity;
  55. set => this.light.intensity = CurrentLightProperty.Intensity = value;
  56. }
  57. public float Range
  58. {
  59. get => CurrentLightProperty.Range;
  60. set => this.light.range = CurrentLightProperty.Range = value;
  61. }
  62. public float SpotAngle
  63. {
  64. get => CurrentLightProperty.SpotAngle;
  65. set
  66. {
  67. this.light.spotAngle = CurrentLightProperty.SpotAngle = value;
  68. this.light.transform.localScale = Vector3.one * value;
  69. }
  70. }
  71. public float ShadowStrength
  72. {
  73. get => CurrentLightProperty.ShadowStrength;
  74. set => this.light.shadowStrength = CurrentLightProperty.ShadowStrength = value;
  75. }
  76. public float LightColorRed
  77. {
  78. get => IsColourMode ? camera.backgroundColor.r : CurrentLightProperty.LightColour.r;
  79. set
  80. {
  81. Color color = IsColourMode ? camera.backgroundColor : this.light.color;
  82. this.LightColour = new Color(value, color.g, color.b);
  83. }
  84. }
  85. public float LightColorGreen
  86. {
  87. get => IsColourMode ? camera.backgroundColor.g : CurrentLightProperty.LightColour.r;
  88. set
  89. {
  90. Color color = IsColourMode ? camera.backgroundColor : this.light.color;
  91. this.LightColour = new Color(color.r, value, color.b);
  92. }
  93. }
  94. public float LightColorBlue
  95. {
  96. get => IsColourMode ? camera.backgroundColor.b : CurrentLightProperty.LightColour.r;
  97. set
  98. {
  99. Color color = IsColourMode ? camera.backgroundColor : this.light.color;
  100. this.LightColour = new Color(color.r, color.g, value);
  101. }
  102. }
  103. public Color LightColour
  104. {
  105. get => IsColourMode ? camera.backgroundColor : CurrentLightProperty.LightColour;
  106. set
  107. {
  108. Color colour = CurrentLightProperty.LightColour = value;
  109. if (IsColourMode) camera.backgroundColor = colour;
  110. else this.light.color = colour;
  111. }
  112. }
  113. public enum LightProp
  114. {
  115. LightRotX, LightRotY, Intensity, ShadowStrength, SpotAngle, Range, Red, Green, Blue
  116. }
  117. public enum MPSLightType
  118. {
  119. Normal, Spot, Point, Disabled
  120. }
  121. public MPSLight(GameObject lightGo = null, bool isMain = false)
  122. {
  123. this.IsMain = isMain;
  124. GameObject gameobject = lightGo ?? new GameObject();
  125. this.light = gameobject.GetOrAddComponent<Light>();
  126. float spotAngle = CurrentLightProperty.SpotAngle;
  127. this.light.transform.position = LightProperty.DefaultPosition;
  128. this.light.transform.rotation = LightProperty.DefaultRotation;
  129. DragLight = BaseDrag.MakeDragPoint<DragDogu>(
  130. PrimitiveType.Cube, Vector3.one * 0.12f, BaseDrag.LightBlue
  131. ).Initialize(this.light.gameObject, this.IsMain, CustomGizmo.GizmoMode.World,
  132. () => this.light.transform.position,
  133. () => this.light.transform.eulerAngles
  134. );
  135. DragLight.scaleFactor = 50f;
  136. DragLight.Select += (s, a) => this.Select?.Invoke(this, EventArgs.Empty);
  137. if (!isMain)
  138. {
  139. DragLight.Delete += (s, a) => Delete?.Invoke(this, EventArgs.Empty);
  140. }
  141. DragLight.SetDragProp(false, false, false);
  142. SetLightType(LightType.Directional);
  143. }
  144. public static void SetLightProperties(Light light, LightProperty prop)
  145. {
  146. light.transform.rotation = prop.Rotation;
  147. light.intensity = prop.Intensity;
  148. light.range = prop.Range;
  149. light.spotAngle = prop.SpotAngle;
  150. light.shadowStrength = prop.ShadowStrength;
  151. light.color = prop.LightColour;
  152. if (light.type == LightType.Spot)
  153. {
  154. light.transform.localScale = Vector3.one * prop.SpotAngle;
  155. }
  156. else if (light.type == LightType.Point)
  157. {
  158. light.transform.localScale = Vector3.one * prop.Range;
  159. }
  160. }
  161. public void Destroy()
  162. {
  163. DragLight.Rotate -= OnRotate;
  164. DragLight.Scale -= OnScale;
  165. GameObject.Destroy(DragLight.gameObject);
  166. if (!IsMain) GameObject.Destroy(this.light.gameObject);
  167. }
  168. public void SetLightType(LightType type)
  169. {
  170. DragLight.Rotate -= OnRotate;
  171. DragLight.Rotate -= OnScale;
  172. string name = "normal";
  173. if (type == LightType.Directional)
  174. {
  175. SelectedLightType = MPSLightType.Normal;
  176. }
  177. else if (type == LightType.Spot)
  178. {
  179. name = "spot";
  180. SelectedLightType = MPSLightType.Spot;
  181. DragLight.Scale += OnScale;
  182. DragLight.Rotate += OnRotate;
  183. }
  184. else
  185. {
  186. name = "point";
  187. SelectedLightType = MPSLightType.Point;
  188. DragLight.Scale += OnScale;
  189. }
  190. this.light.type = type;
  191. SetProps();
  192. this.Name = IsMain ? "main" : name;
  193. }
  194. public void SetRotation(float x, float y)
  195. {
  196. this.Rotation = Quaternion.Euler(x, y, Rotation.eulerAngles.z);
  197. }
  198. public void SetProp(LightProp prop, float value)
  199. {
  200. switch (prop)
  201. {
  202. case LightProp.Intensity:
  203. Intensity = value;
  204. break;
  205. case LightProp.ShadowStrength:
  206. ShadowStrength = value;
  207. break;
  208. case LightProp.SpotAngle:
  209. SpotAngle = value;
  210. break;
  211. case LightProp.Range:
  212. Range = value;
  213. break;
  214. case LightProp.Red:
  215. LightColorRed = value;
  216. break;
  217. case LightProp.Green:
  218. LightColorGreen = value;
  219. break;
  220. case LightProp.Blue:
  221. LightColorBlue = value;
  222. break;
  223. }
  224. }
  225. public void ResetLightProps()
  226. {
  227. LightProperties[(int)SelectedLightType] = new LightProperty();
  228. SetProps();
  229. }
  230. public void ResetLightPosition()
  231. {
  232. this.light.transform.position = LightProperty.DefaultPosition;
  233. }
  234. private void SetProps()
  235. {
  236. SetLightProperties(this.light, CurrentLightProperty);
  237. if (IsColourMode)
  238. {
  239. this.light.color = Color.white;
  240. camera.backgroundColor = CurrentLightProperty.LightColour;
  241. }
  242. }
  243. private void OnRotate(object sender, EventArgs args)
  244. {
  245. CurrentLightProperty.Rotation = this.light.transform.rotation;
  246. OnTransformEvent(Rotate);
  247. }
  248. private void OnScale(object sender, EventArgs args)
  249. {
  250. float value = this.light.transform.localScale.x;
  251. if (SelectedLightType == MPSLightType.Point) Range = value;
  252. else if (SelectedLightType == MPSLightType.Spot) SpotAngle = value;
  253. OnTransformEvent(Scale);
  254. }
  255. private void OnTransformEvent(EventHandler handler)
  256. {
  257. handler?.Invoke(this, EventArgs.Empty);
  258. }
  259. }
  260. internal class LightProperty
  261. {
  262. public static readonly Vector3 DefaultPosition = new Vector3(0f, 1.5f, 0.4f);
  263. public static readonly Quaternion DefaultRotation = Quaternion.Euler(40f, 180f, 0f);
  264. public Quaternion Rotation { get; set; } = DefaultRotation;
  265. public float Intensity { get; set; } = 0.95f;
  266. public float Range { get; set; } = GameMain.Instance.MainLight.GetComponent<Light>().range;
  267. public float SpotAngle { get; set; } = 50f;
  268. public float ShadowStrength { get; set; } = 0.10f;
  269. public Color LightColour { get; set; } = Color.white;
  270. }
  271. }