DragPointLight.cs 9.7 KB

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