Repository navigation
Expand file tree
/
Copy pathtest.glsl
More file actions
201 lines (159 loc) · 4.93 KB
/
Copy pathtest.glsl
File metadata and controls
201 lines (159 loc) · 4.93 KB
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
// THIS IS NOT MY CODE, CREDIT TO evilryu below
// Created by evilryu
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
// whether turn on the animation
#define phase_shift_on
float stime, ctime;
void ry(inout vec3 p, float a){
float c,s;vec3 q=p;
c = cos(a); s = sin(a);
p.x = c * q.x + s * q.z;
p.z = -s * q.x + c * q.z;
}
float pixel_size = 0.0;
/*
z = r*(sin(theta)cos(phi) + i cos(theta) + j sin(theta)sin(phi)
zn+1 = zn^8 +c
z^8 = r^8 * (sin(8*theta)*cos(8*phi) + i cos(8*theta) + j sin(8*theta)*sin(8*theta)
zn+1' = 8 * zn^7 * zn' + 1
*/
vec3 mb(vec3 p) {
p.xyz = p.xzy;
vec3 z = p;
vec3 dz=vec3(0.0);
float power = 8.0;
float r, theta, phi;
float dr = 1.0;
float t0 = 1.0;
for(int i = 0; i < 9; ++i) {
r = length(z);
if(r > 2.0) continue;
theta = atan(z.y / z.x);
#ifdef phase_shift_on
phi = asin(z.z / r) + iTime*0.2;
#else
phi = asin(z.z / r);
#endif
dr = pow(r, power - 1.0) * dr * power + 1.0;
r = pow(r, power);
theta = theta * power;
phi = phi * power;
z = r * vec3(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi)) + p;
t0 = min(t0, r);
}
return vec3(0.5 * log(r) * r / dr, t0, 0.0);
}
vec3 f(vec3 p){
ry(p, iTime*0.2);// rotation of mb
return mb(p);
}
float softshadow(vec3 ro, vec3 rd, float k ){
float akuma=1.0,h=0.0;
float t = 0.01;
for(int i=0; i < 50; ++i){
h=f(ro+rd*t).x;
if(h<0.001)return 0.02;
akuma=min(akuma, k*h/t);
t+=clamp(h,0.01,2.0);
}
return akuma;
}
vec3 nor( in vec3 pos )
{
vec3 eps = vec3(0.001,0.0,0.0);
return normalize( vec3(
f(pos+eps.xyy).x - f(pos-eps.xyy).x,
f(pos+eps.yxy).x - f(pos-eps.yxy).x,
f(pos+eps.yyx).x - f(pos-eps.yyx).x ) );
}
vec3 intersect( in vec3 ro, in vec3 rd )
{
float t = 1.0;
float res_t = 0.0;
float res_d = 1000.0;
vec3 c, res_c;
float max_error = 1000.0;
float d = 1.0;
float pd = 100.0;
float os = 0.0;
float step = 0.0;
float error = 1000.0;
for( int i=0; i<96; i++ )
{
if( error < pixel_size*0.5 || t > 20.0 )
{
}
else{ // avoid broken shader on windows
c = f(ro + rd*t);
d = c.x;
if(d > os)
{
os = 0.4 * d*d/pd;
step = d + os;
pd = d;
}
else
{
step =-os; os = 0.0; pd = 100.0; d = 1.0;
}
error = d / t;
if(error < max_error)
{
max_error = error;
res_t = t;
res_c = c;
}
t += step;
}
}
if( t>20.0/* || max_error > pixel_size*/ ) res_t=-1.0;
return vec3(res_t, res_c.y, res_c.z);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 q=fragCoord.xy/iResolution.xy;
vec2 uv = -1.0 + 2.0*q;
uv.x*=iResolution.x/iResolution.y;
pixel_size = 1.0/(iResolution.x * 3.0);
// camera
stime=0.7+0.3*sin(iTime*0.4);
ctime=0.7+0.3*cos(iTime*0.4);
vec3 ta=vec3(0.0,0.0,0.0);
vec3 ro = vec3(0.01, 2.2, 0.01);
vec3 cf = normalize(ta-ro);
vec3 cs = normalize(cross(cf,vec3(0.0,1.0,0.0)));
vec3 cu = normalize(cross(cs,cf));
vec3 rd = normalize(uv.x*cs + uv.y*cu + /*zoom*/5.0*cf); // transform from view to world
vec3 sundir = normalize(vec3(0.1, 0.8, 0.6));
vec3 sun = vec3(1.64, 1.27, 0.2);
vec3 skycolor = vec3(0.6, 1.5, 1.0);
vec3 bg = exp(uv.y-2.0)*vec3(0.4, 1.6, 1.0);
float halo= clamp(dot(normalize(vec3(-ro.x, -ro.y, -ro.z)), rd), 0.0, 1.0);
vec3 col=bg+vec3(1.0,0.8,0.4)*pow(halo,17.0);
float t=0.0;
vec3 p=ro;
vec3 res = intersect(ro, rd);
if(res.x > 0.0){
p = ro + res.x * rd;
vec3 n=nor(p);
float shadow = softshadow(p, sundir, 10.0 );
float dif = max(0.0, dot(n, sundir));
float sky = 0.6 + 0.4 * max(0.0, dot(n, vec3(0.0, 1.0, 0.0)));
float bac = max(0.3 + 0.7 * dot(vec3(-sundir.x, -1.0, -sundir.z), n), 0.0);
float spe = max(0.0, pow(clamp(dot(sundir, reflect(rd, n)), 0.0, 1.0), 10.0));
vec3 lin = 4.5 * sun * dif * shadow;
lin += 0.8 * bac * sun;
lin += 0.6 * sky * skycolor*shadow;
lin += 3.0 * spe * shadow;
res.y = pow(clamp(res.y, 0.0, 1.0), 0.95);
vec3 tc0 = 0.5 + 0.5 * cos(-6.0*iTime + 9.2 * res.y + vec3(0.9, 0.0, 0.0));
col = lin *vec3(0.9, 0.8, 0.6) * 0.2 * tc0;
col=mix(col,bg, 1.0-exp(-0.001*res.x*res.x));
}
// post
col=pow(clamp(col,0.0,1.0),vec3(0.45));
col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast
col=mix(col, vec3(dot(col, vec3(0.33))), -0.5); // satuation
//col*=0.5+0.5*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting
fragColor = vec4(col.xyz, smoothstep(0.55, .76, 1.-res.x/5.));
}