EnviroHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Enviro
  5. {
  6. public static class EnviroHelper
  7. {
  8. public static Vector3 PingPong (Vector3 value)
  9. {
  10. Vector3 result = value;
  11. if (result.x > 1f)
  12. result.x = -1f;
  13. else if (result.x < -1f)
  14. result.x = 1f;
  15. if (result.y > 1f)
  16. result.y = -1f;
  17. else if (result.y < -1f)
  18. result.y = 1f;
  19. if (result.z > 1f)
  20. result.z = -1f;
  21. else if (result.z < -1f)
  22. result.z = 1f;
  23. return result;
  24. }
  25. public static float Remap(float value, float from1, float to1, float from2, float to2)
  26. {
  27. return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
  28. }
  29. // Checks if Enviro Effects should render on this camera for URP/HDRP
  30. public static bool CanRenderOnCamera (Camera cam)
  31. {
  32. if(EnviroManager.instance != null)
  33. {
  34. if(cam.cameraType == CameraType.SceneView || cam.cameraType == CameraType.Reflection)
  35. return true;
  36. if(cam == EnviroManager.instance.Camera)
  37. return true;
  38. for (int i = 0; i < EnviroManager.instance.Cameras.Count; i++)
  39. {
  40. if(cam == EnviroManager.instance.Cameras[i].camera)
  41. return true;
  42. }
  43. return false;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. ///Get the Light component from Enviro Directional light if lighting module is activated or any other active directional light
  51. public static Light GetDirectionalLight ()
  52. {
  53. Light result = null;
  54. if(EnviroManager.instance.Lighting != null)
  55. {
  56. if(EnviroManager.instance.Lighting.Settings.lightingMode == EnviroLighting.LightingMode.Single)
  57. {
  58. if(EnviroManager.instance.Objects.directionalLight != null)
  59. result = EnviroManager.instance.Objects.directionalLight;
  60. }
  61. else
  62. {
  63. if(!EnviroManager.instance.isNight)
  64. {
  65. if(EnviroManager.instance.Objects.directionalLight != null)
  66. result = EnviroManager.instance.Objects.directionalLight;
  67. }
  68. else
  69. {
  70. if(EnviroManager.instance.Objects.additionalDirectionalLight != null)
  71. result = EnviroManager.instance.Objects.additionalDirectionalLight;
  72. }
  73. }
  74. }
  75. else
  76. {
  77. //Find other Directional Lights in scene
  78. Light[] results = GameObject.FindObjectsOfType<Light>();
  79. for(int i = 0; i < results.Length; i++)
  80. {
  81. if(results[i].type == LightType.Directional && results[i].gameObject.activeSelf && results[i].enabled)
  82. {
  83. result = results[i];
  84. break;
  85. }
  86. }
  87. }
  88. return result;
  89. }
  90. public static void CreateBuffer(ref ComputeBuffer buffer, int count, int stride)
  91. {
  92. if (buffer != null && buffer.count == count)
  93. return;
  94. if(buffer != null)
  95. {
  96. buffer.Release();
  97. buffer = null;
  98. }
  99. if (count <= 0)
  100. return;
  101. buffer = new ComputeBuffer(count, stride);
  102. }
  103. public static void ReleaseComputeBuffer(ref ComputeBuffer buffer)
  104. {
  105. if(buffer != null)
  106. buffer.Release();
  107. buffer = null;
  108. }
  109. public static Vector4 GetProjectionExtents(Camera camera)
  110. {
  111. return GetProjectionExtents(camera, 0.0f, 0.0f);
  112. }
  113. public static Vector4 GetProjectionExtents(Camera camera, float texelOffsetX, float texelOffsetY)
  114. {
  115. if (camera == null)
  116. return Vector4.zero;
  117. float oneExtentY = camera.orthographic ? camera.orthographicSize : Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView);
  118. float oneExtentX = oneExtentY * camera.aspect;
  119. float texelSizeX = oneExtentX / (0.5f * camera.pixelWidth);
  120. float texelSizeY = oneExtentY / (0.5f * camera.pixelHeight);
  121. float oneJitterX = texelSizeX * texelOffsetX;
  122. float oneJitterY = texelSizeY * texelOffsetY;
  123. return new Vector4(oneExtentX, oneExtentY, oneJitterX, oneJitterY);
  124. }
  125. public static Vector4 GetProjectionExtents(Camera camera, Camera.StereoscopicEye eye)
  126. {
  127. return GetProjectionExtents(camera, eye, 0.0f, 0.0f);
  128. }
  129. public static Vector4 GetProjectionExtents(Camera camera, Camera.StereoscopicEye eye, float texelOffsetX, float texelOffsetY)
  130. {
  131. Matrix4x4 inv = Matrix4x4.Inverse(camera.GetStereoProjectionMatrix(eye));
  132. Vector3 ray00 = inv.MultiplyPoint3x4(new Vector3(-1.0f, -1.0f, 0.95f));
  133. Vector3 ray11 = inv.MultiplyPoint3x4(new Vector3(1.0f, 1.0f, 0.95f));
  134. ray00 /= -ray00.z;
  135. ray11 /= -ray11.z;
  136. float oneExtentX = 0.5f * (ray11.x - ray00.x);
  137. float oneExtentY = 0.5f * (ray11.y - ray00.y);
  138. float texelSizeX = oneExtentX / (0.5f * camera.pixelWidth);
  139. float texelSizeY = oneExtentY / (0.5f * camera.pixelHeight);
  140. float oneJitterX = 0.5f * (ray11.x + ray00.x) + texelSizeX * texelOffsetX;
  141. float oneJitterY = 0.5f * (ray11.y + ray00.y) + texelSizeY * texelOffsetY;
  142. return new Vector4(oneExtentX, oneExtentY, oneJitterX, oneJitterY);
  143. }
  144. //Find the default profile.
  145. public static EnviroModule GetDefaultPreset(string name)
  146. {
  147. #if UNITY_EDITOR
  148. string[] assets = UnityEditor.AssetDatabase.FindAssets(name, null);
  149. for (int idx = 0; idx < assets.Length; idx++)
  150. {
  151. string path = UnityEditor.AssetDatabase.GUIDToAssetPath(assets[idx]);
  152. if (path.Contains(".asset"))
  153. {
  154. return UnityEditor.AssetDatabase.LoadAssetAtPath<EnviroModule>(path);
  155. }
  156. }
  157. #endif
  158. return null;
  159. }
  160. #if ENVIRO_HDRP
  161. public static UnityEngine.Rendering.VolumeProfile GetDefaultSkyAndFogProfile(string name)
  162. {
  163. #if UNITY_EDITOR
  164. string[] assets = UnityEditor.AssetDatabase.FindAssets(name, null);
  165. for (int idx = 0; idx < assets.Length; idx++)
  166. {
  167. string path = UnityEditor.AssetDatabase.GUIDToAssetPath(assets[idx]);
  168. if (path.Contains(name + ".asset"))
  169. {
  170. return UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.Rendering.VolumeProfile>(path);
  171. }
  172. }
  173. #endif
  174. return null;
  175. }
  176. #endif
  177. }
  178. }