/
raytracer.glsl
422 lines (322 loc) · 13 KB
/
raytracer.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
#define M_PI 3.141592653589793238462643383279
#define R_SQRT_2 0.7071067811865475
#define DEG_TO_RAD (M_PI/180.0)
#define SQ(x) ((x)*(x))
#define ROT_Y(a) mat3(0, cos(a), sin(a), 1, 0, 0, 0, sin(a), -cos(a))
// spectrum texture lookup helper macros
const float BLACK_BODY_TEXTURE_COORD = 1.0;
const float SINGLE_WAVELENGTH_TEXTURE_COORD = 0.5;
const float TEMPERATURE_LOOKUP_RATIO_TEXTURE_COORD = 0.0;
// black-body texture metadata
const float SPECTRUM_TEX_TEMPERATURE_RANGE = 65504.0;
const float SPECTRUM_TEX_WAVELENGTH_RANGE = 2048.0;
const float SPECTRUM_TEX_RATIO_RANGE = 6.48053329012;
// multi-line macros don't seem to work in WebGL :(
#define BLACK_BODY_COLOR(t) texture2D(spectrum_texture, vec2((t) / SPECTRUM_TEX_TEMPERATURE_RANGE, BLACK_BODY_TEXTURE_COORD))
#define SINGLE_WAVELENGTH_COLOR(lambda) texture2D(spectrum_texture, vec2((lambda) / SPECTRUM_TEX_WAVELENGTH_RANGE, SINGLE_WAVELENGTH_TEXTURE_COORD))
#define TEMPERATURE_LOOKUP(ratio) (texture2D(spectrum_texture, vec2((ratio) / SPECTRUM_TEX_RATIO_RANGE, TEMPERATURE_LOOKUP_RATIO_TEXTURE_COORD)).r * SPECTRUM_TEX_TEMPERATURE_RANGE)
uniform vec2 resolution;
uniform float time;
uniform vec3 cam_pos;
uniform vec3 cam_x;
uniform vec3 cam_y;
uniform vec3 cam_z;
uniform vec3 cam_vel;
uniform float planet_distance, planet_radius;
uniform sampler2D galaxy_texture, star_texture,
accretion_disk_texture, planet_texture, spectrum_texture;
// stepping parameters
const int NSTEPS = {{n_steps}};
const float MAX_REVOLUTIONS = 2.0;
const float ACCRETION_MIN_R = 1.5;
const float ACCRETION_WIDTH = 5.0;
const float ACCRETION_BRIGHTNESS = 0.9;
const float ACCRETION_TEMPERATURE = 3900.0;
const float STAR_MIN_TEMPERATURE = 4000.0;
const float STAR_MAX_TEMPERATURE = 15000.0;
const float STAR_BRIGHTNESS = 1.0;
const float GALAXY_BRIGHTNESS = 0.4;
const float PLANET_AMBIENT = 0.1;
const float PLANET_LIGHTNESS = 1.5;
// background texture coordinate system
mat3 BG_COORDS = ROT_Y(45.0 * DEG_TO_RAD);
// planet texture coordinate system
const float PLANET_AXIAL_TILT = 30.0 * DEG_TO_RAD;
mat3 PLANET_COORDS = ROT_Y(PLANET_AXIAL_TILT);
const float FOV_ANGLE_DEG = 90.0;
float FOV_MULT = 1.0 / tan(DEG_TO_RAD * FOV_ANGLE_DEG*0.5);
// derived "constants" (from uniforms)
float PLANET_RADIUS,
PLANET_DISTANCE,
PLANET_ORBITAL_ANG_VEL,
PLANET_ROTATION_ANG_VEL,
PLANET_GAMMA;
vec2 sphere_map(vec3 p) {
return vec2(atan(p.x,p.y)/M_PI*0.5+0.5, asin(p.z)/M_PI+0.5);
}
float smooth_step(float x, float threshold) {
const float STEEPNESS = 1.0;
return 1.0 / (1.0 + exp(-(x-threshold)*STEEPNESS));
}
vec3 lorentz_velocity_transformation(vec3 moving_v, vec3 frame_v) {
float v = length(frame_v);
if (v > 0.0) {
vec3 v_axis = -frame_v / v;
float gamma = 1.0/sqrt(1.0 - v*v);
float moving_par = dot(moving_v, v_axis);
vec3 moving_perp = moving_v - v_axis*moving_par;
float denom = 1.0 + v*moving_par;
return (v_axis*(moving_par+v)+moving_perp/gamma)/denom;
}
return moving_v;
}
vec3 contract(vec3 x, vec3 d, float mult) {
float par = dot(x,d);
return (x-par*d) + d*par*mult;
}
vec4 planet_intersection(vec3 old_pos, vec3 ray, float t, float dt,
vec3 planet_pos0, float ray_doppler_factor) {
vec4 ret = vec4(0,0,0,0);
vec3 ray0 = ray;
ray = ray/dt;
vec3 planet_dir = vec3(planet_pos0.y, -planet_pos0.x, 0.0) / PLANET_DISTANCE;
{{#light_travel_time}}
float planet_ang1 = (t-dt) * PLANET_ORBITAL_ANG_VEL;
vec3 planet_pos1 = vec3(cos(planet_ang1), sin(planet_ang1), 0)*PLANET_DISTANCE;
vec3 planet_vel = (planet_pos1-planet_pos0)/dt;
// transform to moving planet coordinate system
ray = ray - planet_vel;
{{/light_travel_time}}
{{^light_travel_time}}
vec3 planet_vel = planet_dir * PLANET_ORBITAL_ANG_VEL * PLANET_DISTANCE;
{{/light_travel_time}}
// ray-sphere intersection
vec3 d = old_pos - planet_pos0;
{{#lorentz_contraction}}
ray = contract(ray, planet_dir, PLANET_GAMMA);
d = contract(d, planet_dir, PLANET_GAMMA);
{{/lorentz_contraction}}
float dotp = dot(d,ray);
float c_coeff = dot(d,d) - SQ(PLANET_RADIUS);
float ray2 = dot(ray, ray);
float discr = dotp*dotp - ray2*c_coeff;
if (discr < 0.0) return ret;
float isec_t = (-dotp - sqrt(discr)) / ray2;
float MIN_ISEC_DT = 0.0;
{{#lorentz_contraction}}
MIN_ISEC_DT = -dt;
{{/lorentz_contraction}}
if (isec_t < MIN_ISEC_DT || isec_t > dt) return ret;
vec3 surface_point = (d + isec_t*ray) / PLANET_RADIUS;
isec_t = isec_t/dt;
vec3 light_dir = planet_pos0;
float rot_phase = t;
{{#light_travel_time}}
light_dir += planet_vel*isec_t*dt;
rot_phase -= isec_t*dt;
{{/light_travel_time}}
rot_phase = rot_phase * PLANET_ROTATION_ANG_VEL*0.5/M_PI;
light_dir = light_dir / PLANET_DISTANCE;
{{#light_travel_time}}
light_dir = light_dir - planet_vel;
{{/light_travel_time}}
vec3 surface_normal = surface_point;
{{#lorentz_contraction}}
light_dir = contract(light_dir, planet_dir, PLANET_GAMMA);
{{/lorentz_contraction}}
light_dir = normalize(light_dir);
vec2 tex_coord = sphere_map(surface_point * PLANET_COORDS);
tex_coord.x = mod(tex_coord.x + rot_phase, 1.0);
float diffuse = max(0.0, dot(surface_normal, -light_dir));
float lightness = ((1.0-PLANET_AMBIENT)*diffuse + PLANET_AMBIENT) *
PLANET_LIGHTNESS;
float light_temperature = ACCRETION_TEMPERATURE;
{{#doppler_shift}}
float doppler_factor = SQ(PLANET_GAMMA) *
(1.0 + dot(planet_vel, light_dir)) *
(1.0 - dot(planet_vel, normalize(ray)));
light_temperature /= doppler_factor * ray_doppler_factor;
{{/doppler_shift}}
vec4 light_color = BLACK_BODY_COLOR(light_temperature);
ret = texture2D(planet_texture, tex_coord) * lightness * light_color;
if (isec_t < 0.0) isec_t = 0.5;
ret.w = isec_t;
return ret;
}
vec4 galaxy_color(vec2 tex_coord, float doppler_factor) {
vec4 color = texture2D(galaxy_texture, tex_coord);
{{^observerMotion}}
return color;
{{/observerMotion}}
{{#observerMotion}}
vec4 ret = vec4(0.0,0.0,0.0,0.0);
float red = max(0.0, color.r - color.g);
const float H_ALPHA_RATIO = 0.1;
const float TEMPERATURE_BIAS = 0.95;
color.r -= red*H_ALPHA_RATIO;
float i1 = max(color.r, max(color.g, color.b));
float ratio = (color.g+color.b) / color.r;
if (i1 > 0.0 && color.r > 0.0) {
float temperature = TEMPERATURE_LOOKUP(ratio) * TEMPERATURE_BIAS;
color = BLACK_BODY_COLOR(temperature);
float i0 = max(color.r, max(color.g, color.b));
if (i0 > 0.0) {
temperature /= doppler_factor;
ret = BLACK_BODY_COLOR(temperature) * max(i1/i0,0.0);
}
}
ret += SINGLE_WAVELENGTH_COLOR(656.28 * doppler_factor) * red / 0.214 * H_ALPHA_RATIO;
return ret;
{{/observerMotion}}
}
void main() {
{{#planetEnabled}}
// "constants" derived from uniforms
PLANET_RADIUS = planet_radius;
PLANET_DISTANCE = max(planet_distance,planet_radius+1.5);
PLANET_ORBITAL_ANG_VEL = -1.0 / sqrt(2.0*(PLANET_DISTANCE-1.0)) / PLANET_DISTANCE;
float MAX_PLANET_ROT = max((1.0 + PLANET_ORBITAL_ANG_VEL*PLANET_DISTANCE) / PLANET_RADIUS,0.0);
PLANET_ROTATION_ANG_VEL = -PLANET_ORBITAL_ANG_VEL + MAX_PLANET_ROT * 0.5;
PLANET_GAMMA = 1.0/sqrt(1.0-SQ(PLANET_ORBITAL_ANG_VEL*PLANET_DISTANCE));
{{/planetEnabled}}
vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
p.y *= resolution.y / resolution.x;
vec3 pos = cam_pos;
vec3 ray = normalize(p.x*cam_x + p.y*cam_y + FOV_MULT*cam_z);
{{#aberration}}
ray = lorentz_velocity_transformation(ray, cam_vel);
{{/aberration}}
float ray_intensity = 1.0;
float ray_doppler_factor = 1.0;
float gamma = 1.0/sqrt(1.0-dot(cam_vel,cam_vel));
ray_doppler_factor = gamma*(1.0 + dot(ray,-cam_vel));
{{#beaming}}
ray_intensity /= ray_doppler_factor*ray_doppler_factor*ray_doppler_factor;
{{/beaming}}
{{^doppler_shift}}
ray_doppler_factor = 1.0;
{{/doppler_shift}}
float step = 0.01;
vec4 color = vec4(0.0,0.0,0.0,1.0);
// initial conditions
float u = 1.0 / length(pos), old_u;
float u0 = u;
vec3 normal_vec = normalize(pos);
vec3 tangent_vec = normalize(cross(cross(normal_vec, ray), normal_vec));
float du = -dot(ray,normal_vec) / dot(ray,tangent_vec) * u;
float du0 = du;
float phi = 0.0;
float t = time;
float dt = 1.0;
{{^light_travel_time}}
float planet_ang0 = t * PLANET_ORBITAL_ANG_VEL;
vec3 planet_pos0 = vec3(cos(planet_ang0), sin(planet_ang0), 0)*PLANET_DISTANCE;
{{/light_travel_time}}
vec3 old_pos;
for (int j=0; j < NSTEPS; j++) {
step = MAX_REVOLUTIONS * 2.0*M_PI / float(NSTEPS);
// adaptive step size, some ad hoc formulas
float max_rel_u_change = (1.0-log(u))*10.0 / float(NSTEPS);
if ((du > 0.0 || (du0 < 0.0 && u0/u < 5.0)) && abs(du) > abs(max_rel_u_change*u) / step)
step = max_rel_u_change*u/abs(du);
old_u = u;
{{#light_travel_time}}
{{#gravitational_time_dilation}}
dt = sqrt(du*du + u*u*(1.0-u))/(u*u*(1.0-u))*step;
{{/gravitational_time_dilation}}
{{/light_travel_time}}
// Leapfrog scheme
u += du*step;
float ddu = -u*(1.0 - 1.5*u*u);
du += ddu*step;
if (u < 0.0) break;
phi += step;
old_pos = pos;
pos = (cos(phi)*normal_vec + sin(phi)*tangent_vec)/u;
ray = pos-old_pos;
float solid_isec_t = 2.0;
float ray_l = length(ray);
{{#light_travel_time}}
{{#gravitational_time_dilation}}
float mix = smooth_step(1.0/u, 8.0);
dt = mix*ray_l + (1.0-mix)*dt;
{{/gravitational_time_dilation}}
{{^gravitational_time_dilation}}
dt = ray_l;
{{/gravitational_time_dilation}}
{{/light_travel_time}}
{{#planetEnabled}}
if (
(
old_pos.z * pos.z < 0.0 ||
min(abs(old_pos.z), abs(pos.z)) < PLANET_RADIUS
) &&
max(u, old_u) > 1.0/(PLANET_DISTANCE+PLANET_RADIUS) &&
min(u, old_u) < 1.0/(PLANET_DISTANCE-PLANET_RADIUS)
) {
{{#light_travel_time}}
float planet_ang0 = t * PLANET_ORBITAL_ANG_VEL;
vec3 planet_pos0 = vec3(cos(planet_ang0), sin(planet_ang0), 0)*PLANET_DISTANCE;
{{/light_travel_time}}
vec4 planet_isec = planet_intersection(old_pos, ray, t, dt,
planet_pos0, ray_doppler_factor);
if (planet_isec.w > 0.0) {
solid_isec_t = planet_isec.w;
planet_isec.w = 1.0;
color += planet_isec;
}
}
{{/planetEnabled}}
{{#accretion_disk}}
if (old_pos.z * pos.z < 0.0) {
// crossed plane z=0
float acc_isec_t = -old_pos.z / ray.z;
if (acc_isec_t < solid_isec_t) {
vec3 isec = old_pos + ray*acc_isec_t;
float r = length(isec);
if (r > ACCRETION_MIN_R) {
vec2 tex_coord = vec2(
(r-ACCRETION_MIN_R)/ACCRETION_WIDTH,
atan(isec.x, isec.y)/M_PI*0.5+0.5
);
float accretion_intensity = ACCRETION_BRIGHTNESS;
//accretion_intensity *= 1.0 / abs(ray.z/ray_l);
float temperature = ACCRETION_TEMPERATURE;
vec3 accretion_v = vec3(-isec.y, isec.x, 0.0) / sqrt(2.0*(r-1.0)) / (r*r);
gamma = 1.0/sqrt(1.0-dot(accretion_v,accretion_v));
float doppler_factor = gamma*(1.0+dot(ray/ray_l,accretion_v));
{{#beaming}}
accretion_intensity /= doppler_factor*doppler_factor*doppler_factor;
{{/beaming}}
{{#doppler_shift}}
temperature /= ray_doppler_factor*doppler_factor;
{{/doppler_shift}}
color += texture2D(accretion_disk_texture,tex_coord)
* accretion_intensity
* BLACK_BODY_COLOR(temperature);
}
}
}
{{/accretion_disk}}
{{#light_travel_time}}
t -= dt;
{{/light_travel_time}}
if (solid_isec_t <= 1.0) u = 2.0; // break
if (u > 1.0) break;
}
// the event horizon is at u = 1
if (u < 1.0) {
ray = normalize(pos - old_pos);
vec2 tex_coord = sphere_map(ray * BG_COORDS);
float t_coord;
vec4 star_color = texture2D(star_texture, tex_coord);
if (star_color.r > 0.0) {
t_coord = (STAR_MIN_TEMPERATURE +
(STAR_MAX_TEMPERATURE-STAR_MIN_TEMPERATURE) * star_color.g)
/ ray_doppler_factor;
color += BLACK_BODY_COLOR(t_coord) * star_color.r * STAR_BRIGHTNESS;
}
color += galaxy_color(tex_coord, ray_doppler_factor) * GALAXY_BRIGHTNESS;
}
gl_FragColor = color*ray_intensity;
}