VFXResources.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEditor.PackageManager;
  6. using UnityEngine.Rendering;
  7. namespace UnityEditor.VFX
  8. {
  9. class VFXResources : ScriptableObject
  10. {
  11. public static Values defaultResources
  12. {
  13. get
  14. {
  15. if (s_Values == null)
  16. {
  17. Initialize();
  18. }
  19. return s_Values;
  20. }
  21. }
  22. private static bool m_Searched; // the instance has been searched and it is null
  23. private static VFXResources s_Instance;
  24. private static Values s_Values;
  25. static void LoadUserResourcesIfNeeded()
  26. {
  27. if (s_Instance == null && (!m_Searched || !object.ReferenceEquals(s_Instance, null)))
  28. // if instance is null and either it has never been searched or it was found but it has been destroyed since last time
  29. {
  30. foreach (var guid in AssetDatabase.FindAssets("t:VFXResources"))
  31. {
  32. s_Instance = AssetDatabase.LoadAssetAtPath<VFXResources>(AssetDatabase.GUIDToAssetPath(guid));
  33. if (s_Instance != null)
  34. {
  35. return;
  36. }
  37. }
  38. s_Instance = null;
  39. m_Searched = true;
  40. }
  41. }
  42. void OnEnable()
  43. {
  44. if (AssetDatabase.FindAssets("t:VFXResources").Length > 1)
  45. Debug.LogError("Having more than one VFXResources in your project is unsupported");
  46. s_Instance = this;
  47. m_Searched = false;
  48. }
  49. public class Values
  50. {
  51. public AnimationCurve animationCurve
  52. {
  53. get
  54. {
  55. LoadUserResourcesIfNeeded();
  56. if (s_Instance != null)
  57. return s_Instance.animationCurve;
  58. return defaultAnimationCurve;
  59. }
  60. }
  61. public Gradient gradient
  62. {
  63. get
  64. {
  65. LoadUserResourcesIfNeeded();
  66. if (s_Instance != null)
  67. return s_Instance.gradient;
  68. return defaultGradient;
  69. }
  70. }
  71. public Gradient gradientMapRamp
  72. {
  73. get
  74. {
  75. LoadUserResourcesIfNeeded();
  76. if (s_Instance != null)
  77. return s_Instance.gradientMapRamp;
  78. return defaultGradientMapRamp;
  79. }
  80. }
  81. public Shader shader
  82. {
  83. get
  84. {
  85. LoadUserResourcesIfNeeded();
  86. if (s_Instance != null && s_Instance.shader != null)
  87. return s_Instance.shader;
  88. return defaultShader;
  89. }
  90. }
  91. public Texture2D particleTexture
  92. {
  93. get
  94. {
  95. LoadUserResourcesIfNeeded();
  96. if (s_Instance != null && s_Instance.particleTexture != null)
  97. return s_Instance.particleTexture;
  98. return defaultParticleTexture;
  99. }
  100. }
  101. public Texture2D noiseTexture
  102. {
  103. get
  104. {
  105. LoadUserResourcesIfNeeded();
  106. if (s_Instance != null && s_Instance.noiseTexture != null)
  107. return s_Instance.noiseTexture;
  108. return defaultNoiseTexture;
  109. }
  110. }
  111. public Texture3D vectorField
  112. {
  113. get
  114. {
  115. LoadUserResourcesIfNeeded();
  116. if (s_Instance != null && s_Instance.vectorField != null)
  117. return s_Instance.vectorField;
  118. return defaultVectorField;
  119. }
  120. }
  121. public Texture3D signedDistanceField
  122. {
  123. get
  124. {
  125. LoadUserResourcesIfNeeded();
  126. if (s_Instance != null && s_Instance.signedDistanceField != null)
  127. return s_Instance.signedDistanceField;
  128. return defaultSignedDistanceField;
  129. }
  130. }
  131. public Mesh mesh
  132. {
  133. get
  134. {
  135. LoadUserResourcesIfNeeded();
  136. if (s_Instance != null && s_Instance.mesh != null)
  137. return s_Instance.mesh;
  138. return defaultMesh;
  139. }
  140. }
  141. public string userTemplateDirectory
  142. {
  143. get
  144. {
  145. LoadUserResourcesIfNeeded();
  146. if (s_Instance != null)
  147. return s_Instance.userTemplateDirectory;
  148. return "";
  149. }
  150. set
  151. {
  152. if (s_Instance == null)
  153. s_Instance = FindObjectOfType<VFXResources>();
  154. if (s_Instance != null)
  155. s_Instance.userTemplateDirectory = value;
  156. }
  157. }
  158. }
  159. private static string defaultPath { get { return VisualEffectGraphPackageInfo.assetPackagePath + "/"; } }
  160. private static T SafeLoadAssetAtPath<T>(string assetPath) where T : Object
  161. {
  162. var asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
  163. if (asset == null)
  164. {
  165. AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
  166. asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
  167. }
  168. return asset;
  169. }
  170. private static void Initialize()
  171. {
  172. s_Values = new Values();
  173. defaultShader = Shader.Find("Hidden/Default StaticMeshOutput");
  174. defaultAnimationCurve = new AnimationCurve(new Keyframe[]
  175. {
  176. new Keyframe(0.0f, 0.0f, 0.0f, 0.0f),
  177. new Keyframe(0.25f, 0.25f, 0.0f, 0.0f),
  178. new Keyframe(1.0f, 0.0f, 0.0f, 0.0f),
  179. });
  180. defaultGradient = new Gradient();
  181. defaultGradient.colorKeys = new GradientColorKey[]
  182. {
  183. new GradientColorKey(Color.white, 0.0f),
  184. new GradientColorKey(Color.gray, 1.0f),
  185. };
  186. defaultGradient.alphaKeys = new GradientAlphaKey[]
  187. {
  188. new GradientAlphaKey(0.0f, 0.0f),
  189. new GradientAlphaKey(1.0f, 0.1f),
  190. new GradientAlphaKey(0.8f, 0.8f),
  191. new GradientAlphaKey(0.0f, 1.0f),
  192. };
  193. defaultGradientMapRamp = new Gradient();
  194. defaultGradientMapRamp.colorKeys = new GradientColorKey[]
  195. {
  196. new GradientColorKey(new Color(0.0f, 0.0f, 0.0f), 0.0f),
  197. new GradientColorKey(new Color(0.75f, 0.15f, 0.0f), 0.3f),
  198. new GradientColorKey(new Color(1.25f, 0.56f, 0.12f), 0.5f),
  199. new GradientColorKey(new Color(3.5f, 2.0f, 0.5f), 0.7f),
  200. new GradientColorKey(new Color(4.0f, 3.5f, 1.2f), 0.9f),
  201. new GradientColorKey(new Color(12.0f, 10.0f, 2.5f), 1.0f),
  202. };
  203. defaultGradientMapRamp.alphaKeys = new GradientAlphaKey[]
  204. {
  205. new GradientAlphaKey(0.0f, 0.0f),
  206. new GradientAlphaKey(1.0f, 1.0f),
  207. };
  208. }
  209. static Texture2D m_DefaultParticleTexture;
  210. public static Texture2D defaultParticleTexture
  211. {
  212. get
  213. {
  214. if (m_DefaultParticleTexture == null)
  215. m_DefaultParticleTexture = SafeLoadAssetAtPath<Texture2D>(defaultPath + "Textures/DefaultParticle.tga");
  216. return m_DefaultParticleTexture;
  217. }
  218. }
  219. static Texture2D m_DefaultNoiseTexture;
  220. public static Texture2D defaultNoiseTexture
  221. {
  222. get
  223. {
  224. if (m_DefaultNoiseTexture == null)
  225. m_DefaultNoiseTexture = SafeLoadAssetAtPath<Texture2D>(defaultPath + "Textures/Noise.tga");
  226. return m_DefaultNoiseTexture;
  227. }
  228. }
  229. static Texture3D m_DefaultVectorField;
  230. public static Texture3D defaultVectorField
  231. {
  232. get
  233. {
  234. if (m_DefaultVectorField == null)
  235. m_DefaultVectorField = SafeLoadAssetAtPath<Texture3D>(defaultPath + "Textures/vectorfield.asset");
  236. return m_DefaultVectorField;
  237. }
  238. }
  239. static Texture3D m_DefaultSignedDistanceField;
  240. public static Texture3D defaultSignedDistanceField
  241. {
  242. get
  243. {
  244. if (m_DefaultSignedDistanceField == null)
  245. m_DefaultSignedDistanceField = SafeLoadAssetAtPath<Texture3D>(defaultPath + "Textures/SignedDistanceField.asset");
  246. return m_DefaultSignedDistanceField;
  247. }
  248. }
  249. static Mesh m_DefaultMesh;
  250. static public Mesh defaultMesh
  251. {
  252. get
  253. {
  254. if (m_DefaultMesh == null)
  255. m_DefaultMesh = Resources.GetBuiltinResource<Mesh>("New-Capsule.fbx");
  256. return m_DefaultMesh;
  257. }
  258. }
  259. private static ComputeShader m_SdfNormalsComputeShader;
  260. public static ComputeShader sdfNormalsComputeShader
  261. {
  262. get
  263. {
  264. if (m_SdfNormalsComputeShader == null)
  265. m_SdfNormalsComputeShader = SafeLoadAssetAtPath<ComputeShader>(defaultPath + "Shaders/SDFBaker/GenSdfNormals.compute");
  266. return m_SdfNormalsComputeShader;
  267. }
  268. }
  269. private static ComputeShader m_SdfRayMapComputeShader;
  270. public static ComputeShader sdfRayMapComputeShader
  271. {
  272. get
  273. {
  274. if (m_SdfRayMapComputeShader == null)
  275. m_SdfRayMapComputeShader = SafeLoadAssetAtPath<ComputeShader>(defaultPath + "Shaders/SDFBaker/GenSdfRayMap.compute");
  276. return m_SdfRayMapComputeShader;
  277. }
  278. }
  279. private static Shader m_RayMapVoxelizeShader;
  280. public static Shader rayMapVoxelizeShader
  281. {
  282. get
  283. {
  284. if (m_RayMapVoxelizeShader == null)
  285. m_RayMapVoxelizeShader = SafeLoadAssetAtPath<Shader>(defaultPath + "Shaders/SDFBaker/RayMapVoxelize.shader");
  286. return m_RayMapVoxelizeShader;
  287. }
  288. }
  289. [SerializeField]
  290. AnimationCurve animationCurve = null;
  291. [SerializeField]
  292. Gradient gradient = null;
  293. [SerializeField]
  294. Gradient gradientMapRamp = null;
  295. [SerializeField]
  296. Shader shader = null;
  297. [SerializeField]
  298. Texture2D particleTexture = null;
  299. [SerializeField]
  300. Texture2D noiseTexture = null;
  301. [SerializeField]
  302. Texture3D vectorField = null;
  303. [SerializeField]
  304. Texture3D signedDistanceField = null;
  305. [SerializeField]
  306. Mesh mesh = null;
  307. [SerializeField]
  308. string userTemplateDirectory = "";
  309. static AnimationCurve defaultAnimationCurve;
  310. static Gradient defaultGradient;
  311. static Gradient defaultGradientMapRamp;
  312. static Shader defaultShader;
  313. public void SetDefaults()
  314. {
  315. if (s_Values == null)
  316. Initialize();
  317. animationCurve = defaultAnimationCurve;
  318. gradient = defaultGradient;
  319. gradientMapRamp = defaultGradientMapRamp;
  320. }
  321. }
  322. }