DragPointLight.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. if (Selecting || Moving) ApplyProperties(true, true, false);
  186. else if (SelectedLightType != MPSLightType.Point && Rotating) ApplyProperties(true, true, false);
  187. else if (SelectedLightType != MPSLightType.Normal && Scaling) ApplyProperties(true, true, false);
  188. else if (!IsMain && Deleting) ApplyProperties(true, true, false);
  189. else ApplyProperties(false, false, false);
  190. ApplyColours();
  191. }
  192. public void SetLightType(MPSLightType type)
  193. {
  194. LightType lightType = LightType.Directional;
  195. string name = "normal";
  196. SelectedLightType = type;
  197. if (type == MPSLightType.Spot)
  198. {
  199. lightType = LightType.Spot;
  200. name = "spot";
  201. }
  202. else if (type == MPSLightType.Point)
  203. {
  204. lightType = LightType.Point;
  205. name = "point";
  206. }
  207. this.light.type = lightType;
  208. this.Name = IsMain ? "main" : name;
  209. if (IsMain)
  210. {
  211. environmentManager.BGVisible = !(IsColourMode && SelectedLightType == MPSLightType.Normal);
  212. }
  213. SetProps();
  214. ApplyDragType();
  215. }
  216. public void SetRotation(float x, float y)
  217. {
  218. this.Rotation = Quaternion.Euler(x, y, Rotation.eulerAngles.z);
  219. }
  220. public void SetProp(LightProp prop, float value)
  221. {
  222. switch (prop)
  223. {
  224. case LightProp.Intensity:
  225. Intensity = value;
  226. break;
  227. case LightProp.ShadowStrength:
  228. ShadowStrength = value;
  229. break;
  230. case LightProp.SpotAngle:
  231. SpotAngle = value;
  232. break;
  233. case LightProp.Range:
  234. Range = value;
  235. break;
  236. case LightProp.Red:
  237. LightColorRed = value;
  238. break;
  239. case LightProp.Green:
  240. LightColorGreen = value;
  241. break;
  242. case LightProp.Blue:
  243. LightColorBlue = value;
  244. break;
  245. }
  246. }
  247. public void ResetLightProps()
  248. {
  249. LightProperties[(int)SelectedLightType] = new LightProperty();
  250. SetProps();
  251. }
  252. public void ResetLightPosition()
  253. {
  254. this.light.transform.position = LightProperty.DefaultPosition;
  255. }
  256. private void SetProps()
  257. {
  258. SetLightProperties(this.light, CurrentLightProperty);
  259. if (IsColourMode)
  260. {
  261. this.light.color = Color.white;
  262. camera.backgroundColor = CurrentLightProperty.LightColour;
  263. }
  264. }
  265. }
  266. internal class LightProperty
  267. {
  268. public static readonly Vector3 DefaultPosition = new Vector3(0f, 1.9f, 0.4f);
  269. public static readonly Quaternion DefaultRotation = Quaternion.Euler(40f, 180f, 0f);
  270. public Quaternion Rotation { get; set; } = DefaultRotation;
  271. public float Intensity { get; set; } = 0.95f;
  272. public float Range { get; set; } = GameMain.Instance.MainLight.GetComponent<Light>().range;
  273. public float SpotAngle { get; set; } = 50f;
  274. public float ShadowStrength { get; set; } = 0.10f;
  275. public Color LightColour { get; set; } = Color.white;
  276. public void Serialize(System.IO.BinaryWriter binaryWriter)
  277. {
  278. binaryWriter.WriteQuaternion(Rotation);
  279. binaryWriter.Write(Intensity);
  280. binaryWriter.Write(Range);
  281. binaryWriter.Write(SpotAngle);
  282. binaryWriter.Write(ShadowStrength);
  283. binaryWriter.WriteColour(LightColour);
  284. }
  285. public static LightProperty Deserialize(System.IO.BinaryReader binaryReader)
  286. {
  287. return new LightProperty()
  288. {
  289. Rotation = binaryReader.ReadQuaternion(),
  290. Intensity = binaryReader.ReadSingle(),
  291. Range = binaryReader.ReadSingle(),
  292. SpotAngle = binaryReader.ReadSingle(),
  293. ShadowStrength = binaryReader.ReadSingle(),
  294. LightColour = binaryReader.ReadColour()
  295. };
  296. }
  297. }
  298. }