-
Notifications
You must be signed in to change notification settings - Fork 18
/
WvN.DelphiShader.FX.Displacement.pas
166 lines (127 loc) · 3.65 KB
/
WvN.DelphiShader.FX.Displacement.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
unit WvN.DelphiShader.FX.Displacement;
interface
uses GR32, Types, WvN.DelphiShader.Shader;
type
TDisplacement = class(TShader)
vuv, vrp, prp, vpn,vcv,u, v :Vec3;
constructor Create; override;
procedure PrepareFrame;
function opDisplace(const p:vec3 ):float;inline;
function _displacement(const p:vec3):float;inline;
function RenderPixel(var gl_FragCoord: Vec2): TColor32;
function sceneDistance (const p:vec3):Float;
end;
var
Displacement: TShader;
implementation
uses SysUtils, Math;
const
NaN:integer=256;
c2:vec3=(x:0.1;y:0.1;z:1);
e:vec3=(x:0.1;y:0;z:0);
e_xyy:vec3=(x:0.1;y:0;z:0);
e_yxy:vec3=(x:0;y:0.1;z:0);
e_yyx:vec3=(x:0;y:0;z:0.1);
maxd=64.0; //Max depth
constructor TDisplacement.Create;
begin
inherited;
FrameProc := PrepareFrame;
PixelProc := RenderPixel;
end;
function udRoundBox( const p,b:vec3;r:float):float;inline;
begin
Result := length(max(abs(p)-b,0.0))-r;
end;
{
http://glsl.heroku.com/e#6254.0 // cool blobs!
http://glsl.heroku.com/e#9118.1
http://glsl.heroku.com/e#9135.0
http://glsl.heroku.com/e#9027.0
http://glsl.heroku.com/e#8865.0
http://glsl.heroku.com/e#8806.1
http://glsl.heroku.com/e#8733.3
http://glsl.heroku.com/e#8338.3
http://glsl.heroku.com/e#8770.0 // raytrace
http://glsl.heroku.com/e#8566.0 // kleurtjes!
http://glsl.heroku.com/e#8305.2 // raytrace
}
const
vb:vec3=(x:0.75;y:0.2;z:0.5);
vc:vec3=(x:3;y:3;z:2.5);
v1:vec3=(x:0;y:1;z:0);
function TDisplacement._displacement(const p:vec3):float;
begin
Result := ( system.sin((1.75 * p.x) *
system.cos(iGlobalTime * pi)) *
system.sin((1.75 * p.y) *
-system.sin(iGlobalTime * pi)) *
system.sin(0.75 * p.z));
end;
function TDisplacement.opDisplace(const p:vec3 ):float;
var d1,d2:Float;
begin
d1 := udRoundBox(p, vec3.create(0.75,0.2,0.5), 0.15);
d2 := _displacement(p);
Result := d1+d2;
end;
function TDisplacement.sceneDistance (const p:vec3):Float;
var xx:Vec3;
begin
// repeater
xx := &mod (p,vc)-0.5*vc;
Result := opDisplace(xx);
end;
procedure TDisplacement.PrepareFrame;
begin
//Camera animation
vuv:=v1;//Change camere up vector here
vrp:=v1; //Change camere view here
prp:=vec3.create(0,2,iGlobalTime); //Change camera path position here
//Camera setup
vpn:=normalize(vrp-prp);
u:=normalize(cross(vuv,vpn));
v:=cross(vpn,u);
vcv:=(prp+vpn);
end;
function TDisplacement.RenderPixel(var gl_FragCoord: Vec2): TColor32;
var vPos:Vec2;
scrCoord, scp,c,p,n,fc:Vec3;
f,objDist,b:float;
I: Integer;
begin
vPos:=-1.0+2.0*gl_FragCoord.xy/resolution.xy;
vPos.x := vPos.x - 0.5;
scrCoord:=vcv+vPos.x*u*resolution.x / resolution.y+vPos.y*v;
scp:=normalize(scrCoord-prp);
//Raymarching
f:=0.0;
objDist:=1.0;
for I := 0 to NaN-1 do
begin
if (System.abs(objDist)<0.005) or (f>maxd) then
break;
f:=f +objDist;
p:=prp+scp*f;
objDist:=sceneDistance(p);
end;
if (f<maxd) then
begin
c := c2;
n:=normalize(
vec3.create(objDist-sceneDistance(p-e_xyy),
objDist-sceneDistance(p-e_yxy),
objDist-sceneDistance(p-e_yyx)));
b:=dot(n,-scp);
fc := b*c;
Result := TColor32((b*c+pow(b,8))*(1-f*0.01));//simple phong LightPosition=CameraPosition
end
else
Result := clBlack32; //background color
end;
initialization
Displacement := TDisplacement.Create;
Shaders.Add('Displacement', Displacement);
finalization
FreeandNil(Displacement);
end.