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