Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

565 lines
23 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. #if UNITY_2018_3_OR_NEWER
  4. using System;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using UnityEditor.PackageManager.Requests;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Reflection;
  11. namespace AmplifyShaderEditor
  12. {
  13. public enum ASESRPVersions
  14. {
  15. ASE_SRP_3_0_0 = 030000,
  16. ASE_SRP_3_1_0 = 030100,
  17. ASE_SRP_3_3_0 = 030300,
  18. ASE_SRP_4_1_0 = 040100,
  19. ASE_SRP_4_2_0 = 040200,
  20. ASE_SRP_4_3_0 = 040300,
  21. ASE_SRP_4_6_0 = 040600,
  22. ASE_SRP_4_8_0 = 040800,
  23. ASE_SRP_4_9_0 = 040900,
  24. ASE_SRP_4_10_0 = 041000,
  25. ASE_SRP_5_7_2 = 050702,
  26. ASE_SRP_5_8_2 = 050802,
  27. ASE_SRP_5_9_0 = 050900,
  28. ASE_SRP_5_10_0 = 051000,
  29. ASE_SRP_5_13_0 = 051300,
  30. ASE_SRP_5_16_1 = 051601,
  31. ASE_SRP_6_9_0 = 060900,
  32. ASE_SRP_6_9_1 = 060901,
  33. ASE_SRP_6_9_2 = 060902,
  34. ASE_SRP_7_0_1 = 070001,
  35. ASE_SRP_7_1_1 = 070101,
  36. ASE_SRP_7_1_2 = 070102,
  37. ASE_SRP_7_1_5 = 070105,
  38. ASE_SRP_7_1_6 = 070106,
  39. ASE_SRP_7_1_7 = 070107,
  40. ASE_SRP_7_1_8 = 070108,
  41. ASE_SRP_7_2_0 = 070200,
  42. ASE_SRP_7_2_1 = 070201,
  43. ASE_SRP_7_3_1 = 070301,
  44. ASE_SRP_7_4_1 = 070401,
  45. ASE_SRP_7_4_2 = 070402,
  46. ASE_SRP_7_4_3 = 070403,
  47. ASE_SRP_8_2_0 = 080200,
  48. ASE_SRP_RECENT = 999999
  49. }
  50. public enum ASEImportState
  51. {
  52. None,
  53. Lightweight,
  54. HD,
  55. Both
  56. }
  57. public static class AssetDatabaseEX
  58. {
  59. private static System.Type type = null;
  60. public static System.Type Type { get { return ( type == null ) ? type = System.Type.GetType( "UnityEditor.AssetDatabase, UnityEditor" ) : type; } }
  61. public static void ImportPackageImmediately( string packagePath )
  62. {
  63. AssetDatabaseEX.Type.InvokeMember( "ImportPackageImmediately", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { packagePath } );
  64. }
  65. }
  66. [Serializable]
  67. public static class ASEPackageManagerHelper
  68. {
  69. private static string LightweightNewVersionDetected = "A new Lighweight RP version was detected and new templates are being imported.\n" +
  70. "Please hit the Update button on your ASE canvas to recompile your shader under the newest version.";
  71. private static string HDNewVersionDetected = "A new HD RP version was detected and new templates are being imported.\n" +
  72. "Please hit the Update button on your ASE canvas to recompile your shader under the newest version.";
  73. private static string HDPackageId = "com.unity.render-pipelines.high-definition";
  74. private static string LWPackageId = "com.unity.render-pipelines.lightweight";
  75. private static string UniversalPackageId = "com.unity.render-pipelines.universal";
  76. private static string HDEditorPrefsId = "ASEHDEditorPrefsId";
  77. private static string LWEditorPrefsId = "ASELightweigthEditorPrefsId ";
  78. private static string URPTemplateVersion = "ASEURPtemplate" + Application.productName;
  79. private static string HDRPTemplateVersion = "ASEHDRPtemplate" + Application.productName;
  80. private static string SPKeywordFormat = "ASE_SRP_VERSION {0}";
  81. private static ListRequest m_packageListRequest = null;
  82. private static UnityEditor.PackageManager.PackageInfo m_lwPackageInfo;
  83. private static UnityEditor.PackageManager.PackageInfo m_hdPackageInfo;
  84. // V4.8.0 and bellow
  85. // HD
  86. private static readonly string[] GetNormalWSFunc =
  87. {
  88. "inline void GetNormalWS( FragInputs input, float3 normalTS, out float3 normalWS, float3 doubleSidedConstants )\n",
  89. "{\n",
  90. "\tGetNormalWS( input, normalTS, normalWS );\n",
  91. "}\n"
  92. };
  93. // v4.6.0 and below
  94. private static readonly string[] BuildWordTangentFunc =
  95. {
  96. "float3x3 BuildWorldToTangent(float4 tangentWS, float3 normalWS)\n",
  97. "{\n",
  98. "\tfloat3 unnormalizedNormalWS = normalWS;\n",
  99. "\tfloat renormFactor = 1.0 / length(unnormalizedNormalWS);\n",
  100. "\tfloat3x3 worldToTangent = CreateWorldToTangent(unnormalizedNormalWS, tangentWS.xyz, tangentWS.w > 0.0 ? 1.0 : -1.0);\n",
  101. "\tworldToTangent[0] = worldToTangent[0] * renormFactor;\n",
  102. "\tworldToTangent[1] = worldToTangent[1] * renormFactor;\n",
  103. "\tworldToTangent[2] = worldToTangent[2] * renormFactor;\n",
  104. "\treturn worldToTangent;\n",
  105. "}\n"
  106. };
  107. private static bool m_requireUpdateList = false;
  108. private static ASEImportState m_importingPackage = ASEImportState.None;
  109. private static ASESRPVersions m_currentHDVersion = ASESRPVersions.ASE_SRP_RECENT;
  110. private static ASESRPVersions m_currentLWVersion = ASESRPVersions.ASE_SRP_RECENT;
  111. private static int m_urpTemplateVersion = 11;
  112. private static int m_hdrpTemplateVersion = 8;
  113. private static Dictionary<string, ASESRPVersions> m_srpVersionConverter = new Dictionary<string, ASESRPVersions>()
  114. {
  115. {"3.0.0-preview", ASESRPVersions.ASE_SRP_3_0_0},
  116. {"3.1.0-preview", ASESRPVersions.ASE_SRP_3_1_0},
  117. {"3.3.0-preview", ASESRPVersions.ASE_SRP_3_3_0},
  118. {"4.1.0-preview", ASESRPVersions.ASE_SRP_4_1_0},
  119. {"4.2.0-preview", ASESRPVersions.ASE_SRP_4_2_0},
  120. {"4.3.0-preview", ASESRPVersions.ASE_SRP_4_3_0},
  121. {"4.6.0-preview", ASESRPVersions.ASE_SRP_4_6_0},
  122. {"4.8.0-preview", ASESRPVersions.ASE_SRP_4_8_0},
  123. {"4.9.0-preview", ASESRPVersions.ASE_SRP_4_9_0},
  124. {"4.10.0-preview", ASESRPVersions.ASE_SRP_4_10_0},
  125. {"5.7.2-preview", ASESRPVersions.ASE_SRP_5_7_2},
  126. {"5.7.2", ASESRPVersions.ASE_SRP_5_7_2},
  127. {"5.8.2-preview", ASESRPVersions.ASE_SRP_5_8_2},
  128. {"5.8.2", ASESRPVersions.ASE_SRP_5_8_2},
  129. {"5.9.0-preview", ASESRPVersions.ASE_SRP_5_9_0},
  130. {"5.9.0", ASESRPVersions.ASE_SRP_5_9_0},
  131. {"5.10.0-preview", ASESRPVersions.ASE_SRP_5_10_0},
  132. {"5.10.0", ASESRPVersions.ASE_SRP_5_10_0},
  133. {"5.13.0-preview", ASESRPVersions.ASE_SRP_5_13_0},
  134. {"5.13.0", ASESRPVersions.ASE_SRP_5_13_0},
  135. {"5.16.1-preview", ASESRPVersions.ASE_SRP_5_16_1},
  136. {"5.16.1", ASESRPVersions.ASE_SRP_5_16_1},
  137. {"6.9.0", ASESRPVersions.ASE_SRP_6_9_0},
  138. {"6.9.0-preview", ASESRPVersions.ASE_SRP_6_9_0},
  139. {"6.9.1", ASESRPVersions.ASE_SRP_6_9_1},
  140. {"6.9.1-preview", ASESRPVersions.ASE_SRP_6_9_1},
  141. {"6.9.2", ASESRPVersions.ASE_SRP_6_9_2},
  142. {"6.9.2-preview", ASESRPVersions.ASE_SRP_6_9_2},
  143. {"7.0.1", ASESRPVersions.ASE_SRP_7_0_1},
  144. {"7.0.1-preview", ASESRPVersions.ASE_SRP_7_0_1},
  145. {"7.1.1", ASESRPVersions.ASE_SRP_7_1_1},
  146. {"7.1.1-preview", ASESRPVersions.ASE_SRP_7_1_1},
  147. {"7.1.2", ASESRPVersions.ASE_SRP_7_1_2},
  148. {"7.1.2-preview", ASESRPVersions.ASE_SRP_7_1_2},
  149. {"7.1.5", ASESRPVersions.ASE_SRP_7_1_5},
  150. {"7.1.5-preview", ASESRPVersions.ASE_SRP_7_1_5},
  151. {"7.1.6", ASESRPVersions.ASE_SRP_7_1_6},
  152. {"7.1.6-preview", ASESRPVersions.ASE_SRP_7_1_6},
  153. {"7.1.7", ASESRPVersions.ASE_SRP_7_1_7},
  154. {"7.1.7-preview", ASESRPVersions.ASE_SRP_7_1_7},
  155. {"7.1.8", ASESRPVersions.ASE_SRP_7_1_8},
  156. {"7.1.8-preview", ASESRPVersions.ASE_SRP_7_1_8},
  157. {"7.2.0", ASESRPVersions.ASE_SRP_7_2_0},
  158. {"7.2.0-preview", ASESRPVersions.ASE_SRP_7_2_0},
  159. {"7.2.1", ASESRPVersions.ASE_SRP_7_2_1},
  160. {"7.2.1-preview", ASESRPVersions.ASE_SRP_7_2_1},
  161. {"7.3.1", ASESRPVersions.ASE_SRP_7_3_1},
  162. {"7.3.1-preview", ASESRPVersions.ASE_SRP_7_3_1},
  163. {"7.4.1", ASESRPVersions.ASE_SRP_7_4_1},
  164. {"7.4.1-preview", ASESRPVersions.ASE_SRP_7_4_1},
  165. {"7.4.2", ASESRPVersions.ASE_SRP_7_4_2},
  166. {"7.4.2-preview", ASESRPVersions.ASE_SRP_7_4_2},
  167. {"7.4.3", ASESRPVersions.ASE_SRP_7_4_3},
  168. {"7.4.3-preview", ASESRPVersions.ASE_SRP_7_4_3},
  169. {"8.2.0", ASESRPVersions.ASE_SRP_8_2_0},
  170. {"8.2.0-preview", ASESRPVersions.ASE_SRP_8_2_0},
  171. };
  172. private static Dictionary<ASESRPVersions, string> m_srpToASEPackageLW = new Dictionary<ASESRPVersions, string>()
  173. {
  174. {ASESRPVersions.ASE_SRP_3_0_0, "b53d2f3b156ff104f90d4d7693d769c8"},
  175. {ASESRPVersions.ASE_SRP_3_1_0, "b53d2f3b156ff104f90d4d7693d769c8"},
  176. {ASESRPVersions.ASE_SRP_3_3_0, "b53d2f3b156ff104f90d4d7693d769c8"},
  177. {ASESRPVersions.ASE_SRP_4_1_0, "3e8eabcfae1e5aa4397de89fedeb48db"},
  178. {ASESRPVersions.ASE_SRP_4_2_0, "3e8eabcfae1e5aa4397de89fedeb48db"},
  179. {ASESRPVersions.ASE_SRP_4_3_0, "3e8eabcfae1e5aa4397de89fedeb48db"},
  180. {ASESRPVersions.ASE_SRP_4_6_0, "3e8eabcfae1e5aa4397de89fedeb48db"},
  181. {ASESRPVersions.ASE_SRP_4_8_0, "3e8eabcfae1e5aa4397de89fedeb48db"},
  182. {ASESRPVersions.ASE_SRP_4_9_0, "3e8eabcfae1e5aa4397de89fedeb48db"},
  183. {ASESRPVersions.ASE_SRP_4_10_0, "3e8eabcfae1e5aa4397de89fedeb48db"},
  184. {ASESRPVersions.ASE_SRP_5_7_2, "4c816894a3147d343891060451241bfe"},
  185. {ASESRPVersions.ASE_SRP_5_8_2, "4c816894a3147d343891060451241bfe"},
  186. {ASESRPVersions.ASE_SRP_5_9_0, "4c816894a3147d343891060451241bfe"},
  187. {ASESRPVersions.ASE_SRP_5_10_0, "4c816894a3147d343891060451241bfe"},
  188. {ASESRPVersions.ASE_SRP_5_13_0, "4c816894a3147d343891060451241bfe"},
  189. {ASESRPVersions.ASE_SRP_5_16_1, "4c816894a3147d343891060451241bfe"},
  190. {ASESRPVersions.ASE_SRP_6_9_0, "4c816894a3147d343891060451241bfe"},
  191. {ASESRPVersions.ASE_SRP_6_9_1, "4c816894a3147d343891060451241bfe"},
  192. {ASESRPVersions.ASE_SRP_6_9_2, "4c816894a3147d343891060451241bfe"},
  193. {ASESRPVersions.ASE_SRP_7_0_1, "57fcea0ed8b5eb347923c4c21fa31b57"},
  194. {ASESRPVersions.ASE_SRP_7_1_1, "57fcea0ed8b5eb347923c4c21fa31b57"},
  195. {ASESRPVersions.ASE_SRP_7_1_2, "57fcea0ed8b5eb347923c4c21fa31b57"},
  196. {ASESRPVersions.ASE_SRP_7_1_5, "57fcea0ed8b5eb347923c4c21fa31b57"},
  197. {ASESRPVersions.ASE_SRP_7_1_6, "57fcea0ed8b5eb347923c4c21fa31b57"},
  198. {ASESRPVersions.ASE_SRP_7_1_7, "57fcea0ed8b5eb347923c4c21fa31b57"},
  199. {ASESRPVersions.ASE_SRP_7_1_8, "57fcea0ed8b5eb347923c4c21fa31b57"},
  200. {ASESRPVersions.ASE_SRP_7_2_0, "57fcea0ed8b5eb347923c4c21fa31b57"},
  201. {ASESRPVersions.ASE_SRP_7_2_1, "57fcea0ed8b5eb347923c4c21fa31b57"},
  202. {ASESRPVersions.ASE_SRP_7_3_1, "57fcea0ed8b5eb347923c4c21fa31b57"},
  203. {ASESRPVersions.ASE_SRP_7_4_1, "57fcea0ed8b5eb347923c4c21fa31b57"},
  204. {ASESRPVersions.ASE_SRP_7_4_2, "57fcea0ed8b5eb347923c4c21fa31b57"},
  205. {ASESRPVersions.ASE_SRP_7_4_3, "57fcea0ed8b5eb347923c4c21fa31b57"},
  206. {ASESRPVersions.ASE_SRP_8_2_0, "57fcea0ed8b5eb347923c4c21fa31b57"},
  207. {ASESRPVersions.ASE_SRP_RECENT, "57fcea0ed8b5eb347923c4c21fa31b57"}
  208. };
  209. private static Dictionary<ASESRPVersions, string> m_srpToASEPackageHD = new Dictionary<ASESRPVersions, string>()
  210. {
  211. {ASESRPVersions.ASE_SRP_3_0_0, "4dc1afbcc68875c4780502f5e6b80158"},
  212. {ASESRPVersions.ASE_SRP_3_1_0, "4dc1afbcc68875c4780502f5e6b80158"},
  213. {ASESRPVersions.ASE_SRP_3_3_0, "4dc1afbcc68875c4780502f5e6b80158"},
  214. {ASESRPVersions.ASE_SRP_4_1_0, "5d615bf612f33364e96fb9fd2959ae9c"},
  215. {ASESRPVersions.ASE_SRP_4_2_0, "5d615bf612f33364e96fb9fd2959ae9c"},
  216. {ASESRPVersions.ASE_SRP_4_3_0, "5d615bf612f33364e96fb9fd2959ae9c"},
  217. {ASESRPVersions.ASE_SRP_4_6_0, "5d615bf612f33364e96fb9fd2959ae9c"},
  218. {ASESRPVersions.ASE_SRP_4_8_0, "5d615bf612f33364e96fb9fd2959ae9c"},
  219. {ASESRPVersions.ASE_SRP_4_9_0, "5d615bf612f33364e96fb9fd2959ae9c"},
  220. {ASESRPVersions.ASE_SRP_4_10_0, "5d615bf612f33364e96fb9fd2959ae9c"},
  221. {ASESRPVersions.ASE_SRP_5_7_2, "f51b7b861facbc3429fcc5f1f6f91183"},
  222. {ASESRPVersions.ASE_SRP_5_8_2, "2d7fe4f7c19e90f41b893bc01fc17230"},
  223. {ASESRPVersions.ASE_SRP_5_9_0, "2d7fe4f7c19e90f41b893bc01fc17230"},
  224. {ASESRPVersions.ASE_SRP_5_10_0, "2d7fe4f7c19e90f41b893bc01fc17230"},
  225. {ASESRPVersions.ASE_SRP_5_13_0, "2d7fe4f7c19e90f41b893bc01fc17230"},
  226. {ASESRPVersions.ASE_SRP_5_16_1, "2d7fe4f7c19e90f41b893bc01fc17230"},
  227. {ASESRPVersions.ASE_SRP_6_9_0, "e137dba02f4d0f542ab09dcedea27314"},
  228. {ASESRPVersions.ASE_SRP_6_9_1, "e137dba02f4d0f542ab09dcedea27314"},
  229. {ASESRPVersions.ASE_SRP_6_9_2, "e137dba02f4d0f542ab09dcedea27314"},
  230. {ASESRPVersions.ASE_SRP_7_0_1, "e137dba02f4d0f542ab09dcedea27314"},
  231. {ASESRPVersions.ASE_SRP_7_1_1, "e137dba02f4d0f542ab09dcedea27314"},
  232. {ASESRPVersions.ASE_SRP_7_1_2, "e137dba02f4d0f542ab09dcedea27314"},
  233. {ASESRPVersions.ASE_SRP_7_1_5, "e137dba02f4d0f542ab09dcedea27314"},
  234. {ASESRPVersions.ASE_SRP_7_1_6, "e137dba02f4d0f542ab09dcedea27314"},
  235. {ASESRPVersions.ASE_SRP_7_1_7, "e137dba02f4d0f542ab09dcedea27314"},
  236. {ASESRPVersions.ASE_SRP_7_1_8, "9a5e61a8b3421b944863d0946e32da0a"},
  237. {ASESRPVersions.ASE_SRP_7_2_0, "9a5e61a8b3421b944863d0946e32da0a"},
  238. {ASESRPVersions.ASE_SRP_7_2_1, "9a5e61a8b3421b944863d0946e32da0a"},
  239. {ASESRPVersions.ASE_SRP_7_3_1, "9a5e61a8b3421b944863d0946e32da0a"},
  240. {ASESRPVersions.ASE_SRP_7_4_1, "9a5e61a8b3421b944863d0946e32da0a"},
  241. {ASESRPVersions.ASE_SRP_7_4_2, "9a5e61a8b3421b944863d0946e32da0a"},
  242. {ASESRPVersions.ASE_SRP_7_4_3, "9a5e61a8b3421b944863d0946e32da0a"},
  243. {ASESRPVersions.ASE_SRP_8_2_0, "9a5e61a8b3421b944863d0946e32da0a"},
  244. {ASESRPVersions.ASE_SRP_RECENT, "9a5e61a8b3421b944863d0946e32da0a"}
  245. };
  246. private static Shader m_lateShader;
  247. private static Material m_lateMaterial;
  248. private static AmplifyShaderFunction m_lateShaderFunction;
  249. public static void RequestInfo()
  250. {
  251. if( !m_requireUpdateList && m_importingPackage == ASEImportState.None )
  252. {
  253. m_requireUpdateList = true;
  254. m_packageListRequest = UnityEditor.PackageManager.Client.List( true );
  255. }
  256. }
  257. static void FailedPackageImport( string packageName, string errorMessage )
  258. {
  259. FinishImporter();
  260. }
  261. static void CancelledPackageImport( string packageName )
  262. {
  263. FinishImporter();
  264. }
  265. static void CompletedPackageImport( string packageName )
  266. {
  267. FinishImporter();
  268. }
  269. public static void StartImporting( string packagePath )
  270. {
  271. if( !Preferences.GlobalAutoSRP )
  272. {
  273. m_importingPackage = ASEImportState.None;
  274. return;
  275. }
  276. AssetDatabase.importPackageCancelled += CancelledPackageImport;
  277. AssetDatabase.importPackageCompleted += CompletedPackageImport;
  278. AssetDatabase.importPackageFailed += FailedPackageImport;
  279. AssetDatabase.ImportPackage( packagePath, false );
  280. //AssetDatabaseEX.ImportPackageImmediately( packagePath );
  281. }
  282. public static void FinishImporter()
  283. {
  284. m_importingPackage = ASEImportState.None;
  285. AssetDatabase.importPackageCancelled -= CancelledPackageImport;
  286. AssetDatabase.importPackageCompleted -= CompletedPackageImport;
  287. AssetDatabase.importPackageFailed -= FailedPackageImport;
  288. }
  289. public static void SetupLateShader( Shader shader )
  290. {
  291. RequestInfo();
  292. m_lateShader = shader;
  293. EditorApplication.delayCall += LateShaderOpener;
  294. }
  295. public static void LateShaderOpener()
  296. {
  297. Preferences.LoadDefaults();
  298. Update();
  299. if( IsProcessing )
  300. {
  301. EditorApplication.delayCall += LateShaderOpener;
  302. }
  303. else
  304. {
  305. AmplifyShaderEditorWindow.ConvertShaderToASE( m_lateShader );
  306. m_lateShader = null;
  307. }
  308. }
  309. public static void SetupLateMaterial( Material material )
  310. {
  311. RequestInfo();
  312. m_lateMaterial = material;
  313. EditorApplication.delayCall += LateMaterialOpener;
  314. }
  315. public static void LateMaterialOpener()
  316. {
  317. Preferences.LoadDefaults();
  318. Update();
  319. if( IsProcessing )
  320. {
  321. EditorApplication.delayCall += LateMaterialOpener;
  322. }
  323. else
  324. {
  325. AmplifyShaderEditorWindow.LoadMaterialToASE( m_lateMaterial );
  326. m_lateMaterial = null;
  327. }
  328. }
  329. public static void SetupLateShaderFunction( AmplifyShaderFunction shaderFunction )
  330. {
  331. RequestInfo();
  332. m_lateShaderFunction = shaderFunction;
  333. EditorApplication.delayCall += LateShaderFunctionOpener;
  334. }
  335. public static void LateShaderFunctionOpener()
  336. {
  337. Preferences.LoadDefaults();
  338. Update();
  339. if( IsProcessing )
  340. {
  341. EditorApplication.delayCall += LateShaderFunctionOpener;
  342. }
  343. else
  344. {
  345. AmplifyShaderEditorWindow.LoadShaderFunctionToASE( m_lateShaderFunction, false );
  346. m_lateShaderFunction = null;
  347. }
  348. }
  349. public static void Update()
  350. {
  351. //if( m_lwPackageInfo != null )
  352. //{
  353. // if( m_srpVersionConverter[ m_lwPackageInfo.version ] != m_currentLWVersion )
  354. // {
  355. // m_currentLWVersion = m_srpVersionConverter[ m_lwPackageInfo.version ];
  356. // EditorPrefs.SetInt( LWEditorPrefsId, (int)m_currentLWVersion );
  357. // m_importingPackage = ASEImportState.Lightweight;
  358. // string packagePath = AssetDatabase.GUIDToAssetPath( m_srpToASEPackageLW[ m_currentLWVersion ] );
  359. // StartImporting( packagePath );
  360. // }
  361. //}
  362. //if( m_hdPackageInfo != null )
  363. //{
  364. // if( m_srpVersionConverter[ m_hdPackageInfo.version ] != m_currentHDVersion )
  365. // {
  366. // m_currentHDVersion = m_srpVersionConverter[ m_hdPackageInfo.version ];
  367. // EditorPrefs.SetInt( HDEditorPrefsId, (int)m_currentHDVersion );
  368. // m_importingPackage = ASEImportState.HD;
  369. // string packagePath = AssetDatabase.GUIDToAssetPath( m_srpToASEPackageHD[ m_currentHDVersion ] );
  370. // StartImporting( packagePath );
  371. // }
  372. //}
  373. if( m_requireUpdateList && m_importingPackage == ASEImportState.None )
  374. {
  375. if( m_packageListRequest != null && m_packageListRequest.IsCompleted )
  376. {
  377. m_requireUpdateList = false;
  378. foreach( UnityEditor.PackageManager.PackageInfo pi in m_packageListRequest.Result )
  379. {
  380. if( pi.name.Equals( LWPackageId ) )
  381. {
  382. m_currentLWVersion = ASESRPVersions.ASE_SRP_RECENT;
  383. m_lwPackageInfo = pi;
  384. ASESRPVersions oldVersion = (ASESRPVersions)EditorPrefs.GetInt( LWEditorPrefsId );
  385. if( m_srpVersionConverter.ContainsKey( pi.version ) )
  386. {
  387. m_currentLWVersion = m_srpVersionConverter[ pi.version ];
  388. }
  389. else
  390. {
  391. m_currentLWVersion = ASESRPVersions.ASE_SRP_RECENT;
  392. }
  393. EditorPrefs.SetInt( LWEditorPrefsId, (int)m_currentLWVersion );
  394. bool foundNewVersion = oldVersion != m_currentLWVersion;
  395. if( !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.LightweigthPBRGUID ) ) ||
  396. !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.LightweigthUnlitGUID ) ) ||
  397. foundNewVersion
  398. )
  399. {
  400. if( foundNewVersion )
  401. Debug.Log( LightweightNewVersionDetected );
  402. m_importingPackage = ASEImportState.Lightweight;
  403. string guid = m_srpToASEPackageLW.ContainsKey( m_currentLWVersion ) ? m_srpToASEPackageLW[ m_currentLWVersion ] : m_srpToASEPackageLW[ ASESRPVersions.ASE_SRP_RECENT ];
  404. string packagePath = AssetDatabase.GUIDToAssetPath( guid );
  405. StartImporting( packagePath );
  406. }
  407. }
  408. if( pi.name.Equals( UniversalPackageId ) )
  409. {
  410. m_currentLWVersion = ASESRPVersions.ASE_SRP_RECENT;
  411. m_lwPackageInfo = pi;
  412. ASESRPVersions oldVersion = (ASESRPVersions)EditorPrefs.GetInt( LWEditorPrefsId );
  413. if( m_srpVersionConverter.ContainsKey( pi.version ) )
  414. {
  415. m_currentLWVersion = m_srpVersionConverter[ pi.version ];
  416. }
  417. else
  418. {
  419. m_currentLWVersion = ASESRPVersions.ASE_SRP_RECENT;
  420. }
  421. EditorPrefs.SetInt( LWEditorPrefsId, (int)m_currentLWVersion );
  422. bool foundNewVersion = oldVersion != m_currentLWVersion;
  423. int urpVersion = EditorPrefs.GetInt( URPTemplateVersion, m_urpTemplateVersion );
  424. if( urpVersion < m_urpTemplateVersion )
  425. foundNewVersion = true;
  426. EditorPrefs.SetInt( URPTemplateVersion, m_urpTemplateVersion );
  427. if( !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.UniversalPBRGUID ) ) ||
  428. !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.UniversalUnlitGUID ) ) ||
  429. foundNewVersion
  430. )
  431. {
  432. if( foundNewVersion )
  433. Debug.Log( LightweightNewVersionDetected );
  434. m_importingPackage = ASEImportState.Lightweight;
  435. string guid = m_srpToASEPackageLW.ContainsKey( m_currentLWVersion ) ? m_srpToASEPackageLW[ m_currentLWVersion ] : m_srpToASEPackageLW[ ASESRPVersions.ASE_SRP_RECENT ];
  436. string packagePath = AssetDatabase.GUIDToAssetPath( guid );
  437. StartImporting( packagePath );
  438. }
  439. }
  440. if( pi.name.Equals( HDPackageId ) )
  441. {
  442. m_currentHDVersion = ASESRPVersions.ASE_SRP_RECENT;
  443. m_hdPackageInfo = pi;
  444. ASESRPVersions oldVersion = (ASESRPVersions)EditorPrefs.GetInt( HDEditorPrefsId );
  445. if( m_srpVersionConverter.ContainsKey( pi.version ) )
  446. {
  447. m_currentHDVersion = m_srpVersionConverter[ pi.version ];
  448. }
  449. else
  450. {
  451. m_currentHDVersion = ASESRPVersions.ASE_SRP_RECENT;
  452. }
  453. EditorPrefs.SetInt( HDEditorPrefsId, (int)m_currentHDVersion );
  454. bool foundNewVersion = oldVersion != m_currentHDVersion;
  455. int hdrpVersion = EditorPrefs.GetInt( HDRPTemplateVersion, m_hdrpTemplateVersion );
  456. if( hdrpVersion < m_hdrpTemplateVersion )
  457. foundNewVersion = true;
  458. EditorPrefs.SetInt( HDRPTemplateVersion, m_hdrpTemplateVersion );
  459. #if UNITY_2019_3_OR_NEWER
  460. if( !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.HDNewLitGUID ) ) ||
  461. !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.HDNewPBRGUID ) ) ||
  462. !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.HDNewUnlitGUID ) ) ||
  463. #else
  464. if( !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.HDLitGUID ) ) ||
  465. !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.HDPBRGUID ) ) ||
  466. !File.Exists( AssetDatabase.GUIDToAssetPath( TemplatesManager.HDUnlitGUID ) ) ||
  467. #endif
  468. foundNewVersion
  469. )
  470. {
  471. if( foundNewVersion )
  472. Debug.Log( HDNewVersionDetected );
  473. m_importingPackage = m_importingPackage == ASEImportState.Lightweight ? ASEImportState.Both : ASEImportState.HD;
  474. string guid = m_srpToASEPackageHD.ContainsKey( m_currentHDVersion ) ? m_srpToASEPackageHD[ m_currentHDVersion ] : m_srpToASEPackageHD[ ASESRPVersions.ASE_SRP_RECENT ];
  475. string packagePath = AssetDatabase.GUIDToAssetPath( guid );
  476. StartImporting( packagePath );
  477. }
  478. }
  479. }
  480. }
  481. }
  482. }
  483. public static void SetSRPInfoOnDataCollector( ref MasterNodeDataCollector dataCollector )
  484. {
  485. Preferences.LoadDefaults();
  486. if( m_requireUpdateList )
  487. Update();
  488. if( dataCollector.CurrentSRPType == TemplateSRPType.HD )
  489. {
  490. dataCollector.AddToDirectives( string.Format( SPKeywordFormat, (int)m_currentHDVersion ) ,-1, AdditionalLineType.Define );
  491. if( m_currentHDVersion < ASESRPVersions.ASE_SRP_4_9_0 )
  492. {
  493. dataCollector.AddFunction( GetNormalWSFunc[ 0 ], GetNormalWSFunc, false );
  494. }
  495. if( m_currentHDVersion < ASESRPVersions.ASE_SRP_4_8_0 )
  496. {
  497. dataCollector.AddFunction( BuildWordTangentFunc[ 0 ], BuildWordTangentFunc, false );
  498. }
  499. }
  500. if( dataCollector.CurrentSRPType == TemplateSRPType.Lightweight )
  501. dataCollector.AddToDirectives( string.Format( SPKeywordFormat, (int)m_currentLWVersion ), -1, AdditionalLineType.Define );
  502. }
  503. public static ASESRPVersions CurrentHDVersion { get { return m_currentHDVersion; } }
  504. public static ASESRPVersions CurrentLWVersion { get { return m_currentLWVersion; } }
  505. public static bool CheckImporter { get { return m_importingPackage != ASEImportState.None; } }
  506. public static bool IsProcessing { get { return m_requireUpdateList && m_importingPackage == ASEImportState.None; } }
  507. }
  508. }
  509. #endif