Tools.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace VolumetricFogAndMist2 {
  6. public static class Tools {
  7. public static Color ColorBlack = Color.black;
  8. public static void CheckCamera(ref Camera cam) {
  9. if (cam != null) return;
  10. cam = Camera.main;
  11. if (cam == null) {
  12. Camera[] cameras = Object.FindObjectsOfType<Camera>();
  13. for (int k = 0; k < cameras.Length; k++) {
  14. if (cameras[k].isActiveAndEnabled && cameras[k].gameObject.activeInHierarchy) {
  15. cam = cameras[k];
  16. return;
  17. }
  18. }
  19. }
  20. }
  21. public static VolumetricFogManager CheckMainManager() {
  22. VolumetricFogManager fogManager = VolumetricFogManager.GetManagerIfExists();
  23. if (fogManager == null) {
  24. GameObject go = new GameObject();
  25. fogManager = go.AddComponent<VolumetricFogManager>();
  26. go.name = fogManager.managerName;
  27. #if UNITY_EDITOR
  28. if (UnityEditor.SceneManagement.StageUtility.GetCurrentStage() != UnityEditor.SceneManagement.StageUtility.GetMainStage()) {
  29. UnityEditor.SceneManagement.StageUtility.PlaceGameObjectInCurrentStage(go);
  30. }
  31. Undo.RegisterCreatedObjectUndo(go, "Create Volumetric Fog Manager");
  32. #endif
  33. }
  34. return fogManager;
  35. }
  36. public static void CheckManager<T>(ref T manager) where T : Component {
  37. if (manager == null) {
  38. VolumetricFogManager root = CheckMainManager();
  39. if (root == null) return;
  40. manager = root.GetComponentInChildren<T>(true);
  41. if (manager == null) {
  42. GameObject o = new GameObject();
  43. o.transform.SetParent(root.transform, false);
  44. manager = o.AddComponent<T>();
  45. o.name = ((IVolumetricFogManager)manager).managerName;
  46. #if UNITY_EDITOR
  47. Undo.RegisterCreatedObjectUndo(o, "Create Fog Manager");
  48. #endif
  49. }
  50. }
  51. }
  52. static Mesh _fullScreenMesh;
  53. public static Mesh fullscreenMesh {
  54. get {
  55. if (_fullScreenMesh != null) {
  56. return _fullScreenMesh;
  57. }
  58. float num = 1f;
  59. float num2 = 0f;
  60. Mesh val = new Mesh();
  61. _fullScreenMesh = val;
  62. _fullScreenMesh.SetVertices(new List<Vector3> {
  63. new Vector3 (-1f, -1f, 0f),
  64. new Vector3 (-1f, 1f, 0f),
  65. new Vector3 (1f, -1f, 0f),
  66. new Vector3 (1f, 1f, 0f)
  67. });
  68. _fullScreenMesh.SetUVs(0, new List<Vector2> {
  69. new Vector2 (0f, num2),
  70. new Vector2 (0f, num),
  71. new Vector2 (1f, num2),
  72. new Vector2 (1f, num)
  73. });
  74. _fullScreenMesh.SetIndices(new int[6] { 0, 1, 2, 2, 1, 3 }, (MeshTopology)0, 0, false);
  75. _fullScreenMesh.UploadMeshData(true);
  76. return _fullScreenMesh;
  77. }
  78. }
  79. }
  80. }