DragPointLight.cs 10.0 KB

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