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