forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactionDiffusion.cs
67 lines (61 loc) · 1.6 KB
/
ReactionDiffusion.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Edit -> Project Settings -> Graphics -> Always Included Shader -> ReactionDiffusion
// Add script to quad.
using UnityEngine;
public class ReactionDiffusion : MonoBehaviour
{
Material material;
RenderTexture input, output;
bool swap = true;
int frame = 0;
void Blit(RenderTexture source, RenderTexture destination, Material mat)
{
RenderTexture.active = destination;
mat.SetTexture("_MainTex", source);
GL.PushMatrix();
GL.LoadOrtho();
GL.invertCulling = true;
mat.SetPass(0);
GL.Begin(GL.QUADS);
GL.MultiTexCoord2(0, 0.0f, 0.0f);
GL.Vertex3(0.0f, 0.0f, 0.0f);
GL.MultiTexCoord2(0, 1.0f, 0.0f);
GL.Vertex3(1.0f, 0.0f, 0.0f);
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, 0.0f);
GL.End();
GL.invertCulling = false;
GL.PopMatrix();
}
void OnEnable ()
{
material = new Material(Shader.Find("ReactionDiffusion"));
input = new RenderTexture(1024, 1024, 0, RenderTextureFormat.ARGB32);
output = new RenderTexture(1024, 1024, 0, RenderTextureFormat.ARGB32);
GetComponent<Renderer>().material = material;
}
void Update ()
{
material.SetInt("iFrame",frame);
if (swap)
{
material.SetTexture("_MainTex", input);
Blit(input,output,material);
material.SetTexture("_Buffer", output);
}
else
{
material.SetTexture("_MainTex", output);
Blit(output,input,material);
material.SetTexture("_Buffer", input);
}
swap = !swap;
frame++;
}
void OnDestroy ()
{
input.Release();
output.Release();
}
}