Skip to content

Commit 1b783c5

Browse files
committed
Initial commit.
1 parent 1bd7aa2 commit 1b783c5

17 files changed

+500
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[Ll]ibrary/
2+
[Tt]emp/
3+
[Oo]bj/
4+
[Bb]uild/
5+
[Pp]rojectSettings/
6+
7+
# Autogenerated VS/MD solution and project files
8+
*.csproj
9+
*.unityproj
10+
*.sln
11+
*.suo
12+
*.user
13+
*.userprefs
14+
*.pidb
15+
*.booproj
16+
17+
# Unity3D Generated File On Crash Reports
18+
sysinfo.txt

Captures/suibokuga.gif

988 KB
Loading

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
unity-cellular-automaton-based-ink-simulation
2+
=====================
3+
4+
Cellular automaton based ink simulation for Unity.
5+
6+
![suibokuga](https://raw.githubusercontent.com/mattatz/unity-cellular-automaton-based-ink-simulation/master/Captures/suibokuga.gif)
7+
8+
## Sources
9+
10+
- Simple cellular automation-based simulation of ink behaviour and its application to Suibokuga-like 3D rendering of trees - http://kucg.korea.ac.kr/seminar/2004/src/PA-04-09.pdf
11+

Suibokuga/Assets/Scenes.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Suibokuga/Assets/Scenes/Test.unity

10.8 KB
Binary file not shown.

Suibokuga/Assets/Scenes/Test.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Suibokuga/Assets/Scripts.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
namespace Utils {
6+
public class FboPingPong {
7+
8+
private int _readTex = 0;
9+
private int _writeTex = 1;
10+
private RenderTexture[] _buffer;
11+
12+
/// <summary>
13+
/// Init the specified width_ and height_.
14+
/// </summary>
15+
/// <param name="width_">Width_.</param>
16+
/// <param name="height_">Height_.</param>
17+
public FboPingPong (int width_, int height_, FilterMode filterMode = FilterMode.Point, TextureWrapMode wrapMode = TextureWrapMode.Repeat){
18+
19+
_readTex = 0;
20+
_writeTex = 1;
21+
22+
_buffer = new RenderTexture [2];
23+
24+
for (int i = 0; i < 2; i++){
25+
_buffer [i] = new RenderTexture (width_, height_, 0, RenderTextureFormat.ARGBFloat);
26+
_buffer [i].hideFlags = HideFlags.DontSave;
27+
_buffer [i].filterMode = filterMode;
28+
_buffer [i].wrapMode = wrapMode;
29+
_buffer [i].Create ();
30+
}
31+
32+
Clear ();
33+
}
34+
35+
/// <summary>
36+
/// Swap buffers.
37+
/// </summary>
38+
public void Swap (){
39+
//RenderTexture temp = _buffer[0];
40+
//_buffer [0] = _buffer [1];
41+
//_buffer [1] = temp;
42+
int t = _readTex;
43+
_readTex = _writeTex;
44+
_writeTex = t;
45+
}
46+
47+
/// <summary>
48+
/// Clear buffers.
49+
/// </summary>
50+
public void Clear (){
51+
for (int i = 0; i < _buffer.Length; i++){
52+
RenderTexture.active = _buffer [i];
53+
GL.Clear (false, true, Color.black);
54+
RenderTexture.active = null;
55+
}
56+
}
57+
58+
/// <summary>
59+
/// Delete buffers.
60+
/// </summary>
61+
public void Delete (){
62+
if (_buffer != null) {
63+
for (int i = 0; i < _buffer.Length; i++){
64+
_buffer[i].Release ();
65+
_buffer[i].DiscardContents ();
66+
_buffer[i] = null;
67+
}
68+
}
69+
}
70+
71+
/// <summary>
72+
/// Gets the read tex.
73+
/// </summary>
74+
/// <returns>The read tex.</returns>
75+
public RenderTexture GetReadTex (){
76+
return _buffer [_readTex];
77+
}
78+
79+
/// <summary>
80+
/// Gets the write tex.
81+
/// </summary>
82+
/// <returns>The write tex.</returns>
83+
public RenderTexture GetWriteTex (){
84+
return _buffer [_writeTex];
85+
}
86+
87+
}
88+
89+
}
90+
91+

Suibokuga/Assets/Scripts/FboPingPong.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Suibokuga/Assets/Scripts/Suibokuga.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
using Utils;
5+
6+
[RequireComponent (typeof (Camera) ) ]
7+
public class Suibokuga : MonoBehaviour {
8+
9+
[SerializeField] Shader shader;
10+
Material mat;
11+
12+
[SerializeField] Texture2D paper;
13+
[SerializeField, Range(0f, 1f)] float alpha = 1.0f;
14+
[SerializeField, Range(0f, 0.005f)] float evaporation = 0.0001f;
15+
16+
[SerializeField, Range(0, 10)] int iterations = 4;
17+
[SerializeField] int size = 1024;
18+
19+
FboPingPong fpp;
20+
21+
bool dragging = false;
22+
Vector2 previous;
23+
24+
void Start () {
25+
fpp = new FboPingPong(size, size);
26+
mat = new Material(shader);
27+
28+
mat.SetTexture("_PaperTex", paper);
29+
30+
Init();
31+
}
32+
33+
void Update () {
34+
35+
Vector3 mousePos = Input.mousePosition;
36+
Vector2 current = new Vector2(mousePos.x / Screen.width, mousePos.y / Screen.height);
37+
mat.SetVector("_Prev", previous);
38+
39+
if(dragging) {
40+
mat.SetVector("_Brush", new Vector3(current.x, current.y, 0.015f));
41+
} else {
42+
mat.SetVector("_Brush", new Vector3(0, 0, 0));
43+
}
44+
45+
if(Input.GetMouseButtonDown(0)) {
46+
dragging = true;
47+
} else if(Input.GetMouseButtonUp(0)) {
48+
dragging = false;
49+
}
50+
51+
previous = current;
52+
53+
mat.SetFloat("_Alpha", alpha);
54+
mat.SetFloat("_Evaporation", evaporation);
55+
56+
for(int i = 0; i < iterations; i++) {
57+
Graphics.Blit(fpp.GetReadTex(), fpp.GetWriteTex(), mat, 1); // update
58+
fpp.Swap();
59+
}
60+
}
61+
62+
void Init () {
63+
Graphics.Blit(fpp.GetReadTex(), fpp.GetWriteTex(), mat, 0); // init
64+
fpp.Swap();
65+
}
66+
67+
void OnRenderImage (RenderTexture src, RenderTexture dst) {
68+
Graphics.Blit(fpp.GetReadTex(), dst, mat, 2);
69+
}
70+
71+
void OnGUI () {
72+
const float offset = 10;
73+
if(GUI.Button(new Rect(offset, offset, 100, 30), "Reset")) {
74+
Init();
75+
}
76+
}
77+
78+
}

Suibokuga/Assets/Scripts/Suibokuga.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Suibokuga/Assets/Shaders.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)