TiltShift.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.ImageEffects
  4. {
  5. [RequireComponent (typeof(Camera))]
  6. [AddComponentMenu ("Image Effects/Camera/Tilt Shift (Lens Blur)")]
  7. class TiltShift : PostEffectsBase {
  8. public enum TiltShiftMode
  9. {
  10. TiltShiftMode,
  11. IrisMode,
  12. }
  13. public enum TiltShiftQuality
  14. {
  15. Preview,
  16. Normal,
  17. High,
  18. }
  19. public TiltShiftMode mode = TiltShiftMode.TiltShiftMode;
  20. public TiltShiftQuality quality = TiltShiftQuality.Normal;
  21. [Range(0.0f, 15.0f)]
  22. public float blurArea = 1.0f;
  23. [Range(0.0f, 25.0f)]
  24. public float maxBlurSize = 5.0f;
  25. [Range(0, 1)]
  26. public int downsample = 0;
  27. public Shader tiltShiftShader = null;
  28. private Material tiltShiftMaterial = null;
  29. public override bool CheckResources () {
  30. CheckSupport (true);
  31. tiltShiftMaterial = CheckShaderAndCreateMaterial (tiltShiftShader, tiltShiftMaterial);
  32. if (!isSupported)
  33. ReportAutoDisable ();
  34. return isSupported;
  35. }
  36. void OnRenderImage (RenderTexture source, RenderTexture destination) {
  37. if (CheckResources() == false) {
  38. Graphics.Blit (source, destination);
  39. return;
  40. }
  41. tiltShiftMaterial.SetFloat("_BlurSize", maxBlurSize < 0.0f ? 0.0f : maxBlurSize);
  42. tiltShiftMaterial.SetFloat("_BlurArea", blurArea);
  43. source.filterMode = FilterMode.Bilinear;
  44. RenderTexture rt = destination;
  45. if (downsample > 0f) {
  46. rt = RenderTexture.GetTemporary (source.width>>downsample, source.height>>downsample, 0, source.format);
  47. rt.filterMode = FilterMode.Bilinear;
  48. }
  49. int basePassNr = (int) quality; basePassNr *= 2;
  50. Graphics.Blit (source, rt, tiltShiftMaterial, mode == TiltShiftMode.TiltShiftMode ? basePassNr : basePassNr + 1);
  51. if (downsample > 0) {
  52. tiltShiftMaterial.SetTexture ("_Blurred", rt);
  53. Graphics.Blit (source, destination, tiltShiftMaterial, 6);
  54. }
  55. if (rt != destination)
  56. RenderTexture.ReleaseTemporary (rt);
  57. }
  58. }
  59. }