Post Process Effect -- Unity3D


SUBMITTED BY: Guest

DATE: April 23, 2013, 2:15 p.m.

FORMAT: C#

SIZE: 7.1 kB

HITS: 1673

  1. /* ____ _ _ __ __ _
  2. * | _ \ _ __ ___ (_) ___ ___| |_ \ \ / /_ _ _ __ __| | ___ _ __
  3. * | |_) | '__/ _ \| |/ _ \/ __| __| \ \ /\ / / _` | '_ \ / _` |/ _ \ '__|
  4. * | __/| | | (_) | | __/ (__| |_ \ V V / (_| | | | | (_| | __/ |
  5. * |_| |_| \___// |\___|\___|\__| \_/\_/ \__,_|_| |_|\__,_|\___|_|
  6. * |__/
  7. *
  8. *
  9. * ENJMIN - Hits PlayTime 2013
  10. *
  11. * This file is part of the Wander project,
  12. * created and developped by :
  13. * Louis Schnellbach
  14. *
  15. */
  16. using UnityEngine;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. [ExecuteInEditMode]
  20. [RequireComponent(typeof(Camera))]
  21. public class CameraPostProcess : MonoBehaviour
  22. {
  23. RenderTexture m_finalTex;
  24. List<Source> m_list = new List<Source> ();
  25. public Shader shader;
  26. private Material m_materialPostProcess = null;
  27. private Material m_materialInitTexture = null;
  28. private int m_width;
  29. private int m_height;
  30. float CAMERA_NEAR;
  31. float CAMERA_FAR;
  32. float CAMERA_FOV;
  33. float CAMERA_ASPECT_RATIO;
  34. void Start ()
  35. {
  36. camera.depthTextureMode = DepthTextureMode.DepthNormals;
  37. // Disable if we don't support image effects
  38. if (!SystemInfo.supportsImageEffects) {
  39. enabled = false;
  40. return;
  41. }
  42. // Disable the image effect if the shader can't
  43. // run on the users graphics card
  44. if (!shader || !shader.isSupported)
  45. enabled = false;
  46. }
  47. //Free texture
  48. void OnDisable ()
  49. {
  50. m_list.Clear ();
  51. DestroyImmediate (m_finalTex);
  52. DestroyImmediate (m_materialPostProcess);
  53. DestroyImmediate (m_materialInitTexture);
  54. }
  55. void OnEnable ()
  56. {
  57. if (m_materialPostProcess == null) {
  58. m_materialPostProcess = new Material (shader);
  59. m_materialPostProcess.hideFlags = HideFlags.HideAndDontSave;
  60. }
  61. if (m_materialInitTexture == null) {
  62. m_materialInitTexture = new Material (Shader.Find ("Xleek/BlackTexture"));
  63. m_materialInitTexture.hideFlags = HideFlags.HideAndDontSave;
  64. }
  65. m_list.Clear ();
  66. camera.depthTextureMode = DepthTextureMode.DepthNormals;
  67. m_finalTex = new RenderTexture ((int)camera.pixelWidth, (int)camera.pixelHeight, 0);
  68. }
  69. public void AddSource (Source obj)
  70. {
  71. m_list.Add (obj);
  72. //Debug.Log ("Added Source. (" + m_list.Count.ToString () + ")");
  73. }
  74. public void RemoveSource (Source obj)
  75. {
  76. m_list.Remove (obj);
  77. //Debug.Log ("Removed Source. (" + m_list.Count.ToString () + ")");
  78. }
  79. // Called by camera to apply image effect
  80. void OnRenderImage (RenderTexture source, RenderTexture destination)
  81. {
  82. //Clear FinalTexture before each display
  83. Graphics.Blit (null, m_finalTex, m_materialInitTexture);
  84. // Setup the texture and floating point values in the shader
  85. m_materialPostProcess.SetTexture ("_MainTex", source);
  86. //Send to shader the frustum corner
  87. m_materialPostProcess.SetMatrix ("_FrustumCornersWS", frustumCorner ());
  88. //And the camera position (computed once)
  89. m_materialPostProcess.SetVector ("_CameraWS", camera.transform.position);
  90. //Start rendering each source
  91. m_list.ForEach (delegate(Source src)
  92. {
  93. if (src != null) {
  94. m_materialPostProcess.SetTexture ("_FinalTex", m_finalTex);
  95. m_materialPostProcess.SetVector ("_Position", src.Position);
  96. m_materialPostProcess.SetColor ("_SourceColor", src.SourceColor);
  97. m_materialPostProcess.SetFloat ("_Radius", src.Radius);
  98. m_materialPostProcess.SetFloat ("_MaxRadius", src.MaxRadius);
  99. m_materialPostProcess.SetFloat ("_VisibleWidth", src.VisibleWidth);
  100. m_materialPostProcess.SetFloat ("_HaloWidth", src.HaloWidth);
  101. CustomGraphicBlit (source, m_finalTex, m_materialPostProcess);
  102. }
  103. });
  104. //Final (at least !) blit
  105. Graphics.Blit (m_finalTex, destination);
  106. }
  107. void CustomGraphicBlit (RenderTexture source, RenderTexture destination, Material material)
  108. {
  109. RenderTexture.active = destination;
  110. GL.PushMatrix ();
  111. GL.LoadOrtho ();
  112. material.SetPass (0);
  113. GL.Begin (GL.QUADS);
  114. GL.MultiTexCoord2 (0, 0.0f, 0.0f);
  115. GL.Vertex3 (0.0f, 0.0f, 3.0f); // BL
  116. GL.MultiTexCoord2 (0, 1.0f, 0.0f);
  117. GL.Vertex3 (1.0f, 0.0f, 2.0f); // BR
  118. GL.MultiTexCoord2 (0, 1.0f, 1.0f);
  119. GL.Vertex3 (1.0f, 1.0f, 1.0f); // TR
  120. GL.MultiTexCoord2 (0, 0.0f, 1.0f);
  121. GL.Vertex3 (0.0f, 1.0f, 0.0f); // TL
  122. GL.End ();
  123. GL.PopMatrix ();
  124. }
  125. //Avoid to write this in the render method
  126. Matrix4x4 frustumCorner ()
  127. {
  128. Matrix4x4 frustumCorners = Matrix4x4.identity;
  129. CAMERA_NEAR = camera.nearClipPlane;
  130. CAMERA_FAR = camera.farClipPlane;
  131. CAMERA_FOV = camera.fieldOfView;
  132. CAMERA_ASPECT_RATIO = camera.aspect;
  133. float fovWHalf = CAMERA_FOV * 0.5f;
  134. Vector3 toRight = camera.transform.right * CAMERA_NEAR * Mathf.Tan (fovWHalf * Mathf.Deg2Rad) * CAMERA_ASPECT_RATIO;
  135. Vector3 toTop = camera.transform.up * CAMERA_NEAR * Mathf.Tan (fovWHalf * Mathf.Deg2Rad);
  136. Vector3 topLeft = (camera.transform.forward * CAMERA_NEAR - toRight + toTop);
  137. float CAMERA_SCALE = topLeft.magnitude * CAMERA_FAR / CAMERA_NEAR;
  138. topLeft.Normalize ();
  139. topLeft *= CAMERA_SCALE;
  140. Vector3 topRight = (camera.transform.forward * CAMERA_NEAR + toRight + toTop);
  141. topRight.Normalize ();
  142. topRight *= CAMERA_SCALE;
  143. Vector3 bottomRight = (camera.transform.forward * CAMERA_NEAR + toRight - toTop);
  144. bottomRight.Normalize ();
  145. bottomRight *= CAMERA_SCALE;
  146. Vector3 bottomLeft = (camera.transform.forward * CAMERA_NEAR - toRight - toTop);
  147. bottomLeft.Normalize ();
  148. bottomLeft *= CAMERA_SCALE;
  149. frustumCorners.SetRow (0, topLeft);
  150. frustumCorners.SetRow (1, topRight);
  151. frustumCorners.SetRow (2, bottomRight);
  152. frustumCorners.SetRow (3, bottomLeft);
  153. return frustumCorners;
  154. }
  155. }

comments powered by Disqus