UnityMol  0.9.6-875
UnityMol viewer / In developement
Antialiasing.cs
Go to the documentation of this file.
1 using System;
2 using UnityEngine;
3 
5 {
6  public enum AAMode
7  {
8  FXAA2 = 0,
9  FXAA3Console = 1,
10  FXAA1PresetA = 2,
11  FXAA1PresetB = 3,
12  NFAA = 4,
13  SSAA = 5,
14  DLAA = 6,
15  }
16 
17  [ExecuteInEditMode]
18  [RequireComponent(typeof (Camera))]
19  [AddComponentMenu("Image Effects/Other/Antialiasing")]
21  {
22  public AAMode mode = AAMode.FXAA3Console;
23 
24  public bool showGeneratedNormals = false;
25  public float offsetScale = 0.2f;
26  public float blurRadius = 18.0f;
27 
28  public float edgeThresholdMin = 0.05f;
29  public float edgeThreshold = 0.2f;
30  public float edgeSharpness = 4.0f;
31 
32  public bool dlaaSharp = false;
33 
34  public Shader ssaaShader;
35  private Material ssaa;
36  public Shader dlaaShader;
37  private Material dlaa;
38  public Shader nfaaShader;
39  private Material nfaa;
40  public Shader shaderFXAAPreset2;
41  private Material materialFXAAPreset2;
42  public Shader shaderFXAAPreset3;
43  private Material materialFXAAPreset3;
44  public Shader shaderFXAAII;
45  private Material materialFXAAII;
46  public Shader shaderFXAAIII;
47  private Material materialFXAAIII;
48 
49 
50  public Material CurrentAAMaterial()
51  {
52  Material returnValue = null;
53 
54  switch (mode)
55  {
56  case AAMode.FXAA3Console:
57  returnValue = materialFXAAIII;
58  break;
59  case AAMode.FXAA2:
60  returnValue = materialFXAAII;
61  break;
62  case AAMode.FXAA1PresetA:
63  returnValue = materialFXAAPreset2;
64  break;
65  case AAMode.FXAA1PresetB:
66  returnValue = materialFXAAPreset3;
67  break;
68  case AAMode.NFAA:
69  returnValue = nfaa;
70  break;
71  case AAMode.SSAA:
72  returnValue = ssaa;
73  break;
74  case AAMode.DLAA:
75  returnValue = dlaa;
76  break;
77  default:
78  returnValue = null;
79  break;
80  }
81 
82  return returnValue;
83  }
84 
85 
86  public override bool CheckResources()
87  {
88  CheckSupport(false);
89 
90  materialFXAAPreset2 = CreateMaterial(shaderFXAAPreset2, materialFXAAPreset2);
91  materialFXAAPreset3 = CreateMaterial(shaderFXAAPreset3, materialFXAAPreset3);
92  materialFXAAII = CreateMaterial(shaderFXAAII, materialFXAAII);
93  materialFXAAIII = CreateMaterial(shaderFXAAIII, materialFXAAIII);
94  nfaa = CreateMaterial(nfaaShader, nfaa);
95  ssaa = CreateMaterial(ssaaShader, ssaa);
96  dlaa = CreateMaterial(dlaaShader, dlaa);
97 
98  if (!ssaaShader.isSupported)
99  {
100  NotSupported();
101  ReportAutoDisable();
102  }
103 
104  return isSupported;
105  }
106 
107 
108  public void OnRenderImage(RenderTexture source, RenderTexture destination)
109  {
110  if (CheckResources() == false)
111  {
112  Graphics.Blit(source, destination);
113  return;
114  }
115 
116  // ----------------------------------------------------------------
117  // FXAA antialiasing modes
118 
119  if (mode == AAMode.FXAA3Console && (materialFXAAIII != null))
120  {
121  materialFXAAIII.SetFloat("_EdgeThresholdMin", edgeThresholdMin);
122  materialFXAAIII.SetFloat("_EdgeThreshold", edgeThreshold);
123  materialFXAAIII.SetFloat("_EdgeSharpness", edgeSharpness);
124 
125  Graphics.Blit(source, destination, materialFXAAIII);
126  }
127  else if (mode == AAMode.FXAA1PresetB && (materialFXAAPreset3 != null))
128  {
129  Graphics.Blit(source, destination, materialFXAAPreset3);
130  }
131  else if (mode == AAMode.FXAA1PresetA && materialFXAAPreset2 != null)
132  {
133  source.anisoLevel = 4;
134  Graphics.Blit(source, destination, materialFXAAPreset2);
135  source.anisoLevel = 0;
136  }
137  else if (mode == AAMode.FXAA2 && materialFXAAII != null)
138  {
139  Graphics.Blit(source, destination, materialFXAAII);
140  }
141  else if (mode == AAMode.SSAA && ssaa != null)
142  {
143  // ----------------------------------------------------------------
144  // SSAA antialiasing
145  Graphics.Blit(source, destination, ssaa);
146  }
147  else if (mode == AAMode.DLAA && dlaa != null)
148  {
149  // ----------------------------------------------------------------
150  // DLAA antialiasing
151 
152  source.anisoLevel = 0;
153  RenderTexture interim = RenderTexture.GetTemporary(source.width, source.height);
154  Graphics.Blit(source, interim, dlaa, 0);
155  Graphics.Blit(interim, destination, dlaa, dlaaSharp ? 2 : 1);
156  RenderTexture.ReleaseTemporary(interim);
157  }
158  else if (mode == AAMode.NFAA && nfaa != null)
159  {
160  // ----------------------------------------------------------------
161  // nfaa antialiasing
162 
163  source.anisoLevel = 0;
164 
165  nfaa.SetFloat("_OffsetScale", offsetScale);
166  nfaa.SetFloat("_BlurRadius", blurRadius);
167 
168  Graphics.Blit(source, destination, nfaa, showGeneratedNormals ? 1 : 0);
169  }
170  else
171  {
172  // none of the AA is supported, fallback to a simple blit
173  Graphics.Blit(source, destination);
174  }
175  }
176  }
177 }
A Utility class for performing various image based rendering tasks.
Definition: ImageEffects.cs:8
void OnRenderImage(RenderTexture source, RenderTexture destination)