SSAOEffect.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using UnityEngine;
  2. [ExecuteInEditMode]
  3. [RequireComponent (typeof(Camera))]
  4. [AddComponentMenu("Image Effects/Screen Space Ambient Occlusion")]
  5. public class SSAOEffect : MonoBehaviour
  6. {
  7. public enum SSAOSamples {
  8. Low = 0,
  9. Medium = 1,
  10. High = 2,
  11. }
  12. public float m_Radius = 0.4f;
  13. public SSAOSamples m_SampleCount = SSAOSamples.Medium;
  14. public float m_OcclusionIntensity = 1.5f;
  15. public int m_Blur = 2;
  16. public int m_Downsampling = 2;
  17. public float m_OcclusionAttenuation = 1.0f;
  18. public float m_MinZ = 0.01f;
  19. public Shader m_SSAOShader;
  20. private Material m_SSAOMaterial;
  21. public Texture2D m_RandomTexture;
  22. private bool m_Supported;
  23. private static Material CreateMaterial (Shader shader)
  24. {
  25. if (!shader)
  26. return null;
  27. Material m = new Material (shader);
  28. m.hideFlags = HideFlags.HideAndDontSave;
  29. return m;
  30. }
  31. private static void DestroyMaterial (Material mat)
  32. {
  33. if (mat)
  34. {
  35. DestroyImmediate (mat);
  36. mat = null;
  37. }
  38. }
  39. void OnDisable()
  40. {
  41. DestroyMaterial (m_SSAOMaterial);
  42. }
  43. void Start()
  44. {
  45. if (!SystemInfo.supportsImageEffects || !SystemInfo.SupportsRenderTextureFormat (RenderTextureFormat.Depth))
  46. {
  47. m_Supported = false;
  48. enabled = false;
  49. return;
  50. }
  51. CreateMaterials ();
  52. if (!m_SSAOMaterial || m_SSAOMaterial.passCount != 5)
  53. {
  54. m_Supported = false;
  55. enabled = false;
  56. return;
  57. }
  58. //CreateRandomTable (26, 0.2f);
  59. m_Supported = true;
  60. }
  61. void OnEnable () {
  62. GetComponent<Camera>().depthTextureMode |= DepthTextureMode.DepthNormals;
  63. }
  64. private void CreateMaterials ()
  65. {
  66. if (!m_SSAOMaterial && m_SSAOShader.isSupported)
  67. {
  68. m_SSAOMaterial = CreateMaterial (m_SSAOShader);
  69. m_SSAOMaterial.SetTexture ("_RandomTexture", m_RandomTexture);
  70. }
  71. }
  72. void OnRenderImage (RenderTexture source, RenderTexture destination)
  73. {
  74. if (!m_Supported || !m_SSAOShader.isSupported) {
  75. enabled = false;
  76. return;
  77. }
  78. CreateMaterials ();
  79. m_Downsampling = Mathf.Clamp (m_Downsampling, 1, 6);
  80. m_Radius = Mathf.Clamp (m_Radius, 0.05f, 1.0f);
  81. m_MinZ = Mathf.Clamp (m_MinZ, 0.00001f, 0.5f);
  82. m_OcclusionIntensity = Mathf.Clamp (m_OcclusionIntensity, 0.5f, 4.0f);
  83. m_OcclusionAttenuation = Mathf.Clamp (m_OcclusionAttenuation, 0.2f, 2.0f);
  84. m_Blur = Mathf.Clamp (m_Blur, 0, 4);
  85. // Render SSAO term into a smaller texture
  86. RenderTexture rtAO = RenderTexture.GetTemporary (source.width / m_Downsampling, source.height / m_Downsampling, 0);
  87. float fovY = GetComponent<Camera>().fieldOfView;
  88. float far = GetComponent<Camera>().farClipPlane;
  89. float y = Mathf.Tan (fovY * Mathf.Deg2Rad * 0.5f) * far;
  90. float x = y * GetComponent<Camera>().aspect;
  91. m_SSAOMaterial.SetVector ("_FarCorner", new Vector3(x,y,far));
  92. int noiseWidth, noiseHeight;
  93. if (m_RandomTexture) {
  94. noiseWidth = m_RandomTexture.width;
  95. noiseHeight = m_RandomTexture.height;
  96. } else {
  97. noiseWidth = 1; noiseHeight = 1;
  98. }
  99. m_SSAOMaterial.SetVector ("_NoiseScale", new Vector3 ((float)rtAO.width / noiseWidth, (float)rtAO.height / noiseHeight, 0.0f));
  100. m_SSAOMaterial.SetVector ("_Params", new Vector4(
  101. m_Radius,
  102. m_MinZ,
  103. 1.0f / m_OcclusionAttenuation,
  104. m_OcclusionIntensity));
  105. bool doBlur = m_Blur > 0;
  106. Graphics.Blit (doBlur ? null : source, rtAO, m_SSAOMaterial, (int)m_SampleCount);
  107. if (doBlur)
  108. {
  109. // Blur SSAO horizontally
  110. RenderTexture rtBlurX = RenderTexture.GetTemporary (source.width, source.height, 0);
  111. m_SSAOMaterial.SetVector ("_TexelOffsetScale",
  112. new Vector4 ((float)m_Blur / source.width, 0,0,0));
  113. m_SSAOMaterial.SetTexture ("_SSAO", rtAO);
  114. Graphics.Blit (null, rtBlurX, m_SSAOMaterial, 3);
  115. RenderTexture.ReleaseTemporary (rtAO); // original rtAO not needed anymore
  116. // Blur SSAO vertically
  117. RenderTexture rtBlurY = RenderTexture.GetTemporary (source.width, source.height, 0);
  118. m_SSAOMaterial.SetVector ("_TexelOffsetScale",
  119. new Vector4 (0, (float)m_Blur/source.height, 0,0));
  120. m_SSAOMaterial.SetTexture ("_SSAO", rtBlurX);
  121. Graphics.Blit (source, rtBlurY, m_SSAOMaterial, 3);
  122. RenderTexture.ReleaseTemporary (rtBlurX); // blurX RT not needed anymore
  123. rtAO = rtBlurY; // AO is the blurred one now
  124. }
  125. // Modulate scene rendering with SSAO
  126. m_SSAOMaterial.SetTexture ("_SSAO", rtAO);
  127. Graphics.Blit (source, destination, m_SSAOMaterial, 4);
  128. RenderTexture.ReleaseTemporary (rtAO);
  129. }
  130. /*
  131. private void CreateRandomTable (int count, float minLength)
  132. {
  133. Random.seed = 1337;
  134. Vector3[] samples = new Vector3[count];
  135. // initial samples
  136. for (int i = 0; i < count; ++i)
  137. samples[i] = Random.onUnitSphere;
  138. // energy minimization: push samples away from others
  139. int iterations = 100;
  140. while (iterations-- > 0) {
  141. for (int i = 0; i < count; ++i) {
  142. Vector3 vec = samples[i];
  143. Vector3 res = Vector3.zero;
  144. // minimize with other samples
  145. for (int j = 0; j < count; ++j) {
  146. Vector3 force = vec - samples[j];
  147. float fac = Vector3.Dot (force, force);
  148. if (fac > 0.00001f)
  149. res += force * (1.0f / fac);
  150. }
  151. samples[i] = (samples[i] + res * 0.5f).normalized;
  152. }
  153. }
  154. // now scale samples between minLength and 1.0
  155. for (int i = 0; i < count; ++i) {
  156. samples[i] = samples[i] * Random.Range (minLength, 1.0f);
  157. }
  158. string table = string.Format ("#define SAMPLE_COUNT {0}\n", count);
  159. table += "const float3 RAND_SAMPLES[SAMPLE_COUNT] = {\n";
  160. for (int i = 0; i < count; ++i) {
  161. Vector3 v = samples[i];
  162. table += string.Format("\tfloat3({0},{1},{2}),\n", v.x, v.y, v.z);
  163. }
  164. table += "};\n";
  165. Debug.Log (table);
  166. }
  167. */
  168. }