PostEffectsHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.ImageEffects
  4. {
  5. [ExecuteInEditMode]
  6. [RequireComponent (typeof(Camera))]
  7. class PostEffectsHelper : MonoBehaviour
  8. {
  9. void OnRenderImage (RenderTexture source, RenderTexture destination)
  10. {
  11. Debug.Log("OnRenderImage in Helper called ...");
  12. }
  13. static void DrawLowLevelPlaneAlignedWithCamera (
  14. float dist ,
  15. RenderTexture source, RenderTexture dest ,
  16. Material material ,
  17. Camera cameraForProjectionMatrix )
  18. {
  19. // Make the destination texture the target for all rendering
  20. RenderTexture.active = dest;
  21. // Assign the source texture to a property from a shader
  22. material.SetTexture("_MainTex", source);
  23. bool invertY = true; // source.texelSize.y < 0.0f;
  24. // Set up the simple Matrix
  25. GL.PushMatrix();
  26. GL.LoadIdentity();
  27. GL.LoadProjectionMatrix(cameraForProjectionMatrix.projectionMatrix);
  28. float fovYHalfRad = cameraForProjectionMatrix.fieldOfView * 0.5f * Mathf.Deg2Rad;
  29. float cotangent = Mathf.Cos(fovYHalfRad) / Mathf.Sin(fovYHalfRad);
  30. float asp = cameraForProjectionMatrix.aspect;
  31. float x1 = asp/-cotangent;
  32. float x2 = asp/cotangent;
  33. float y1 = 1.0f/-cotangent;
  34. float y2 = 1.0f/cotangent;
  35. float sc = 1.0f; // magic constant (for now)
  36. x1 *= dist * sc;
  37. x2 *= dist * sc;
  38. y1 *= dist * sc;
  39. y2 *= dist * sc;
  40. float z1 = -dist;
  41. for (int i = 0; i < material.passCount; i++)
  42. {
  43. material.SetPass(i);
  44. GL.Begin(GL.QUADS);
  45. float y1_; float y2_;
  46. if (invertY)
  47. {
  48. y1_ = 1.0f; y2_ = 0.0f;
  49. }
  50. else
  51. {
  52. y1_ = 0.0f; y2_ = 1.0f;
  53. }
  54. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, z1);
  55. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, z1);
  56. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, z1);
  57. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, z1);
  58. GL.End();
  59. }
  60. GL.PopMatrix();
  61. }
  62. static void DrawBorder (
  63. RenderTexture dest ,
  64. Material material )
  65. {
  66. float x1;
  67. float x2;
  68. float y1;
  69. float y2;
  70. RenderTexture.active = dest;
  71. bool invertY = true; // source.texelSize.y < 0.0ff;
  72. // Set up the simple Matrix
  73. GL.PushMatrix();
  74. GL.LoadOrtho();
  75. for (int i = 0; i < material.passCount; i++)
  76. {
  77. material.SetPass(i);
  78. float y1_; float y2_;
  79. if (invertY)
  80. {
  81. y1_ = 1.0f; y2_ = 0.0f;
  82. }
  83. else
  84. {
  85. y1_ = 0.0f; y2_ = 1.0f;
  86. }
  87. // left
  88. x1 = 0.0f;
  89. x2 = 0.0f + 1.0f/(dest.width*1.0f);
  90. y1 = 0.0f;
  91. y2 = 1.0f;
  92. GL.Begin(GL.QUADS);
  93. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  94. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  95. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  96. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  97. // right
  98. x1 = 1.0f - 1.0f/(dest.width*1.0f);
  99. x2 = 1.0f;
  100. y1 = 0.0f;
  101. y2 = 1.0f;
  102. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  103. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  104. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  105. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  106. // top
  107. x1 = 0.0f;
  108. x2 = 1.0f;
  109. y1 = 0.0f;
  110. y2 = 0.0f + 1.0f/(dest.height*1.0f);
  111. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  112. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  113. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  114. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  115. // bottom
  116. x1 = 0.0f;
  117. x2 = 1.0f;
  118. y1 = 1.0f - 1.0f/(dest.height*1.0f);
  119. y2 = 1.0f;
  120. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  121. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  122. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  123. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  124. GL.End();
  125. }
  126. GL.PopMatrix();
  127. }
  128. static void DrawLowLevelQuad ( float x1, float x2, float y1, float y2, RenderTexture source, RenderTexture dest, Material material )
  129. {
  130. // Make the destination texture the target for all rendering
  131. RenderTexture.active = dest;
  132. // Assign the source texture to a property from a shader
  133. material.SetTexture("_MainTex", source);
  134. bool invertY = true; // source.texelSize.y < 0.0f;
  135. // Set up the simple Matrix
  136. GL.PushMatrix();
  137. GL.LoadOrtho();
  138. for (int i = 0; i < material.passCount; i++)
  139. {
  140. material.SetPass(i);
  141. GL.Begin(GL.QUADS);
  142. float y1_; float y2_;
  143. if (invertY)
  144. {
  145. y1_ = 1.0f; y2_ = 0.0f;
  146. }
  147. else
  148. {
  149. y1_ = 0.0f; y2_ = 1.0f;
  150. }
  151. GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
  152. GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
  153. GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
  154. GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
  155. GL.End();
  156. }
  157. GL.PopMatrix();
  158. }
  159. }
  160. }