-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragment.glsl
677 lines (622 loc) · 22.2 KB
/
fragment.glsl
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#define EPSILON 1e-5
#define PI 3.14159265358979
#define PI2 6.283185307179586
#define PHI 1.61803398874989484820459
uniform sampler2D acc_frame;
uniform mat4 iview, iproj;
uniform vec3 cam_pos;
uniform vec3 cam_vdir;
uniform vec3 cam_rdir;
uniform vec2 fsize;
uniform float focus_distance;
uniform float aperture;
uniform float realtime;
uniform int samples;
uniform int bounces;
uniform int simple;
#define SPHERE_FLOATS 5
#define TRIANGLE_FLOATS 10
#define MATERIAL_FLOATS 16
#define MAX_RECURSIVE_MATERIALS 32
#define SKYBOX_MATERIAL_ID 0
uniform sampler2D sphere_data;
uniform sampler2D triangle_data;
uniform sampler2D material_data;
uniform int material_chain[MAX_RECURSIVE_MATERIALS];
uniform int material_chain_len;
// something to indicate selected objects
struct Sphere {
vec3 center;
float radius;
int _mat;
};
struct Triangle {
vec3 a, b, c;
int _mat;
};
struct Material {
float specular_n[3]; // refraction index n for each wavelength RGB
float specular_k[3]; // refraction index k for each wavelength RGB --> most materials will set this to all 0's
float diffusive[3]; // lambertian diffusion of each wavelength RGB
float transmissive[3]; // transmission of each wavelength RGB // all energy (light) that isn't reflected as per fresnel is either diffused or transmitted (physically these are the same thing) --> diffusive and transmissive should in theory add to a maximum of 1 (per wavelength)
float emmissive[3]; // emmission for each wavelength RGB
float specular_roughness; // glossiness - a measure of micro-geometry
};
void sphere_at(in int num, out Sphere s) {
s.center = vec3(
texelFetch(sphere_data, ivec2(0, num), 0).x,
texelFetch(sphere_data, ivec2(1, num), 0).x,
texelFetch(sphere_data, ivec2(2, num), 0).x);
s.radius =
texelFetch(sphere_data, ivec2(3, num), 0).x;
s._mat = int(
texelFetch(sphere_data, ivec2(4, num), 0).x);
}
void triangle_at(in int num, out Triangle t) {
t.a = vec3(
texelFetch(triangle_data, ivec2(0, num), 0).x,
texelFetch(triangle_data, ivec2(1, num), 0).x,
texelFetch(triangle_data, ivec2(2, num), 0).x);
t.b = vec3(
texelFetch(triangle_data, ivec2(3, num), 0).x,
texelFetch(triangle_data, ivec2(4, num), 0).x,
texelFetch(triangle_data, ivec2(5, num), 0).x);
t.c = vec3(
texelFetch(triangle_data, ivec2(6, num), 0).x,
texelFetch(triangle_data, ivec2(7, num), 0).x,
texelFetch(triangle_data, ivec2(8, num), 0).x);
t._mat = int(
texelFetch(triangle_data, ivec2(9, num), 0).x);
}
void material_at(in int num, out Material m) {
m.specular_n[0] =
texelFetch(material_data, ivec2(0, num), 0).x;
m.specular_n[1] =
texelFetch(material_data, ivec2(1, num), 0).x;
m.specular_n[2] =
texelFetch(material_data, ivec2(2, num), 0).x;
m.specular_k[0] =
texelFetch(material_data, ivec2(3, num), 0).x;
m.specular_k[1] =
texelFetch(material_data, ivec2(4, num), 0).x;
m.specular_k[2] =
texelFetch(material_data, ivec2(5, num), 0).x;
m.diffusive[0] =
texelFetch(material_data, ivec2(6, num), 0).x;
m.diffusive[1] =
texelFetch(material_data, ivec2(7, num), 0).x;
m.diffusive[2] =
texelFetch(material_data, ivec2(8, num), 0).x;
m.transmissive[0] =
texelFetch(material_data, ivec2(9, num), 0).x;
m.transmissive[1] =
texelFetch(material_data, ivec2(10, num), 0).x;
m.transmissive[2] =
texelFetch(material_data, ivec2(11, num), 0).x;
m.emmissive[0] =
texelFetch(material_data, ivec2(12, num), 0).x;
m.emmissive[1] =
texelFetch(material_data, ivec2(13, num), 0).x;
m.emmissive[2] =
texelFetch(material_data, ivec2(14, num), 0).x;
m.specular_roughness =
texelFetch(material_data, ivec2(15, num), 0).x;
}
int sphere_count() {
return int(textureSize(sphere_data, 0).y);
}
int triangle_count() {
return int(textureSize(triangle_data, 0).y);
}
int material_count() {
return int(textureSize(material_data, 0).y);
}
/* RANDOM */
const vec3 _rc1_ = vec3(42.9898, 78.233, 151.7182);
const vec3 _rc2_ = vec3(63.7264, 10.873, 623.6736);
const vec3 _rc3_ = vec3(36.7539, 50.3658, 306.2759);
highp float _rseed_ = (PI / PHI);
float rseed() {
_rseed_ += (fract(sqrt(realtime)) + 0.148392798);
return _rseed_;
}
float s_random_gen(in vec3 scale, in float seed) { // random in the range [0.0, 1.0)
highp float d = 43758.5453;
highp float dt = dot(gl_FragCoord.xyz + seed, scale);
highp float sn = mod(dt, PI);
return fract(sin(sn) * d);
}
float random_gen(in vec3 scale) {
return s_random_gen(scale, rseed());
}
float s_random(in float seed) {
return s_random_gen(gl_FragCoord.xyz * realtime, seed);
}
float random() {
return random_gen(gl_FragCoord.xyz * realtime);
}
vec2 s_randomVec2(in float seed) { // vectors are in the range (-1, 1)
return (vec2(
s_random_gen(_rc1_, seed),
s_random_gen(_rc2_, seed)
) * 2.0 - 1.0);
}
vec2 randomVec2() {
return (vec2(
random_gen(_rc1_),
random_gen(_rc2_)
) * 2.0 - 1.0);
}
vec3 s_randomVec3(in float seed) {
return (vec3(
s_random_gen(_rc1_, seed),
s_random_gen(_rc2_, seed),
s_random_gen(_rc3_, seed)
) * 2.0 - 1.0);
}
vec3 randomVec3() {
return (vec3(
random_gen(_rc1_),
random_gen(_rc2_),
random_gen(_rc3_)
) * 2.0 - 1.0);
}
vec2 s_randomUnitVec2_Reject(float seed) { // random unit vectors using a rejection method -- not necessarily uniform
while(true) {
vec2 test = s_randomVec2(seed);
if(dot(test, test) <= 1.0) {
return test;
}
seed += 1.0;
}
}
vec3 s_randomUnitVec3_Reject(float seed) {
while(true) {
vec3 test = s_randomVec3(seed);
if(dot(test, test) <= 1.0) {
return test;
}
seed += 1.0;
}
}
vec3 uniformlyRandomUnitVec3(float seed) { // uniform unit vector generation using spherical/polar coords
float cos_phi = s_random_gen(_rc1_, seed) * 2.0 - 1.0;
float theta = s_random_gen(_rc2_, seed) * PI2;
float sin_phi = sqrt(1.0 - cos_phi * cos_phi);
return vec3(sin_phi * cos(theta), sin_phi * sin(theta), cos_phi);
}
vec3 uniformlyRandomVec3(float seed) {
return uniformlyRandomUnitVec3(seed) * sqrt(s_random_gen(_rc3_, seed));
}
vec2 uniformlyRandomUnitVec2(float seed) { // random vec2 of length 1
float theta = PI2 * s_random_gen(_rc1_, seed);
return vec2(cos(theta), sin(theta));
}
vec2 uniformlyRandomVec2(float seed) { // random vec2 within a unit circle
return uniformlyRandomUnitVec2(seed) * s_random_gen(_rc2_, seed);
}
/* GEOMETRY and INTERACTIONS */
struct Ray {
vec3 origin;
vec3 direction;
};
struct Hit {
bool internal;
float time;
Ray normal;
//vec2 uv;
};
void _reflect(in Ray src, in Hit hit, out Ray ret) {
ret.origin = hit.normal.origin;
ret.direction = reflect(src.direction, hit.normal.direction);
}
void _refract(in Ray src, in Hit hit, out Ray ret, in float eta) {
ret.origin = hit.normal.origin;
ret.direction = refract(src.direction, hit.normal.direction, eta);
}
float reflectance_approx(float cos, float r0) { // Schlick approx
return r0 + (1.0 - r0) * pow(1.0 - cos, 5.0);
}
float reflectance_exact(float cosi, float cost, float n1, float n2) { // Fresnel w/o complex refractive indices
float r1 = (n1*cosi - n2*cost) / (n1*cosi + n2*cost);
float r2 = (n1*cost - n2*cosi) / (n1*cost + n2*cosi);
return (r1*r1 + r2*r2) / 2.0;
}
float reflectance_complex(float cosi, float sini, float eta, float k) { // Fresnel w/ complex refractive indices
float cosi2 = cosi * cosi;
float sini2 = sini * sini;
float sini4 = sini2 * sini2;
float eta2 = eta * eta;
float k2 = k * k;
float x = eta2 - k2 - sini2;
float c = sqrt(x*x + 4.0*eta2*k2);
float a = 2.0*sqrt(0.5*(c + x)); // a is not actually multplied by 2, but is in all the places where it is used
float r1 = (c - a*cosi + cosi2) / (c + a*cosi + cosi2);
float r2 = r1 * ((cosi2*c - a*cosi*sini2 + sini4) / (cosi2*c + a*cosi*sini2 + sini4));
return (r1 + r2) / 2.0;
}
float ir_eta(float n1, float k1, float n2, float k2) {
if(k1 == 0.0 && k2 == 0.0) { return n2 / n1; }
return (n2*n1 + k2*k1) / (n1*n1 + k1*k1);
}
float ir_eta_k(float n1, float k1, float n2, float k2) {
if(k1 == 0.0 && k2 == 0.0) { return 0.0; }
return (k2*n1 - n2*k1) / (n1*n1 + k1*k1);
}
vec2 ir_eta_complex(float n1, float k1, float n2, float k2) {
if(k1 == 0.0 && k2 == 0.0) { return vec2(n2 / n1, 0); }
float d = (n1*n1 + k1*k1);
return vec2(
(n2*n1 + k2*k1) / d,
(k2*n1 - n2*k1) / d );
}
float sini2cost(float sini2, float n1, float n2) { // sini2 param is the sin of the incident angle squared
float e = (n1 / n2);
return sqrt(1.0 - e*e*sini2);
}
float simple_r0(float n1, float n2) {
float r = (n1 - n2) / (n1 + n2);
return r*r;
}
float complex_r0(float eta, float k) {
float k2 = k*k;
float a = eta - 1.0;
float b = eta + 1.0;
return (a*a + k2) / (b*b + k2);
}
bool interactsSphere(in Ray ray, in Sphere s, inout Hit hit, float t_min, float t_max) {
vec3 o = ray.origin - s.center;
float a = dot(ray.direction, ray.direction);
float b = 2.0 * dot(o, ray.direction);
float c = dot(o, o) - (s.radius * s.radius);
float d = (b * b) - (4.0 * a * c);
if(d < 0.0) {
return false;
}
hit.time = (sqrt(d) + b) / (-2.0 * a);
if(hit.time < t_min || hit.time > t_max) {
hit.time = (-sqrt(d) + b) / (-2.0 * a);
if(hit.time < t_min || hit.time > t_max) {
return false;
}
}
hit.normal.origin = ray.direction * hit.time + ray.origin;
hit.normal.direction = normalize(hit.normal.origin - s.center);
hit.internal = dot(hit.normal.direction, ray.direction) > 0.0;
hit.normal.origin = s.center + hit.normal.direction * s.radius;
if(hit.internal) {
hit.normal.direction *= -1.0;
}
//hit.normal.origin += hit.normal.direction * EPSILON; // no accidental re-collision
return true;
}
bool interactsTriangle(in Ray ray, in Triangle t, inout Hit hit, float t_min, float t_max) {
vec3 h, s, q;
vec3 s1 = t.b - t.a, s2 = t.c - t.a;
float a, f, u, v;
h = cross(ray.direction, s2);
a = dot(s1, h);
if(a > -EPSILON && a < EPSILON) { return false; }
f = 1.0 / a;
s = ray.origin - t.a;
u = f * dot(s, h);
if(u < 0.0 || u > 1.0) { return false; }
q = cross(s, s1);
v = f * dot(ray.direction, q);
if(v < 0.0 || u + v > 1.0) { return false; }
hit.time = f * dot(s2, q);
if(hit.time <= EPSILON || hit.time < t_min || hit.time > t_max) { return false; }
hit.normal.origin = ray.origin + ray.direction * hit.time;
hit.normal.direction = normalize(cross(s1, s2));
hit.internal = false;
if(dot(hit.normal.direction, ray.direction) > 0.0) {
hit.normal.direction *= -1.0;
}
return true;
}
int interactsScene(in Ray src, inout Hit hit) {
int ret = -1;
Hit tmp;
Sphere s;
Triangle t;
for(int i = 0; i < sphere_count(); i++) {
sphere_at(i, s);
if(interactsSphere(src, s, tmp, EPSILON, hit.time)) {
hit = tmp;
ret = s._mat;
}
}
for(int i = 0; i < triangle_count(); i++) {
triangle_at(i, t);
if(interactsTriangle(src, t, tmp, EPSILON, hit.time)) {
hit = tmp;
ret = t._mat;
}
}
// add more here for each obj type...
return ret;
}
/* RAY EVALUATION */
vec3 getSourceRay(in vec2 proportional, in mat4 inv_proj, in mat4 inv_view) {
vec4 t = inv_proj * vec4( (proportional * 2.0 - 1.0), 1.0, 1.0);
return vec3( inv_view * vec4( normalize(vec3(t) / t.w), 0) );
}
void makeDOFRay(inout Ray ray, vec3 vdir, vec3 rdir, float aperature, float focus_dist) {
vec3 p = ray.direction * focus_dist;
vec2 r = uniformlyRandomVec2(rseed());
vec3 o = ((vdir * r.x + rdir * r.y) * aperature / 2.0);
// vec3 o = ((vdir * s_random(rseed()) + rdir * s_random(rseed())) * aperature / 2.0); // square bokeh
ray.direction = normalize(p - o);
ray.origin += o;
}
vec3 evalRaySimple(in Ray ray) {
Hit hit;
hit.time = 1e10;
int id = interactsScene(ray, hit);
if(id < 0) { id = SKYBOX_MATERIAL_ID; }
Material mat;
material_at(id, mat);
vec3 lum = vec3(mat.emmissive[0], mat.emmissive[1], mat.emmissive[2]);
vec3 clr = vec3(mat.diffusive[0], mat.diffusive[1], mat.diffusive[2]);
if(any(greaterThan(lum, vec3(0.0)))) {
return lum;
} else if(any(greaterThan(clr, vec3(0.0)))) {
return clr;
} else {
return vec3(mat.transmissive[0], mat.transmissive[1], mat.transmissive[2]);
}
}
float evalChannel(in int i, in Ray src, in int bounces) {
float total = 0.0;
int mat_chain[MAX_RECURSIVE_MATERIALS] = material_chain;
int mat_chain_len = material_chain_len;
Ray ray = src;
Hit hit;
Material mat, _mat;
for(int b = bounces; b >= 0; b--) {
hit.time = 1e10;
int id = interactsScene(ray, hit);
if(id < 0) {
material_at(mat_chain[SKYBOX_MATERIAL_ID], mat);
total += mat.emmissive[i];
break;
}
vec2 eta_cx;
if(id == mat_chain[mat_chain_len]) { // if interacting with the same material as the last time (internal transfer)
material_at(id, mat);
material_at(mat_chain[mat_chain_len - 1], _mat); // the outer material should be one back in the chain
eta_cx = ir_eta_complex(
mat.specular_n[i], mat.specular_k[i],
_mat.specular_n[i], _mat.specular_k[i]);
} else {
material_at(mat_chain[mat_chain_len], _mat);
material_at(id, mat);
eta_cx = ir_eta_complex(
_mat.specular_n[i], _mat.specular_k[i],
mat.specular_n[i], mat.specular_k[i]);
}
if(b == 0 || mat.emmissive[i] >= 1.0) {
return total + mat.emmissive[i]; // emmissive * cache , but this is currently always 1
}
vec3 src = ray.direction;
float cosi = min(dot(-ray.direction, hit.normal.direction), 1.0);
float sini = sqrt(1.0 - cosi*cosi);
// test if r0 or not complex --> use optimized computations
float refl = reflectance_complex(cosi, sini, eta_cx.x, eta_cx.y);
float r = random();
if(r <= refl) { // specular reflections
ray.origin = hit.normal.origin;
ray.direction = reflect(ray.direction, hit.normal.direction) + (uniformlyRandomVec3(rseed()) * mat.specular_roughness);
} else { // transmission --> can be diffused or transmitted (transparently), or [insert subsurface scattering here]
if(r <= mat.diffusive[i]) {
ray.origin = hit.normal.origin;
ray.direction = hit.normal.direction + uniformlyRandomVec3(rseed());
} else if(mat.transmissive[i] > 0.0) { // compare to transmissive --> but this is just the conjugate of diffusive
vec3 r_out_perp = (1.0 / eta_cx.x) * (ray.direction + cosi * hit.normal.direction);
vec3 r_out_para = -sqrt(abs(1.0 - dot(r_out_perp, r_out_perp))) * hit.normal.direction;
ray.direction = normalize(r_out_perp + r_out_para) + (uniformlyRandomVec3(rseed()) * mat.specular_roughness);
ray.origin = hit.normal.origin;
//ray.direction = refract(ray.direction, hit.normal.direction, eta_cx.x) + (randomUnitVec3_Reject(rseed()) * mat.specular_roughness); // figure out complex ir transmission angle?
} else {
return 0.0; // absorption has occurred
}
}
ray.direction = normalize(ray.direction);
if(dot(hit.normal.direction, src) * dot(hit.normal.direction, ray.direction) > 0.0) { // transmission
if(id == mat_chain[mat_chain_len]) { // transmitting out of a media
mat_chain_len--;
} else { // transmitting into a media
mat_chain_len++; // compare to max length
mat_chain[mat_chain_len] = id;
}
}
total += mat.emmissive[i]; // see earlier comment about multiplying by cache if ever not 1
}
return total;
}
vec3 evalRay(in Ray ray, in int bounces) {
vec3 ret;
ret.x = evalChannel(0, ray, bounces);
ret.y = evalChannel(1, ray, bounces);
ret.z = evalChannel(2, ray, bounces);
return ret;
}
out vec4 fragColor;
void main() {
Ray base = Ray(cam_pos, vec3(0.0));
vec3 clr = texelFetch(acc_frame, ivec2(gl_FragCoord.xy), 0).rgb;
if(simple > 0) {
for(int i = 0; i < samples; i++) {
float r = random();
base.direction = getSourceRay((gl_FragCoord.xy + vec2(r)) / fsize, iproj, iview);
clr += evalRaySimple(base);
}
} else {
Ray dof;
for(int i = 0; i < samples; i++) {
float r = random();
base.direction = getSourceRay((vec2(gl_FragCoord) + vec2(r)) / fsize, iproj, iview);
dof = base;
makeDOFRay(dof, cam_vdir, cam_rdir, aperture, focus_distance);
clr += evalRay(dof, bounces);
}
}
fragColor = vec4(clr, 1.0);
}
// debug/outdated algos
// bool reflectGlossy(in Ray src, in Hit hit, out Ray ret, float gloss) {
// ret.origin = hit.normal.origin;
// ret.direction = reflect(src.direction, hit.normal.direction) + (randomUnitVec3_Reject(rseed()) * gloss);
// return dot(ret.direction, hit.normal.direction) > 0.0;
// }
// bool refractGlossy(in Ray src, in Hit hit, out Ray ret, in float ir, in float gloss) {
// if(!hit.internal) { ir = 1.0 / ir; }
// float cos_theta = min(dot(-src.direction, hit.normal.direction), 1.0);
// float sin_theta = sqrt(1.0 - cos_theta * cos_theta);
// float r = rand();
// if ((ir * sin_theta) > 1.0 || (reflectance_approx(cos_theta, ir) > r)) {
// return reflectGlossy(src, hit, ret, gloss);
// }
// vec3 r_out_perp = ir * (src.direction + cos_theta * hit.normal.direction);
// vec3 r_out_para = -sqrt(abs(1.0 - dot(r_out_perp, r_out_perp))) * hit.normal.direction;
// ret.direction = r_out_perp + r_out_para + (randomUnitVec3_Reject(rseed()) * gloss);
// ret.origin = hit.normal.origin;
// return true;
// }
// bool diffuse(in Hit hit, out Ray ret) {
// ret.origin = hit.normal.origin;
// ret.direction = hit.normal.direction + randomUnitVec3_Reject(rseed());
// return true;
// }
// bool redirectRay(in Ray src, in Hit hit, in Material mat, out Ray ret) {
// float r = rand();
// if(r < mat.specular_roughness) {
// return diffuse(hit, ret);
// } else if(r < mat.transmissive[0]) {
// return refractGlossy(src, hit, ret, mat.specular_n[0], mat.specular_roughness);
// } else {
// return reflectGlossy(src, hit, ret, mat.specular_roughness);
// }
// }
// float testChannel(in int i, in Ray src, in int bounces) {
// int material_path[MAX_RECURSIVE_MATERIALS];
// int mat_chain_len = 0;
// material_path[0] = SKYBOX_MATERIAL_ID; // or eqivelant skybox material id
// float ret_ = 0.0;
// Ray ray = src;
// Hit hit;
// Material mat, _mat;
// for(int b = bounces; b >= 0; b--) {
// hit.time = 1e10;
// int id = interactsScene(ray, hit);
// if(id != -1) {
// vec2 eta_cx;
// if(id == material_path[mat_chain_len]) { // if interacting with the same material as the last time (internal transfer)
// material_at(id, mat);
// material_at(material_path[mat_chain_len - 1], _mat); // the outer material should be one back in the chain
// eta_cx = ir_eta_complex(
// mat.specular_n[i], mat.specular_k[i],
// _mat.specular_n[i], _mat.specular_k[i]);
// } else {
// material_at(material_path[mat_chain_len], _mat);
// material_at(id, mat);
// eta_cx = ir_eta_complex(
// _mat.specular_n[i], _mat.specular_k[i],
// mat.specular_n[i], mat.specular_k[i]);
// }
// vec3 src = ray.direction;
// float cosi = min(dot(-ray.direction, hit.normal.direction), 1.0);
// float sini = sqrt(1.0 - cosi*cosi);
// // test if r0 or not complex --> use optimized computations
// float refl = reflectance_complex(cosi, sini, eta_cx.x, eta_cx.y);
// float r = rand();
// if(r <= refl) { // specular reflections
// ray.origin = hit.normal.origin;
// ray.direction = reflect(ray.direction, hit.normal.direction) + (randomUnitVec3_Reject(rseed()) * mat.specular_roughness);
// } else { // transmission --> can be diffused or transmitted (transparently), or [insert subsurface scattering here]
// if(r <= mat.diffusive[i]) {
// ray.origin = hit.normal.origin;
// ray.direction = normalize(hit.normal.direction + randomUnitVec3_Reject(rseed()));
// } else if(mat.transmissive[i] > 0.0) { // compare to transmissive --> but this is just the conjugate of diffusive
// vec3 r_out_perp = (1.0 / eta_cx.x) * (ray.direction + cosi * hit.normal.direction);
// vec3 r_out_para = -sqrt(abs(1.0 - dot(r_out_perp, r_out_perp))) * hit.normal.direction;
// ray.direction = normalize(r_out_perp + r_out_para) + (randomUnitVec3_Reject(rseed()) * mat.specular_roughness);
// ray.origin = hit.normal.origin;
// //ray.direction = refract(ray.direction, hit.normal.direction, eta_cx.x) + (randomUnitVec3_Reject(rseed()) * mat.specular_roughness); // figure out complex ir transmission angle?
// } else {
// return ret_; // absorption has occurred
// }
// }
// // ret_ = float(id) / float(material_count());
// if(dot(hit.normal.direction, src) * dot(hit.normal.direction, ray.direction) > 0.0) { // transmission
// if(id == material_path[mat_chain_len]) { // transmitting out of a media
// mat_chain_len--;
// // ret_ = float(material_path[mat_chain_len]) / float(material_count());
// } else { // transmitting into a media
// mat_chain_len++; // compare to max length
// material_path[mat_chain_len] = id;
// }
// }
// continue;
// }
// break;
// }
// return ret_;
// }
// vec3 evalRayOld(in Ray src, in int bounces) {
// vec3 total = vec3(0.0);
// vec3 cache = vec3(1.0);
// Ray ray = src;
// Hit hit;
// Material mat;
// for(int b = bounces; b >= 0; b--) {
// hit.time = 1e10;
// int id = interactsScene(ray, hit);
// if(id != -1) {
// material_at(id, mat);
// vec3 lum = vec3(mat.emmissive[0], mat.emmissive[1], mat.emmissive[2]);
// vec3 clr = vec3(mat.diffusive[0], mat.diffusive[1], mat.diffusive[2]);
// if(b == 0 || ((lum.x + lum.y + lum.z) / 3.0) >= 1.0) {
// total += cache * lum;
// return total;
// }
// Ray redirect;
// float r = rand();
// if(r < mat.specular_roughness) {
// diffuse(hit, redirect);
// total += cache * lum;
// cache *= clr;
// ray = redirect;
// continue;
// } else {
// float ir = mat.specular_n[0];
// if(!hit.internal) { ir = 1.0 / ir; }
// float cos_theta = min(dot(-ray.direction, hit.normal.direction), 1.0);
// float sin_theta = sqrt(1.0 - cos_theta * cos_theta);
// if ((ir * sin_theta) > 1.0 || (reflectance_approx(cos_theta, complex_r0(ir, 0.0)) > r)) {
// reflectGlossy(ray, hit, redirect, mat.specular_roughness);
// total += cache * lum;
// cache *= clr;
// ray = redirect;
// } else {
// total += cache * lum;
// cache *= vec3(mat.transmissive[0], mat.transmissive[1], mat.transmissive[2]);
// vec3 r_out_perp = ir * (ray.direction + cos_theta * hit.normal.direction);
// vec3 r_out_para = -sqrt(abs(1.0 - dot(r_out_perp, r_out_perp))) * hit.normal.direction;
// ray.direction = r_out_perp + r_out_para + (randomUnitVec3_Reject(rseed()) * mat.specular_roughness);
// ray.origin = hit.normal.origin;
// }
// continue;
// }
// }
// material_at(SKYBOX_MATERIAL_ID, mat);
// total += cache * vec3(mat.emmissive[0], mat.emmissive[1], mat.emmissive[2]);
// break;
// }
// return total;
// }