PostEffectsBase.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. @script ExecuteInEditMode
  2. @script RequireComponent (Camera)
  3. class PostEffectsBase extends MonoBehaviour
  4. {
  5. function CheckShaderAndCreateMaterial(s : Shader, m2Create : Material) : Material
  6. {
  7. if(m2Create && m2Create.shader == s) return m2Create;
  8. if(!s) {
  9. Debug.Log("Missing shader in "+this.ToString());
  10. enabled = false;
  11. return null;
  12. }
  13. if(!s.isSupported)
  14. {
  15. ReportNotSupported();
  16. Debug.LogError("The shader " + s.ToString() + " on effect "+this.ToString()+" is not supported on this platform!");
  17. return null;
  18. }
  19. else
  20. {
  21. m2Create = new Material(s);
  22. m2Create.hideFlags = HideFlags.HideAndDontSave;
  23. if(m2Create) return m2Create;
  24. else return null;
  25. }
  26. }
  27. function CheckSupport(needDepth : boolean) : boolean
  28. {
  29. if (!SystemInfo.supportsImageEffects || !SystemInfo.supportsRenderTextures)
  30. {
  31. ReportNotSupported();
  32. enabled = false;
  33. return false;
  34. }
  35. if(needDepth && !SystemInfo.SupportsRenderTextureFormat (RenderTextureFormat.Depth))
  36. {
  37. ReportNotSupported();
  38. enabled = false;
  39. return false;
  40. }
  41. return true;
  42. }
  43. // Was used by Image Effect scripts in 3.0/3.1, keeping it here so that old
  44. // scripts still work.
  45. function CheckShader(s : Shader) : boolean {
  46. Debug.Log("The shader " + s.ToString() + " on effect "+this.ToString()+" is not part of the Unity 3.2 effects suite anymore. For best performance and quality, please ensure you are using the latest Standard Assets Image Effects (Pro only) package.");
  47. if(!s.isSupported) {
  48. ReportNotSupported ();
  49. return false;
  50. } else {
  51. return false;
  52. }
  53. }
  54. function ReportNotSupported() {
  55. Debug.LogError("The image effect " + this.ToString() + " on "+this.name+" is not supported on this platform!");
  56. enabled = false;
  57. return;
  58. }
  59. function OnRenderImage(source : RenderTexture, destination : RenderTexture) {
  60. Debug.Log("OnRenderImage in Base called ...");
  61. }
  62. function DrawLowLevelPlaneAlignedWithCamera(
  63. dist : float,
  64. source : RenderTexture, dest : RenderTexture,
  65. material : Material,
  66. cameraForProjectionMatrix : Camera )
  67. {
  68. // Make the destination texture the target for all rendering
  69. RenderTexture.active = dest;
  70. // Assign the source texture to a property from a shader
  71. material.SetTexture("_MainTex", source);
  72. var invertY : boolean = true; // source.texelSize.y < 0.0f;
  73. // Set up the simple Matrix
  74. GL.PushMatrix();
  75. GL.LoadIdentity();
  76. GL.LoadProjectionMatrix(cameraForProjectionMatrix.projectionMatrix);
  77. var fovYHalfRad : float = cameraForProjectionMatrix.fieldOfView * 0.5 * Mathf.Deg2Rad;
  78. var cotangent : float = Mathf.Cos(fovYHalfRad) / Mathf.Sin(fovYHalfRad);
  79. var asp : float = cameraForProjectionMatrix.aspect;
  80. var x1 : float = asp/-cotangent;
  81. var x2 : float = asp/cotangent;
  82. var y1 : float = 1.0/-cotangent;
  83. var y2 : float = 1.0/cotangent;
  84. var sc : float = 1.0; // magic constant (for now)
  85. x1 *= dist * sc;
  86. x2 *= dist * sc;
  87. y1 *= dist * sc;
  88. y2 *= dist * sc;
  89. var z1 : float = -dist;
  90. for (var i : int = 0; i < material.passCount; i++)
  91. {
  92. material.SetPass(i);
  93. GL.Begin(GL.QUADS);
  94. var y1_ : float; var y2_ : float;
  95. if (invertY)
  96. {
  97. y1_ = 1.0; y2_ = 0.0;
  98. }
  99. else
  100. {
  101. y1_ = 0.0; y2_ = 1.0;
  102. }
  103. GL.TexCoord2(0.0, y1_); GL.Vertex3(x1, y1, z1);
  104. GL.TexCoord2(1.0, y1_); GL.Vertex3(x2, y1, z1);
  105. GL.TexCoord2(1.0, y2_); GL.Vertex3(x2, y2, z1);
  106. GL.TexCoord2(0.0, y2_); GL.Vertex3(x1, y2, z1);
  107. GL.End();
  108. }
  109. GL.PopMatrix();
  110. }
  111. function DrawBorder (
  112. dest : RenderTexture,
  113. material : Material )
  114. {
  115. var x1 : float;
  116. var x2 : float;
  117. var y1 : float;
  118. var y2 : float;
  119. RenderTexture.active = dest;
  120. var invertY : boolean = true; // source.texelSize.y < 0.0f;
  121. // Set up the simple Matrix
  122. GL.PushMatrix();
  123. GL.LoadOrtho();
  124. for (var i : int = 0; i < material.passCount; i++)
  125. {
  126. material.SetPass(i);
  127. var y1_ : float; var y2_ : float;
  128. if (invertY)
  129. {
  130. y1_ = 1.0; y2_ = 0.0;
  131. }
  132. else
  133. {
  134. y1_ = 0.0; y2_ = 1.0;
  135. }
  136. // left
  137. x1 = 0.0;
  138. x2 = 0.0 + 1.0/(dest.width*1.0);
  139. y1 = 0.0;
  140. y2 = 1.0;
  141. GL.Begin(GL.QUADS);
  142. GL.TexCoord2(0.0, y1_); GL.Vertex3(x1, y1, 0.1);
  143. GL.TexCoord2(1.0, y1_); GL.Vertex3(x2, y1, 0.1);
  144. GL.TexCoord2(1.0, y2_); GL.Vertex3(x2, y2, 0.1);
  145. GL.TexCoord2(0.0, y2_); GL.Vertex3(x1, y2, 0.1);
  146. // right
  147. x1 = 1.0 - 1.0/(dest.width*1.0);
  148. x2 = 1.0;
  149. y1 = 0.0;
  150. y2 = 1.0;
  151. GL.TexCoord2(0.0, y1_); GL.Vertex3(x1, y1, 0.1);
  152. GL.TexCoord2(1.0, y1_); GL.Vertex3(x2, y1, 0.1);
  153. GL.TexCoord2(1.0, y2_); GL.Vertex3(x2, y2, 0.1);
  154. GL.TexCoord2(0.0, y2_); GL.Vertex3(x1, y2, 0.1);
  155. // top
  156. x1 = 0.0;
  157. x2 = 1.0;
  158. y1 = 0.0;
  159. y2 = 0.0 + 1.0/(dest.height*1.0);
  160. GL.TexCoord2(0.0, y1_); GL.Vertex3(x1, y1, 0.1);
  161. GL.TexCoord2(1.0, y1_); GL.Vertex3(x2, y1, 0.1);
  162. GL.TexCoord2(1.0, y2_); GL.Vertex3(x2, y2, 0.1);
  163. GL.TexCoord2(0.0, y2_); GL.Vertex3(x1, y2, 0.1);
  164. // bottom
  165. x1 = 0.0;
  166. x2 = 1.0;
  167. y1 = 1.0 - 1.0/(dest.height*1.0);
  168. y2 = 1.0;
  169. GL.TexCoord2(0.0, y1_); GL.Vertex3(x1, y1, 0.1);
  170. GL.TexCoord2(1.0, y1_); GL.Vertex3(x2, y1, 0.1);
  171. GL.TexCoord2(1.0, y2_); GL.Vertex3(x2, y2, 0.1);
  172. GL.TexCoord2(0.0, y2_); GL.Vertex3(x1, y2, 0.1);
  173. GL.End();
  174. }
  175. GL.PopMatrix();
  176. }
  177. function DrawLowLevelQuad( x1 : float, x2 : float, y1 : float, y2 : float, source : RenderTexture, dest : RenderTexture, material : Material )
  178. {
  179. // Make the destination texture the target for all rendering
  180. RenderTexture.active = dest;
  181. // Assign the source texture to a property from a shader
  182. material.SetTexture("_MainTex", source);
  183. var invertY : boolean = true; // source.texelSize.y < 0.0f;
  184. // Set up the simple Matrix
  185. GL.PushMatrix();
  186. GL.LoadOrtho();
  187. for (var i : int = 0; i < material.passCount; i++)
  188. {
  189. material.SetPass(i);
  190. GL.Begin(GL.QUADS);
  191. var y1_ : float; var y2_ : float;
  192. if (invertY)
  193. {
  194. y1_ = 1.0; y2_ = 0.0;
  195. }
  196. else
  197. {
  198. y1_ = 0.0; y2_ = 1.0;
  199. }
  200. GL.TexCoord2(0.0, y1_); GL.Vertex3(x1, y1, 0.1);
  201. GL.TexCoord2(1.0, y1_); GL.Vertex3(x2, y1, 0.1);
  202. GL.TexCoord2(1.0, y2_); GL.Vertex3(x2, y2, 0.1);
  203. GL.TexCoord2(0.0, y2_); GL.Vertex3(x1, y2, 0.1);
  204. GL.End();
  205. }
  206. GL.PopMatrix();
  207. }
  208. }