DragPointLight.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. public 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. DefaultPosition = LightProperty.DefaultPosition;
  160. }
  161. protected override void OnDestroy()
  162. {
  163. if (!IsMain) Destroy(light.gameObject);
  164. base.OnDestroy();
  165. }
  166. protected override void OnRotate()
  167. {
  168. CurrentLightProperty.Rotation = light.transform.rotation;
  169. base.OnRotate();
  170. }
  171. protected override void OnScale()
  172. {
  173. float value = light.transform.localScale.x;
  174. if (SelectedLightType == MPSLightType.Point) Range = value;
  175. else if (SelectedLightType == MPSLightType.Spot) SpotAngle = value;
  176. base.OnScale();
  177. }
  178. protected override void ApplyDragType()
  179. {
  180. if (Selecting || Moving) ApplyProperties(true, true, false);
  181. else if (SelectedLightType != MPSLightType.Point && Rotating) ApplyProperties(true, true, false);
  182. else if (SelectedLightType != MPSLightType.Normal && Scaling) ApplyProperties(true, true, false);
  183. else if (!IsMain && Deleting) ApplyProperties(true, true, false);
  184. else ApplyProperties(false, false, false);
  185. ApplyColours();
  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. light.type = lightType;
  203. 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) => Rotation = Quaternion.Euler(x, y, Rotation.eulerAngles.z);
  212. public void SetProp(LightProp prop, float value)
  213. {
  214. switch (prop)
  215. {
  216. case LightProp.Intensity:
  217. Intensity = value;
  218. break;
  219. case LightProp.ShadowStrength:
  220. ShadowStrength = value;
  221. break;
  222. case LightProp.SpotAngle:
  223. SpotAngle = value;
  224. break;
  225. case LightProp.Range:
  226. Range = value;
  227. break;
  228. case LightProp.Red:
  229. LightColorRed = value;
  230. break;
  231. case LightProp.Green:
  232. LightColorGreen = value;
  233. break;
  234. case LightProp.Blue:
  235. LightColorBlue = value;
  236. break;
  237. }
  238. }
  239. public void ResetLightProps()
  240. {
  241. LightProperties[(int)SelectedLightType] = new LightProperty();
  242. SetProps();
  243. }
  244. public void ResetLightPosition() => light.transform.position = LightProperty.DefaultPosition;
  245. private void SetProps()
  246. {
  247. SetLightProperties(light, CurrentLightProperty);
  248. if (IsColourMode)
  249. {
  250. light.color = Color.white;
  251. camera.backgroundColor = CurrentLightProperty.LightColour;
  252. }
  253. }
  254. }
  255. public class LightProperty
  256. {
  257. public static readonly Vector3 DefaultPosition = new Vector3(0f, 1.9f, 0.4f);
  258. public static readonly Quaternion DefaultRotation = Quaternion.Euler(40f, 180f, 0f);
  259. public Quaternion Rotation { get; set; } = DefaultRotation;
  260. public float Intensity { get; set; } = 0.95f;
  261. public float Range { get; set; } = GameMain.Instance.MainLight.GetComponent<Light>().range;
  262. public float SpotAngle { get; set; } = 50f;
  263. public float ShadowStrength { get; set; } = 0.10f;
  264. public Color LightColour { get; set; } = Color.white;
  265. public void Serialize(System.IO.BinaryWriter binaryWriter)
  266. {
  267. binaryWriter.WriteQuaternion(Rotation);
  268. binaryWriter.Write(Intensity);
  269. binaryWriter.Write(Range);
  270. binaryWriter.Write(SpotAngle);
  271. binaryWriter.Write(ShadowStrength);
  272. binaryWriter.WriteColour(LightColour);
  273. }
  274. public static LightProperty Deserialize(System.IO.BinaryReader binaryReader)
  275. {
  276. return new LightProperty()
  277. {
  278. Rotation = binaryReader.ReadQuaternion(),
  279. Intensity = binaryReader.ReadSingle(),
  280. Range = binaryReader.ReadSingle(),
  281. SpotAngle = binaryReader.ReadSingle(),
  282. ShadowStrength = binaryReader.ReadSingle(),
  283. LightColour = binaryReader.ReadColour()
  284. };
  285. }
  286. }
  287. }