MPSLight.cs 10 KB

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