DragPointLight.cs 11 KB

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