-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathWvN.DelphiShader.FX.MengerSponge.pas
207 lines (169 loc) · 4.78 KB
/
WvN.DelphiShader.FX.MengerSponge.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
unit WvN.DelphiShader.FX.MengerSponge;
interface
uses GR32, Types, WvN.DelphiShader.Shader, Math;
type
TMengerSponge = class(TShader)
const
pi2=pi*2;
vec3_1: vec3 = (x: 1.0; y: 0.97; z: 0.85);
vec3_2: vec3 = (x: 1.0; y: 0.97; z: 0.85);
vec3_3: vec3 = (x: 0.1; y: 0.15; z: 0.2);
precis = 0.001;
eps100:vec3 = (x: precis; y: 0; z: 0);
eps010:vec3 = (x: 0; y: precis; z: 0);
eps001:vec3 = (x: 0; y: 0; z: precis);
var
light,light2 : vec3;
ro, ww, uu, vv, rd, col: vec3;
function calcNormal(const pos: vec3): vec3;
constructor Create; override;
procedure PrepareFrame;
function RenderPixel(var gl_FragCoord: Vec2): TColor32;
end;
var
MengerSponge: TShader;
implementation
uses SysUtils;
const
half = 0.5;
constructor TMengerSponge.Create;
begin
inherited;
FrameProc := PrepareFrame;
PixelProc := RenderPixel;
end;
function maxcomp(const p: vec3): float; inline;
begin
Result := Math.Max(p.x, Math.Max(p.y, p.z));
end;
function sdBox(const p: vec3; b: vec3): float;inline;
var
di: vec3;
mc: float;
begin
di := abs(p) - b;
mc := maxcomp(di);
Result := Math.min(mc, length(Max(di, 0)));
end;
function map(const p: vec3): vec3;
var
s : float;
da, db, dc, c, d: float;
a, r : vec3;
m : Integer;
begin
d := sdBox(p, vec3White);
result.x := d;
result.y := 1;
result.z := 0;
s := 1.0;
for m := 0 to 4 do
begin
a.x := fmods(p.x * s, 2) - 1;
a.y := fmods(p.y * s, 2) - 1;
a.z := fmods(p.z * s, 2) - 1;
s := s * 3;
r := abs(1 - 3 * abs(a));
da := Math.Max(r.x, r.y);
db := Math.Max(r.y, r.z);
dc := Math.Max(r.z, r.x);
c := (Math.min(da, Math.min(db, dc)) - 1) / s;
if c > d then
begin
d := c;
result.x := d;
result.y := 0.2 * da * db * dc;
result.z := (1 + m) / 4;
end;
end;
end;
function intersect(const ro, rd: vec3): vec4;
var
t: TVecType;
i: Integer;
h: vec4;
begin
t := 0;
for i := 0 to 63 do
begin
h.xyz := map(ro + rd * t);
if (h.x < 0.005) then
begin
Result := h;
Result.x := t;
Exit;
end;
t := t + h.x;
end;
Result.x := -1;
end;
function TMengerSponge.calcNormal(const pos: vec3): vec3;
begin
Result.x := map(pos + eps100).x - map(pos - eps100).x;
Result.y := map(pos + eps010).x - map(pos - eps010).x;
Result.z := map(pos + eps001).x - map(pos - eps001).x;
Result.NormalizeSelf;
end;
procedure TMengerSponge.PrepareFrame;
var
cTime: Double;
begin
// light
light := normalize(vec3.Create(1.0, 0.8, -0.6));
light2 := vec3.Create( -light.x, light.y, -light.z);
cTime := iGlobalTime;
// camera
ro := 1.1 * vec3.Create(2.5 * cosLarge(0.5 * cTime), 1.5 * cosLarge(cTime * 0.23), 2.5 * sinLarge(0.5 * cTime));
ww := normalize(vec3Black - ro);
uu := normalize(cross(vec3Green, ww));
vv := normalize(cross(ww, uu));
end;
function TMengerSponge.RenderPixel(var gl_FragCoord: Vec2): TColor32;
var
p : Vec2;
matcol, pos, nor : vec3;
shadow, tmat : vec4;
dif1, dif2, ldis, ao: float;
begin
p.x := -1.0 + 2.0 * gl_FragCoord.x / Resolution.x;
p.y := -1.0 + 2.0 * gl_FragCoord.y / Resolution.y;
p.x := p.x * 1.33;
// p.x := gl_FragCoord.x;
// p.y := gl_FragCoord.y;
rd := normalize(p.x * uu + p.y * vv + 1.5 * ww);
col := vec3Black;
tmat := intersect(ro, rd);
if (tmat.x > 0) then
begin
pos := ro + tmat.x * rd;
nor := calcNormal(pos);
dif1 := Max(0.4 + 0.6 * dot(nor, light), 0);
dif2 := Max(0.4 + 0.6 * dot(nor, light2), 0.0);
// shadow
ldis := 4;
shadow := intersect(pos + light * ldis, -light);
if ((shadow.x > 0) and (shadow.x < (ldis - 0.01))) then
dif1 := 0;
ao := tmat.y;
col := (1.0 * ao * vec3.Create(0.2, 0.2, 0.2))
+ (2 * (0.5 + 0.5 * ao) * dif1 * vec3_1)
+ (0.2 * (0.5 + 0.5 * ao) * dif2 * vec3_2)
+ (1 * (0.5 + 0.5 * ao) * (0.5 + 0.5 * nor.y) * vec3_3);
// gamma lighting
col.x := col.x * 0.5 + 0.5 * system.sqrt(col.x) * 1.2;
col.y := col.y * 0.5 + 0.5 * system.sqrt(col.y) * 1.2;
col.z := col.z * 0.5 + 0.5 * system.sqrt(col.z) * 1.2;
matcol.x := 0.6 + 0.4 * system.cos(5.0 + pi2 * tmat.z);
matcol.y := 0.6 + 0.4 * system.cos(5.4 + pi2 * tmat.z);
matcol.z := 0.6 + 0.4 * system.cos(5.7 + pi2 * tmat.z);
col := col * matcol;
col := col * (1.5 * System.Exp(-0.5 * tmat.x));
end;
Result := TColor32(col);
end;
initialization
MengerSponge := TMengerSponge.Create;
Shaders.Add('MengerSponge', MengerSponge);
finalization
FreeandNil(MengerSponge);
end.