PostEffectsHelper.js 5.5 KB

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