PropManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. internal class PropManager
  9. {
  10. private MeidoManager meidoManager;
  11. private static bool cubeActive = true;
  12. public static bool CubeActive
  13. {
  14. get => cubeActive;
  15. set
  16. {
  17. if (value != cubeActive)
  18. {
  19. cubeActive = value;
  20. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  21. }
  22. }
  23. }
  24. private static bool cubeSmall;
  25. public static bool CubeSmall
  26. {
  27. get => cubeSmall;
  28. set
  29. {
  30. if (value != cubeSmall)
  31. {
  32. cubeSmall = value;
  33. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  34. }
  35. }
  36. }
  37. private static event EventHandler CubeActiveChange;
  38. private static event EventHandler CubeSmallChange;
  39. private List<DragDogu> doguList = new List<DragDogu>();
  40. private DragType dragTypeOld = DragType.None;
  41. private DragType currentDragType = DragType.None;
  42. private bool showGizmos = false;
  43. private enum DragType
  44. {
  45. None, Move, Rotate, Scale, Delete, Other
  46. }
  47. public int DoguCount => doguList.Count;
  48. public event EventHandler DoguListChange;
  49. public string[] PropNameList
  50. {
  51. get
  52. {
  53. return doguList.Count == 0
  54. ? new[] { Translation.Get("systemMessage", "noProps") }
  55. : doguList.Select(dogu => dogu.Name).ToArray();
  56. }
  57. }
  58. public PropManager(MeidoManager meidoManager)
  59. {
  60. this.meidoManager = meidoManager;
  61. this.meidoManager.BeginCallMeidos += DetachProps;
  62. this.meidoManager.EndCallMeidos += ReattachProps;
  63. }
  64. public void Activate()
  65. {
  66. CubeSmallChange += OnCubeSmall;
  67. }
  68. public void Deactivate()
  69. {
  70. foreach (DragDogu dogu in doguList)
  71. {
  72. dogu.Delete -= DeleteDogu;
  73. GameObject.Destroy(dogu.gameObject);
  74. }
  75. doguList.Clear();
  76. CubeSmallChange -= OnCubeSmall;
  77. }
  78. public void Update()
  79. {
  80. if (Input.GetKeyDown(KeyCode.Space))
  81. {
  82. // showGizmos = !showGizmos;
  83. // currentDragType = dragTypeOld = DragType.None;
  84. // UpdateDragType();
  85. }
  86. if (CubeActive && (Input.GetKey(KeyCode.Z) || Input.GetKey(KeyCode.X) || Input.GetKey(KeyCode.C)
  87. || Input.GetKey(KeyCode.D))
  88. )
  89. {
  90. currentDragType = DragType.Other;
  91. }
  92. else
  93. {
  94. currentDragType = DragType.None;
  95. }
  96. if (currentDragType != dragTypeOld) UpdateDragType();
  97. dragTypeOld = currentDragType;
  98. }
  99. private void UpdateDragType()
  100. {
  101. bool dragPointActive = (currentDragType == DragType.Other);
  102. foreach (DragDogu dogu in doguList)
  103. {
  104. dogu.SetDragProp(showGizmos, dragPointActive, dragPointActive);
  105. }
  106. }
  107. private GameObject GetDeploymentObject()
  108. {
  109. GameObject go = GameObject.Find("Deployment Object Parent");
  110. if (go == null) go = new GameObject("Deployment Object Parent");
  111. return go;
  112. }
  113. public void SpawnObject(string assetName)
  114. {
  115. // TODO: Add a couple more things to ignore list
  116. GameObject dogu = null;
  117. string doguName = Translation.Get("propNames", assetName, false);
  118. Vector3 doguPosition = new Vector3(0f, 0f, 0.5f);
  119. Vector3 doguScale = Vector3.one;
  120. if (assetName.EndsWith(".menu"))
  121. {
  122. dogu = MenuFileUtility.LoadModel(assetName);
  123. string handItem = Utility.HandItemToOdogu(assetName);
  124. if (Translation.Has("propNames", handItem))
  125. {
  126. doguName = Translation.Get("propNames", handItem);
  127. }
  128. }
  129. else if (assetName.StartsWith("BG_"))
  130. {
  131. assetName = assetName.Remove(0, 3);
  132. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName);
  133. if (obj == null)
  134. {
  135. obj = (Resources.Load("BG/" + assetName) ?? Resources.Load("BG/2_0/" + assetName)) as GameObject;
  136. }
  137. if (obj != null)
  138. {
  139. dogu = GameObject.Instantiate(obj);
  140. doguPosition = Vector3.zero;
  141. doguScale = Vector3.one * 0.1f;
  142. doguName = Translation.Get("bgNames", assetName);
  143. }
  144. }
  145. else if (assetName.StartsWith("mirror"))
  146. {
  147. Material mirrorMaterial = new Material(Shader.Find("Mirror"));
  148. dogu = GameObject.CreatePrimitive(PrimitiveType.Plane);
  149. Renderer mirrorRenderer = dogu.GetComponent<Renderer>();
  150. mirrorRenderer.material = mirrorMaterial;
  151. mirrorRenderer.enabled = true;
  152. MirrorReflection2 mirrorReflection = dogu.AddComponent<MirrorReflection2>();
  153. mirrorReflection.m_TextureSize = 2048;
  154. Vector3 localPosition = new Vector3(0f, 0.96f, 0f);
  155. dogu.transform.Rotate(dogu.transform.right, 90f);
  156. switch (assetName)
  157. {
  158. case "mirror1":
  159. dogu.transform.localScale = new Vector3(0.2f, 0.4f, 0.2f);
  160. break;
  161. case "mirror2":
  162. dogu.transform.localScale = new Vector3(0.1f, 0.4f, 0.2f);
  163. break;
  164. case "mirror3":
  165. localPosition.y = 0.85f;
  166. dogu.transform.localScale = new Vector3(0.03f, 0.18f, 0.124f);
  167. break;
  168. }
  169. dogu.transform.localPosition = localPosition;
  170. }
  171. else if (assetName.IndexOf(':') >= 0)
  172. {
  173. string[] assetParts = assetName.Split(':');
  174. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetParts[0]);
  175. if (obj == null)
  176. {
  177. obj = Resources.Load("BG/" + assetParts[0]) as GameObject;
  178. }
  179. GameObject bg = GameObject.Instantiate(obj);
  180. int num = int.Parse(assetParts[1]);
  181. dogu = bg.transform.GetChild(num).gameObject;
  182. dogu.transform.SetParent(null);
  183. GameObject.Destroy(bg);
  184. bg.SetActive(false);
  185. }
  186. else
  187. {
  188. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName);
  189. if (obj == null) obj = Resources.Load("Prefab/" + assetName) as GameObject;
  190. dogu = GameObject.Instantiate(obj) as GameObject;
  191. dogu.transform.localPosition = Vector3.zero;
  192. MeshRenderer[] meshRenderers = dogu.GetComponentsInChildren<MeshRenderer>();
  193. for (int i = 0; i < meshRenderers.Length; i++)
  194. {
  195. if (meshRenderers[i] != null
  196. && meshRenderers[i].gameObject.name.ToLower().IndexOf("castshadow") < 0
  197. )
  198. {
  199. meshRenderers[i].shadowCastingMode = ShadowCastingMode.Off;
  200. }
  201. }
  202. Collider collider = dogu.transform.GetComponent<Collider>();
  203. if (collider != null) collider.enabled = false;
  204. foreach (Transform transform in dogu.transform)
  205. {
  206. collider = transform.GetComponent<Collider>();
  207. if (collider != null)
  208. {
  209. collider.enabled = false;
  210. }
  211. }
  212. #region particle system experiment
  213. // if (asset.StartsWith("Particle/"))
  214. // {
  215. // ParticleSystem particleSystem = go.GetComponent<ParticleSystem>();
  216. // if (particleSystem != null)
  217. // {
  218. // ParticleSystem.MainModule main;
  219. // main = particleSystem.main;
  220. // main.loop = true;
  221. // main.duration = Mathf.Infinity;
  222. // ParticleSystem[] particleSystems = particleSystem.GetComponents<ParticleSystem>();
  223. // foreach (ParticleSystem part in particleSystems)
  224. // {
  225. // ParticleSystem.EmissionModule emissionModule = part.emission;
  226. // ParticleSystem.Burst[] bursts = new ParticleSystem.Burst[emissionModule.burstCount];
  227. // emissionModule.GetBursts(bursts);
  228. // for (int i = 0; i < bursts.Length; i++)
  229. // {
  230. // bursts[i].cycleCount = Int32.MaxValue;
  231. // }
  232. // emissionModule.SetBursts(bursts);
  233. // main = part.main;
  234. // main.loop = true;
  235. // main.duration = Mathf.Infinity;
  236. // }
  237. // }
  238. // }
  239. #endregion
  240. }
  241. if (dogu != null)
  242. {
  243. // TODO: Figure out why some props aren't centred properly
  244. // Doesn't happen in MM but even after copy pasting the code, it doesn't work :/
  245. GameObject deploymentObject = GetDeploymentObject();
  246. GameObject finalDogu = new GameObject(doguName);
  247. dogu.transform.localScale = doguScale;
  248. dogu.transform.SetParent(finalDogu.transform, true);
  249. finalDogu.transform.SetParent(deploymentObject.transform, false);
  250. finalDogu.transform.position = doguPosition;
  251. DragDogu dragDogu = BaseDrag.MakeDragPoint<DragDogu>(
  252. PrimitiveType.Cube, Vector3.one * 0.12f, BaseDrag.LightBlue
  253. ).Initialize(finalDogu);
  254. dragDogu.Delete += DeleteDogu;
  255. dragDogu.SetDragProp(showGizmos, false, false);
  256. doguList.Add(dragDogu);
  257. dragDogu.DragPointScale = CubeSmall ? 0.4f : 1f;
  258. OnDoguListChange();
  259. }
  260. else
  261. {
  262. Debug.LogError($"Could not spawn object '{assetName}'");
  263. }
  264. }
  265. public DragDogu GetDogu(int doguIndex)
  266. {
  267. if (doguList.Count == 0 || doguIndex >= doguList.Count || doguIndex < 0) return null;
  268. return doguList[doguIndex];
  269. }
  270. public void AttachProp(
  271. int doguIndex, DragPointManager.AttachPoint attachPoint, Meido meido, bool worldPositionStays = true
  272. )
  273. {
  274. if (doguList.Count == 0 || doguIndex >= doguList.Count || doguIndex < 0) return;
  275. AttachProp(doguList[doguIndex], attachPoint, meido, worldPositionStays);
  276. }
  277. private void AttachProp(
  278. DragDogu dragDogu, DragPointManager.AttachPoint attachPoint, Meido meido, bool worldPositionStays = true
  279. )
  280. {
  281. GameObject dogu = dragDogu.Dogu;
  282. Transform attachPointTransform = meido?.GetBoneTransform(attachPoint) ?? GetDeploymentObject().transform;
  283. dragDogu.attachPointInfo = new DragPointManager.AttachPointInfo(
  284. attachPoint: meido == null ? DragPointManager.AttachPoint.None : attachPoint,
  285. maidGuid: meido == null ? String.Empty : meido.Maid.status.guid,
  286. maidIndex: meido == null ? -1 : meido.ActiveSlot
  287. );
  288. worldPositionStays = meido == null ? true : worldPositionStays;
  289. Vector3 position = dogu.transform.position;
  290. Quaternion rotation = dogu.transform.rotation;
  291. dogu.transform.SetParent(attachPointTransform, worldPositionStays);
  292. if (worldPositionStays)
  293. {
  294. dogu.transform.position = position;
  295. dogu.transform.rotation = rotation;
  296. }
  297. else
  298. {
  299. dogu.transform.localPosition = Vector3.zero;
  300. dogu.transform.rotation = Quaternion.identity;
  301. }
  302. if (meido == null) Utility.FixGameObjectScale(dogu);
  303. }
  304. private void DetachProps(object sender, EventArgs args)
  305. {
  306. foreach (DragDogu dogu in doguList)
  307. {
  308. if (dogu.attachPointInfo.AttachPoint != DragPointManager.AttachPoint.None)
  309. {
  310. dogu.Dogu.transform.SetParent(GetDeploymentObject().transform, true);
  311. }
  312. }
  313. }
  314. private void ReattachProps(object sender, EventArgs args)
  315. {
  316. foreach (DragDogu dragDogu in doguList)
  317. {
  318. Meido meido = this.meidoManager.GetMeido(dragDogu.attachPointInfo.MaidGuid);
  319. bool worldPositionStays = meido == null;
  320. AttachProp(dragDogu, dragDogu.attachPointInfo.AttachPoint, meido, worldPositionStays);
  321. }
  322. }
  323. private void DeleteDogu(object sender, EventArgs args)
  324. {
  325. doguList.RemoveAll(dragDogu =>
  326. {
  327. if (dragDogu.DeleteMe)
  328. {
  329. GameObject.Destroy(dragDogu.gameObject);
  330. return true;
  331. }
  332. return false;
  333. }
  334. );
  335. OnDoguListChange();
  336. }
  337. private void OnCubeSmall(object sender, EventArgs args)
  338. {
  339. foreach (DragDogu dogu in doguList)
  340. {
  341. dogu.DragPointScale = CubeSmall ? 0.4f : 1f;
  342. }
  343. }
  344. private void OnDoguListChange()
  345. {
  346. this.DoguListChange?.Invoke(this, EventArgs.Empty);
  347. }
  348. }
  349. }