-
Notifications
You must be signed in to change notification settings - Fork 18
/
WvN.DelphiShader.FX.InfiniteFractalRoads.pas
295 lines (249 loc) · 7.11 KB
/
WvN.DelphiShader.FX.InfiniteFractalRoads.pas
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
unit WvN.DelphiShader.FX.InfiniteFractalRoads;
interface
uses GR32, Types, WvN.DelphiShader.Shader;
type
TInfiniteFractalRoads = class(TShader)
const
ang = -37 * (pi / 180);
vec3_1: vec3 = (x: 0; y: - 0.15; z: - 1);
vec3_2: vec3 = (x: 0; y: - 1; z: 0);
vec3_3: vec3 = (x: 1; y: 1; z: 0);
vec3_4: vec3 = (x: 1; y: 1; z: 0);
vec3_5: vec3 = (x: 0.8; y: 0.9; z: 1);
vec3_6: vec3 = (x: 1; y: 0.9; z: 0.65);
vec3_7: vec3 = (x: 1; y: 0.9; z: 0.7);
vec3_8: vec3 = (x: 0.6; y: 0.8; z: 1);
vec3_9: vec3 = (x: 1; y: 0.95; z: 0.8);
vec3_10: vec3 = (x: 1; y: 0.95; z: 0.93);
vec3_11: vec3 = (x: 0.05; y: 0.02; z: 0);
vec3_12: vec3 = (x: 1.1; y: 1.03; z: 1);
vec3_13: vec3 = (x: 1; y: 0.85; z: 0.7);
v3_2: vec3 = (x: 2; y: 2; z: 2);
Iterations = 13;
width = 0.22;
detail = 0.00003;
Scale = 2.30;
var
t, h: float;
v2:Vec2;
from : vec3;
FMouse:Vec2;
function de(const pos: vec3): float;
function normal(const p: vec3): vec3;
function shadow(const pos, sdir: vec3): float;
function AO(const pos, nor: vec3): float;
function light(const p, dir: vec3): vec3;
function raymarch(const from, dir: vec3): vec3;
function Main(var gl_FragCoord: Vec2): TColor32;
var
lightdir : vec3;
skydir : vec3;
rota : mat2;
rota2 : mat2;
ot : float;
det : float;
constructor Create; override;
procedure PrepareFrame;
end;
var
InfiniteFractalRoads: TShader;
implementation
uses SysUtils, Math;
constructor TInfiniteFractalRoads.Create;
begin
inherited;
FrameProc := PrepareFrame;
PixelProc := Main;
Det := 0;
end;
procedure TInfiniteFractalRoads.PrepareFrame;
var ca,sa, cam,sam:double;
begin
lightdir := normalize(vec3_1);
skydir := normalize(vec3_2);
ca := system.Cos(ang);
sa := system.Sin(ang);
cam := system.Cos(-ang);
sam := system.Sin(-ang);
rota := mat2.Create(ca , sa , -sa , ca);
rota2 := mat2.Create(cam,sam, -sam, cam);
ot := 0;
det := 0;
t := radians(45 - iGlobalTime * 10);
h := system.sin(iGlobalTime * 0.2) * 0.0031;
Fmouse := (iMouse.xy/resolution.xy-0.5);
if Fmouse.x < -0.499 then
Fmouse := vec2Black;
v2 := Vec2.Create(iGlobalTime * 0.15);
from := vec3.Create(-4.975 + h, 1.256 - h, -3 + iGlobalTime * 0.005);
end;
// "Infinite Fractal Roads" by Kali
function TInfiniteFractalRoads.de(const pos: vec3): float;
var
DEfactor: float;
p : vec3;
i : integer;
r2 : float;
begin
DEfactor := Scale;
p := pos;
ot := 1000;
for i := 0 to Iterations - 1 do
begin
p := v3_2 - abs(abs(p.xyz + vec3_4) - v3_2) - vec3_3;
r2 := dot(p.xyz, p.xyz);
ot := clamp(ot, 0, r2);
p := p * (Scale / clamp(r2));
DEfactor := DEfactor * (Scale / clamp(r2, 0, 1));
p := p + pos.yxz;
end;
Exit(length(p.xyz) / DEfactor);
end;
function TInfiniteFractalRoads.normal(const p: vec3): vec3;
var
e: vec3;
begin
e := Default(vec3);
e.y := det;
Result := normalize(vec3.Create(
de(p + e.yxx) - de(p - e.yxx),
de(p + e.xyx) - de(p - e.xyx),
de(p + e.xxy) - de(p - e.xxy)));
end;
function TInfiniteFractalRoads.shadow(const pos, sdir: vec3): float;
var
totalDist: float;
sh : float;
steps : integer;
p : vec3;
dist : float;
begin
totalDist := 5 * detail;
sh := 1;
for steps := 0 to 19 do
begin
if totalDist < 0.3 then
begin
p := pos - totalDist * sdir;
dist := de(p);
if dist < det then
sh := 0.2;
totalDist := totalDist + (dist);
end;
end;
Exit(sh);
end;
function TInfiniteFractalRoads.AO(const pos, nor: vec3): float;
var
i,
aoi : integer;
totao: float;
sca : float;
hr : float;
aopos: vec3;
dd : float;
begin
// by iq...
aoi := 0;
totao := 0;
sca := 20;
for I := 0 to 4 do
begin
hr := 0.015 + 0.01 * aoi * aoi;
aopos := nor * hr + pos;
dd := de(aopos);
totao := totao + (-(dd - hr) * sca);
sca := sca * (0.4);
Inc(aoi);
end;
Exit(1 - clamp(totao, 0, 1));
end;
function TInfiniteFractalRoads.light(const p, dir: vec3): vec3;
var
n : vec3;
sh : float;
_ao : float;
diff: float;
amb : float;
r : vec3;
spec: float;
begin
n := normal(p);
sh := shadow(p, lightdir);
_ao := AO(p, n);
diff := Math.max(0, dot(lightdir, -n));
amb := (0.2 + 0.6 * Math.max(0, dot(normalize(skydir), -n))) * _ao;
r := reflect(lightdir, n);
spec := Math.max(0, dot(dir, -r));
Exit((diff * 1.5 + pow(spec, 20) * 0.4) * sh * vec3_6 + amb * vec3_5);
end;
function TInfiniteFractalRoads.raymarch(const from, dir: vec3): vec3;
var
st : float;
d : float;
totdist: float;
p : vec3;
col : vec3;
i : integer;
l : float;
backg : vec3;
begin
st := 0;
d := 1;
totdist := 0;
for i := 0 to 139 do
begin
if (d > det) and (totdist < 0.5) then
begin
p := from + totdist * dir;
d := de(p) * 0.8;
det := detail * (1 + totdist * 50);
totdist := totdist + (d);
st := st + (max(0, 0.032 - d));
end;
end;
l := pow(Math.max(0, dot(normalize(-dir), normalize(lightdir))), 4);
backg := vec3_8 * 0.4 * (0.4 + clamp(l, 0, 0.6)) + vec3_7 * l * 0.5;
if d < det then
begin
ot := clamp(pow(ot, 2) * 4);
col := 0.5 + vec3.Create(ot * ot, ot, ot * ot * ot * 1.1) * 0.5;
col := col * (light(p - det * dir, dir));
col := mix(col, backg, 1 - exp(-9 * totdist * totdist));
end
else
begin
col := backg * (1 + texture2D(tex[0], dir.xy * rota2 * 0.5 - Vec2.Create(0, iGlobalTime * 0.004)).z * 0.25);
end;
Exit(col + st * vec3_9 * 0.15);
end;
function TInfiniteFractalRoads.Main(var gl_FragCoord: Vec2): TColor32;
var
uv : Vec2;
uv2 : Vec2;
dir : vec3;
col : vec3;
flare: vec3;
begin
uv := gl_FragCoord.xy / resolution.xy * 2 - 1;
uv.y := uv.y * (resolution.y / resolution.x);
uv2 := uv;
uv := uv + (Fmouse * 1.5);
uv.y := uv.y - (0.1);
uv := uv * (rota);
uv := uv + ((texture2D(tex[14], v2).xy - 0.5) * Math.max(0, h) * 7);
dir := normalize(vec3.Create(uv * 1.3, 1));
col := raymarch(from, dir);
flare := vec3_10 * pow(max(0, 0.8 - length(uv2 - Fmouse * 1.5)) / 0.8, 1.5) * 0.3;
col := col + (flare * dot(uv2, uv2));
col := col * (length(clamp((0.6 - pow(abs(uv2), vec2_3_3)), Vec2Black, Vec2White)));
col := col * vec3_12 + vec3_11;
col := col + (vec3_13 * power(max(0, 0.3 - length(uv)) / 0.3, 2) * 0.5);
Result := TColor32(col);
end;
initialization
InfiniteFractalRoads := TInfiniteFractalRoads.Create;
Shaders.Add('InfiniteFractalRoads', InfiniteFractalRoads);
finalization
FreeandNil(InfiniteFractalRoads);
end.