MPSLight.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. );
  132. DragLight.Initialize(this.light.gameObject, this.IsMain, CustomGizmo.GizmoMode.World,
  133. () => this.light.transform.position,
  134. () => this.light.transform.eulerAngles
  135. );
  136. DragLight.scaleFactor = 50f;
  137. DragLight.Select += (s, a) => this.Select?.Invoke(this, EventArgs.Empty);
  138. if (!isMain)
  139. {
  140. DragLight.Delete += (s, a) => Delete?.Invoke(this, EventArgs.Empty);
  141. }
  142. DragLight.SetDragProp(false, false, false);
  143. SetLightType(LightType.Directional);
  144. }
  145. public static void SetLightProperties(Light light, LightProperty prop)
  146. {
  147. light.transform.rotation = prop.Rotation;
  148. light.intensity = prop.Intensity;
  149. light.range = prop.Range;
  150. light.spotAngle = prop.SpotAngle;
  151. light.shadowStrength = prop.ShadowStrength;
  152. light.color = prop.LightColour;
  153. if (light.type == LightType.Spot)
  154. {
  155. light.transform.localScale = Vector3.one * prop.SpotAngle;
  156. }
  157. else if (light.type == LightType.Point)
  158. {
  159. light.transform.localScale = Vector3.one * prop.Range;
  160. }
  161. }
  162. public void Destroy()
  163. {
  164. DragLight.Rotate -= OnRotate;
  165. DragLight.Scale -= OnScale;
  166. GameObject.Destroy(DragLight.gameObject);
  167. if (!IsMain) GameObject.Destroy(this.light.gameObject);
  168. }
  169. public void SetLightType(LightType type)
  170. {
  171. DragLight.Rotate -= OnRotate;
  172. DragLight.Rotate -= OnScale;
  173. string name = "normal";
  174. if (type == LightType.Directional)
  175. {
  176. SelectedLightType = MPSLightType.Normal;
  177. }
  178. else if (type == LightType.Spot)
  179. {
  180. name = "spot";
  181. SelectedLightType = MPSLightType.Spot;
  182. DragLight.Scale += OnScale;
  183. DragLight.Rotate += OnRotate;
  184. }
  185. else
  186. {
  187. name = "point";
  188. SelectedLightType = MPSLightType.Point;
  189. DragLight.Scale += OnScale;
  190. }
  191. this.light.type = type;
  192. SetProps();
  193. this.Name = IsMain ? "main" : name;
  194. }
  195. public void SetRotation(float x, float y)
  196. {
  197. this.Rotation = Quaternion.Euler(x, y, Rotation.eulerAngles.z);
  198. }
  199. public void SetProp(LightProp prop, float value)
  200. {
  201. switch (prop)
  202. {
  203. case LightProp.Intensity:
  204. Intensity = value;
  205. break;
  206. case LightProp.ShadowStrength:
  207. ShadowStrength = value;
  208. break;
  209. case LightProp.SpotAngle:
  210. SpotAngle = value;
  211. break;
  212. case LightProp.Range:
  213. Range = value;
  214. break;
  215. case LightProp.Red:
  216. LightColorRed = value;
  217. break;
  218. case LightProp.Green:
  219. LightColorGreen = value;
  220. break;
  221. case LightProp.Blue:
  222. LightColorBlue = value;
  223. break;
  224. }
  225. }
  226. public void ResetLightProps()
  227. {
  228. LightProperties[(int)SelectedLightType] = new LightProperty();
  229. SetProps();
  230. }
  231. public void ResetLightPosition()
  232. {
  233. this.light.transform.position = LightProperty.DefaultPosition;
  234. }
  235. private void SetProps()
  236. {
  237. SetLightProperties(this.light, CurrentLightProperty);
  238. if (IsColourMode)
  239. {
  240. this.light.color = Color.white;
  241. camera.backgroundColor = CurrentLightProperty.LightColour;
  242. }
  243. }
  244. private void OnRotate(object sender, EventArgs args)
  245. {
  246. CurrentLightProperty.Rotation = this.light.transform.rotation;
  247. OnTransformEvent(Rotate);
  248. }
  249. private void OnScale(object sender, EventArgs args)
  250. {
  251. float value = this.light.transform.localScale.x;
  252. if (SelectedLightType == MPSLightType.Point) Range = value;
  253. else if (SelectedLightType == MPSLightType.Spot) SpotAngle = value;
  254. OnTransformEvent(Scale);
  255. }
  256. private void OnTransformEvent(EventHandler handler)
  257. {
  258. handler?.Invoke(this, EventArgs.Empty);
  259. }
  260. }
  261. internal class LightProperty
  262. {
  263. public static readonly Vector3 DefaultPosition = new Vector3(0f, 1.5f, 0.4f);
  264. public static readonly Quaternion DefaultRotation = Quaternion.Euler(40f, 180f, 0f);
  265. public Quaternion Rotation { get; set; } = DefaultRotation;
  266. public float Intensity { get; set; } = 0.95f;
  267. public float Range { get; set; } = GameMain.Instance.MainLight.GetComponent<Light>().range;
  268. public float SpotAngle { get; set; } = 50f;
  269. public float ShadowStrength { get; set; } = 0.10f;
  270. public Color LightColour { get; set; } = Color.white;
  271. }
  272. }