forked from rodrigomas/DCTLs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blob.dctl
260 lines (218 loc) · 5.32 KB
/
Blob.dctl
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Blob DCTL
#define BUMP_FACTOR 2
__DEVICE__ float2 add(float2 A, float2 B)
{
float2 C;
C.x = A.x + B.x;
C.y = A.y + B.y;
return C;
}
__DEVICE__ float2 add(float2 A, float2 B, float2 C)
{
float2 D;
D.x = A.x + B.x + C.x;
D.y = A.y + B.y + C.y;
return D;
}
__DEVICE__ float3 add(float3 A, float3 B)
{
float3 C;
C.x = A.x + B.x;
C.y = A.y + B.y;
C.z = A.z + B.z;
return C;
}
__DEVICE__ float dot(float2 A, float2 B)
{
float C = A.x * B.x + A.y * B.y;
return C;
}
__DEVICE__ float dot(float3 A, float3 B)
{
float C = A.x * B.x + A.y * B.y + A.z * B.z;
return C;
}
__DEVICE__ float2 floor(float2 A)
{
float2 B;
B.x = _floor(A.x);
B.y = _floor(A.y);
return B;
}
__DEVICE__ float fract(float A)
{
float B;
B = A - _floor(A);
return B;
}
__DEVICE__ float2 fract(float2 A)
{
float2 B;
B.x = A.x - _floor(A.x);
B.y = A.y - _floor(A.y);
return B;
}
__DEVICE__ float length(float3 A)
{
float B = _sqrtf(A.x * A.x + A.y * A.y + A.z * A.z);
return B;
}
__DEVICE__ float2 minus(float2 A, float2 B)
{
float2 C;
C.x = A.x - B.x;
C.y = A.y - B.y;
return C;
}
__DEVICE__ float3 minus(float3 A, float3 B)
{
float3 C;
C.x = A.x - B.x;
C.y = A.y - B.y;
C.z = A.z - B.z;
return C;
}
__DEVICE__ float2 multi(float2 A, float B)
{
float2 C;
C.x = A.x * B;
C.y = A.y * B;
return C;
}
__DEVICE__ float2 multi(float2 A, float2 B)
{
float2 C;
C.x = A.x * B.x;
C.y = A.y * B.y;
return C;
}
__DEVICE__ float3 multi(float3 A, float B)
{
float3 C;
C.x = A.x * B;
C.y = A.y * B;
C.z = A.z * B;
return C;
}
__DEVICE__ float3 multi(float3 A, float3 B)
{
float3 C;
C.x = A.x * B.x;
C.y = A.y * B.y;
C.z = A.z * B.z;
return C;
}
__DEVICE__ float2 multi(float4 A, float2 B)
{
float2 C;
C.x = B.x * A.x + B.y * A.y;
C.y = B.x * A.z + B.y * A.w;
return C;
}
__DEVICE__ float3 normalize(float3 A)
{
float3 B;
float C = _sqrtf((A.x * A.x) + (A.y * A.y) + (A.z * A.z));
B.x = A.x / C;
B.y = A.y / C;
B.z = A.z / C;
return B;
}
__DEVICE__ float3 reflect(float3 A, float3 B)
{
float3 C;
C.x = A.x - 2.0f * dot(B, A) * B.x;
C.y = A.y - 2.0f * dot(B, A) * B.y;
C.z = A.z - 2.0f * dot(B, A) * B.z;
return C;
}
__DEVICE__ float2 hash22(float2 p)
{
float n = _sinf(dot(p, make_float2(41.0f, 289.0f)));
return fract(multi(make_float2(262144.0f, 32768.0f), n));
}
__DEVICE__ float Voronoi(float2 p)
{
float2 ip = floor(p);
p = fract(p);
float d = 1.0f;
for (float i = -1.0f; i < 1.1f; i++){
for (float j = -1.0f; j < 1.1f; j++){
float2 cellRef = make_float2(i, j);
float2 offset = hash22(add(ip, cellRef));
float2 r = add(cellRef, offset, make_float2(-p.x, -p.y));
float d2 = dot(r, r);
d = _fminf(d, d2);
}
}
return _sqrtf(d);
}
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, __TEXTURE__ p_TexR, __TEXTURE__ p_TexG, __TEXTURE__ p_TexB)
{
//float iTime = _tex2D(p_TexR, 500, 500) / 24;
float iTime = _tex2D(p_TexR, 500, 500) * 10;
float X = (float)p_X;
float Y = (float)(p_Height - p_Y);
float2 XY = make_float2(X, Y);
float2 resolution = make_float2((float)p_Width, (float)p_Height);
float2 uv;
uv.x = (X - resolution.x * 0.5f) / resolution.y;
uv.y = (Y - resolution.y * 0.5f) / resolution.y;
float t = iTime, s, a, b, e;
float th = _sinf(iTime * 0.1f) * _sinf(iTime * 0.13f) * 4.0f;
float cs = _cosf(th), si = _sinf(th);
//uv *= mat2(cs, -si, si, cs);
uv = make_float2(uv.x * cs + uv.y * -si, uv.x * si + uv.y * cs);
float3 sp = make_float3(uv.x, uv.y, 0.0f);
float3 ro = make_float3(0.0f, 0.0f, -1.0f);
float3 rd = normalize(minus(sp, ro));
float3 lp = make_float3(_cosf(iTime) * 0.375f, _sinf(iTime) * 0.1f, -1.0f);
const float L = 8.0f;
const float gFreq = 0.5f;
float sum = 0.0f;
th = 3.14159265f * 0.7071f / L;
cs = _cosf(th), si = _sinf(th);
float4 M = make_float4(cs, -si, si, cs);
float3 col = make_float3(0.0f, 0.0f, 0.0f);
float f=0.0f, fx=0.0f, fy=0.0f;
float2 eps = make_float2(4.0f / resolution.y, 0.0f);
float2 offs = make_float2(0.1f, 0.1f);
for (float i = 0.0f; i < L; i++)
{
s = fract((i - t * 2.0f) / L);
e = _exp2f(s * L) * gFreq;
a = (1.0f - _cosf(s * 6.283f)) / e;
float2 spxy = make_float2(sp.x, sp.y);
float2 epsxy = make_float2(eps.x, eps.y);
float2 epsyx = make_float2(eps.y, eps.x);
f += Voronoi(add(multi(multi(M, spxy), e), offs)) * a;
fx += Voronoi(add(multi(multi(M, minus(spxy, epsxy)), e), offs)) * a;
fy += Voronoi(add(multi(multi(M, minus(spxy, epsyx)), e), offs)) * a;
sum += a;
M = make_float4(M.x * M.x + M.y * M.z, M.x * M.y + M.y * M.w, M.z * M.x + M.w * M.z, M.z * M.y + M.w * M.w);
}
sum = _fmaxf(sum, 0.001f);
f /= sum;
fx /= sum;
fy /= sum;
float bumpFactor = BUMP_FACTOR / 10.0f;
fx = (fx - f) / eps.x;
fy = (fy - f) / eps.x;
float3 n = normalize( add(make_float3(0.0f, 0.0f, -1.0f), multi(make_float3(fx, fy, 0.0f), bumpFactor)) );
float3 ld = minus(lp, sp);
float lDist = _fmaxf(length(ld), 0.001);
ld.x /= lDist;
ld.y /= lDist;
ld.z /= lDist;
float atten = 1.25f / (1.0f + lDist * 0.15f + lDist * lDist * 0.15f);
float diff = _fmaxf(dot(n, ld), 0.0f);
diff = _powf(diff, 2.0f) * 0.66f + _powf(diff, 4.0f) * 0.34f;
float spec = _powf(_fmaxf(dot( reflect(make_float3(-ld.x, -ld.y, -ld.z), n), make_float3(-rd.x, -rd.y, -rd.z)), 0.0f), 16.0f);
float3 objCol = make_float3(f * f, _powf(f, 5.0f) * 0.05f, f * f * 0.36f);
col = multi(add(multi(objCol, (diff + 0.5f)), multi(make_float3(0.4f, 0.6f, 1.0f), spec * 1.5f)), atten);
float3 COL;
COL.x = _sqrtf(_fminf(col.x, 1.0f));
COL.y = _sqrtf(_fminf(col.y, 1.0f));
COL.z = _sqrtf(_fminf(col.z, 1.0f));
return COL;
}