DragPointLight.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. }
  159. protected override void OnDestroy()
  160. {
  161. if (!IsMain) Destroy(light.gameObject);
  162. base.OnDestroy();
  163. }
  164. protected override void OnRotate()
  165. {
  166. CurrentLightProperty.Rotation = light.transform.rotation;
  167. base.OnRotate();
  168. }
  169. protected override void OnScale()
  170. {
  171. float value = light.transform.localScale.x;
  172. if (SelectedLightType == MPSLightType.Point) Range = value;
  173. else if (SelectedLightType == MPSLightType.Spot) SpotAngle = value;
  174. base.OnScale();
  175. }
  176. protected override void ApplyDragType()
  177. {
  178. if (Selecting || Moving) ApplyProperties(true, true, false);
  179. else if (SelectedLightType != MPSLightType.Point && Rotating) ApplyProperties(true, true, false);
  180. else if (SelectedLightType != MPSLightType.Normal && Scaling) ApplyProperties(true, true, false);
  181. else if (!IsMain && Deleting) ApplyProperties(true, true, false);
  182. else ApplyProperties(false, false, false);
  183. ApplyColours();
  184. }
  185. public void SetLightType(MPSLightType type)
  186. {
  187. LightType lightType = LightType.Directional;
  188. string name = "normal";
  189. SelectedLightType = type;
  190. if (type == MPSLightType.Spot)
  191. {
  192. lightType = LightType.Spot;
  193. name = "spot";
  194. }
  195. else if (type == MPSLightType.Point)
  196. {
  197. lightType = LightType.Point;
  198. name = "point";
  199. }
  200. light.type = lightType;
  201. Name = IsMain ? "main" : name;
  202. if (IsMain)
  203. {
  204. EnvironmentManager.BGVisible = !(IsColourMode && SelectedLightType == MPSLightType.Normal);
  205. }
  206. SetProps();
  207. ApplyDragType();
  208. }
  209. public void SetRotation(float x, float y) => Rotation = Quaternion.Euler(x, y, Rotation.eulerAngles.z);
  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() => light.transform.position = LightProperty.DefaultPosition;
  243. private void SetProps()
  244. {
  245. SetLightProperties(light, CurrentLightProperty);
  246. if (IsColourMode)
  247. {
  248. light.color = Color.white;
  249. camera.backgroundColor = CurrentLightProperty.LightColour;
  250. }
  251. }
  252. }
  253. internal class LightProperty
  254. {
  255. public static readonly Vector3 DefaultPosition = new Vector3(0f, 1.9f, 0.4f);
  256. public static readonly Quaternion DefaultRotation = Quaternion.Euler(40f, 180f, 0f);
  257. public Quaternion Rotation { get; set; } = DefaultRotation;
  258. public float Intensity { get; set; } = 0.95f;
  259. public float Range { get; set; } = GameMain.Instance.MainLight.GetComponent<Light>().range;
  260. public float SpotAngle { get; set; } = 50f;
  261. public float ShadowStrength { get; set; } = 0.10f;
  262. public Color LightColour { get; set; } = Color.white;
  263. public void Serialize(System.IO.BinaryWriter binaryWriter)
  264. {
  265. binaryWriter.WriteQuaternion(Rotation);
  266. binaryWriter.Write(Intensity);
  267. binaryWriter.Write(Range);
  268. binaryWriter.Write(SpotAngle);
  269. binaryWriter.Write(ShadowStrength);
  270. binaryWriter.WriteColour(LightColour);
  271. }
  272. public static LightProperty Deserialize(System.IO.BinaryReader binaryReader)
  273. {
  274. return new LightProperty()
  275. {
  276. Rotation = binaryReader.ReadQuaternion(),
  277. Intensity = binaryReader.ReadSingle(),
  278. Range = binaryReader.ReadSingle(),
  279. SpotAngle = binaryReader.ReadSingle(),
  280. ShadowStrength = binaryReader.ReadSingle(),
  281. LightColour = binaryReader.ReadColour()
  282. };
  283. }
  284. }
  285. }