/racer/Assets/Pro Standard Assets/Image Based/ImageEffects.cs

https://bitbucket.org/marvin0038/racer · C# · 181 lines · 144 code · 19 blank · 18 comment · 9 complexity · ff0548c4ba971cf810a7762ad406e297 MD5 · raw file

  1. using UnityEngine;
  2. /// Blending modes use by the ImageEffects.Blit functions.
  3. public enum BlendMode {
  4. Copy,
  5. Multiply,
  6. MultiplyDouble,
  7. Add,
  8. AddSmoooth,
  9. Blend
  10. }
  11. /// A Utility class for performing various image based rendering tasks.
  12. [AddComponentMenu("")]
  13. public class ImageEffects {
  14. static Material[] m_BlitMaterials = {null, null, null, null, null, null};
  15. static public Material GetBlitMaterial (BlendMode mode) {
  16. int index = (int)mode;
  17. if (m_BlitMaterials[index] != null)
  18. return m_BlitMaterials[index];
  19. // Blit Copy Material
  20. m_BlitMaterials[0] = new Material (
  21. "Shader \"BlitCopy\" {\n" +
  22. " SubShader { Pass {\n" +
  23. " ZTest Always Cull Off ZWrite Off Fog { Mode Off }\n" +
  24. " SetTexture [__RenderTex] { combine texture}" +
  25. " }}\n" +
  26. "Fallback Off }"
  27. );
  28. // Blit Multiply
  29. m_BlitMaterials[1] = new Material (
  30. "Shader \"BlitMultiply\" {\n" +
  31. " SubShader { Pass {\n" +
  32. " Blend DstColor Zero\n" +
  33. " ZTest Always Cull Off ZWrite Off Fog { Mode Off }\n" +
  34. " SetTexture [__RenderTex] { combine texture }" +
  35. " }}\n" +
  36. "Fallback Off }"
  37. );
  38. // Blit Multiply 2X
  39. m_BlitMaterials[2] = new Material (
  40. "Shader \"BlitMultiplyDouble\" {\n" +
  41. " SubShader { Pass {\n" +
  42. " Blend DstColor SrcColor\n" +
  43. " ZTest Always Cull Off ZWrite Off Fog { Mode Off }\n" +
  44. " SetTexture [__RenderTex] { combine texture }" +
  45. " }}\n" +
  46. "Fallback Off }"
  47. );
  48. // Blit Add
  49. m_BlitMaterials[3] = new Material (
  50. "Shader \"BlitAdd\" {\n" +
  51. " SubShader { Pass {\n" +
  52. " Blend One One\n" +
  53. " ZTest Always Cull Off ZWrite Off Fog { Mode Off }\n" +
  54. " SetTexture [__RenderTex] { combine texture }" +
  55. " }}\n" +
  56. "Fallback Off }"
  57. );
  58. // Blit AddSmooth
  59. m_BlitMaterials[4] = new Material (
  60. "Shader \"BlitAddSmooth\" {\n" +
  61. " SubShader { Pass {\n" +
  62. " Blend OneMinusDstColor One\n" +
  63. " ZTest Always Cull Off ZWrite Off Fog { Mode Off }\n" +
  64. " SetTexture [__RenderTex] { combine texture }" +
  65. " }}\n" +
  66. "Fallback Off }"
  67. );
  68. // Blit Blend
  69. m_BlitMaterials[5] = new Material (
  70. "Shader \"BlitBlend\" {\n" +
  71. " SubShader { Pass {\n" +
  72. " Blend SrcAlpha OneMinusSrcAlpha\n" +
  73. " ZTest Always Cull Off ZWrite Off Fog { Mode Off }\n" +
  74. " SetTexture [__RenderTex] { combine texture }" +
  75. " }}\n" +
  76. "Fallback Off }"
  77. );
  78. for( int i = 0; i < m_BlitMaterials.Length; ++i ) {
  79. m_BlitMaterials[i].hideFlags = HideFlags.HideAndDontSave;
  80. m_BlitMaterials[i].shader.hideFlags = HideFlags.HideAndDontSave;
  81. }
  82. return m_BlitMaterials[index];
  83. }
  84. /// Copies one render texture onto another.
  85. /// This function copies /source/ onto /dest/, optionally using a custom blend mode.
  86. /// If /blendMode/ is left out, the default operation is simply to copy one texture on to another.
  87. /// This function will copy the whole source texture on to the whole destination texture. If the sizes differ,
  88. /// the image in the source texture will get stretched to fit.
  89. /// The source and destination textures cannot be the same.
  90. public static void Blit (RenderTexture source, RenderTexture dest, BlendMode blendMode) {
  91. Blit (source, new Rect (0,0,1,1), dest, new Rect (0,0,1,1), blendMode);
  92. }
  93. public static void Blit (RenderTexture source, RenderTexture dest) {
  94. Blit (source, dest, BlendMode.Copy);
  95. }
  96. /// Copies one render texture onto another.
  97. public static void Blit (RenderTexture source, Rect sourceRect, RenderTexture dest, Rect destRect, BlendMode blendMode) {
  98. // Make the destination texture the target for all rendering
  99. RenderTexture.active = dest;
  100. // Assign the source texture to a property from a shader
  101. source.SetGlobalShaderProperty ("__RenderTex");
  102. bool invertY = source.texelSize.y < 0.0f;
  103. // Set up the simple Matrix
  104. GL.PushMatrix ();
  105. GL.LoadOrtho ();
  106. Material blitMaterial = GetBlitMaterial(blendMode);
  107. for (int i = 0; i < blitMaterial.passCount; i++) {
  108. blitMaterial.SetPass (i);
  109. DrawQuad(invertY);
  110. }
  111. GL.PopMatrix ();
  112. }
  113. public static void BlitWithMaterial (Material material, RenderTexture source, RenderTexture destination)
  114. {
  115. Graphics.Blit (source, destination, material);
  116. }
  117. public static void RenderDistortion (Material material, RenderTexture source, RenderTexture destination, float angle, Vector2 center, Vector2 radius)
  118. {
  119. bool invertY = source.texelSize.y < 0.0f;
  120. if (invertY) {
  121. center.y = 1.0f-center.y;
  122. angle = -angle;
  123. }
  124. Matrix4x4 rotationMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0, 0, angle), Vector3.one);
  125. material.SetMatrix("_RotationMatrix", rotationMatrix);
  126. material.SetVector("_CenterRadius", new Vector4(center.x,center.y,radius.x,radius.y) );
  127. material.SetFloat("_Angle", angle * Mathf.Deg2Rad);
  128. Graphics.Blit (source, destination, material);
  129. }
  130. public static void DrawQuad(bool invertY)
  131. {
  132. GL.Begin (GL.QUADS);
  133. float y1, y2;
  134. if (invertY) {
  135. y1 = 1.0f; y2 = 0.0f;
  136. } else {
  137. y1 = 0.0f; y2 = 1.0f;
  138. }
  139. GL.TexCoord2( 0.0f, y1 ); GL.Vertex3( 0.0f, 0.0f, 0.1f );
  140. GL.TexCoord2( 1.0f, y1 ); GL.Vertex3( 1.0f, 0.0f, 0.1f );
  141. GL.TexCoord2( 1.0f, y2 ); GL.Vertex3( 1.0f, 1.0f, 0.1f );
  142. GL.TexCoord2( 0.0f, y2 ); GL.Vertex3( 0.0f, 1.0f, 0.1f );
  143. GL.End();
  144. }
  145. public static void DrawGrid (int xSize, int ySize)
  146. {
  147. GL.Begin (GL.QUADS);
  148. float xDelta = 1.0F / xSize;
  149. float yDelta = 1.0F / ySize;
  150. for (int y=0;y<xSize;y++)
  151. {
  152. for (int x=0;x<ySize;x++)
  153. {
  154. GL.TexCoord2 ((x+0) * xDelta, (y+0) * yDelta); GL.Vertex3 ((x+0) * xDelta, (y+0) * yDelta, 0.1f);
  155. GL.TexCoord2 ((x+1) * xDelta, (y+0) * yDelta); GL.Vertex3 ((x+1) * xDelta, (y+0) * yDelta, 0.1f);
  156. GL.TexCoord2 ((x+1) * xDelta, (y+1) * yDelta); GL.Vertex3 ((x+1) * xDelta, (y+1) * yDelta, 0.1f);
  157. GL.TexCoord2 ((x+0) * xDelta, (y+1) * yDelta); GL.Vertex3 ((x+0) * xDelta, (y+1) * yDelta, 0.1f);
  158. }
  159. }
  160. GL.End();
  161. }
  162. }