-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathWvN.DelphiShader.FX.AngelicParticles.pas
126 lines (94 loc) · 2.75 KB
/
WvN.DelphiShader.FX.AngelicParticles.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
unit WvN.DelphiShader.FX.AngelicParticles;
interface
uses GR32, Types, WvN.DelphiShader.Shader;
type
TAngelicParticles=class(TShader)
const
vec12 :vec3=(x:1.2;y:1.2;z:1.2);
var
ray:Vec3;
constructor Create;override;
procedure PrepareFrame;
function RenderPixel(var gl_FragCoord:Vec2): TColor32;
const
STEPS=30;
VEC3_NUL:Vec3=(x:0;y:0;z:0);
end;
var
AngelicParticles:TShader;
implementation
uses SysUtils, Math;
// by srtuss, 2013
// did some research on kali's "comsos" and came up with this.
// as always, not optimized, just pretty :)
//
// name & inspiration was taken from here:
// http://www.youtube.com/watch?v=BzQmeeXcDwQ
function rotate(p:Vec2; a:float):Vec2;
begin
Result := vec2.Create(p.x * system.cos(a) - p.y * system.sin(a),
p.x * system.sin(a) + p.y * system.cos(a));
end;
constructor TAngelicParticles.Create;
begin
inherited;
Image.FrameProc := prepareFrame;
Image.PixelProc := RenderPixel;
end;
procedure TAngelicParticles.PrepareFrame;
begin
ray.x := sinLarge(iGlobalTime * 0.10) * 0.2;
ray.y := cosLarge(iGlobalTime * 0.13) * 0.2;
ray.z := 1.5;
end;
function TAngelicParticles.RenderPixel(var gl_FragCoord:Vec2): TColor32;
var
uv:Vec2;
dp,v,it,br:Float;
r,dir,acc,p,col:Vec3;
inc:Float; i,j:Integer;
begin
uv.x := 2 * gl_FragCoord.x/ Resolution.x - 1;
uv.y := 2 * gl_FragCoord.y/ Resolution.y - 1;
// uv.x := uv.x * (iResolution.x / iResolution.y);
v := 0;
r := ray;
dir := normalize(vec3.create(uv, 1.0));
r.z := r.z + iGlobalTime * 0.1 - 20;
dir.xz := rotate(dir.xz, sinLarge(iGlobalTime * 0.1) * 2);
dir.xy := rotate(dir.xy, iGlobalTime * 0.2);
// very little steps for the sake of a good framerate
inc := 0.35 / STEPS;
acc := VEC3_NUL;
for j := 0 to STEPS-1 do
begin
p := r * 0.1;
// do you like cubes?
// p := floor(r * 20.0) / 20.0;
// fractal from "cosmos"
for I := 0 to 13 do
begin
dp := dot(p, p);
// p := (abs(p) / dot(p, p)) * 2.0 - 1.0;
p.x := (abs(p.x) / dp) * 2 - 1;
p.y := (abs(p.y) / dp) * 2 - 1;
p.z := (abs(p.z) / dp) * 2 - 1;
end;
it := 0.001 * length(p * p);
v := v + it;
// cheap coloring
acc := acc + (System.sqrt(it) * texture2D(tex[2], r.xy * 0.1 + r.z * 0.1).xyz);
r := r + (dir * inc);
end;
// old blueish colorset
// Exit(Tcolor32(256*pow(vec3(v), 4.0 * vec3.create(0.9, 0.3, 0.1))));
br := pow(v * 4, 3) * 0.1;
col := pow(acc * 0.3, vec12) + br;
Result := TColor32(col*4);
end;
initialization
AngelicParticles := TAngelicParticles.Create;
Shaders.Add('AngelicParticles',AngelicParticles);
finalization
FreeandNil(AngelicParticles);
end.