PropManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 static bool cubeActive = true;
  11. public static bool CubeActive
  12. {
  13. get => cubeActive;
  14. set
  15. {
  16. if (value != cubeActive)
  17. {
  18. cubeActive = value;
  19. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  20. }
  21. }
  22. }
  23. private static bool cubeSmall;
  24. public static bool CubeSmall
  25. {
  26. get => cubeSmall;
  27. set
  28. {
  29. if (value != cubeSmall)
  30. {
  31. cubeSmall = value;
  32. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  33. }
  34. }
  35. }
  36. private static event EventHandler CubeActiveChange;
  37. private static event EventHandler CubeSmallChange;
  38. private List<DragDogu> doguList = new List<DragDogu>();
  39. private DragType dragTypeOld = DragType.None;
  40. private DragType currentDragType = DragType.None;
  41. private bool showGizmos = false;
  42. private enum DragType
  43. {
  44. None, Move, Rotate, Scale, Delete, Other
  45. }
  46. public void Activate()
  47. {
  48. CubeSmallChange += OnCubeSmall;
  49. }
  50. public void Deactivate()
  51. {
  52. foreach (DragDogu dogu in doguList)
  53. {
  54. GameObject.Destroy(dogu.gameObject);
  55. }
  56. doguList.Clear();
  57. CubeSmallChange -= OnCubeSmall;
  58. }
  59. public void Update()
  60. {
  61. if (Input.GetKeyDown(KeyCode.Space))
  62. {
  63. // showGizmos = !showGizmos;
  64. // currentDragType = dragTypeOld = DragType.None;
  65. // UpdateDragType();
  66. }
  67. if (CubeActive && (Input.GetKey(KeyCode.Z) || Input.GetKey(KeyCode.X) || Input.GetKey(KeyCode.C)
  68. || Input.GetKey(KeyCode.D))
  69. )
  70. {
  71. currentDragType = DragType.Other;
  72. }
  73. else
  74. {
  75. currentDragType = DragType.None;
  76. }
  77. if (currentDragType != dragTypeOld) UpdateDragType();
  78. dragTypeOld = currentDragType;
  79. }
  80. private void UpdateDragType()
  81. {
  82. bool dragPointActive = (currentDragType == DragType.Other);
  83. foreach (DragDogu dogu in doguList)
  84. {
  85. dogu.SetDragProp(showGizmos, dragPointActive, dragPointActive);
  86. }
  87. }
  88. private GameObject GetDeploymentObject()
  89. {
  90. GameObject go = GameObject.Find("Deployment Object Parent");
  91. if (go == null) go = new GameObject("Deployment Object Parent");
  92. return go;
  93. }
  94. public void SpawnObject(string assetName)
  95. {
  96. // TODO: Add a couple more things to ignore list
  97. GameObject dogu = null;
  98. string doguName = assetName;
  99. Vector3 doguPosition = new Vector3(0f, 0f, 0.5f);
  100. Vector3 doguScale = Vector3.one;
  101. if (assetName.EndsWith(".menu"))
  102. {
  103. dogu = MenuFileUtility.LoadModel(assetName);
  104. }
  105. else if (assetName.StartsWith("BG_"))
  106. {
  107. assetName = assetName.Remove(0, 3);
  108. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName);
  109. if (obj == null)
  110. {
  111. obj = (Resources.Load("BG/" + assetName) ?? Resources.Load("BG/2_0/" + assetName)) as GameObject;
  112. }
  113. if (obj != null)
  114. {
  115. dogu = GameObject.Instantiate(obj);
  116. doguPosition = Vector3.zero;
  117. doguScale = Vector3.one * 0.1f;
  118. }
  119. }
  120. else if (assetName.StartsWith("mirror"))
  121. {
  122. Material mirrorMaterial = new Material(Shader.Find("Mirror"));
  123. dogu = GameObject.CreatePrimitive(PrimitiveType.Plane);
  124. Renderer mirrorRenderer = dogu.GetComponent<Renderer>();
  125. mirrorRenderer.material = mirrorMaterial;
  126. mirrorRenderer.enabled = true;
  127. MirrorReflection2 mirrorReflection = dogu.AddComponent<MirrorReflection2>();
  128. mirrorReflection.m_TextureSize = 2048;
  129. Vector3 localPosition = new Vector3(0f, 0.96f, 0f);
  130. dogu.transform.Rotate(dogu.transform.right, 90f);
  131. switch (assetName)
  132. {
  133. case "mirror1":
  134. dogu.transform.localScale = new Vector3(0.2f, 0.4f, 0.2f);
  135. break;
  136. case "mirror2":
  137. dogu.transform.localScale = new Vector3(0.1f, 0.4f, 0.2f);
  138. break;
  139. case "mirror3":
  140. localPosition.y = 0.85f;
  141. dogu.transform.localScale = new Vector3(0.03f, 0.18f, 0.124f);
  142. break;
  143. }
  144. dogu.transform.localPosition = localPosition;
  145. }
  146. else if (assetName.IndexOf(':') >= 0)
  147. {
  148. string[] assetParts = assetName.Split(':');
  149. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetParts[0]);
  150. if (obj == null)
  151. {
  152. obj = Resources.Load("BG/" + assetParts[0]) as GameObject;
  153. }
  154. GameObject bg = GameObject.Instantiate(obj);
  155. int num = int.Parse(assetParts[1]);
  156. dogu = bg.transform.GetChild(num).gameObject;
  157. dogu.transform.SetParent(null);
  158. GameObject.Destroy(bg);
  159. bg.SetActive(false);
  160. }
  161. else
  162. {
  163. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName);
  164. if (obj == null) obj = Resources.Load("Prefab/" + assetName) as GameObject;
  165. dogu = GameObject.Instantiate(obj) as GameObject;
  166. dogu.transform.localPosition = Vector3.zero;
  167. MeshRenderer[] meshRenderers = dogu.GetComponentsInChildren<MeshRenderer>();
  168. for (int i = 0; i < meshRenderers.Length; i++)
  169. {
  170. if (meshRenderers[i] != null
  171. && meshRenderers[i].gameObject.name.ToLower().IndexOf("castshadow") < 0
  172. )
  173. {
  174. meshRenderers[i].shadowCastingMode = ShadowCastingMode.Off;
  175. }
  176. }
  177. Collider collider = dogu.transform.GetComponent<Collider>();
  178. if (collider != null) collider.enabled = false;
  179. foreach (Transform transform in dogu.transform)
  180. {
  181. collider = transform.GetComponent<Collider>();
  182. if (collider != null)
  183. {
  184. collider.enabled = false;
  185. }
  186. }
  187. #region particle system experiment
  188. // if (asset.StartsWith("Particle/"))
  189. // {
  190. // ParticleSystem particleSystem = go.GetComponent<ParticleSystem>();
  191. // if (particleSystem != null)
  192. // {
  193. // ParticleSystem.MainModule main;
  194. // main = particleSystem.main;
  195. // main.loop = true;
  196. // main.duration = Mathf.Infinity;
  197. // ParticleSystem[] particleSystems = particleSystem.GetComponents<ParticleSystem>();
  198. // foreach (ParticleSystem part in particleSystems)
  199. // {
  200. // ParticleSystem.EmissionModule emissionModule = part.emission;
  201. // ParticleSystem.Burst[] bursts = new ParticleSystem.Burst[emissionModule.burstCount];
  202. // emissionModule.GetBursts(bursts);
  203. // for (int i = 0; i < bursts.Length; i++)
  204. // {
  205. // bursts[i].cycleCount = Int32.MaxValue;
  206. // }
  207. // emissionModule.SetBursts(bursts);
  208. // main = part.main;
  209. // main.loop = true;
  210. // main.duration = Mathf.Infinity;
  211. // }
  212. // }
  213. // }
  214. #endregion
  215. }
  216. if (dogu != null)
  217. {
  218. // TODO: Figure out why some props aren't centred properly
  219. // Doesn't happen in MM but even after copy pasting the code, it doesn't work :/
  220. GameObject deploymentObject = GetDeploymentObject();
  221. GameObject finalDogu = new GameObject();
  222. dogu.transform.localScale = doguScale;
  223. dogu.transform.SetParent(finalDogu.transform, true);
  224. finalDogu.transform.SetParent(deploymentObject.transform, false);
  225. finalDogu.transform.position = doguPosition;
  226. finalDogu.name = doguName;
  227. GameObject dragPoint = BaseDrag.MakeDragPoint(
  228. PrimitiveType.Cube, Vector3.one * 0.12f, BaseDrag.LightBlue
  229. );
  230. DragDogu dragDogu = dragPoint.AddComponent<DragDogu>();
  231. dragDogu.Initialize(finalDogu);
  232. dragDogu.Delete += (s, a) => DeleteDogu();
  233. dragDogu.SetDragProp(showGizmos, false, false);
  234. doguList.Add(dragDogu);
  235. dragDogu.DragPointScale = dragDogu.BaseScale * (CubeSmall ? 0.4f : 1f);
  236. }
  237. else
  238. {
  239. Debug.LogError($"Could not spawn object '{assetName}'");
  240. }
  241. }
  242. private void DeleteDogu()
  243. {
  244. doguList.RemoveAll(dragDogu =>
  245. {
  246. if (dragDogu.DeleteMe)
  247. {
  248. GameObject.Destroy(dragDogu.gameObject);
  249. return true;
  250. }
  251. return false;
  252. }
  253. );
  254. }
  255. private void OnCubeSmall(object sender, EventArgs args)
  256. {
  257. foreach (DragDogu dogu in doguList)
  258. {
  259. dogu.DragPointScale = dogu.BaseScale * (CubeSmall ? 0.4f : 1f);
  260. }
  261. }
  262. }
  263. }