Skip to content

Commit ea99431

Browse files
ShaderToy buffer to Unity example.
1 parent 6d54d6c commit ea99431

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Diff for: fluid_dynamics.compute

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//Simplified version of shader, which full version was created by Florian Berger: https://www.shadertoy.com/view/MsGSRd
2+
//Translated from ShaderToy to Unity by Przemyslaw Zaworski, 18.12.2017, https://github.com/przemyslawzaworski/Unity3D-CG-programming
3+
//Additional support: Seyed Morteza Kamaly, https://github.com/smkplus
4+
//To simulate self-accumulated buffer, I use two render textures which are swapped between every render frame.
5+
//Usage: apply fluid_dynamics.cs script to gameobject, select fluid_dynamics.compute and any material with _MainTex free slot,
6+
//for example built-in Unlit/Texture.
7+
8+
#pragma kernel CSMain
9+
10+
Texture2D<float4> reader;
11+
RWTexture2D<float4> writer;
12+
SamplerState _LinearClamp;
13+
14+
[numthreads(8,8,1)]
15+
void CSMain (uint2 id : SV_DispatchThreadID)
16+
{
17+
float2 f = float2(id.x,id.y); //fragCoord
18+
float2 b = float2(0.31,0.95);
19+
float2 iResolution=float2(1024,1024); //texture resolution
20+
float2 v = float2(0.0,0.0);
21+
float4 c = float4(0,0,0,1); //initial color
22+
for(int l=0;l<20;l++)
23+
{
24+
if ( dot(b,b) > pow(iResolution.y,2.0) ) break;
25+
float2 p = b;
26+
for(int i=0;i<5;i++)
27+
{
28+
float2 pos = f+p;
29+
float rot=0.0;
30+
for(int i=0;i<5;i++)
31+
{
32+
rot+=dot(reader.SampleLevel(_LinearClamp,float2(frac((pos+p)/iResolution.xy)),0).xy-float2(0.5,0.5),mul(float2(1,-1),p.yx));
33+
p=float2(0.31*p.x+0.95*p.y,-0.95*p.x+0.31*p.y);
34+
}
35+
v+=p.yx* rot/5.0/dot(b,b);
36+
p=float2(0.31*p.x+0.95*p.y,-0.95*p.x+0.31*p.y);
37+
}
38+
b*=2.0;
39+
}
40+
float4 color=reader.SampleLevel(_LinearClamp,frac((f+v*float2(-2,2))/iResolution.xy),0);
41+
float2 s=(f.xy/iResolution.xy)*2.0-float2(1.0,1.0);
42+
color.xy += (0.01*s.xy / (dot(s,s)/0.1+0.3));
43+
c = color;
44+
writer[id]=c;
45+
}

0 commit comments

Comments
 (0)