Advertisement
remo9211

Base Post Processing file

May 23rd, 2024
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // 要求附加在有Camera组件的GameObject上
  6. [RequireComponent(typeof(Camera))]
  7. public class BasePP : MonoBehaviour
  8. {
  9. // 计算着色器的引用
  10. public ComputeShader shader = null;
  11.  
  12. // 计算着色器中主内核函数的名称
  13. protected string kernelName = "CSMain";
  14.  
  15. // 纹理的尺寸
  16. protected Vector2Int texSize = new Vector2Int(0,0);
  17. // 计算着色器的线程组大小
  18. protected Vector2Int groupSize = new Vector2Int();
  19. // 指向相机组件的引用
  20. protected Camera thisCamera;
  21.  
  22. // 输出用的渲染纹理
  23. protected RenderTexture output = null;
  24. // 原始图像的渲染纹理
  25. protected RenderTexture renderedSource = null;
  26.  
  27. // 计算着色器内核的句柄
  28. protected int kernelHandle = -1;
  29. // 初始化标志
  30. protected bool init = false;
  31.  
  32. // 初始化后处理效果的方法
  33. protected virtual void Init()
  34. {
  35. // 检查是否支持计算着色器
  36. if (!SystemInfo.supportsComputeShaders)
  37. {
  38. Debug.LogError("It seems your target Hardware does not support Compute Shaders.");
  39. return;
  40. }
  41.  
  42. // 检查着色器是否赋值
  43. if (!shader)
  44. {
  45. Debug.LogError("No shader");
  46. return;
  47. }
  48.  
  49. // 获取着色器内核的句柄
  50. kernelHandle = shader.FindKernel(kernelName);
  51.  
  52. // 获取Camera组件
  53. thisCamera = GetComponent<Camera>();
  54.  
  55. if (!thisCamera)
  56. {
  57. Debug.LogError("Object has no Camera");
  58. return;
  59. }
  60.  
  61. // 创建必要的纹理
  62. CreateTextures();
  63.  
  64. // 标记为初始化完成
  65. init = true;
  66. }
  67.  
  68. // 清理指定的渲染纹理
  69. protected void ClearTexture(ref RenderTexture textureToClear)
  70. {
  71. if (null != textureToClear)
  72. {
  73. textureToClear.Release();
  74. textureToClear = null;
  75. }
  76. }
  77.  
  78. // 清理所有纹理资源
  79. protected virtual void ClearTextures()
  80. {
  81. ClearTexture(ref output);
  82. ClearTexture(ref renderedSource);
  83. }
  84.  
  85. // 创建一个渲染纹理
  86. protected void CreateTexture(ref RenderTexture textureToMake, int divide=1)
  87. {
  88. textureToMake = new RenderTexture(texSize.x/divide, texSize.y/divide, 0);
  89. textureToMake.enableRandomWrite = true;
  90. textureToMake.Create();
  91. }
  92.  
  93. // 创建所需的纹理并设置它们的尺寸和绑定
  94. protected virtual void CreateTextures()
  95. {
  96. texSize.x = thisCamera.pixelWidth;
  97. texSize.y = thisCamera.pixelHeight;
  98.  
  99. if (shader)
  100. {
  101. uint x, y;
  102. shader.GetKernelThreadGroupSizes(kernelHandle, out x, out y, out _);
  103. groupSize.x = Mathf.CeilToInt((float)texSize.x / (float)x);
  104. groupSize.y = Mathf.CeilToInt((float)texSize.y / (float)y);
  105. }
  106.  
  107. CreateTexture(ref output);
  108. CreateTexture(ref renderedSource);
  109.  
  110. shader.SetTexture(kernelHandle, "source", renderedSource);
  111. shader.SetTexture(kernelHandle, "outputrt", output);
  112. }
  113.  
  114. // 在启用时初始化
  115. protected virtual void OnEnable()
  116. {
  117. Init();
  118. }
  119.  
  120. // 在禁用时清理资源
  121. protected virtual void OnDisable()
  122. {
  123. ClearTextures();
  124. init = false;
  125. }
  126.  
  127. // 在销毁时清理资源
  128. protected virtual void OnDestroy()
  129. {
  130. ClearTextures();
  131. init = false;
  132. }
  133.  
  134. // 用计算着色器处理图像
  135. protected virtual void DispatchWithSource(ref RenderTexture source, ref RenderTexture destination)
  136. {
  137. Graphics.Blit(source, renderedSource);
  138.  
  139. shader.Dispatch(kernelHandle, groupSize.x, groupSize.y, 1);
  140.  
  141. Graphics.Blit(output, destination);
  142. }
  143.  
  144. // 检查分辨率是否改变
  145. protected void CheckResolution(out bool resChange )
  146. {
  147. resChange = false;
  148.  
  149. if (texSize.x != thisCamera.pixelWidth || texSize.y != thisCamera.pixelHeight)
  150. {
  151. resChange = true;
  152. CreateTextures();
  153. }
  154. }
  155.  
  156. // Unity的渲染事件,处理和渲染图像
  157. protected virtual void OnRenderImage(RenderTexture source, RenderTexture destination)
  158. {
  159. if (!init || shader == null)
  160. {
  161. Graphics.Blit(source, destination);
  162. }
  163. else
  164. {
  165. CheckResolution(out _);
  166. DispatchWithSource(ref source, ref destination);
  167. }
  168. }
  169. }
Tags: C#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement