PostProcessingBehaviour.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Rendering;
  4. namespace UnityEngine.PostProcessing
  5. {
  6. [ImageEffectAllowedInSceneView]
  7. [RequireComponent(typeof(Camera))]
  8. [DisallowMultipleComponent]
  9. [ExecuteInEditMode]
  10. [AddComponentMenu("Effects/Post-Processing Behaviour", -1)]
  11. public class PostProcessingBehaviour : MonoBehaviour
  12. {
  13. private void OnEnable()
  14. {
  15. this.m_CommandBuffers = new Dictionary<Type, KeyValuePair<CameraEvent, CommandBuffer>>();
  16. this.m_MaterialFactory = new MaterialFactory();
  17. this.m_RenderTextureFactory = new RenderTextureFactory();
  18. this.m_Context = new PostProcessingContext();
  19. this.m_Components = new List<PostProcessingComponentBase>();
  20. this.m_DebugViews = this.AddComponent<BuiltinDebugViewsComponent>(new BuiltinDebugViewsComponent());
  21. this.m_AmbientOcclusion = this.AddComponent<AmbientOcclusionComponent>(new AmbientOcclusionComponent());
  22. this.m_ScreenSpaceReflection = this.AddComponent<ScreenSpaceReflectionComponent>(new ScreenSpaceReflectionComponent());
  23. this.m_FogComponent = this.AddComponent<FogComponent>(new FogComponent());
  24. this.m_MotionBlur = this.AddComponent<MotionBlurComponent>(new MotionBlurComponent());
  25. this.m_Taa = this.AddComponent<TaaComponent>(new TaaComponent());
  26. this.m_EyeAdaptation = this.AddComponent<EyeAdaptationComponent>(new EyeAdaptationComponent());
  27. this.m_DepthOfField = this.AddComponent<DepthOfFieldComponent>(new DepthOfFieldComponent());
  28. this.m_Bloom = this.AddComponent<BloomComponent>(new BloomComponent());
  29. this.m_ChromaticAberration = this.AddComponent<ChromaticAberrationComponent>(new ChromaticAberrationComponent());
  30. this.m_ColorGrading = this.AddComponent<ColorGradingComponent>(new ColorGradingComponent());
  31. this.m_UserLut = this.AddComponent<UserLutComponent>(new UserLutComponent());
  32. this.m_Grain = this.AddComponent<GrainComponent>(new GrainComponent());
  33. this.m_Vignette = this.AddComponent<VignetteComponent>(new VignetteComponent());
  34. this.m_Dithering = this.AddComponent<DitheringComponent>(new DitheringComponent());
  35. this.m_Fxaa = this.AddComponent<FxaaComponent>(new FxaaComponent());
  36. this.m_ComponentStates = new Dictionary<PostProcessingComponentBase, bool>();
  37. foreach (PostProcessingComponentBase key in this.m_Components)
  38. {
  39. this.m_ComponentStates.Add(key, false);
  40. }
  41. base.useGUILayout = false;
  42. }
  43. private void OnPreCull()
  44. {
  45. this.m_Camera = base.GetComponent<Camera>();
  46. if (this.profile == null || this.m_Camera == null)
  47. {
  48. return;
  49. }
  50. PostProcessingContext postProcessingContext = this.m_Context.Reset();
  51. postProcessingContext.profile = this.profile;
  52. postProcessingContext.renderTextureFactory = this.m_RenderTextureFactory;
  53. postProcessingContext.materialFactory = this.m_MaterialFactory;
  54. postProcessingContext.camera = this.m_Camera;
  55. this.m_DebugViews.Init(postProcessingContext, this.profile.debugViews);
  56. this.m_AmbientOcclusion.Init(postProcessingContext, this.profile.ambientOcclusion);
  57. this.m_ScreenSpaceReflection.Init(postProcessingContext, this.profile.screenSpaceReflection);
  58. this.m_FogComponent.Init(postProcessingContext, this.profile.fog);
  59. this.m_MotionBlur.Init(postProcessingContext, this.profile.motionBlur);
  60. this.m_Taa.Init(postProcessingContext, this.profile.antialiasing);
  61. this.m_EyeAdaptation.Init(postProcessingContext, this.profile.eyeAdaptation);
  62. this.m_DepthOfField.Init(postProcessingContext, this.profile.depthOfField);
  63. this.m_Bloom.Init(postProcessingContext, this.profile.bloom);
  64. this.m_ChromaticAberration.Init(postProcessingContext, this.profile.chromaticAberration);
  65. this.m_ColorGrading.Init(postProcessingContext, this.profile.colorGrading);
  66. this.m_UserLut.Init(postProcessingContext, this.profile.userLut);
  67. this.m_Grain.Init(postProcessingContext, this.profile.grain);
  68. this.m_Vignette.Init(postProcessingContext, this.profile.vignette);
  69. this.m_Dithering.Init(postProcessingContext, this.profile.dithering);
  70. this.m_Fxaa.Init(postProcessingContext, this.profile.antialiasing);
  71. if (this.m_PreviousProfile != this.profile)
  72. {
  73. this.DisableComponents();
  74. this.m_PreviousProfile = this.profile;
  75. }
  76. this.CheckObservers();
  77. DepthTextureMode depthTextureMode = postProcessingContext.camera.depthTextureMode;
  78. foreach (PostProcessingComponentBase postProcessingComponentBase in this.m_Components)
  79. {
  80. if (postProcessingComponentBase.active)
  81. {
  82. depthTextureMode |= postProcessingComponentBase.GetCameraFlags();
  83. }
  84. }
  85. postProcessingContext.camera.depthTextureMode = depthTextureMode;
  86. if (!this.m_RenderingInSceneView && this.m_Taa.active && !this.profile.debugViews.willInterrupt)
  87. {
  88. this.m_Taa.SetProjectionMatrix(this.jitteredMatrixFunc);
  89. }
  90. }
  91. private void OnPreRender()
  92. {
  93. if (this.profile == null)
  94. {
  95. return;
  96. }
  97. this.TryExecuteCommandBuffer<BuiltinDebugViewsModel>(this.m_DebugViews);
  98. this.TryExecuteCommandBuffer<AmbientOcclusionModel>(this.m_AmbientOcclusion);
  99. this.TryExecuteCommandBuffer<ScreenSpaceReflectionModel>(this.m_ScreenSpaceReflection);
  100. this.TryExecuteCommandBuffer<FogModel>(this.m_FogComponent);
  101. if (!this.m_RenderingInSceneView)
  102. {
  103. this.TryExecuteCommandBuffer<MotionBlurModel>(this.m_MotionBlur);
  104. }
  105. }
  106. private void OnPostRender()
  107. {
  108. if (this.profile == null || this.m_Camera == null)
  109. {
  110. return;
  111. }
  112. if (!this.m_RenderingInSceneView && this.m_Taa.active && !this.profile.debugViews.willInterrupt)
  113. {
  114. this.m_Context.camera.ResetProjectionMatrix();
  115. }
  116. }
  117. private void OnRenderImage(RenderTexture source, RenderTexture destination)
  118. {
  119. if (this.profile == null || this.m_Camera == null)
  120. {
  121. Graphics.Blit(source, destination);
  122. return;
  123. }
  124. bool flag = false;
  125. bool active = this.m_Fxaa.active;
  126. bool flag2 = this.m_Taa.active && !this.m_RenderingInSceneView;
  127. bool flag3 = this.m_DepthOfField.active && !this.m_RenderingInSceneView;
  128. Material material = this.m_MaterialFactory.Get("Hidden/Post FX/Uber Shader");
  129. material.shaderKeywords = null;
  130. RenderTexture renderTexture = source;
  131. if (flag2)
  132. {
  133. RenderTexture renderTexture2 = this.m_RenderTextureFactory.Get(renderTexture);
  134. this.m_Taa.Render(renderTexture, renderTexture2);
  135. renderTexture = renderTexture2;
  136. }
  137. Texture texture = GraphicsUtils.whiteTexture;
  138. if (this.m_EyeAdaptation.active)
  139. {
  140. flag = true;
  141. texture = this.m_EyeAdaptation.Prepare(renderTexture, material);
  142. }
  143. material.SetTexture("_AutoExposure", texture);
  144. if (flag3)
  145. {
  146. flag = true;
  147. this.m_DepthOfField.Prepare(renderTexture, material, flag2, this.m_Taa.jitterVector, this.m_Taa.model.settings.taaSettings.motionBlending);
  148. }
  149. if (this.m_Bloom.active)
  150. {
  151. flag = true;
  152. this.m_Bloom.Prepare(renderTexture, material, texture);
  153. }
  154. flag |= this.TryPrepareUberImageEffect<ChromaticAberrationModel>(this.m_ChromaticAberration, material);
  155. flag |= this.TryPrepareUberImageEffect<ColorGradingModel>(this.m_ColorGrading, material);
  156. flag |= this.TryPrepareUberImageEffect<VignetteModel>(this.m_Vignette, material);
  157. flag |= this.TryPrepareUberImageEffect<UserLutModel>(this.m_UserLut, material);
  158. Material material2 = (!active) ? null : this.m_MaterialFactory.Get("Hidden/Post FX/FXAA");
  159. if (active)
  160. {
  161. material2.shaderKeywords = null;
  162. this.TryPrepareUberImageEffect<GrainModel>(this.m_Grain, material2);
  163. this.TryPrepareUberImageEffect<DitheringModel>(this.m_Dithering, material2);
  164. if (flag)
  165. {
  166. RenderTexture renderTexture3 = this.m_RenderTextureFactory.Get(renderTexture);
  167. Graphics.Blit(renderTexture, renderTexture3, material, 0);
  168. renderTexture = renderTexture3;
  169. }
  170. this.m_Fxaa.Render(renderTexture, destination);
  171. }
  172. else
  173. {
  174. flag |= this.TryPrepareUberImageEffect<GrainModel>(this.m_Grain, material);
  175. flag |= this.TryPrepareUberImageEffect<DitheringModel>(this.m_Dithering, material);
  176. if (flag)
  177. {
  178. if (!GraphicsUtils.isLinearColorSpace)
  179. {
  180. material.EnableKeyword("UNITY_COLORSPACE_GAMMA");
  181. }
  182. Graphics.Blit(renderTexture, destination, material, 0);
  183. }
  184. }
  185. if (!flag && !active)
  186. {
  187. Graphics.Blit(renderTexture, destination);
  188. }
  189. this.m_RenderTextureFactory.ReleaseAll();
  190. }
  191. private void OnGUI()
  192. {
  193. if (Event.current.type != EventType.Repaint)
  194. {
  195. return;
  196. }
  197. if (this.profile == null || this.m_Camera == null)
  198. {
  199. return;
  200. }
  201. if (this.m_EyeAdaptation.active && this.profile.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.EyeAdaptation))
  202. {
  203. this.m_EyeAdaptation.OnGUI();
  204. }
  205. else if (this.m_ColorGrading.active && this.profile.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.LogLut))
  206. {
  207. this.m_ColorGrading.OnGUI();
  208. }
  209. else if (this.m_UserLut.active && this.profile.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.UserLut))
  210. {
  211. this.m_UserLut.OnGUI();
  212. }
  213. }
  214. private void OnDisable()
  215. {
  216. foreach (KeyValuePair<CameraEvent, CommandBuffer> keyValuePair in this.m_CommandBuffers.Values)
  217. {
  218. this.m_Camera.RemoveCommandBuffer(keyValuePair.Key, keyValuePair.Value);
  219. keyValuePair.Value.Dispose();
  220. }
  221. this.m_CommandBuffers.Clear();
  222. if (this.profile != null)
  223. {
  224. this.DisableComponents();
  225. }
  226. this.m_Components.Clear();
  227. this.m_MaterialFactory.Dispose();
  228. this.m_RenderTextureFactory.Dispose();
  229. GraphicsUtils.Dispose();
  230. }
  231. public void ResetTemporalEffects()
  232. {
  233. this.m_Taa.ResetHistory();
  234. this.m_MotionBlur.ResetHistory();
  235. this.m_EyeAdaptation.ResetHistory();
  236. }
  237. private void CheckObservers()
  238. {
  239. foreach (KeyValuePair<PostProcessingComponentBase, bool> keyValuePair in this.m_ComponentStates)
  240. {
  241. PostProcessingComponentBase key = keyValuePair.Key;
  242. bool enabled = key.GetModel().enabled;
  243. if (enabled != keyValuePair.Value)
  244. {
  245. if (enabled)
  246. {
  247. this.m_ComponentsToEnable.Add(key);
  248. }
  249. else
  250. {
  251. this.m_ComponentsToDisable.Add(key);
  252. }
  253. }
  254. }
  255. for (int i = 0; i < this.m_ComponentsToDisable.Count; i++)
  256. {
  257. PostProcessingComponentBase postProcessingComponentBase = this.m_ComponentsToDisable[i];
  258. this.m_ComponentStates[postProcessingComponentBase] = false;
  259. postProcessingComponentBase.OnDisable();
  260. }
  261. for (int j = 0; j < this.m_ComponentsToEnable.Count; j++)
  262. {
  263. PostProcessingComponentBase postProcessingComponentBase2 = this.m_ComponentsToEnable[j];
  264. this.m_ComponentStates[postProcessingComponentBase2] = true;
  265. postProcessingComponentBase2.OnEnable();
  266. }
  267. this.m_ComponentsToDisable.Clear();
  268. this.m_ComponentsToEnable.Clear();
  269. }
  270. private void DisableComponents()
  271. {
  272. foreach (PostProcessingComponentBase postProcessingComponentBase in this.m_Components)
  273. {
  274. PostProcessingModel model = postProcessingComponentBase.GetModel();
  275. if (model != null && model.enabled)
  276. {
  277. postProcessingComponentBase.OnDisable();
  278. }
  279. }
  280. }
  281. private CommandBuffer AddCommandBuffer<T>(CameraEvent evt, string name) where T : PostProcessingModel
  282. {
  283. CommandBuffer value = new CommandBuffer
  284. {
  285. name = name
  286. };
  287. KeyValuePair<CameraEvent, CommandBuffer> value2 = new KeyValuePair<CameraEvent, CommandBuffer>(evt, value);
  288. this.m_CommandBuffers.Add(typeof(T), value2);
  289. this.m_Camera.AddCommandBuffer(evt, value2.Value);
  290. return value2.Value;
  291. }
  292. private void RemoveCommandBuffer<T>() where T : PostProcessingModel
  293. {
  294. Type typeFromHandle = typeof(T);
  295. KeyValuePair<CameraEvent, CommandBuffer> keyValuePair;
  296. if (!this.m_CommandBuffers.TryGetValue(typeFromHandle, out keyValuePair))
  297. {
  298. return;
  299. }
  300. this.m_Camera.RemoveCommandBuffer(keyValuePair.Key, keyValuePair.Value);
  301. this.m_CommandBuffers.Remove(typeFromHandle);
  302. keyValuePair.Value.Dispose();
  303. }
  304. private CommandBuffer GetCommandBuffer<T>(CameraEvent evt, string name) where T : PostProcessingModel
  305. {
  306. KeyValuePair<CameraEvent, CommandBuffer> keyValuePair;
  307. CommandBuffer result;
  308. if (!this.m_CommandBuffers.TryGetValue(typeof(T), out keyValuePair))
  309. {
  310. result = this.AddCommandBuffer<T>(evt, name);
  311. }
  312. else if (keyValuePair.Key != evt)
  313. {
  314. this.RemoveCommandBuffer<T>();
  315. result = this.AddCommandBuffer<T>(evt, name);
  316. }
  317. else
  318. {
  319. result = keyValuePair.Value;
  320. }
  321. return result;
  322. }
  323. private void TryExecuteCommandBuffer<T>(PostProcessingComponentCommandBuffer<T> component) where T : PostProcessingModel
  324. {
  325. if (component.active)
  326. {
  327. CommandBuffer commandBuffer = this.GetCommandBuffer<T>(component.GetCameraEvent(), component.GetName());
  328. commandBuffer.Clear();
  329. component.PopulateCommandBuffer(commandBuffer);
  330. }
  331. else
  332. {
  333. this.RemoveCommandBuffer<T>();
  334. }
  335. }
  336. private bool TryPrepareUberImageEffect<T>(PostProcessingComponentRenderTexture<T> component, Material material) where T : PostProcessingModel
  337. {
  338. if (!component.active)
  339. {
  340. return false;
  341. }
  342. component.Prepare(material);
  343. return true;
  344. }
  345. private T AddComponent<T>(T component) where T : PostProcessingComponentBase
  346. {
  347. this.m_Components.Add(component);
  348. return component;
  349. }
  350. public PostProcessingProfile profile;
  351. public Func<Vector2, Matrix4x4> jitteredMatrixFunc;
  352. private Dictionary<Type, KeyValuePair<CameraEvent, CommandBuffer>> m_CommandBuffers;
  353. private List<PostProcessingComponentBase> m_Components;
  354. private Dictionary<PostProcessingComponentBase, bool> m_ComponentStates;
  355. private MaterialFactory m_MaterialFactory;
  356. private RenderTextureFactory m_RenderTextureFactory;
  357. private PostProcessingContext m_Context;
  358. private Camera m_Camera;
  359. private PostProcessingProfile m_PreviousProfile;
  360. private bool m_RenderingInSceneView;
  361. private BuiltinDebugViewsComponent m_DebugViews;
  362. private AmbientOcclusionComponent m_AmbientOcclusion;
  363. private ScreenSpaceReflectionComponent m_ScreenSpaceReflection;
  364. private FogComponent m_FogComponent;
  365. private MotionBlurComponent m_MotionBlur;
  366. private TaaComponent m_Taa;
  367. private EyeAdaptationComponent m_EyeAdaptation;
  368. private DepthOfFieldComponent m_DepthOfField;
  369. private BloomComponent m_Bloom;
  370. private ChromaticAberrationComponent m_ChromaticAberration;
  371. private ColorGradingComponent m_ColorGrading;
  372. private UserLutComponent m_UserLut;
  373. private GrainComponent m_Grain;
  374. private VignetteComponent m_Vignette;
  375. private DitheringComponent m_Dithering;
  376. private FxaaComponent m_Fxaa;
  377. private List<PostProcessingComponentBase> m_ComponentsToEnable = new List<PostProcessingComponentBase>();
  378. private List<PostProcessingComponentBase> m_ComponentsToDisable = new List<PostProcessingComponentBase>();
  379. }
  380. }