UnityMol  0.9.6-875
UnityMol viewer / In developement
VignetteAndChromaticAberration.cs
Go to the documentation of this file.
1 using System;
2 using UnityEngine;
3 
4 namespace UnityStandardAssets.ImageEffects
5 {
6  [ExecuteInEditMode]
7  [RequireComponent (typeof(Camera))]
8  [AddComponentMenu ("Image Effects/Camera/Vignette and Chromatic Aberration")]
10  {
11  public enum AberrationMode
12  {
13  Simple = 0,
14  Advanced = 1,
15  }
16 
17 
18  public AberrationMode mode = AberrationMode.Simple;
19  public float intensity = 0.036f; // intensity == 0 disables pre pass (optimization)
20  public float chromaticAberration = 0.2f;
21  public float axialAberration = 0.5f;
22  public float blur = 0.0f; // blur == 0 disables blur pass (optimization)
23  public float blurSpread = 0.75f;
24  public float luminanceDependency = 0.25f;
25  public float blurDistance = 2.5f;
26  public Shader vignetteShader;
27  public Shader separableBlurShader;
28  public Shader chromAberrationShader;
29 
30 
31  private Material m_VignetteMaterial;
32  private Material m_SeparableBlurMaterial;
33  private Material m_ChromAberrationMaterial;
34 
35 
36  public override bool CheckResources ()
37  {
38  CheckSupport (false);
39 
40  m_VignetteMaterial = CheckShaderAndCreateMaterial (vignetteShader, m_VignetteMaterial);
41  m_SeparableBlurMaterial = CheckShaderAndCreateMaterial (separableBlurShader, m_SeparableBlurMaterial);
42  m_ChromAberrationMaterial = CheckShaderAndCreateMaterial (chromAberrationShader, m_ChromAberrationMaterial);
43 
44  if (!isSupported)
45  ReportAutoDisable ();
46  return isSupported;
47  }
48 
49 
50  void OnRenderImage (RenderTexture source, RenderTexture destination)
51  {
52  if ( CheckResources () == false)
53  {
54  Graphics.Blit (source, destination);
55  return;
56  }
57 
58  int rtW = source.width;
59  int rtH = source.height;
60 
61  bool doPrepass = (Mathf.Abs(blur)>0.0f || Mathf.Abs(intensity)>0.0f);
62 
63  float widthOverHeight = (1.0f * rtW) / (1.0f * rtH);
64  const float oneOverBaseSize = 1.0f / 512.0f;
65 
66  RenderTexture color = null;
67  RenderTexture color2A = null;
68 
69  if (doPrepass)
70  {
71  color = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
72 
73  // Blur corners
74  if (Mathf.Abs (blur)>0.0f)
75  {
76  color2A = RenderTexture.GetTemporary (rtW / 2, rtH / 2, 0, source.format);
77 
78  Graphics.Blit (source, color2A, m_ChromAberrationMaterial, 0);
79 
80  for(int i = 0; i < 2; i++)
81  { // maybe make iteration count tweakable
82  m_SeparableBlurMaterial.SetVector ("offsets",new Vector4 (0.0f, blurSpread * oneOverBaseSize, 0.0f, 0.0f));
83  RenderTexture color2B = RenderTexture.GetTemporary (rtW / 2, rtH / 2, 0, source.format);
84  Graphics.Blit (color2A, color2B, m_SeparableBlurMaterial);
85  RenderTexture.ReleaseTemporary (color2A);
86 
87  m_SeparableBlurMaterial.SetVector ("offsets",new Vector4 (blurSpread * oneOverBaseSize / widthOverHeight, 0.0f, 0.0f, 0.0f));
88  color2A = RenderTexture.GetTemporary (rtW / 2, rtH / 2, 0, source.format);
89  Graphics.Blit (color2B, color2A, m_SeparableBlurMaterial);
90  RenderTexture.ReleaseTemporary (color2B);
91  }
92  }
93 
94  m_VignetteMaterial.SetFloat("_Intensity", (1.0f / (1.0f - intensity) - 1.0f)); // intensity for vignette
95  m_VignetteMaterial.SetFloat("_Blur", (1.0f / (1.0f - blur)) - 1.0f); // blur intensity
96  m_VignetteMaterial.SetTexture ("_VignetteTex", color2A); // blurred texture
97 
98  Graphics.Blit (source, color, m_VignetteMaterial, 0); // prepass blit: darken & blur corners
99  }
100 
101  m_ChromAberrationMaterial.SetFloat ("_ChromaticAberration", chromaticAberration);
102  m_ChromAberrationMaterial.SetFloat ("_AxialAberration", axialAberration);
103  m_ChromAberrationMaterial.SetVector ("_BlurDistance", new Vector2 (-blurDistance, blurDistance));
104  m_ChromAberrationMaterial.SetFloat ("_Luminance", 1.0f/Mathf.Max(Mathf.Epsilon, luminanceDependency));
105 
106  if (doPrepass) color.wrapMode = TextureWrapMode.Clamp;
107  else source.wrapMode = TextureWrapMode.Clamp;
108  Graphics.Blit (doPrepass ? color : source, destination, m_ChromAberrationMaterial, mode == AberrationMode.Advanced ? 2 : 1);
109 
110  RenderTexture.ReleaseTemporary (color);
111  RenderTexture.ReleaseTemporary (color2A);
112  }
113  }
114 }
void OnRenderImage(RenderTexture source, RenderTexture destination)