fxvFogAssetConfig.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace FXV.Internal
  7. {
  8. [DefaultExecutionOrder(1000)]
  9. #if UNITY_EDITOR
  10. [UnityEditor.InitializeOnLoad]
  11. #endif
  12. public class fxvFogAssetConfig
  13. {
  14. internal enum Pipeline
  15. {
  16. BuiltIn = 0,
  17. URP = 1,
  18. HDRP = 2
  19. }
  20. internal static string AssetPath = null;
  21. internal static Pipeline ActiveRenderPipeline = Pipeline.BuiltIn;
  22. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  23. internal static void UpdatePipelineConfig()
  24. {
  25. #if UNITY_EDITOR
  26. var g = AssetDatabase.FindAssets($"t:Script {nameof(fxvFogAssetConfig)}");
  27. string scriptPath = AssetDatabase.GUIDToAssetPath(g[0]);
  28. AssetPath = Path.GetDirectoryName(scriptPath);
  29. AssetPath = Path.GetDirectoryName(AssetPath);
  30. AssetPath = Path.GetDirectoryName(AssetPath);
  31. #endif
  32. #if FX_DEBUG_LOGS
  33. Debug.Log("UpdatePipelineConfig AssetPath " + AssetPath);
  34. #endif
  35. OnActiveRenderPipelineChanged();
  36. #if UNITY_2021_1_OR_NEWER
  37. RenderPipelineManager.activeRenderPipelineTypeChanged += OnActiveRenderPipelineChanged;
  38. #endif
  39. }
  40. static fxvFogAssetConfig()
  41. {
  42. UpdatePipelineConfig();
  43. }
  44. private static void OnActiveRenderPipelineChanged()
  45. {
  46. var currentRP = GraphicsSettings.currentRenderPipeline;
  47. if (currentRP == null)
  48. {
  49. ActiveRenderPipeline = Pipeline.BuiltIn;
  50. OnActiveRenderPipelineChanged(ActiveRenderPipeline);
  51. return;
  52. }
  53. var curPipeline = currentRP.GetType().ToString().ToLower();
  54. if (curPipeline.Contains("universal"))
  55. {
  56. ActiveRenderPipeline = Pipeline.URP;
  57. }
  58. else if (curPipeline.Contains("high definition") || curPipeline.Contains("highdefinition"))
  59. {
  60. ActiveRenderPipeline = Pipeline.HDRP;
  61. }
  62. OnActiveRenderPipelineChanged(ActiveRenderPipeline);
  63. }
  64. public static void UpdateShadersForActiveRenderPipeline()
  65. {
  66. UpdateShaders(ActiveRenderPipeline);
  67. }
  68. private static void UpdateShaders(Pipeline newPipeline)
  69. {
  70. #if UNITY_EDITOR
  71. string[] shaderGuids = AssetDatabase.FindAssets("t:shader", new[] { AssetPath });
  72. foreach (string guid in shaderGuids)
  73. {
  74. string assetPath = AssetDatabase.GUIDToAssetPath(guid);
  75. var lines = File.ReadAllLines(assetPath);
  76. bool changed = false;
  77. for (int i = 0; i < lines.Length; ++i)
  78. {
  79. if (newPipeline == Pipeline.BuiltIn)
  80. {
  81. if (lines[i].Contains("#define FXV_VOLUMEFOG_URP"))
  82. {
  83. lines[i] = lines[i].Replace("#define FXV_VOLUMEFOG_URP", "#define FXV_VOLUMEFOG_BUILTIN");
  84. changed = true;
  85. }
  86. }
  87. else if (newPipeline == Pipeline.URP)
  88. {
  89. if (lines[i].Contains("#define FXV_VOLUMEFOG_BUILTIN"))
  90. {
  91. lines[i] = lines[i].Replace("#define FXV_VOLUMEFOG_BUILTIN", "#define FXV_VOLUMEFOG_URP");
  92. changed = true;
  93. }
  94. }
  95. }
  96. if (changed)
  97. {
  98. #if FX_DEBUG_LOGS
  99. Debug.Log("changed shader to new RP " + assetPath);
  100. #endif
  101. File.WriteAllLines(assetPath, lines);
  102. Shader shader = AssetDatabase.LoadAssetAtPath<Shader>(assetPath);
  103. if (shader)
  104. {
  105. EditorUtility.SetDirty(shader);
  106. AssetDatabase.SaveAssetIfDirty(shader);
  107. AssetDatabase.ImportAsset(assetPath);
  108. }
  109. }
  110. }
  111. #endif
  112. }
  113. private static void OnActiveRenderPipelineChanged(Pipeline newPipeline)
  114. {
  115. #if FX_DEBUG_LOGS
  116. Debug.Log("OnActiveRenderPipelineChanged newPipeline " + newPipeline);
  117. #endif
  118. #if UNITY_EDITOR
  119. #if UNITY_2022_2_OR_NEWER
  120. VolumeFog[] fogObjects = GameObject.FindObjectsByType<VolumeFog>(FindObjectsInactive.Include, FindObjectsSortMode.None);
  121. #else
  122. VolumeFog[] fogObjects = GameObject.FindObjectsOfType<VolumeFog>(true);
  123. #endif
  124. foreach (VolumeFog obj in fogObjects)
  125. {
  126. VolumeFog.SetupFogMaterial(obj);
  127. }
  128. UpdateShaders(newPipeline);
  129. string[] matGuids = AssetDatabase.FindAssets("t:material", new[] { AssetPath });
  130. foreach (string guid in matGuids)
  131. {
  132. string assetPath = AssetDatabase.GUIDToAssetPath(guid);
  133. Material mat = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
  134. if (mat == null || mat.shader == null)
  135. {
  136. #if FX_DEBUG_LOGS
  137. Debug.Log("material or shader is null " + assetPath);
  138. #endif
  139. continue;
  140. }
  141. #if FX_DEBUG_LOGS
  142. Debug.Log("processing material " + assetPath);
  143. #endif
  144. if (newPipeline == Pipeline.BuiltIn)
  145. {
  146. if (mat.shader.name == "Universal Render Pipeline/Lit" || mat.shader.name == "HDRP/Lit")
  147. {
  148. mat.shader = Shader.Find("Standard");
  149. EditorUtility.SetDirty(mat);
  150. AssetDatabase.SaveAssetIfDirty(mat);
  151. }
  152. }
  153. else if (newPipeline == Pipeline.URP)
  154. {
  155. if (mat.shader.name == "Standard" || mat.shader.name == "HDRP/Lit")
  156. {
  157. Color mainColor = mat.GetColor("_Color");
  158. Texture mainTex = GetMainTexture(mat);
  159. Texture metallicGlossTex = GetMetallicGlossTexture(mat);//.GetTexture("_MetallicGlossMap");
  160. float glossScale = mat.GetFloat("_GlossMapScale");
  161. float gloss = mat.GetFloat("_Glossiness");
  162. mat.shader = Shader.Find("Universal Render Pipeline/Lit");
  163. mat.SetColor("_BaseColor", mainColor);
  164. mat.SetTexture("_BaseMap", mainTex);
  165. mat.SetTexture("_MetallicGlossMap", metallicGlossTex);
  166. mat.SetFloat("_Smoothness", glossScale);
  167. mat.SetFloat("_Glossiness", gloss);
  168. EditorUtility.SetDirty(mat);
  169. AssetDatabase.SaveAssetIfDirty(mat);
  170. }
  171. }
  172. else if (newPipeline == Pipeline.HDRP)
  173. {
  174. if (mat.shader.name == "Standard" || mat.shader.name == "Universal Render Pipeline/Lit")
  175. {
  176. Texture mainTex = mat.mainTexture;
  177. #if FX_DEBUG_LOGS
  178. Debug.Log(" mainTex " + mainTex);
  179. #endif
  180. if (mainTex == null)
  181. {
  182. if (mat.HasTexture("_BaseMap"))
  183. {
  184. mainTex = mat.GetTexture("_BaseMap");
  185. }
  186. if (mainTex == null)
  187. {
  188. if (mat.HasTexture("_MainTex"))
  189. {
  190. mainTex = mat.GetTexture("_MainTex");
  191. }
  192. }
  193. }
  194. mat.shader = Shader.Find("HDRP/Lit");
  195. EditorUtility.SetDirty(mat);
  196. AssetDatabase.SaveAssetIfDirty(mat);
  197. mat.SetTexture("_BaseColorMap", mainTex);
  198. EditorUtility.SetDirty(mat);
  199. AssetDatabase.SaveAssetIfDirty(mat);
  200. }
  201. }
  202. }
  203. #endif
  204. }
  205. #if UNITY_EDITOR
  206. public static Texture GetMainTexture(Material mat)
  207. {
  208. Texture mainTex = mat.mainTexture;
  209. #if FX_DEBUG_LOGS
  210. Debug.Log(" mainTex " + mainTex);
  211. #endif
  212. if (mainTex == null)
  213. {
  214. if (mat.HasTexture("_BaseMap"))
  215. {
  216. mainTex = mat.GetTexture("_BaseMap");
  217. }
  218. if (mainTex == null)
  219. {
  220. if (mat.HasTexture("_MainTex"))
  221. {
  222. mainTex = mat.GetTexture("_MainTex");
  223. }
  224. }
  225. }
  226. return mainTex;
  227. }
  228. public static Texture GetMetallicGlossTexture(Material mat)
  229. {
  230. Texture retTex = null;
  231. if (mat.HasTexture("_MetallicGlossMap"))
  232. {
  233. retTex = mat.GetTexture("_MetallicGlossMap");
  234. }
  235. #if FX_DEBUG_LOGS
  236. Debug.Log(" metallicTex " + mainTex);
  237. #endif
  238. /* if (retTex == null)
  239. {
  240. if (mat.HasTexture("_BaseMap"))
  241. {
  242. retTex = mat.GetTexture("_BaseMap");
  243. }
  244. if (retTex == null)
  245. {
  246. if (mat.HasTexture("_MainTex"))
  247. {
  248. retTex = mat.GetTexture("_MainTex");
  249. }
  250. }
  251. }*/
  252. return retTex;
  253. }
  254. #endif
  255. }
  256. }