Advertisement
443eb9

Untitled

Aug 20th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4.  
  5. namespace HexagonSurvive.RenderFeatures.Environment
  6. {
  7. public class CloudsRenderFeature : ScriptableRendererFeature
  8. {
  9. private class CloudsPass : ScriptableRenderPass
  10. {
  11. private Mesh _planeMesh;
  12. private RTHandle _cameraHandle;
  13. private RTHandle _tempHandle;
  14. private Material _cloudMaterial;
  15. private CloudsSettings _settings;
  16.  
  17. private static readonly int CameraTex = Shader.PropertyToID("_MainTex");
  18.  
  19. public CloudsPass(CloudsSettings settings, RenderPassEvent ev)
  20. {
  21. _settings = settings;
  22. renderPassEvent = ev;
  23. _tempHandle = RTHandles.Alloc(Screen.width, Screen.height);
  24. profilingSampler = new ProfilingSampler("Clouds");
  25. }
  26.  
  27. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  28. {
  29. ConfigureTarget(_cameraHandle);
  30. ConfigureClear(ClearFlag.All, Color.black);
  31. }
  32.  
  33. public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
  34. {
  35. RenderingUtils.ReAllocateIfNeeded(ref _tempHandle, renderingData.cameraData.cameraTargetDescriptor);
  36. }
  37.  
  38. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  39. {
  40. if (_cloudMaterial == null)
  41. return;
  42.  
  43. CommandBuffer cmd = CommandBufferPool.Get("Clouds");
  44. context.ExecuteCommandBuffer(cmd);
  45. cmd.Clear();
  46.  
  47. cmd.SetGlobalTexture(CameraTex, _cameraHandle);
  48. Blitter.BlitCameraTexture(cmd, _cameraHandle, _tempHandle, _cloudMaterial, 0);
  49. Blitter.BlitCameraTexture(cmd, _tempHandle, _cameraHandle);
  50.  
  51. context.ExecuteCommandBuffer(cmd);
  52. cmd.Clear();
  53. CommandBufferPool.Release(cmd);
  54. }
  55.  
  56. public void Setup(RTHandle color, Material cloudMaterial)
  57. {
  58. _cameraHandle = color;
  59. _cloudMaterial = cloudMaterial;
  60. }
  61. }
  62.  
  63. [System.Serializable]
  64. public class CloudsSettings
  65. {
  66. public Vector4 boundsMax;
  67. public Vector4 boundsMin;
  68. }
  69.  
  70. public Material cloudMaterial;
  71. public RenderPassEvent renderPassEvent;
  72. public CloudsSettings settings;
  73.  
  74. private CloudsPass _cloudsPass;
  75.  
  76. public override void Create()
  77. {
  78. _cloudsPass = new CloudsPass(settings, renderPassEvent);
  79. }
  80.  
  81. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  82. {
  83. if (renderingData.cameraData.isPreviewCamera)
  84. return;
  85.  
  86. renderer.EnqueuePass(_cloudsPass);
  87. }
  88.  
  89. public override void SetupRenderPasses(ScriptableRenderer renderer, in RenderingData renderingData)
  90. {
  91. _cloudsPass.Setup(renderer.cameraColorTargetHandle, cloudMaterial);
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement