Debian Codegraph @

demos/gtk-demo/alienplanet.glsl

raw
1 // Originally from: https://www.shadertoy.com/view/wsjBD3
2 // License CC0: A battered alien planet
3 // Been experimenting with space inspired shaders
4
5 #define PI 3.141592654
6 #define TAU (2.0*PI)
7
8 #define TOLERANCE 0.00001
9 #define MAX_ITER 65
10 #define MIN_DISTANCE 0.01
11 #define MAX_DISTANCE 9.0
12
13 const vec3 skyCol1 = vec3(0.35, 0.45, 0.6);
14 const vec3 skyCol2 = vec3(0.4, 0.7, 1.0);
15 const vec3 skyCol3 = pow(skyCol1, vec3(0.25));
16 const vec3 sunCol1 = vec3(1.0,0.6,0.4);
17 const vec3 sunCol2 = vec3(1.0,0.9,0.7);
18 const vec3 smallSunCol1 = vec3(1.0,0.5,0.25)*0.5;
19 const vec3 smallSunCol2 = vec3(1.0,0.5,0.25)*0.5;
20 const vec3 mountainColor = 1.0*sqrt(vec3(0.95, 0.65, 0.45));
21 const float cellWidth = 1.0;
22 const vec4 planet = vec4(80.0, -20.0, 100.0, 50.0)*1000.0;
23
24 void rot(inout vec2 p, float a) {
25 float c = cos(a);
26 float s = sin(a);
27 p = vec2(p.x*c + p.y*s, -p.x*s + p.y*c);
28 }
29
30 vec2 mod2(inout vec2 p, vec2 size) {
31 vec2 c = floor((p + size*0.5)/size);
32 p = mod(p + size*0.5,size) - size*0.5;
33 return c;
34 }
35
36 float circle(vec2 p, float r) {
37 return length(p) - r;
38 }
39
40 float egg(vec2 p, float ra, float rb) {
41 const float k = sqrt(3.0);
42 p.x = abs(p.x);
43 float r = ra - rb;
44 return ((p.y<0.0) ? length(vec2(p.x, p.y )) - r :
45 (k*(p.x+r)<p.y) ? length(vec2(p.x, p.y-k*r)) :
46 length(vec2(p.x+r,p.y )) - 2.0*r) - rb;
47 }
48
49 vec2 hash(vec2 p) {
50 p = vec2(dot (p, vec2 (127.1, 311.7)), dot (p, vec2 (269.5, 183.3)));
51 return -1. + 2.*fract (sin (p)*43758.5453123);
52 }
53
54 vec2 raySphere(vec3 ro, vec3 rd, vec4 sphere) {
55 vec3 center = sphere.xyz;
56 float radius = sphere.w;
57 vec3 m = ro - center.xyz;
58 float b = dot(m, rd);
59 float c = dot(m, m) - radius*radius;
60 if(c > 0.0 && b > 0.0) return vec2(-1.0, -1.0);
61 float discr = b * b - c;
62 if(discr < 0.0) return vec2(-1.0);
63 float normalMultiplier = 1.0;
64 float s = sqrt(discr);
65 float t0 = -b - s;
66 float t1 = -b + s;;
67 return vec2(t0, t1);
68 }
69
70 float noize1(vec2 p) {
71 vec2 n = mod2(p, vec2(cellWidth));
72 vec2 hh = hash(sqrt(2.0)*(n+1000.0));
73 hh.x *= hh.y;
74
75 float r = 0.225*cellWidth;
76
77 float d = circle(p, 2.0*r);
78
79 float h = hh.x*smoothstep(0.0, r, -d);
80
81 return h*0.25;
82 }
83
84 float noize2(vec2 p) {
85 vec2 n = mod2(p, vec2(cellWidth));
86 vec2 hh = hash(sqrt(2.0)*(n+1000.0));
87 hh.x *= hh.y;
88
89 rot(p, TAU*hh.y);
90 float r = 0.45*cellWidth;
91
92 // float d = circle(p, 1.0*r);
93 float d = egg(p, 0.75*r, 0.5*r*abs(hh.y));
94
95 float h = (hh.x)*smoothstep(0.0, r, -2.0*d);
96
97 return h*0.275;
98 }
99
100
101 float height(vec2 p, float dd, int mx) {
102 const float aa = 0.45;
103 const float ff = 2.03;
104 const float tt = 1.2;
105 const float oo = 3.93;
106 const float near = 0.25;
107 const float far = 0.65;
108
109 float a = 1.0;
110 float o = 0.2;
111 float s = 0.0;
112 float d = 0.0;
113
114 int i = 0;
115
116 for (; i < 4;++i) {
117 float nn = a*noize2(p);
118 s += nn;
119 d += abs(a);
120 p += o;
121 a *= aa;
122 p *= ff;
123 o *= oo;
124 rot(p, tt);
125 }
126
127 float lod = s/d;
128
129 float rdd = dd/MAX_DISTANCE;
130 mx = int(mix(float(4), float(mx), step(rdd, far)));
131
132 for (; i < mx; ++i) {
133 float nn = a*noize1(p);
134 s += nn;
135 d += abs(a);
136 p += o;
137 a *= aa;
138 p *= ff;
139 o *= oo;
140 rot(p, tt);
141 }
142
143 float hid = (s/d);
144
145 return mix(hid, lod, smoothstep(near, far, rdd));
146 }
147
148 float loheight(vec2 p, float d) {
149 return height(p, d, 0);
150 }
151
152 float height(vec2 p, float d) {
153 return height(p, d, 6);
154 }
155
156 float hiheight(vec2 p, float d) {
157 return height(p, d, 8);
158 }
159
160 vec3 normal(vec2 p, float d) {
161 vec2 eps = vec2(0.00125, 0.0);
162
163 vec3 n;
164
165 n.x = (hiheight(p - eps.xy, d) - hiheight(p + eps.xy, d));
166 n.y = 2.0*eps.x;
167 n.z = (hiheight(p - eps.yx, d) - hiheight(p + eps.yx, d));
168
169 return normalize(n);
170 }
171
172 const float stepLength[] = float[](0.9, 0.25);
173
174
175 float march(vec3 ro, vec3 rd, out int max_iter) {
176 float dt = 0.1;
177 float d = MIN_DISTANCE;
178 int currentStep = 0;
179 float lastd = d;
180 for (int i = 0; i < MAX_ITER; ++i)
181 {
182 vec3 p = ro + d*rd;
183 float h = height(p.xz, d);
184
185 if (d > MAX_DISTANCE) {
186 max_iter = i;
187 return MAX_DISTANCE;
188 }
189
190 float hd = p.y - h;
191
192 if (hd < TOLERANCE) {
193 ++currentStep;
194 if (currentStep >= stepLength.length()) {
195 max_iter = i;
196 return d;
197 }
198
199 d = lastd;
200 continue;
201 }
202
203 float sl = stepLength[currentStep];
204
205 dt = max(hd, TOLERANCE)*sl + 0.0025*d;
206 lastd = d;
207 d += dt;
208 }
209
210 max_iter = MAX_ITER;
211 return MAX_DISTANCE;
212 }
213
214 vec3 sunDirection() {
215 return normalize(vec3(-0.5, 0.085, 1.0));
216 }
217
218 vec3 smallSunDirection() {
219 return normalize(vec3(-0.2, -0.05, 1.0));
220 }
221
222 float psin(float f) {
223 return 0.5 + 0.5*sin(f);
224 }
225
226 vec3 skyColor(vec3 ro, vec3 rd) {
227 vec3 sunDir = sunDirection();
228 vec3 smallSunDir = smallSunDirection();
229
230 float sunDot = max(dot(rd, sunDir), 0.0);
231 float smallSunDot = max(dot(rd, smallSunDir), 0.0);
232
233 float angle = atan(rd.y, length(rd.xz))*2.0/PI;
234
235 vec3 skyCol = mix(mix(skyCol1, skyCol2, max(0.0, angle)), skyCol3, clamp(-angle*2.0, 0.0, 1.0));
236
237 vec3 sunCol = 0.5*sunCol1*pow(sunDot, 20.0) + 8.0*sunCol2*pow(sunDot, 2000.0);
238 vec3 smallSunCol = 0.5*smallSunCol1*pow(smallSunDot, 200.0) + 8.0*smallSunCol2*pow(smallSunDot, 20000.0);
239
240 vec3 dust = pow(sunCol2*mountainColor, vec3(1.75))*smoothstep(0.05, -0.1, rd.y)*0.5;
241
242 vec2 si = raySphere(ro, rd, planet);
243
244 vec3 planetSurface = ro + si.x*rd;
245 vec3 planetNormal = normalize(planetSurface - planet.xyz);
246 float planetDiff = max(dot(planetNormal, sunDir), 0.0);
247 float planetBorder = max(dot(planetNormal, -rd), 0.0);
248 float planetLat = (planetSurface.x+planetSurface.y)*0.0005;
249 vec3 planetCol = mix(1.3*vec3(0.9, 0.8, 0.7), 0.3*vec3(0.9, 0.8, 0.7), pow(psin(planetLat+1.0)*psin(sqrt(2.0)*planetLat+2.0)*psin(sqrt(3.5)*planetLat+3.0), 0.5));
250
251 vec3 final = vec3(0.0);
252
253 final += step(0.0, si.x)*pow(planetDiff, 0.75)*planetCol*smoothstep(-0.075, 0.0, rd.y)*smoothstep(0.0, 0.1, planetBorder);
254
255 final += skyCol + sunCol + smallSunCol + dust;
256
257 return final;
258 }
259
260 vec3 getColor(vec3 ro, vec3 rd) {
261 int max_iter = 0;
262 vec3 skyCol = skyColor(ro, rd);
263 vec3 col = vec3(0);
264
265 float d = march(ro, rd, max_iter);
266
267 if (d < MAX_DISTANCE) {
268 vec3 sunDir = sunDirection();
269 vec3 osunDir = sunDir*vec3(-1.0, .0, -1.0);
270 vec3 p = ro + d*rd;
271
272 vec3 normal = normal(p.xz, d);
273
274 float amb = 0.2;
275
276 float dif1 = max(0.0, dot(sunDir, normal));
277 vec3 shd1 = sunCol2*mix(amb, 1.0, pow(dif1, 0.75));
278
279 float dif2 = max(0.0, dot(osunDir, normal));
280 vec3 shd2 = sunCol1*mix(amb, 1.0, pow(dif2, 0.75));
281
282 vec3 ref = reflect(rd, normal);
283 vec3 rcol = skyColor(p, ref);
284
285 col = mountainColor*amb*skyCol3;
286 col += mix(shd1, shd2, -0.5)*mountainColor;
287 float fre = max(dot(normal, -rd), 0.0);
288 fre = pow(1.0 - fre, 5.0);
289 col += rcol*fre*0.5;
290 col += (1.0*p.y);
291 col = tanh(col);
292 col = mix(col, skyCol, smoothstep(0.5*MAX_DISTANCE, 1.0*MAX_DISTANCE, d));
293
294 } else {
295 col = skyCol;
296 }
297
298 // col += vec3(1.1, 0.0, 0.0)* smoothstep(0.25, 1.0,(float(max_iter)/float(MAX_ITER)));
299 return col;
300 }
301
302 vec3 getSample1(vec2 p, float time) {
303 float off = 0.5*iTime;
304
305 vec3 ro = vec3(0.5, 1.0-0.25, -2.0 + off);
306 vec3 la = ro + vec3(0.0, -0.30, 2.0);
307
308 vec3 ww = normalize(la - ro);
309 vec3 uu = normalize(cross(vec3(0.0,1.0,0.0), ww));
310 vec3 vv = normalize(cross(ww, uu));
311 vec3 rd = normalize(p.x*uu + p.y*vv + 2.0*ww);
312
313 vec3 col = getColor(ro, rd) ;
314
315 return col;
316 }
317
318 vec3 getSample2(vec2 p, float time) {
319 p.y-=time*0.25;
320 float h = height(p, 0.0);
321 vec3 n = normal(p, 0.0);
322
323 vec3 lp = vec3(10.0, -1.2, 0.0);
324
325 vec3 ld = normalize(vec3(p.x, h, p.y)- lp);
326
327 float d = max(dot(ld, n), 0.0);
328
329 vec3 col = vec3(0.0);
330
331 col = vec3(1.0)*(h+0.1);
332 col += vec3(1.5)*pow(d, 0.75);
333
334 return col;
335 }
336
337 void mainImage(out vec4 fragColor, vec2 fragCoord) {
338 vec2 q = fragCoord.xy/iResolution.xy;
339 vec2 p = -1.0 + 2.0*q;
340 p.x *= iResolution.x/iResolution.y;
341
342 vec3 col = getSample1(p, iTime);
343
344 fragColor = vec4(col, 1.0);
345 }