DragPointLight.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 void Serialize(System.IO.BinaryWriter binaryWriter)
  119. {
  120. foreach (LightProperty lightProperty in LightProperties)
  121. {
  122. lightProperty.Serialize(binaryWriter);
  123. }
  124. binaryWriter.WriteVector3(MyObject.position);
  125. binaryWriter.Write((int)SelectedLightType);
  126. binaryWriter.Write(IsColourMode);
  127. binaryWriter.Write(IsDisabled);
  128. }
  129. public void Deserialize(System.IO.BinaryReader binaryReader)
  130. {
  131. for (int i = 0; i < LightProperties.Length; i++)
  132. {
  133. LightProperties[i] = LightProperty.Deserialize(binaryReader);
  134. }
  135. MyObject.position = binaryReader.ReadVector3();
  136. SetLightType((MPSLightType)binaryReader.ReadInt32());
  137. IsColourMode = binaryReader.ReadBoolean();
  138. IsDisabled = binaryReader.ReadBoolean();
  139. }
  140. public static void SetLightProperties(Light light, LightProperty prop)
  141. {
  142. light.transform.rotation = prop.Rotation;
  143. light.intensity = prop.Intensity;
  144. light.range = prop.Range;
  145. light.spotAngle = prop.SpotAngle;
  146. light.shadowStrength = prop.ShadowStrength;
  147. light.color = prop.LightColour;
  148. if (light.type == LightType.Spot)
  149. {
  150. light.transform.localScale = Vector3.one * prop.SpotAngle;
  151. }
  152. else if (light.type == LightType.Point)
  153. {
  154. light.transform.localScale = Vector3.one * prop.Range;
  155. }
  156. }
  157. public override void Set(Transform myObject)
  158. {
  159. base.Set(myObject);
  160. this.light = myObject.gameObject.GetOrAddComponent<Light>();
  161. this.light.transform.position = LightProperty.DefaultPosition;
  162. this.light.transform.rotation = LightProperty.DefaultRotation;
  163. SetLightType(MPSLightType.Normal);
  164. this.ScaleFactor = 50f;
  165. }
  166. protected override void OnDestroy()
  167. {
  168. if (!IsMain) GameObject.Destroy(this.light.gameObject);
  169. base.OnDestroy();
  170. }
  171. protected override void OnRotate()
  172. {
  173. CurrentLightProperty.Rotation = light.transform.rotation;
  174. base.OnRotate();
  175. }
  176. protected override void OnScale()
  177. {
  178. float value = light.transform.localScale.x;
  179. if (SelectedLightType == MPSLightType.Point) Range = value;
  180. else if (SelectedLightType == MPSLightType.Spot) SpotAngle = value;
  181. base.OnScale();
  182. }
  183. protected override void ApplyDragType()
  184. {
  185. DragType current = CurrentDragType;
  186. if (current == DragType.Select || current == DragType.MoveXZ || current == DragType.MoveY)
  187. {
  188. ApplyProperties(true, true, false);
  189. }
  190. else if (current == DragType.RotY || current == DragType.RotLocalXZ || current == DragType.RotLocalY)
  191. {
  192. bool canRotate = SelectedLightType != MPSLightType.Point;
  193. ApplyProperties(canRotate, canRotate, false);
  194. }
  195. else if (current == DragType.Scale)
  196. {
  197. bool canScale = SelectedLightType != MPSLightType.Normal;
  198. ApplyProperties(canScale, canScale, false);
  199. }
  200. else if (current == DragType.Delete)
  201. {
  202. ApplyProperties(!IsMain, !IsMain, false);
  203. }
  204. else
  205. {
  206. ApplyProperties(false, false, false);
  207. }
  208. }
  209. public void SetLightType(MPSLightType type)
  210. {
  211. LightType lightType = LightType.Directional;
  212. string name = "normal";
  213. SelectedLightType = type;
  214. if (type == MPSLightType.Spot)
  215. {
  216. lightType = LightType.Spot;
  217. name = "spot";
  218. }
  219. else if (type == MPSLightType.Point)
  220. {
  221. lightType = LightType.Point;
  222. name = "point";
  223. }
  224. this.light.type = lightType;
  225. this.Name = IsMain ? "main" : name;
  226. if (IsMain)
  227. {
  228. environmentManager.BGVisible = !(IsColourMode && SelectedLightType == MPSLightType.Normal);
  229. }
  230. SetProps();
  231. ApplyDragType();
  232. }
  233. public void SetRotation(float x, float y)
  234. {
  235. this.Rotation = Quaternion.Euler(x, y, Rotation.eulerAngles.z);
  236. }
  237. public void SetProp(LightProp prop, float value)
  238. {
  239. switch (prop)
  240. {
  241. case LightProp.Intensity:
  242. Intensity = value;
  243. break;
  244. case LightProp.ShadowStrength:
  245. ShadowStrength = value;
  246. break;
  247. case LightProp.SpotAngle:
  248. SpotAngle = value;
  249. break;
  250. case LightProp.Range:
  251. Range = value;
  252. break;
  253. case LightProp.Red:
  254. LightColorRed = value;
  255. break;
  256. case LightProp.Green:
  257. LightColorGreen = value;
  258. break;
  259. case LightProp.Blue:
  260. LightColorBlue = value;
  261. break;
  262. }
  263. }
  264. public void ResetLightProps()
  265. {
  266. LightProperties[(int)SelectedLightType] = new LightProperty();
  267. SetProps();
  268. }
  269. public void ResetLightPosition()
  270. {
  271. this.light.transform.position = LightProperty.DefaultPosition;
  272. }
  273. private void SetProps()
  274. {
  275. SetLightProperties(this.light, CurrentLightProperty);
  276. if (IsColourMode)
  277. {
  278. this.light.color = Color.white;
  279. camera.backgroundColor = CurrentLightProperty.LightColour;
  280. }
  281. }
  282. }
  283. internal class LightProperty
  284. {
  285. public static readonly Vector3 DefaultPosition = new Vector3(0f, 1.9f, 0.4f);
  286. public static readonly Quaternion DefaultRotation = Quaternion.Euler(40f, 180f, 0f);
  287. public Quaternion Rotation { get; set; } = DefaultRotation;
  288. public float Intensity { get; set; } = 0.95f;
  289. public float Range { get; set; } = GameMain.Instance.MainLight.GetComponent<Light>().range;
  290. public float SpotAngle { get; set; } = 50f;
  291. public float ShadowStrength { get; set; } = 0.10f;
  292. public Color LightColour { get; set; } = Color.white;
  293. public void Serialize(System.IO.BinaryWriter binaryWriter)
  294. {
  295. binaryWriter.WriteQuaternion(Rotation);
  296. binaryWriter.Write(Intensity);
  297. binaryWriter.Write(Range);
  298. binaryWriter.Write(SpotAngle);
  299. binaryWriter.Write(ShadowStrength);
  300. binaryWriter.WriteColour(LightColour);
  301. }
  302. public static LightProperty Deserialize(System.IO.BinaryReader binaryReader)
  303. {
  304. return new LightProperty()
  305. {
  306. Rotation = binaryReader.ReadQuaternion(),
  307. Intensity = binaryReader.ReadSingle(),
  308. Range = binaryReader.ReadSingle(),
  309. SpotAngle = binaryReader.ReadSingle(),
  310. ShadowStrength = binaryReader.ReadSingle(),
  311. LightColour = binaryReader.ReadColour()
  312. };
  313. }
  314. }
  315. }