PropManager.cs 13 KB

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