VolumetricFogPreT.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //------------------------------------------------------------------------------------------------------------------
  2. // Volumetric Fog & Mist
  3. // Created by Ramiro Oliva (Kronnect)
  4. //------------------------------------------------------------------------------------------------------------------
  5. using UnityEngine;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace VolumetricFogAndMist
  10. {
  11. [ExecuteInEditMode]
  12. [RequireComponent(typeof(Camera), typeof(VolumetricFog))]
  13. public class VolumetricFogPreT : MonoBehaviour, IVolumetricFogRenderComponent
  14. {
  15. public VolumetricFog fog { get; set; }
  16. RenderTexture opaqueFrame;
  17. [ImageEffectOpaque]
  18. void OnRenderImage(RenderTexture source, RenderTexture destination)
  19. {
  20. if (fog == null || !fog.enabled)
  21. {
  22. Graphics.Blit(source, destination);
  23. return;
  24. }
  25. if (fog.renderBeforeTransparent)
  26. {
  27. fog.DoOnRenderImage(source, destination);
  28. }
  29. else
  30. {
  31. // Save frame buffer
  32. RenderTextureDescriptor desc = source.descriptor;
  33. opaqueFrame = RenderTexture.GetTemporary(desc);
  34. fog.DoOnRenderImage(source, opaqueFrame);
  35. Shader.SetGlobalTexture("_VolumetricFog_OpaqueFrame", opaqueFrame);
  36. Graphics.Blit(opaqueFrame, destination);
  37. }
  38. }
  39. void OnPostRender()
  40. {
  41. if (opaqueFrame != null)
  42. {
  43. RenderTexture.ReleaseTemporary(opaqueFrame);
  44. opaqueFrame = null;
  45. }
  46. }
  47. public void DestroySelf() {
  48. DestroyImmediate(this);
  49. }
  50. }
  51. }